blob: 46943b510eaa244ea34e9ddfa9100cc39c5e6e4e [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;
Craig Mautner360310b2012-10-26 15:13:08 -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;
Craig Mautner360310b2012-10-26 15:13:08 -070056import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Daniel Sandler924b9932013-06-12 00:38:25 -040073import android.view.*;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080074import android.view.View.OnLongClickListener;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080075import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080076import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070077import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070078import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080079import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.inputmethod.InputMethodManager;
Daniel Sandler924b9932013-06-12 00:38:25 -040081import android.widget.*;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070082
Daniel Sandler325dc232013-06-05 22:57:57 -040083import com.android.launcher3.R;
84import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080085
Adam Cohenc0dcf592011-06-01 15:30:43 -070086import java.io.DataInputStream;
87import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070088import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070089import java.io.FileNotFoundException;
90import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070091import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -070092import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -070093import java.util.Collection;
94import java.util.Collections;
95import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -070097import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -070098import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -070099import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Default launcher application.
103 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100104public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700105 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700106 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700108 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Daniel Sandler924b9932013-06-12 00:38:25 -0400110 static final boolean PROFILE_STARTUP = true;
Daniel Sandler843e8602010-06-07 14:59:01 -0400111 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700112 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandler924b9932013-06-12 00:38:25 -0400113 static final boolean DEBUG_RESUME_TIME = true;
114 static final boolean DEBUG_MEMORY = true;
Romain Guy6fefcf12009-06-11 13:07:43 -0700115
Winson Chung70d72102011-08-12 11:24:35 -0700116 private static final int MENU_GROUP_WALLPAPER = 1;
117 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
118 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700119 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
120 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int REQUEST_PICK_APPLICATION = 6;
125 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700127 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Michael Jurka8b805b12012-04-18 14:23:14 -0700129 private static final int REQUEST_BIND_APPWIDGET = 11;
130
Mathew Inwood876a8462013-06-14 14:12:41 +0100131 /**
132 * IntentStarter uses request codes starting with this. This must be greater than all activity
133 * request codes used internally.
134 */
135 protected static final int REQUEST_LAST = 100;
136
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800143 // To turn on these properties, type
144 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
145 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
146 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Winson Chung2672ff92012-05-04 16:22:30 -0700148 // The Intent extra that defines whether to ignore the launch animation
149 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400150 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
158 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700168 // Type: int
169 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
170 // Type: int
171 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
172 // Type: parcelable
173 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100175 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700176 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100177 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700178 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100179 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700182 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700183 private State mState = State.WORKSPACE;
184 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800185 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700188 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
189 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700190 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700191 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800194 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
Winson Chunga2413752012-04-03 14:22:34 -0700196 // How long to wait before the new-shortcut animation automatically pans the workspace
197 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
198
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800199 private final BroadcastReceiver mCloseSystemDialogsReceiver
200 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800201 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private LayoutInflater mInflater;
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800206 private View mQsbDivider;
207 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700208 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800209 private DragLayer mDragLayer;
210 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700211
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private AppWidgetManager mAppWidgetManager;
213 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Michael Jurkac9d95c52011-08-29 14:03:34 -0700215 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700216 private AppWidgetProviderInfo mPendingAddWidgetInfo;
217
Michael Jurka0280c3b2010-09-17 15:00:07 -0700218 private int[] mTmpAddItemCellCoordinates = new int[2];
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private FolderInfo mFolderInfo;
221
Winson Chung3d503fb2011-07-13 17:25:49 -0700222 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800223 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700224
Winson Chungc51db6a2011-10-05 11:44:49 -0700225 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700226 private AppsCustomizeTabHost mAppsCustomizeTabHost;
227 private AppsCustomizePagedView mAppsCustomizeContent;
228 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700231 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
232 // scroll issues (because the workspace may not have been measured yet) and extra work.
233 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
234 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235
236 private SpannableStringBuilder mDefaultKeySsb = null;
237
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238 private boolean mWorkspaceLoading = true;
239
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800240 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241 private boolean mRestoring;
242 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700243 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244
Michael Jurka7607c2f2013-04-03 14:33:19 -0700245 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
246
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // Keep track of whether the user has left launcher
248 private static boolean sPausedFromUserAction = false;
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Bundle mSavedInstanceState;
251
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800254 private boolean mUserPresent = true;
255 private boolean mVisible = false;
256 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700257 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static LocaleConfiguration sLocaleConfiguration = null;
260
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700261 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700279 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
280 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Craig Mautner360310b2012-10-26 15:13:08 -0700283 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700284
Adam Cohen1462de32012-07-24 22:34:36 -0700285 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
286
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700287 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
288
Winson Chung46353de2012-02-16 14:05:10 -0800289 // We only want to get the SharedPreferences once since it does an FS stat each time we get
290 // it from the context.
291 private SharedPreferences mSharedPrefs;
292
Adam Cohene25af792013-06-06 23:08:25 -0700293 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
294
Winson Chungf0c6ae02012-03-21 16:10:31 -0700295 // Holds the page that we need to animate to, and the icon views that we need to animate up
296 // when we scroll to that page on resume.
297 private int mNewShortcutAnimatePage = -1;
298 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700299 private ImageView mFolderIconImageView;
300 private Bitmap mFolderIconBitmap;
301 private Canvas mFolderIconCanvas;
302 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700303
Michael Jurkaddd62e92011-02-16 17:49:14 -0800304 private BubbleTextView mWaitingForResume;
305
Michael Jurka22143132012-10-04 17:42:38 +0200306 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
307 = new HideFromAccessibilityHelper();
308
Michael Jurkac1f5d262011-09-30 19:32:27 -0700309 private Runnable mBuildLayersRunnable = new Runnable() {
310 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700311 if (mWorkspace != null) {
312 mWorkspace.buildPageHardwareLayers();
313 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700314 }
315 };
316
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800317 private static ArrayList<PendingAddArguments> sPendingAddList
318 = new ArrayList<PendingAddArguments>();
319
Michael Jurka0a457bf2012-11-19 14:05:05 -0800320 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
321
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800322 private static class PendingAddArguments {
323 int requestCode;
324 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700325 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800326 int screen;
327 int cellX;
328 int cellY;
329 }
330
Michael Jurka0a457bf2012-11-19 14:05:05 -0800331 private static boolean isPropertyEnabled(String propertyName) {
332 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700333 }
334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 @Override
336 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700337 if (DEBUG_STRICT_MODE) {
338 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
339 .detectDiskReads()
340 .detectDiskWrites()
341 .detectNetwork() // or .detectAll() for all detectable problems
342 .penaltyLog()
343 .build());
344 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
345 .detectLeakedSqlLiteObjects()
346 .detectLeakedClosableObjects()
347 .penaltyLog()
348 .penaltyDeath()
349 .build());
350 }
351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 super.onCreate(savedInstanceState);
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400353 LauncherAppState app = LauncherAppState.getInstance();
354 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700355 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800356 mModel = app.setLauncher(this);
357 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400358 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700360
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700361 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700362 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
363 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700364
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700365 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
366 // this also ensures that any synchronous binding below doesn't re-trigger another
367 // LauncherModel load.
368 mPaused = false;
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200371 android.os.Debug.startMethodTracing(
372 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
374
375 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 setContentView(R.layout.launcher);
377 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700378 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800380 registerContentObservers();
381
Joe Onorato7c312c12009-08-13 21:36:53 -0700382 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 mSavedState = savedInstanceState;
385 restoreState(mSavedState);
386
Winson Chunga12a2502010-12-20 14:41:35 -0800387 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700388 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200389 mAppsCustomizeContent.onPackagesUpdated(
390 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700391 }
Winson Chunga12a2502010-12-20 14:41:35 -0800392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 if (PROFILE_STARTUP) {
394 android.os.Debug.stopMethodTracing();
395 }
396
397 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700398 if (sPausedFromUserAction) {
399 // If the user leaves launcher, then we should just load items asynchronously when
400 // they return.
401 mModel.startLoader(true, -1);
402 } else {
403 // We only load the page synchronously if the user rotates (or triggers a
404 // configuration change) while launcher is in the foreground
405 mModel.startLoader(true, mWorkspace.getCurrentPage());
406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
Michael Jurkac57b7a82011-08-09 22:02:20 -0700409 if (!mModel.isAllAppsLoaded()) {
410 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
411 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
412 }
413
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 // For handling default keys
415 mDefaultKeySsb = new SpannableStringBuilder();
416 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800417
418 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
419 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800420
Adam Cohenf9426d52012-06-04 17:26:21 -0700421 updateGlobalIcons();
422
423 // On large interfaces, we want the screen to auto-rotate based on the current orientation
424 unlockScreenOrientation(true);
425 }
426
Winson Chung4a2afa32012-07-19 14:53:05 -0700427 protected void onUserLeaveHint() {
428 super.onUserLeaveHint();
429 sPausedFromUserAction = true;
430 }
431
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700433 boolean searchVisible = false;
434 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800435 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700436 int coi = getCurrentOrientationIndexForGlobalIcons();
437 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
438 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700439 updateAppMarketIcon();
440 searchVisible = updateGlobalSearchIcon();
441 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700442 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700443 if (sGlobalSearchIcon[coi] != null) {
444 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sVoiceSearchIcon[coi] != null) {
448 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sAppMarketIcon[coi] != null) {
452 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800453 }
Winson Chungadf0c182012-08-23 14:59:07 -0700454 if (mSearchDropTargetBar != null) {
455 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
456 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
Romain Guycbb89e42009-06-08 15:52:54 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 if (sLocaleConfiguration == null) {
461 new AsyncTask<Void, Void, LocaleConfiguration>() {
462 @Override
463 protected LocaleConfiguration doInBackground(Void... unused) {
464 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
465 readConfiguration(Launcher.this, localeConfiguration);
466 return localeConfiguration;
467 }
468
469 @Override
470 protected void onPostExecute(LocaleConfiguration result) {
471 sLocaleConfiguration = result;
472 checkForLocaleChange(); // recursive, but now with a locale configuration
473 }
474 }.execute();
475 return;
476 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 final Configuration configuration = getResources().getConfiguration();
479
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700480 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 final String locale = configuration.locale.toString();
482
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 final int mcc = configuration.mcc;
485
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700486 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 final int mnc = configuration.mnc;
488
Romain Guy84f296c2009-11-04 15:00:44 -0800489 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490
Romain Guy84f296c2009-11-04 15:00:44 -0800491 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 sLocaleConfiguration.locale = locale;
493 sLocaleConfiguration.mcc = mcc;
494 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700495
Joe Onorato0589f0f2010-02-08 13:44:00 -0800496 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497
498 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
499 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700500 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 public void run() {
502 writeConfiguration(Launcher.this, localeConfiguration);
503 }
504 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700505 }
506 }
507
508 private static class LocaleConfiguration {
509 public String locale;
510 public int mcc = -1;
511 public int mnc = -1;
512 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700513
Romain Guy98d01652009-06-30 16:21:04 -0700514 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
515 DataInputStream in = null;
516 try {
517 in = new DataInputStream(context.openFileInput(PREFERENCES));
518 configuration.locale = in.readUTF();
519 configuration.mcc = in.readInt();
520 configuration.mnc = in.readInt();
521 } catch (FileNotFoundException e) {
522 // Ignore
523 } catch (IOException e) {
524 // Ignore
525 } finally {
526 if (in != null) {
527 try {
528 in.close();
529 } catch (IOException e) {
530 // Ignore
531 }
532 }
533 }
534 }
535
536 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
537 DataOutputStream out = null;
538 try {
539 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
540 out.writeUTF(configuration.locale);
541 out.writeInt(configuration.mcc);
542 out.writeInt(configuration.mnc);
543 out.flush();
544 } catch (FileNotFoundException e) {
545 // Ignore
546 } catch (IOException e) {
547 //noinspection ResultOfMethodCallIgnored
548 context.getFileStreamPath(PREFERENCES).delete();
549 } finally {
550 if (out != null) {
551 try {
552 out.close();
553 } catch (IOException e) {
554 // Ignore
555 }
556 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 }
558 }
559
Bjorn Bringertc459e522013-06-07 19:36:01 +0100560 public LayoutInflater getInflater() {
561 return mInflater;
562 }
563
Adam Cohen716b51e2011-06-30 12:09:54 -0700564 public DragLayer getDragLayer() {
565 return mDragLayer;
566 }
567
Winson Chung36a62fe2012-05-06 18:04:42 -0700568 boolean isDraggingEnabled() {
569 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
570 // that is subsequently removed from the workspace in startBinding().
571 return !mModel.isLoadingWorkspace();
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 static int getScreen() {
575 synchronized (sLock) {
576 return sScreen;
577 }
578 }
579
580 static void setScreen(int screen) {
581 synchronized (sLock) {
582 sScreen = screen;
583 }
584 }
585
Winson Chung557d6ed2011-07-08 15:34:52 -0700586 /**
587 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
588 * a configuration step, this allows the proper animations to run after other transitions.
589 */
590 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700591 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 switch (args.requestCode) {
593 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700594 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
595 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 break;
597 case REQUEST_PICK_SHORTCUT:
598 processShortcut(args.intent);
599 break;
600 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
602 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 result = true;
604 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 case REQUEST_CREATE_APPWIDGET:
606 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_WALLPAPER:
611 // We just wanted the activity result here so we can clear mWaitingForResult
612 break;
613 }
Michael Jurka27614d22012-04-02 06:40:22 -0700614 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
615 // if you turned the screen off and then back while in All Apps, Launcher would not
616 // return to the workspace. Clearing mAddInfo.container here fixes this issue
617 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 protected void onActivityResult(
623 final int requestCode, final int resultCode, final Intent data) {
624 if (requestCode == REQUEST_BIND_APPWIDGET) {
625 int appWidgetId = data != null ?
626 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
627 if (resultCode == RESULT_CANCELED) {
628 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
629 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700630 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700634 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637 mWaitingForResult = false;
638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
641 int appWidgetId = data != null ?
642 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700643 if (appWidgetId < 0) {
644 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
645 "widget configuration activity.");
646 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
647 } else {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId);
649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700658 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.container = mPendingAddInfo.container;
663 args.screen = mPendingAddInfo.screen;
664 args.cellX = mPendingAddInfo.cellX;
665 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 if (isWorkspaceLocked()) {
667 sPendingAddList.add(args);
668 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
675 null);
676 }
677
678 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 Runnable onCompleteRunnable = null;
682 int animationType = 0;
683
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 if (resultCode == RESULT_OK) {
686 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
687 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700688 mPendingAddWidgetInfo);
689 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 onCompleteRunnable = new Runnable() {
691 @Override
692 public void run() {
693 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
694 mPendingAddInfo.screen, layout, null);
695 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
696 null);
697 }
698 };
699 } else if (resultCode == RESULT_CANCELED) {
700 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
705 null);
706 }
707 };
708 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700709 if (mDragLayer.getAnimatedView() != null) {
710 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
711 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
712 animationType, boundWidget, true);
713 } else {
714 // The animated view may be null in the case of a rotation during widget configuration
715 onCompleteRunnable.run();
716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700720 protected void onStop() {
721 super.onStop();
722 FirstFrameAnimatorHelper.setIsVisible(false);
723 }
724
725 @Override
726 protected void onStart() {
727 super.onStart();
728 FirstFrameAnimatorHelper.setIsVisible(true);
729 }
730
731 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200733 long startTime = 0;
734 if (DEBUG_RESUME_TIME) {
735 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400736 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700739
Winson Chung4a2afa32012-07-19 14:53:05 -0700740 // Restore the previous launcher state
741 if (mOnResumeState == State.WORKSPACE) {
742 showWorkspace(false);
743 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
744 showAllApps(false);
745 }
746 mOnResumeState = State.NONE;
747
Craig Mautner360310b2012-10-26 15:13:08 -0700748 // Background was set to gradient in onPause(), restore to black if in all apps.
749 setWorkspaceBackground(mState == State.WORKSPACE);
750
Winson Chungde0fb8f2012-05-08 14:37:08 -0700751 // Process any items that were added while Launcher was away
752 InstallShortcutReceiver.flushInstallQueue(this);
753
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800754 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700755 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700756 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700758 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700760 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200762 if (mOnResumeCallbacks.size() > 0) {
763 // We might have postponed some bind calls until onResume (see waitUntilResume) --
764 // execute them here
765 long startTimeCallbacks = 0;
766 if (DEBUG_RESUME_TIME) {
767 startTimeCallbacks = System.currentTimeMillis();
768 }
769
770 if (mAppsCustomizeContent != null) {
771 mAppsCustomizeContent.setBulkBind(true);
772 }
773 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
774 mOnResumeCallbacks.get(i).run();
775 }
776 if (mAppsCustomizeContent != null) {
777 mAppsCustomizeContent.setBulkBind(false);
778 }
779 mOnResumeCallbacks.clear();
780 if (DEBUG_RESUME_TIME) {
781 Log.d(TAG, "Time spent processing callbacks in onResume: " +
782 (System.currentTimeMillis() - startTimeCallbacks));
783 }
Michael Jurka447bf842013-05-15 14:52:15 +0200784 }
Winson Chunge4e50662012-01-23 14:45:13 -0800785
786 // Reset the pressed state of icons that were locked in the press state while activities
787 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800788 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800789 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800790 mWaitingForResume.setStayPressed(false);
791 }
Winson Chunge4e50662012-01-23 14:45:13 -0800792 if (mAppsCustomizeContent != null) {
793 // Resets the previous all apps icon press state
794 mAppsCustomizeContent.resetDrawableState();
795 }
Adam Cohen06dff352012-06-01 17:17:08 -0700796 // It is possible that widgets can receive updates while launcher is not in the foreground.
797 // Consequently, the widgets will be inflated in the orientation of the foreground activity
798 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
799 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700800 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700801
802 // Again, as with the above scenario, it's possible that one or more of the global icons
803 // were updated in the wrong orientation.
804 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200805 if (DEBUG_RESUME_TIME) {
806 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
807 }
Adam Cohen06dff352012-06-01 17:17:08 -0700808 }
809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700811 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700812 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
813 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
814 // when Launcher resumes and we are still in AllApps.
815 updateWallpaperVisibility(true);
816
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700817 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700818 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800819 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700820 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700821 }
Romain Guycbb89e42009-06-08 15:52:54 -0700822
Adam Cohen66a01fd2013-06-11 12:48:00 -0700823 protected void onFinishBindingItems() {
824 }
825
826 // Add a fullscreen unpadded view to the workspace to the left all other screens.
827 public void addCustomContentToLeft(View customContent) {
828 CellLayout customScreen = (CellLayout)
829 getLayoutInflater().inflate(R.layout.workspace_custom_content, null);
830
831 int spanX = customScreen.getCountX();
832 int spanY = customScreen.getCountY();
833
834 CellLayout.LayoutParams lp = new CellLayout.LayoutParams(0, 0, spanX, spanY);
835 lp.canReorder = false;
836
837 customScreen.addViewToCellLayout(customContent, 0, 0, lp, true);
838
839 mWorkspace.addView(customScreen, 0);
840
Adam Cohene61a9a22013-06-11 15:45:31 -0700841 // Ensure that the current page and default page are maintained.
842 mWorkspace.incrementDefaultScreen();
843 mWorkspace.setCurrentPage(mWorkspace.getCurrentPage() + 1);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700844 }
845
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700846 @Override
847 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400848 // Flag the loader to stop early before switching
849 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700850 if (mAppsCustomizeContent != null) {
851 mAppsCustomizeContent.surrender();
852 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800853 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700854 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700855
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800856 // We can't hide the IME if it was forced open. So don't bother
857 /*
858 @Override
859 public void onWindowFocusChanged(boolean hasFocus) {
860 super.onWindowFocusChanged(hasFocus);
861
862 if (hasFocus) {
863 final InputMethodManager inputManager = (InputMethodManager)
864 getSystemService(Context.INPUT_METHOD_SERVICE);
865 WindowManager.LayoutParams lp = getWindow().getAttributes();
866 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
867 android.os.Handler()) {
868 protected void onReceiveResult(int resultCode, Bundle resultData) {
869 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
870 }
871 });
872 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
873 }
874 }
875 */
876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 private boolean acceptFilter() {
878 final InputMethodManager inputManager = (InputMethodManager)
879 getSystemService(Context.INPUT_METHOD_SERVICE);
880 return !inputManager.isFullscreenMode();
881 }
882
883 @Override
884 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700885 final int uniChar = event.getUnicodeChar();
886 final boolean handled = super.onKeyDown(keyCode, event);
887 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
888 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
890 keyCode, event);
891 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700892 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700893 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700894 // showSearchDialog()
895 // If there are multiple keystrokes before the search dialog takes focus,
896 // onSearchRequested() will be called for every keystroke,
897 // but it is idempotent, so it's fine.
898 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 }
900 }
901
Joe Onorato8a9625e2010-01-28 15:55:35 -0800902 // Eat the long press event so the keyboard doesn't come up.
903 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
904 return true;
905 }
906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 return handled;
908 }
909
Karl Rosaen138a0412009-04-23 19:00:21 -0700910 private String getTypedText() {
911 return mDefaultKeySsb.toString();
912 }
913
914 private void clearTypedText() {
915 mDefaultKeySsb.clear();
916 mDefaultKeySsb.clearSpans();
917 Selection.setSelection(mDefaultKeySsb, 0);
918 }
919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700921 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
922 * State
923 */
924 private static State intToState(int stateOrdinal) {
925 State state = State.WORKSPACE;
926 final State[] stateValues = State.values();
927 for (int i = 0; i < stateValues.length; i++) {
928 if (stateValues[i].ordinal() == stateOrdinal) {
929 state = stateValues[i];
930 break;
931 }
932 }
933 return state;
934 }
935
936 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 * Restores the previous state, if it exists.
938 *
939 * @param savedState The previous state.
940 */
941 private void restoreState(Bundle savedState) {
942 if (savedState == null) {
943 return;
944 }
945
Michael Jurka883f55b2010-10-21 15:47:14 -0700946 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700947 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700948 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800949 }
950
Winson Chungf0c6ae02012-03-21 16:10:31 -0700951 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700953 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
957 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700958
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
960 mPendingAddInfo.container = pendingAddContainer;
961 mPendingAddInfo.screen = pendingAddScreen;
962 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
963 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700964 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
965 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
966 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700967 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 mRestoring = true;
969 }
970
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700971
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
973 if (renameFolder) {
974 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700975 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 mRestoring = true;
977 }
Winson Chunga12a2502010-12-20 14:41:35 -0800978
Winson Chung785d2eb2011-04-14 16:08:02 -0700979
980 // Restore the AppsCustomize tab
981 if (mAppsCustomizeTabHost != null) {
982 String curTab = savedState.getString("apps_customize_currentTab");
983 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700984 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700986 mAppsCustomizeContent.loadAssociatedPages(
987 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 }
989
Winson Chung5afbf7b2011-07-25 11:53:08 -0700990 int currentIndex = savedState.getInt("apps_customize_currentIndex");
991 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 /**
996 * Finds all the views we need and configure them properly.
997 */
998 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700999 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001000
Craig Mautner360310b2012-10-26 15:13:08 -07001001 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001002 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1003 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001004 mQsbDivider = findViewById(R.id.qsb_divider);
1005 mDockDivider = findViewById(R.id.dock_divider);
1006
1007 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1008 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009
Winson Chung4c98d922011-05-31 16:50:48 -07001010 // Setup the drag layer
1011 mDragLayer.setup(this, dragController);
1012
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 // Setup the hotseat
1014 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1015 if (mHotseat != null) {
1016 mHotseat.setup(this);
1017 }
1018
Winson Chung4c98d922011-05-31 16:50:48 -07001019 // Setup the workspace
1020 mWorkspace.setHapticFeedbackEnabled(false);
1021 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001022 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001023 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001024
Winson Chungf0ea4d32011-06-06 14:27:16 -07001025 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001026 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001027
Winson Chungf0ea4d32011-06-06 14:27:16 -07001028 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001029 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001030 mAppsCustomizeContent = (AppsCustomizePagedView)
1031 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1032 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001033
Winson Chung3d503fb2011-07-13 17:25:49 -07001034 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001035 dragController.setDragScoller(mWorkspace);
1036 dragController.setScrollView(mDragLayer);
1037 dragController.setMoveTarget(mWorkspace);
1038 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001039 if (mSearchDropTargetBar != null) {
1040 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001041 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001042
1043 if (DEBUG_MEMORY) {
1044 Log.v(TAG, "adding WeightWatcher");
1045 ((FrameLayout) mLauncherView).addView(new WeightWatcher(this),
1046 new FrameLayout.LayoutParams(
1047 FrameLayout.LayoutParams.MATCH_PARENT,
1048 44,
1049 Gravity.BOTTOM)
1050 );
1051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
1054 /**
1055 * Creates a view representing a shortcut.
1056 *
1057 * @param info The data structure describing the shortcut.
1058 *
1059 * @return A View inflated from R.layout.application.
1060 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001061 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001063 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
1066 /**
1067 * Creates a view representing a shortcut inflated from the specified resource.
1068 *
1069 * @param layoutResId The id of the XML layout used to create the shortcut.
1070 * @param parent The group the shortcut belongs to.
1071 * @param info The data structure describing the shortcut.
1072 *
1073 * @return A View inflated from layoutResId.
1074 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001075 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001076 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1077 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 return favorite;
1080 }
1081
1082 /**
1083 * Add an application shortcut to the workspace.
1084 *
1085 * @param data The intent describing the application.
1086 * @param cellInfo The position on screen where to create the shortcut.
1087 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001088 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001090 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091
Adam Cohenfbba09b2011-07-18 21:43:05 -07001092 // First we check if we already know the exact location where we want to add this item.
1093 if (cellX >= 0 && cellY >= 0) {
1094 cellXY[0] = cellX;
1095 cellXY[1] = cellY;
1096 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001097 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 return;
1099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001101 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001102
Romain Guy73b979d2009-06-09 12:57:21 -07001103 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001107 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001108 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001109 } else {
1110 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 /**
1115 * Add a shortcut to the workspace.
1116 *
1117 * @param data The intent describing the shortcut.
1118 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1121 int cellY) {
1122 int[] cellXY = mTmpAddItemCellCoordinates;
1123 int[] touchXY = mPendingAddInfo.dropPos;
1124 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001125
Michael Jurkad3ef3062010-11-23 16:23:58 -08001126 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001127
Adam Cohen558baaf2011-08-15 15:22:57 -07001128 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001129 if (info == null) {
1130 return;
1131 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001132 final View view = createShortcut(info);
1133
Adam Cohenfbba09b2011-07-18 21:43:05 -07001134 // First we check if we already know the exact location where we want to add this item.
1135 if (cellX >= 0 && cellY >= 0) {
1136 cellXY[0] = cellX;
1137 cellXY[1] = cellY;
1138 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001139
1140 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001141 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001142 true, null,null)) {
1143 return;
1144 }
1145 DragObject dragObject = new DragObject();
1146 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001147 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1148 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001149 return;
1150 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001151 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 foundCellSpan = (result != null);
1155 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001156 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001157 }
1158
1159 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001160 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 return;
1162 }
1163
Adam Cohen558baaf2011-08-15 15:22:57 -07001164 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
1166 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001167 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1168 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
1171
Adam Cohen2f093b62012-04-30 18:59:53 -07001172 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1173 int minHeight) {
1174 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001175 // We want to account for the extra amount of padding that we are adding to the widget
1176 // to ensure that it gets the full amount of space that it has requested
1177 int requiredWidth = minWidth + padding.left + padding.right;
1178 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001180 }
1181
Adam Cohen2f093b62012-04-30 18:59:53 -07001182 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1183 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001184 }
1185
Adam Cohen2f093b62012-04-30 18:59:53 -07001186 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1187 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001188 }
1189
Adam Cohen2f093b62012-04-30 18:59:53 -07001190 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1191 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001192 }
1193
Adam Cohen2f093b62012-04-30 18:59:53 -07001194 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1195 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001196 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001202 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001205 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1206 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1207 if (appWidgetInfo == null) {
1208 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001211 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001213
Adam Cohen2f093b62012-04-30 18:59:53 -07001214 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1215 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001218 // We have saved the position to which the widget was dragged-- this really only matters
1219 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001220 int[] cellXY = mTmpAddItemCellCoordinates;
1221 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1225 cellXY[0] = mPendingAddInfo.cellX;
1226 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001227 spanXY[0] = mPendingAddInfo.spanX;
1228 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001229 foundCellSpan = true;
1230 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001232 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1234 spanXY[1], cellXY, finalSpan);
1235 spanXY[0] = finalSpan[0];
1236 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001237 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001240 }
1241
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001243 if (appWidgetId != -1) {
1244 // Deleting an app widget ID is a void call but writes to disk before returning
1245 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001246 new Thread("deleteAppWidgetId") {
1247 public void run() {
1248 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1249 }
1250 }.start();
1251 }
Winson Chung93eef082012-03-23 15:59:27 -07001252 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001253 return;
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001257 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1258 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 launcherInfo.spanX = spanXY[0];
1260 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001261 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1262 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001265 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
1267 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001268 if (hostView == null) {
1269 // Perform actual inflation because we're live
1270 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1271 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1272 } else {
1273 // The AppWidgetHostView has already been inflated and instantiated
1274 launcherInfo.hostView = hostView;
1275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001279 launcherInfo.notifyWidgetSizeChanged(this);
1280
Winson Chung3d503fb2011-07-13 17:25:49 -07001281 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001283
1284 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001286 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Romain Guycbb89e42009-06-08 15:52:54 -07001288
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1290 @Override
1291 public void onReceive(Context context, Intent intent) {
1292 final String action = intent.getAction();
1293 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1294 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001295 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001297
Winson Chungc100e8e2011-08-09 16:02:43 -07001298 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001299 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001300 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1301 mAppsCustomizeTabHost.reset();
1302 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1305 mUserPresent = true;
1306 updateRunning();
1307 }
1308 }
1309 };
1310
1311 @Override
1312 public void onAttachedToWindow() {
1313 super.onAttachedToWindow();
1314
1315 // Listen for broadcasts related to user-presence
1316 final IntentFilter filter = new IntentFilter();
1317 filter.addAction(Intent.ACTION_SCREEN_OFF);
1318 filter.addAction(Intent.ACTION_USER_PRESENT);
1319 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001320 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001321 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 mVisible = true;
1323 }
1324
1325 @Override
1326 public void onDetachedFromWindow() {
1327 super.onDetachedFromWindow();
1328 mVisible = false;
1329
Adam Cohend113e0c2010-11-11 10:48:05 -08001330 if (mAttached) {
1331 unregisterReceiver(mReceiver);
1332 mAttached = false;
1333 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 updateRunning();
1335 }
1336
1337 public void onWindowVisibilityChanged(int visibility) {
1338 mVisible = visibility == View.VISIBLE;
1339 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001340 // The following code used to be in onResume, but it turns out onResume is called when
1341 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1342 // is a more appropriate event to handle
1343 if (mVisible) {
1344 mAppsCustomizeTabHost.onWindowVisible();
1345 if (!mWorkspaceLoading) {
1346 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001347 // We want to let Launcher draw itself at least once before we force it to build
1348 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001349 // apps is nice and speedy.
1350 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001351 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001352 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001353 if (mStarted) return;
1354 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001355 // We delay the layer building a bit in order to give
1356 // other message processing a time to run. In particular
1357 // this avoids a delay in hiding the IME if it was
1358 // currently shown, because doing that may involve
1359 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001360 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001361 final ViewTreeObserver.OnDrawListener listener = this;
1362 mWorkspace.post(new Runnable() {
1363 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001364 if (mWorkspace != null &&
1365 mWorkspace.getViewTreeObserver() != null) {
1366 mWorkspace.getViewTreeObserver().
1367 removeOnDrawListener(listener);
1368 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001369 }
1370 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001371 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001372 }
1373 });
1374 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001375 // When Launcher comes back to foreground, a different Activity might be responsible for
1376 // the app market intent, so refresh the icon
1377 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001378 clearTypedText();
1379 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 }
1381
1382 private void sendAdvanceMessage(long delay) {
1383 mHandler.removeMessages(ADVANCE_MSG);
1384 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1385 mHandler.sendMessageDelayed(msg, delay);
1386 mAutoAdvanceSentTime = System.currentTimeMillis();
1387 }
1388
1389 private void updateRunning() {
1390 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1391 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1392 mAutoAdvanceRunning = autoAdvanceRunning;
1393 if (autoAdvanceRunning) {
1394 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1395 sendAdvanceMessage(delay);
1396 } else {
1397 if (!mWidgetsToAdvance.isEmpty()) {
1398 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1399 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1400 }
1401 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001402 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001403 }
1404 }
1405 }
1406
1407 private final Handler mHandler = new Handler() {
1408 @Override
1409 public void handleMessage(Message msg) {
1410 if (msg.what == ADVANCE_MSG) {
1411 int i = 0;
1412 for (View key: mWidgetsToAdvance.keySet()) {
1413 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1414 final int delay = mAdvanceStagger * i;
1415 if (v instanceof Advanceable) {
1416 postDelayed(new Runnable() {
1417 public void run() {
1418 ((Advanceable) v).advance();
1419 }
1420 }, delay);
1421 }
1422 i++;
1423 }
1424 sendAdvanceMessage(mAdvanceInterval);
1425 }
1426 }
1427 };
1428
1429 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001430 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1432 if (v instanceof Advanceable) {
1433 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001434 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001435 updateRunning();
1436 }
1437 }
1438
1439 void removeWidgetToAutoAdvance(View hostView) {
1440 if (mWidgetsToAdvance.containsKey(hostView)) {
1441 mWidgetsToAdvance.remove(hostView);
1442 updateRunning();
1443 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001444 }
1445
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001447 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 launcherInfo.hostView = null;
1449 }
1450
Winson Chung93eef082012-03-23 15:59:27 -07001451 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1452 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1453 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001454 }
1455
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001456 public LauncherAppWidgetHost getAppWidgetHost() {
1457 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Winson Chunga9abd0e2010-10-27 17:18:37 -07001460 public LauncherModel getModel() {
1461 return mModel;
1462 }
1463
Bjorn Bringertc459e522013-06-07 19:36:01 +01001464 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001465 getWindow().closeAllPanels();
1466
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001467 // Whatever we were doing is hereby canceled.
1468 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001469 }
1470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 @Override
1472 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001473 long startTime = 0;
1474 if (DEBUG_RESUME_TIME) {
1475 startTime = System.currentTimeMillis();
1476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 super.onNewIntent(intent);
1478
1479 // Close the menu
1480 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001481 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001482 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001483
Jamie Genniscb222e82012-10-23 15:44:26 -07001484 final boolean alreadyOnHome =
1485 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001486 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001487
Jamie Genniscb222e82012-10-23 15:44:26 -07001488 Runnable processIntent = new Runnable() {
1489 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001490 if (mWorkspace == null) {
1491 // Can be cases where mWorkspace is null, this prevents a NPE
1492 return;
1493 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001494 Folder openFolder = mWorkspace.getOpenFolder();
1495 // In all these cases, only animate if we're already on home
1496 mWorkspace.exitWidgetResizeMode();
1497 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1498 openFolder == null) {
1499 mWorkspace.moveToDefaultScreen(true);
1500 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001501
Jamie Genniscb222e82012-10-23 15:44:26 -07001502 closeFolder();
1503 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001504
Jamie Genniscb222e82012-10-23 15:44:26 -07001505 // If we are already on home, then just animate back to the workspace,
1506 // otherwise, just wait until onResume to set the state back to Workspace
1507 if (alreadyOnHome) {
1508 showWorkspace(true);
1509 } else {
1510 mOnResumeState = State.WORKSPACE;
1511 }
1512
1513 final View v = getWindow().peekDecorView();
1514 if (v != null && v.getWindowToken() != null) {
1515 InputMethodManager imm = (InputMethodManager)getSystemService(
1516 INPUT_METHOD_SERVICE);
1517 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1518 }
1519
1520 // Reset AllApps to its initial state
1521 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1522 mAppsCustomizeTabHost.reset();
1523 }
1524 }
1525 };
1526
1527 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1528 // Delay processing of the intent to allow the status bar animation to finish
1529 // first in order to avoid janky animations.
1530 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001531 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001532 // Process the intent immediately.
1533 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001534 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Michael Jurka447bf842013-05-15 14:52:15 +02001537 if (DEBUG_RESUME_TIME) {
1538 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
1542 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001543 public void onRestoreInstanceState(Bundle state) {
1544 super.onRestoreInstanceState(state);
1545 for (int page: mSynchronouslyBoundPages) {
1546 mWorkspace.restoreInstanceStateForChild(page);
1547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549
1550 @Override
1551 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001552 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001553 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
Michael Jurka883f55b2010-10-21 15:47:14 -07001555 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001556 // We close any open folder since it will not be re-opened, and we need to make sure
1557 // this state is reflected.
1558 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1561 mWaitingForResult) {
1562 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1563 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1564 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1565 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001566 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1567 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1568 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 if (mFolderInfo != null && mWaitingForResult) {
1572 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1573 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1574 }
Michael Jurka946ad472010-07-09 18:05:18 -07001575
Winson Chung785d2eb2011-04-14 16:08:02 -07001576 // Save the current AppsCustomize tab
1577 if (mAppsCustomizeTabHost != null) {
1578 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1579 if (currentTabTag != null) {
1580 outState.putString("apps_customize_currentTab", currentTabTag);
1581 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001582 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1583 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
1587 @Override
1588 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Winson Chunge7a03942011-08-05 15:05:12 -07001591 // Remove all pending runnables
1592 mHandler.removeMessages(ADVANCE_MSG);
1593 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001594 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001595
Winson Chungcd2b0142011-06-08 16:02:26 -07001596 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001597 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001598 mModel.stopLoader();
1599 app.setLauncher(null);
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001602 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001604 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606 mAppWidgetHost = null;
1607
1608 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
1610 TextKeyListener.getInstance().release();
1611
Winson Chung81b52252012-08-27 15:34:29 -07001612 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1613 // to prevent leaking Launcher activities on orientation change.
1614 if (mModel != null) {
1615 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1616 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001617
1618 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001619 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001620
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001621 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001622 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1623 mWorkspace.removeAllViews();
1624 mWorkspace = null;
1625 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001626
Michael Jurka2ecf9952012-06-18 12:52:28 -07001627 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629
Adam Cohena9cf38f2011-05-02 15:36:58 -07001630 public DragController getDragController() {
1631 return mDragController;
1632 }
1633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 @Override
1635 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001636 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 super.startActivityForResult(intent, requestCode);
1638 }
1639
Winson Chung70d72102011-08-12 11:24:35 -07001640 /**
1641 * Indicates that we want global search for this activity by setting the globalSearch
1642 * argument for {@link #startSearch} to true.
1643 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001645 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001647
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 if (initialQuery == null) {
1651 // Use any text typed in the launcher as the initial query
1652 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 if (appSearchData == null) {
1655 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001656 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
Winson Chungadf0c182012-08-23 14:59:07 -07001658 Rect sourceBounds = new Rect();
1659 if (mSearchDropTargetBar != null) {
1660 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Bjorn Bringertc459e522013-06-07 19:36:01 +01001663 startSearch(initialQuery, selectInitialQuery,
1664 appSearchData, sourceBounds);
1665 }
1666
1667 public void startSearch(String initialQuery,
1668 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001669 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001670 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001671 }
1672
1673 /**
1674 * Starts the global search activity. This code is a copied from SearchManager
1675 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001676 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001677 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001678 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001679 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1680 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1681 if (globalSearchActivity == null) {
1682 Log.w(TAG, "No global search activity found.");
1683 return;
1684 }
1685 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1686 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1687 intent.setComponent(globalSearchActivity);
1688 // Make sure that we have a Bundle to put source in
1689 if (appSearchData == null) {
1690 appSearchData = new Bundle();
1691 } else {
1692 appSearchData = new Bundle(appSearchData);
1693 }
1694 // Set source to package name of app that starts global search, if not set already.
1695 if (!appSearchData.containsKey("source")) {
1696 appSearchData.putString("source", getPackageName());
1697 }
1698 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1699 if (!TextUtils.isEmpty(initialQuery)) {
1700 intent.putExtra(SearchManager.QUERY, initialQuery);
1701 }
1702 if (selectInitialQuery) {
1703 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1704 }
1705 intent.setSourceBounds(sourceBounds);
1706 try {
1707 startActivity(intent);
1708 } catch (ActivityNotFoundException ex) {
1709 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 }
1712
Winson Chung70d72102011-08-12 11:24:35 -07001713 @Override
1714 public boolean onCreateOptionsMenu(Menu menu) {
1715 if (isWorkspaceLocked()) {
1716 return false;
1717 }
1718
1719 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001720
1721 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1722 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1723 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001724 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1725 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1726 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001727 String helpUrl = getString(R.string.help_url);
1728 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001729 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1730 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1731
Winson Chung70d72102011-08-12 11:24:35 -07001732 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1733 .setIcon(android.R.drawable.ic_menu_gallery)
1734 .setAlphabeticShortcut('W');
1735 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1736 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001737 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001738 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001739 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1740 .setIcon(android.R.drawable.ic_menu_preferences)
1741 .setIntent(settings)
1742 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001743 if (!helpUrl.isEmpty()) {
1744 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1745 .setIcon(android.R.drawable.ic_menu_help)
1746 .setIntent(help)
1747 .setAlphabeticShortcut('H');
1748 }
Winson Chung70d72102011-08-12 11:24:35 -07001749 return true;
1750 }
1751
1752 @Override
1753 public boolean onPrepareOptionsMenu(Menu menu) {
1754 super.onPrepareOptionsMenu(menu);
1755
1756 if (mAppsCustomizeTabHost.isTransitioning()) {
1757 return false;
1758 }
1759 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1760 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1761
1762 return true;
1763 }
1764
1765 @Override
1766 public boolean onOptionsItemSelected(MenuItem item) {
1767 switch (item.getItemId()) {
1768 case MENU_WALLPAPER_SETTINGS:
1769 startWallpaper();
1770 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001771 }
1772
1773 return super.onOptionsItemSelected(item);
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001776 @Override
1777 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001778 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001779 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001780 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001781 }
1782
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 public boolean isWorkspaceLocked() {
1784 return mWorkspaceLoading || mWaitingForResult;
1785 }
1786
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001788 mPendingAddInfo.container = ItemInfo.NO_ID;
1789 mPendingAddInfo.screen = -1;
1790 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1791 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001792 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001793 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001794 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001795
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001796 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1797 AppWidgetProviderInfo appWidgetInfo) {
1798 if (appWidgetInfo.configure != null) {
1799 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001800
Bjorn Bringert7984c942009-12-09 15:38:25 +00001801 // Launch over to configure widget, if needed
1802 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001803 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001804 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001805 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001807 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001808 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1809 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001810 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001811 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815 /**
1816 * Process a shortcut drop.
1817 *
1818 * @param componentName The name of the component
1819 * @param screen The screen where it should be added
1820 * @param cell The cell it should be added to, optional
1821 * @param position The location on the screen where it was dropped, optional
1822 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001823 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1824 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001826 mPendingAddInfo.container = container;
1827 mPendingAddInfo.screen = screen;
1828 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001829
1830 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 mPendingAddInfo.cellX = cell[0];
1832 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834
1835 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1836 createShortcutIntent.setComponent(componentName);
1837 processShortcut(createShortcutIntent);
1838 }
1839
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 /**
1841 * Process a widget drop.
1842 *
1843 * @param info The PendingAppWidgetInfo of the widget being added.
1844 * @param screen The screen where it should be added
1845 * @param cell The cell it should be added to, optional
1846 * @param position The location on the screen where it was dropped, optional
1847 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001848 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001849 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001850 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001851 mPendingAddInfo.container = info.container = container;
1852 mPendingAddInfo.screen = info.screen = screen;
1853 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001854 mPendingAddInfo.minSpanX = info.minSpanX;
1855 mPendingAddInfo.minSpanY = info.minSpanY;
1856
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001858 mPendingAddInfo.cellX = cell[0];
1859 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001860 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001861 if (span != null) {
1862 mPendingAddInfo.spanX = span[0];
1863 mPendingAddInfo.spanY = span[1];
1864 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001865
Adam Cohened66b2b2012-01-23 17:28:51 -08001866 AppWidgetHostView hostView = info.boundWidget;
1867 int appWidgetId;
1868 if (hostView != null) {
1869 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001871 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 // In this case, we either need to start an activity to get permission to bind
1873 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001874 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001875 Bundle options = info.bindOptions;
1876
1877 boolean success = false;
1878 if (options != null) {
1879 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1880 info.componentName, options);
1881 } else {
1882 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1883 info.componentName);
1884 }
1885 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001886 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001887 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001888 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001889 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1890 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1891 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001892 // TODO: we need to make sure that this accounts for the options bundle.
1893 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001894 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1895 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001896 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001897 }
1898
Joe Onoratodeb98af2010-02-19 14:59:39 -08001899 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 // Handle case where user selected "Applications"
1901 String applicationName = getResources().getString(R.string.group_applications);
1902 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 if (applicationName != null && applicationName.equals(shortcutName)) {
1905 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1906 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001907
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001908 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1909 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001910 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001911 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001913 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916
Winson Chung24ab2f12010-09-16 14:10:47 -07001917 void processWallpaper(Intent intent) {
1918 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1919 }
1920
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1922 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001923 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 folderInfo.title = getText(R.string.folder_name);
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001927 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1928 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001929 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930
1931 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 FolderIcon newFolder =
1933 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1934 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1935 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001936 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
Romain Guycbb89e42009-06-08 15:52:54 -07001938
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001940 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001944 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001946 Intent chooser = Intent.createChooser(pickWallpaper,
1947 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001948 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1949 // Removed in Eclair MR1
1950// WallpaperManager wm = (WallpaperManager)
1951// getSystemService(Context.WALLPAPER_SERVICE);
1952// WallpaperInfo wi = wm.getWallpaperInfo();
1953// if (wi != null && wi.getSettingsActivity() != null) {
1954// LabeledIntent li = new LabeledIntent(getPackageName(),
1955// R.string.configure_wallpaper, 0);
1956// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1957// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1958// }
Mike Clerona0618e42009-10-22 13:55:21 -07001959 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001962 /**
1963 * Registers various content observers. The current implementation registers
1964 * only a favorites observer to keep track of the favorites applications.
1965 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001966 private void registerContentObservers() {
1967 ContentResolver resolver = getContentResolver();
1968 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1969 true, mWidgetObserver);
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 @Override
1973 public boolean dispatchKeyEvent(KeyEvent event) {
1974 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1975 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001977 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001978 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001979 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001980 dumpState();
1981 return true;
1982 }
1983 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 }
1985 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1986 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001987 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 return true;
1989 }
1990 }
1991
1992 return super.dispatchKeyEvent(event);
1993 }
1994
Joe Onorato88ec0992009-11-19 13:16:06 -08001995 @Override
1996 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001997 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001998 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001999 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002000 Folder openFolder = mWorkspace.getOpenFolder();
2001 if (openFolder.isEditingName()) {
2002 openFolder.dismissEditingName();
2003 } else {
2004 closeFolder();
2005 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002006 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002007 mWorkspace.exitWidgetResizeMode();
2008
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002009 // Back button is a no-op here, but give at least some feedback for the button press
2010 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002011 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002015 * Re-listen when widgets are reset.
2016 */
2017 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002018 if (mAppWidgetHost != null) {
2019 mAppWidgetHost.startListening();
2020 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002021 }
2022
2023 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 * Launches the intent referred by the clicked shortcut.
2025 *
2026 * @param v The view representing the clicked shortcut.
2027 */
2028 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002029 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2030 // view has detached (it's possible for this to happen if the view is removed mid touch).
2031 if (v.getWindowToken() == null) {
2032 return;
2033 }
2034
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002035 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002036 return;
2037 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002040 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002042 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002043
2044 ComponentName widgetComp = new ComponentName(this, WidgetAdder.class);
2045 if (intent.getComponent().getClassName().equals(widgetComp.getClassName())) {
2046 showAllApps(true);
2047 return;
2048 }
Joe Onorato13724ea2009-12-02 21:16:35 -08002049 int[] pos = new int[2];
2050 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002051 intent.setSourceBounds(new Rect(pos[0], pos[1],
2052 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002053
2054 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002055
2056 if (success && v instanceof BubbleTextView) {
2057 mWaitingForResume = (BubbleTextView) v;
2058 mWaitingForResume.setStayPressed(true);
2059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002061 if (v instanceof FolderIcon) {
2062 FolderIcon fi = (FolderIcon) v;
2063 handleFolderClick(fi);
2064 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002065 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002066 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002067 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002068 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002069 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002070 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072 }
2073
Michael Jurka0e260592010-06-30 17:07:39 -07002074 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002075 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002076 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002077 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2078 showWorkspace(true);
2079 }
Michael Jurka0e260592010-06-30 17:07:39 -07002080 return false;
2081 }
2082
Michael Jurkaaf442092010-06-10 17:01:57 -07002083 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002084 * Event handler for the search button
2085 *
2086 * @param v The view that was clicked.
2087 */
2088 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002089 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2090
Amith Yamasania135ba82011-08-09 17:42:01 -07002091 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002092 }
2093
2094 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002095 * Event handler for the voice button
2096 *
2097 * @param v The view that was clicked.
2098 */
2099 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002100 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002101
Bjorn Bringertc459e522013-06-07 19:36:01 +01002102 startVoice();
2103 }
2104
2105 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002106 try {
2107 final SearchManager searchManager =
2108 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2109 ComponentName activityName = searchManager.getGlobalSearchActivity();
2110 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002111 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002112 if (activityName != null) {
2113 intent.setPackage(activityName.getPackageName());
2114 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002115 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002116 } catch (ActivityNotFoundException e) {
2117 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002118 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002119 startActivitySafely(null, intent, "onClickVoiceButton");
2120 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002121 }
2122
2123 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002124 * Event handler for the "grid" button that appears on the home screen, which
2125 * enters all apps mode.
2126 *
2127 * @param v The view that was clicked.
2128 */
2129 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002130 showAllApps(true);
2131 }
2132
2133 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002134 // Provide the same haptic feedback that the system offers for virtual keys.
2135 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002136 }
2137
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002138 public void onClickAppMarketButton(View v) {
2139 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002140 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002141 } else {
2142 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002143 }
2144 }
2145
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002146 void startApplicationDetailsActivity(ComponentName componentName) {
2147 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002148 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2149 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002150 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002151 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002152 }
2153
Patrick Dubroy5539af72010-09-07 15:22:01 -07002154 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2155 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2156 // System applications cannot be installed. For now, show a toast explaining that.
2157 // We may give them the option of disabling apps this way.
2158 int messageId = R.string.uninstall_system_app_text;
2159 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2160 } else {
2161 String packageName = appInfo.componentName.getPackageName();
2162 String className = appInfo.componentName.getClassName();
2163 Intent intent = new Intent(
2164 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002165 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2166 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002167 startActivity(intent);
2168 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002169 }
2170
Michael Jurka86a720e2012-05-09 11:23:23 -07002171 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002175 // Only launch using the new animation if the shortcut has not opted out (this is a
2176 // private contract between launcher and may be ignored in the future).
2177 boolean useLaunchAnimation = (v != null) &&
2178 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2179 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002180 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2181 v.getMeasuredWidth(), v.getMeasuredHeight());
2182
2183 startActivity(intent, opts.toBundle());
2184 } else {
2185 startActivity(intent);
2186 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002187 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 } catch (SecurityException e) {
2189 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002190 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002192 "or use the exported attribute for this activity. "
2193 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002195 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002197
Michael Jurka86a720e2012-05-09 11:23:23 -07002198 boolean startActivitySafely(View v, Intent intent, Object tag) {
2199 boolean success = false;
2200 try {
2201 success = startActivity(v, intent, tag);
2202 } catch (ActivityNotFoundException e) {
2203 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2204 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2205 }
2206 return success;
2207 }
2208
Romain Guy8e633c52010-03-04 12:51:36 -08002209 void startActivityForResultSafely(Intent intent, int requestCode) {
2210 try {
2211 startActivityForResult(intent, requestCode);
2212 } catch (ActivityNotFoundException e) {
2213 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2214 } catch (SecurityException e) {
2215 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2216 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2217 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2218 "or use the exported attribute for this activity.", e);
2219 }
2220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221
Adam Cohena9cf38f2011-05-02 15:36:58 -07002222 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002223 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002224 Folder openFolder = mWorkspace.getFolderForTag(info);
2225
2226 // If the folder info reports that the associated folder is open, then verify that
2227 // it is actually opened. There have been a few instances where this gets out of sync.
2228 if (info.opened && openFolder == null) {
2229 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2230 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2231 info.opened = false;
2232 }
2233
Adam Cohenfb91f302012-06-11 15:45:18 -07002234 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 // Close any open folder
2236 closeFolder();
2237 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002238 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 } else {
2240 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 int folderScreen;
2242 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002243 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 // .. and close it
2245 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002246 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 // Close any folder open on the current screen
2248 closeFolder();
2249 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002250 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 }
2252 }
2253 }
2254 }
2255
Adam Cohen268c4752012-06-06 17:47:33 -07002256 /**
2257 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2258 * in the DragLayer in the exact absolute location of the original FolderIcon.
2259 */
2260 private void copyFolderIconToImage(FolderIcon fi) {
2261 final int width = fi.getMeasuredWidth();
2262 final int height = fi.getMeasuredHeight();
2263
2264 // Lazy load ImageView, Bitmap and Canvas
2265 if (mFolderIconImageView == null) {
2266 mFolderIconImageView = new ImageView(this);
2267 }
2268 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2269 mFolderIconBitmap.getHeight() != height) {
2270 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2271 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2272 }
2273
2274 DragLayer.LayoutParams lp;
2275 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2276 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2277 } else {
2278 lp = new DragLayer.LayoutParams(width, height);
2279 }
2280
Adam Cohen307fe232012-08-16 17:55:58 -07002281 // The layout from which the folder is being opened may be scaled, adjust the starting
2282 // view size by this scale factor.
2283 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002284 lp.customPosition = true;
2285 lp.x = mRectForFolderAnimation.left;
2286 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002287 lp.width = (int) (scale * width);
2288 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002289
2290 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2291 fi.draw(mFolderIconCanvas);
2292 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002293 if (fi.getFolder() != null) {
2294 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2295 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002296 }
Adam Cohen268c4752012-06-06 17:47:33 -07002297 // Just in case this image view is still in the drag layer from a previous animation,
2298 // we remove it and re-add it.
2299 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2300 mDragLayer.removeView(mFolderIconImageView);
2301 }
2302 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002303 if (fi.getFolder() != null) {
2304 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002305 }
Adam Cohen268c4752012-06-06 17:47:33 -07002306 }
2307
Adam Cohen2801caf2011-05-13 20:57:39 -07002308 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002309 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002310 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2311 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2312 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2313
Adam Cohenc51934b2011-07-26 21:07:43 -07002314 FolderInfo info = (FolderInfo) fi.getTag();
2315 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2316 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002317 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2318 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002319 }
2320
Adam Cohen268c4752012-06-06 17:47:33 -07002321 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2322 copyFolderIconToImage(fi);
2323 fi.setVisibility(View.INVISIBLE);
2324
Michael Jurka2ecf9952012-06-18 12:52:28 -07002325 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002326 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002327 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2328 oa.start();
2329 }
2330
Adam Cohen268c4752012-06-06 17:47:33 -07002331 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002332 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002333 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2334 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2335 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2336
Adam Cohen268c4752012-06-06 17:47:33 -07002337 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002338
Adam Cohen268c4752012-06-06 17:47:33 -07002339 // We remove and re-draw the FolderIcon in-case it has changed
2340 mDragLayer.removeView(mFolderIconImageView);
2341 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002342 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002343 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002344 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002345 oa.addListener(new AnimatorListenerAdapter() {
2346 @Override
2347 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002348 if (cl != null) {
2349 cl.clearFolderLeaveBehind();
2350 // Remove the ImageView copy of the FolderIcon and make the original visible.
2351 mDragLayer.removeView(mFolderIconImageView);
2352 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002353 }
2354 }
2355 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002356 oa.start();
2357 }
2358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002360 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 * is animated relative to the specified View. If the View is null, no animation
2362 * is played.
2363 *
2364 * @param folderInfo The FolderInfo describing the folder to open.
2365 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002366 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002367 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002368 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369
Adam Cohena9cf38f2011-05-02 15:36:58 -07002370 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371
Adam Cohen4554ee12011-08-03 16:13:21 -07002372 // Just verify that the folder hasn't already been added to the DragLayer.
2373 // There was a one-off crash where the folder had a parent already.
2374 if (folder.getParent() == null) {
2375 mDragLayer.addView(folder);
2376 mDragController.addDropTarget((DropTarget) folder);
2377 } else {
2378 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2379 folder.getParent() + ").");
2380 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002381 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002382 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002383
2384 // Notify the accessibility manager that this folder "window" has appeared and occluded
2385 // the workspace items
2386 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2387 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002388 }
2389
2390 public void closeFolder() {
2391 Folder folder = mWorkspace.getOpenFolder();
2392 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002393 if (folder.isEditingName()) {
2394 folder.dismissEditingName();
2395 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002396 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002397
2398 // Dismiss the folder cling
2399 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002400 }
2401 }
2402
2403 void closeFolder(Folder folder) {
2404 folder.getInfo().opened = false;
2405
2406 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2407 if (parent != null) {
2408 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2409 shrinkAndFadeInFolderIcon(fi);
2410 }
2411 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002412
2413 // Notify the accessibility manager that this folder "window" has disappeard and no
2414 // longer occludeds the workspace items
2415 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002419 if (!isDraggingEnabled()) return false;
2420 if (isWorkspaceLocked()) return false;
2421 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422
2423 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002424 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 }
2426
Michael Jurka0280c3b2010-09-17 15:00:07 -07002427 resetAddInfo();
2428 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002430 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 return true;
2432 }
2433
Winson Chung3d503fb2011-07-13 17:25:49 -07002434 // The hotseat touch handling does not go through Workspace, and we always allow long press
2435 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002436 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002437 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2438 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002439 if (itemUnderLongClick == null) {
2440 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002441 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2442 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002443 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002447 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 }
2449 }
2450 }
2451 return true;
2452 }
2453
Winson Chung3d503fb2011-07-13 17:25:49 -07002454 boolean isHotseatLayout(View layout) {
2455 return mHotseat != null && layout != null &&
2456 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2457 }
2458 Hotseat getHotseat() {
2459 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002460 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002461 SearchDropTargetBar getSearchBar() {
2462 return mSearchDropTargetBar;
2463 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002464
Winson Chung3d503fb2011-07-13 17:25:49 -07002465 /**
2466 * Returns the CellLayout of the specified container at the specified screen.
2467 */
2468 CellLayout getCellLayout(long container, int screen) {
2469 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2470 if (mHotseat != null) {
2471 return mHotseat.getLayout();
2472 } else {
2473 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002474 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002475 } else {
2476 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002477 }
2478 }
2479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 Workspace getWorkspace() {
2481 return mWorkspace;
2482 }
2483
Daniel Sandler843e8602010-06-07 14:59:01 -04002484 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
Craig Mautner360310b2012-10-26 15:13:08 -07002485 @Override
Daniel Sandler843e8602010-06-07 14:59:01 -04002486 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002487 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002488 }
2489
Craig Mautner360310b2012-10-26 15:13:08 -07002490 @Override
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002491 public boolean isAllAppsButtonRank(int rank) {
2492 return mHotseat.isAllAppsButtonRank(rank);
2493 }
2494
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 /**
2496 * Helper method for the cameraZoomIn/cameraZoomOut animations
2497 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498 * @param scaleFactor The scale factor used for the zoom
2499 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002500 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002501 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002502 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002504
Winson Chung18f41f82012-05-09 13:28:10 -07002505 void disableWallpaperIfInAllApps() {
2506 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002507 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002508 if (mAppsCustomizeTabHost != null &&
2509 !mAppsCustomizeTabHost.isTransitioning()) {
2510 updateWallpaperVisibility(false);
2511 }
2512 }
2513 }
2514
Craig Mautner360310b2012-10-26 15:13:08 -07002515 private void setWorkspaceBackground(boolean workspace) {
2516 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002517 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002518 }
2519
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002520 void updateWallpaperVisibility(boolean visible) {
2521 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2522 int curflags = getWindow().getAttributes().flags
2523 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2524 if (wpflags != curflags) {
2525 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2526 }
Craig Mautner360310b2012-10-26 15:13:08 -07002527 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002528 }
2529
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002530 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2531 if (v instanceof LauncherTransitionable) {
2532 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2533 }
2534 }
2535
Michael Jurkabed61d22012-02-14 22:51:29 -08002536 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2537 if (v instanceof LauncherTransitionable) {
2538 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2539 }
Winson Chung70442722012-02-10 15:43:22 -08002540
2541 // Update the workspace transition step as well
2542 dispatchOnLauncherTransitionStep(v, 0f);
2543 }
2544
2545 private void dispatchOnLauncherTransitionStep(View v, float t) {
2546 if (v instanceof LauncherTransitionable) {
2547 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2548 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002549 }
2550
2551 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2552 if (v instanceof LauncherTransitionable) {
2553 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2554 }
Winson Chung70442722012-02-10 15:43:22 -08002555
2556 // Update the workspace transition step as well
2557 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002558 }
2559
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002560 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002561 * Things to test when changing the following seven functions.
2562 * - Home from workspace
2563 * - from center screen
2564 * - from other screens
2565 * - Home from all apps
2566 * - from center screen
2567 * - from other screens
2568 * - Back from all apps
2569 * - from center screen
2570 * - from other screens
2571 * - Launch app from workspace and quit
2572 * - with back
2573 * - with home
2574 * - Launch app from all apps and quit
2575 * - with back
2576 * - with home
2577 * - Go to a screen that's not the default, then all
2578 * apps, and launch and app, and go back
2579 * - with back
2580 * -with home
2581 * - On workspace, long press power and go back
2582 * - with back
2583 * - with home
2584 * - On all apps, long press power and go back
2585 * - with back
2586 * - with home
2587 * - On workspace, power off
2588 * - On all apps, power off
2589 * - Launch an app and turn off the screen while in that app
2590 * - Go back with home key
2591 * - Go back with back key TODO: make this not go to workspace
2592 * - From all apps
2593 * - From workspace
2594 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2595 * - From all apps
2596 * - From the center workspace
2597 * - From another workspace
2598 */
2599
2600 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002601 * Zoom the camera out from the workspace to reveal 'toView'.
2602 * Assumes that the view to show is anchored at either the very top or very bottom
2603 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002604 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002605 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002606 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002607 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002608 mStateAnimation.cancel();
2609 mStateAnimation = null;
2610 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002611 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002612
Winson Chungf0ea4d32011-06-06 14:27:16 -07002613 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2614 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2615 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002616 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002617 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002618 final int startDelay =
2619 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002620
Michael Jurkab3e22d92011-10-31 15:58:33 -07002621 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622
Michael Jurkad74c9842011-07-10 12:44:21 -07002623 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002624 Animator workspaceAnim =
2625 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626
2627 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002628 toView.setScaleX(scale);
2629 toView.setScaleY(scale);
2630 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2631 scaleAnim.
2632 scaleX(1f).scaleY(1f).
2633 setDuration(duration).
2634 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002635
Winson Chungf0ea4d32011-06-06 14:27:16 -07002636 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002637 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002638 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002639 .ofFloat(toView, "alpha", 0f, 1f)
2640 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002642 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2643 @Override
2644 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002645 if (animation == null) {
2646 throw new RuntimeException("animation is null");
2647 }
Winson Chung70442722012-02-10 15:43:22 -08002648 float t = (Float) animation.getAnimatedValue();
2649 dispatchOnLauncherTransitionStep(fromView, t);
2650 dispatchOnLauncherTransitionStep(toView, t);
2651 }
2652 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002653
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002654 // toView should appear right at the end of the workspace shrink
2655 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002656 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002657 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002658 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002659
2660 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002661 boolean animationCancelled = false;
2662
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002663 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002664 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002665 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002666 // Prepare the position
2667 toView.setTranslationX(0.0f);
2668 toView.setTranslationY(0.0f);
2669 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002670 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002671 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002672 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002673 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002674 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2675 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002676
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002677 if (mWorkspace != null && !springLoaded && !LauncherAppState.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002678 // Hide the workspace scrollbar
2679 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002680 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002681 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002682 if (!animationCancelled) {
2683 updateWallpaperVisibility(false);
2684 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002685
2686 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002687 if (mSearchDropTargetBar != null) {
2688 mSearchDropTargetBar.hideSearchBar(false);
2689 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002690 }
2691
Adam Cohencff6af82011-09-13 14:51:53 -07002692 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002693 public void onAnimationCancel(Animator animation) {
2694 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002695 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002696 });
2697
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002698 if (workspaceAnim != null) {
2699 mStateAnimation.play(workspaceAnim);
2700 }
Michael Jurka1899a362011-11-03 13:50:45 -07002701
2702 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002703
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002704 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2705 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002706
2707 // If any of the objects being animated haven't been measured/laid out
2708 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002709 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002710 (mWorkspace.getMeasuredWidth() == 0) ||
2711 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002712 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002713 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002715 final AnimatorSet stateAnimation = mStateAnimation;
2716 final Runnable startAnimRunnable = new Runnable() {
2717 public void run() {
2718 // Check that mStateAnimation hasn't changed while
2719 // we waited for a layout/draw pass
2720 if (mStateAnimation != stateAnimation)
2721 return;
2722 setPivotsForZoom(toView, scale);
2723 dispatchOnLauncherTransitionStart(fromView, animated, false);
2724 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002725 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002726 }
2727 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002728 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002729 final ViewTreeObserver observer = toView.getViewTreeObserver();
2730 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2731 public void onGlobalLayout() {
2732 startAnimRunnable.run();
2733 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2734 }
2735 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002736 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002737 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002738 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002739 } else {
2740 toView.setTranslationX(0.0f);
2741 toView.setTranslationY(0.0f);
2742 toView.setScaleX(1.0f);
2743 toView.setScaleY(1.0f);
2744 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002745 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002746
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002747 if (!springLoaded && !LauncherAppState.isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002748 // Hide the workspace scrollbar
2749 mWorkspace.hideScrollingIndicator(true);
2750 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002751
2752 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002753 if (mSearchDropTargetBar != null) {
2754 mSearchDropTargetBar.hideSearchBar(false);
2755 }
Michael Jurkaabded662011-03-04 12:06:57 -08002756 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002757 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002758 dispatchOnLauncherTransitionStart(fromView, animated, false);
2759 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002760 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002761 dispatchOnLauncherTransitionStart(toView, animated, false);
2762 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002763 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002764 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002765 }
2766
2767 /**
2768 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002769 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002770 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002772 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2773 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002774
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002776 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002777 mStateAnimation.cancel();
2778 mStateAnimation = null;
2779 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002781
Winson Chungf0ea4d32011-06-06 14:27:16 -07002782 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002783 final int fadeOutDuration =
2784 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002785 final float scaleFactor = (float)
2786 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2787 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002788 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002789 Animator workspaceAnim = null;
2790
2791 if (toState == State.WORKSPACE) {
2792 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2793 workspaceAnim = mWorkspace.getChangeStateAnimation(
2794 Workspace.State.NORMAL, animated, stagger);
2795 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2796 workspaceAnim = mWorkspace.getChangeStateAnimation(
2797 Workspace.State.SPRING_LOADED, animated);
2798 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002799
Michael Jurkab3e22d92011-10-31 15:58:33 -07002800 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002801 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002802 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002804 final LauncherViewPropertyAnimator scaleAnim =
2805 new LauncherViewPropertyAnimator(fromView);
2806 scaleAnim.
2807 scaleX(scaleFactor).scaleY(scaleFactor).
2808 setDuration(duration).
2809 setInterpolator(new Workspace.ZoomInInterpolator());
2810
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002811 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002812 .ofFloat(fromView, "alpha", 1f, 0f)
2813 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002814 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002815 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2816 @Override
2817 public void onAnimationUpdate(ValueAnimator animation) {
2818 float t = 1f - (Float) animation.getAnimatedValue();
2819 dispatchOnLauncherTransitionStep(fromView, t);
2820 dispatchOnLauncherTransitionStep(toView, t);
2821 }
2822 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002823
Michael Jurka2ecf9952012-06-18 12:52:28 -07002824 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002825
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002826 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2827 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002828 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002829
2830 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002831 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002832 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002833 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002835 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2836 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002837 if (mWorkspace != null) {
2838 mWorkspace.hideScrollingIndicator(false);
2839 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002840 if (onCompleteRunnable != null) {
2841 onCompleteRunnable.run();
2842 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002843 mAppsCustomizeContent.updateCurrentPageScroll();
2844 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002845 }
2846 });
2847
Winson Chungf0ea4d32011-06-06 14:27:16 -07002848 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002849 if (workspaceAnim != null) {
2850 mStateAnimation.play(workspaceAnim);
2851 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002852 dispatchOnLauncherTransitionStart(fromView, animated, true);
2853 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002854 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 } else {
2856 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002857 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002858 dispatchOnLauncherTransitionStart(fromView, animated, true);
2859 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002860 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002861 dispatchOnLauncherTransitionStart(toView, animated, true);
2862 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002863 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002864 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002865 }
2866
Michael Jurkae326f182011-11-21 14:05:46 -08002867 @Override
2868 public void onTrimMemory(int level) {
2869 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002870 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002871 mAppsCustomizeTabHost.onTrimMemory();
2872 }
2873 }
2874
Winson Chung18f41f82012-05-09 13:28:10 -07002875 @Override
2876 public void onWindowFocusChanged(boolean hasFocus) {
2877 if (!hasFocus) {
2878 // When another window occludes launcher (like the notification shade, or recents),
2879 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2880 updateWallpaperVisibility(true);
2881 } else {
2882 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002883 mWorkspace.postDelayed(new Runnable() {
2884 @Override
2885 public void run() {
2886 disableWallpaperIfInAllApps();
2887 }
2888 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002889 }
2890 }
2891
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002893 showWorkspace(animated, null);
2894 }
2895
2896 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002897 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002898 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002899 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002900 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002901
Winson Chungc7d2b602012-05-16 17:02:20 -07002902 // Show the search bar (only animate if we were showing the drop target bar in spring
2903 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002904 if (mSearchDropTargetBar != null) {
2905 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2906 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002907
Michael Jurkab737ee62011-11-15 15:57:22 -08002908 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002909 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002910
2911 // Set focus to the AppsCustomize button
2912 if (mAllAppsButton != null) {
2913 mAllAppsButton.requestFocus();
2914 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002915 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002916
Michael Jurkab737ee62011-11-15 15:57:22 -08002917 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002918
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002919 // Change the state *after* we've called all the transition code
2920 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002921
Winson Chung5fb63472011-02-02 17:03:37 -08002922 // Resume the auto-advance of widgets
2923 mUserPresent = true;
2924 updateRunning();
2925
alanv1d4fde62012-10-17 13:15:47 -07002926 // Send an accessibility event to announce the context change
2927 getWindow().getDecorView()
2928 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002929
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002930 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002931 }
2932
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002933 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002934 }
2935
Michael Jurkab3e22d92011-10-31 15:58:33 -07002936 void showAllApps(boolean animated) {
2937 if (mState != State.WORKSPACE) return;
2938
2939 showAppsCustomizeHelper(animated, false);
2940 mAppsCustomizeTabHost.requestFocus();
2941
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 // Change the state *after* we've called all the transition code
2943 mState = State.APPS_CUSTOMIZE;
2944
2945 // Pause the auto-advance of widgets until we are out of AllApps
2946 mUserPresent = false;
2947 updateRunning();
2948 closeFolder();
2949
2950 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002951 getWindow().getDecorView()
2952 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002953 }
2954
Adam Cohen7777d962011-08-18 18:58:38 -07002955 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002956 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002957 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002958 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002959 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002960 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002961 }
Adam Cohen7777d962011-08-18 18:58:38 -07002962
Adam Cohened66b2b2012-01-23 17:28:51 -08002963 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2964 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002965 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2966
Winson Chunge7a03942011-08-05 15:05:12 -07002967 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002968 @Override
2969 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002970 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002971 // Before we show workspace, hide all apps again because
2972 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2973 // clean up our state transition functions
2974 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002975 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002976 } else {
2977 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002978 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002979 }
2980 }, (extendedDelay ?
2981 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2982 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2983 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002984
Michael Jurkad3ef3062010-11-23 16:23:58 -08002985 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002986 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987 final boolean animated = true;
2988 final boolean springLoaded = true;
2989 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002990 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002991 }
2992 // Otherwise, we are not in spring loaded mode, so don't do anything.
2993 }
2994
Michael Jurkab737ee62011-11-15 15:57:22 -08002995 void hideDockDivider() {
2996 if (mQsbDivider != null && mDockDivider != null) {
2997 mQsbDivider.setVisibility(View.INVISIBLE);
2998 mDockDivider.setVisibility(View.INVISIBLE);
2999 }
3000 }
3001
3002 void showDockDivider(boolean animated) {
3003 if (mQsbDivider != null && mDockDivider != null) {
3004 mQsbDivider.setVisibility(View.VISIBLE);
3005 mDockDivider.setVisibility(View.VISIBLE);
3006 if (mDividerAnimator != null) {
3007 mDividerAnimator.cancel();
3008 mQsbDivider.setAlpha(1f);
3009 mDockDivider.setAlpha(1f);
3010 mDividerAnimator = null;
3011 }
3012 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003013 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3014 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3015 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003016 int duration = 0;
3017 if (mSearchDropTargetBar != null) {
3018 duration = mSearchDropTargetBar.getTransitionInDuration();
3019 }
3020 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003021 mDividerAnimator.start();
3022 }
3023 }
3024 }
3025
Michael Jurkab3e22d92011-10-31 15:58:33 -07003026 void lockAllApps() {
3027 // TODO
3028 }
3029
3030 void unlockAllApps() {
3031 // TODO
3032 }
3033
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003035 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003037 void showHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003038 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003039 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003040 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003041 int duration = 0;
3042 if (mSearchDropTargetBar != null) {
3043 duration = mSearchDropTargetBar.getTransitionInDuration();
3044 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003045 mHotseat.animate().alpha(1f).setDuration(duration);
3046 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003047 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003049 }
3050 }
3051 }
3052
3053 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003054 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003056 void hideHotseat(boolean animated) {
Daniel Sandlercc8befa2013-06-11 14:45:48 -04003057 if (!LauncherAppState.isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003058 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003059 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003060 int duration = 0;
3061 if (mSearchDropTargetBar != null) {
3062 duration = mSearchDropTargetBar.getTransitionOutDuration();
3063 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003064 mHotseat.animate().alpha(0f).setDuration(duration);
3065 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003066 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003067 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003068 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003069 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003070 }
3071
Patrick Dubroy5f445422011-02-18 14:35:21 -08003072 /**
3073 * Add an item from all apps or customize onto the given workspace screen.
3074 * If layout is null, add to the current screen.
3075 */
3076 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003077 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003078 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003079 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003080 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003081
Winson Chungdff8ebb2011-09-08 17:25:31 -07003082 /** Maps the current orientation to an index for referencing orientation correct global icons */
3083 private int getCurrentOrientationIndexForGlobalIcons() {
3084 // default - 0, landscape - 1
3085 switch (getResources().getConfiguration().orientation) {
3086 case Configuration.ORIENTATION_LANDSCAPE:
3087 return 1;
3088 default:
3089 return 0;
3090 }
3091 }
3092
Michael Jurkaae65ee32012-04-30 11:45:54 -07003093 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003094 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003095 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 // Look for the toolbar icon specified in the activity meta-data
3097 Bundle metaData = packageManager.getActivityInfo(
3098 activityName, PackageManager.GET_META_DATA).metaData;
3099 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003100 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003101 if (iconResId != 0) {
3102 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003103 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 }
3105 }
3106 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003107 // This can happen if the activity defines an invalid drawable
3108 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3109 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003110 } catch (Resources.NotFoundException nfe) {
3111 // This can happen if the activity defines an invalid drawable
3112 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3113 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003114 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003115 return null;
3116 }
3117
3118 // if successful in getting icon, return it; otherwise, set button to use default drawable
3119 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003120 int buttonId, ComponentName activityName, int fallbackDrawableId,
3121 String toolbarResourceName) {
3122 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003123 Resources r = getResources();
3124 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3125 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003126
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003127 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003128 // If we were unable to find the icon via the meta-data, use a generic one
3129 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003130 toolbarIcon = r.getDrawable(fallbackDrawableId);
3131 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003132 if (button != null) {
3133 button.setCompoundDrawables(toolbarIcon, null, null, null);
3134 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003135 return null;
3136 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003137 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003138 if (button != null) {
3139 button.setCompoundDrawables(toolbarIcon, null, null, null);
3140 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003141 return toolbarIcon.getConstantState();
3142 }
3143 }
3144
3145 // if successful in getting icon, return it; otherwise, set button to use default drawable
3146 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003147 int buttonId, ComponentName activityName, int fallbackDrawableId,
3148 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003149 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003150 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003151
Michael Jurka19e0fc52011-07-22 18:00:21 -07003152 if (button != null) {
3153 // If we were unable to find the icon via the meta-data, use a
3154 // generic one
3155 if (toolbarIcon == null) {
3156 button.setImageResource(fallbackDrawableId);
3157 } else {
3158 button.setImageDrawable(toolbarIcon);
3159 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003160 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003161
3162 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3163
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 }
3165
Winson Chung1b884092012-06-08 17:13:02 -07003166 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003167 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003168 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003169 }
3170
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003171 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003172 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003173 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003174 }
3175
Winson Chungbb185bd2011-11-21 12:31:42 -08003176 private void invalidatePressedFocusedStates(View container, View button) {
3177 if (container instanceof HolographicLinearLayout) {
3178 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3179 layout.invalidatePressedFocusedStates();
3180 } else if (button instanceof HolographicImageView) {
3181 HolographicImageView view = (HolographicImageView) button;
3182 view.invalidatePressedFocusedStates();
3183 }
3184 }
3185
Winson Chungc51db6a2011-10-05 11:44:49 -07003186 private boolean updateGlobalSearchIcon() {
3187 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003188 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003189 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003190 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003191
Winson Chung1cad91e2011-05-25 17:41:01 -07003192 final SearchManager searchManager =
3193 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3194 ComponentName activityName = searchManager.getGlobalSearchActivity();
3195 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003196 int coi = getCurrentOrientationIndexForGlobalIcons();
3197 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003198 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3199 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3200 if (sGlobalSearchIcon[coi] == null) {
3201 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3202 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3203 TOOLBAR_ICON_METADATA_NAME);
3204 }
3205
Winson Chungc51db6a2011-10-05 11:44:49 -07003206 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3207 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003208 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003209 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003210 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003211 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3213 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3214 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003215 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003216 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003217 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003218 }
3219 }
3220
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003221 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003222 final View searchButtonContainer = findViewById(R.id.search_button_container);
3223 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003224 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003225 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003226 }
3227
Winson Chungc51db6a2011-10-05 11:44:49 -07003228 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003229 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003230 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003231
Winson Chungc51db6a2011-10-05 11:44:49 -07003232 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003233 final SearchManager searchManager =
3234 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3235 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3236
3237 ComponentName activityName = null;
3238 if (globalSearchActivity != null) {
3239 // Check if the global search activity handles voice search
3240 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3241 intent.setPackage(globalSearchActivity.getPackageName());
3242 activityName = intent.resolveActivity(getPackageManager());
3243 }
3244
3245 if (activityName == null) {
3246 // Fallback: check if an activity other than the global search activity
3247 // resolves this
3248 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3249 activityName = intent.resolveActivity(getPackageManager());
3250 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003251 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003252 int coi = getCurrentOrientationIndexForGlobalIcons();
3253 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003254 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3255 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3256 if (sVoiceSearchIcon[coi] == null) {
3257 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3258 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3259 TOOLBAR_ICON_METADATA_NAME);
3260 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003262 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003263 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003264 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003266 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003268 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003269 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003270 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003271 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003272 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003273
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003274 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003275 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3276 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003277 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003278 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003279 }
3280
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003281 public void setVoiceButtonProxyVisible(boolean visible) {
3282 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3283 if (voiceButtonProxy != null) {
3284 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3285 }
3286 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003287 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003288 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003289 */
3290 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003291 final View marketButton = findViewById(R.id.market_button);
3292 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3293 // Find the app market activity by resolving an intent.
3294 // (If multiple app markets are installed, it will return the ResolverActivity.)
3295 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003296 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003297 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003298 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003299 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003300 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3301 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003302 marketButton.setVisibility(View.VISIBLE);
3303 } else {
3304 // We should hide and disable the view so that we don't try and restore the visibility
3305 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3306 marketButton.setVisibility(View.GONE);
3307 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003308 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003309 }
3310
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003311 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003312 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3313 Resources r = getResources();
3314 Drawable marketIconDrawable = d.newDrawable(r);
3315 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3316 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3317 marketIconDrawable.setBounds(0, 0, w, h);
3318
3319 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003320 }
3321
Michael Jurkad7c28052012-04-27 15:43:36 -07003322 @Override
3323 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003324 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003325 final List<CharSequence> text = event.getText();
3326 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003327 // Populate event with a fake title based on the current state.
3328 if (mState == State.APPS_CUSTOMIZE) {
3329 text.add(getString(R.string.all_apps_button_label));
3330 } else {
3331 text.add(getString(R.string.all_apps_home_button_label));
3332 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003333 return result;
3334 }
3335
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003336 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003337 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003338 */
3339 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3340 @Override
3341 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003342 closeSystemDialogs();
3343 }
3344 }
3345
3346 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003347 * Receives notifications whenever the appwidgets are reset.
3348 */
3349 private class AppWidgetResetObserver extends ContentObserver {
3350 public AppWidgetResetObserver() {
3351 super(new Handler());
3352 }
3353
3354 @Override
3355 public void onChange(boolean selfChange) {
3356 onAppWidgetReset();
3357 }
3358 }
3359
3360 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003361 * If the activity is currently paused, signal that we need to run the passed Runnable
3362 * in onResume.
3363 *
3364 * This needs to be called from incoming places where resources might have been loaded
3365 * while we are paused. That is becaues the Configuration might be wrong
3366 * when we're not running, and if it comes back to what it was when we
3367 * were paused, we are not restarted.
3368 *
3369 * Implementation of the method from LauncherModel.Callbacks.
3370 *
3371 * @return true if we are currently paused. The caller might be able to
3372 * skip some work in that case since we will come back again.
3373 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003374 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003375 if (mPaused) {
3376 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003377 if (deletePreviousRunnables) {
3378 while (mOnResumeCallbacks.remove(run)) {
3379 }
3380 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003381 mOnResumeCallbacks.add(run);
3382 return true;
3383 } else {
3384 return false;
3385 }
3386 }
3387
Michael Jurkac402cd92013-05-20 15:49:32 +02003388 private boolean waitUntilResume(Runnable run) {
3389 return waitUntilResume(run, false);
3390 }
3391
Michael Jurka7607c2f2013-04-03 14:33:19 -07003392 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003393 * If the activity is currently paused, signal that we need to re-run the loader
3394 * in onResume.
3395 *
3396 * This needs to be called from incoming places where resources might have been loaded
3397 * while we are paused. That is becaues the Configuration might be wrong
3398 * when we're not running, and if it comes back to what it was when we
3399 * were paused, we are not restarted.
3400 *
3401 * Implementation of the method from LauncherModel.Callbacks.
3402 *
3403 * @return true if we are currently paused. The caller might be able to
3404 * skip some work in that case since we will come back again.
3405 */
3406 public boolean setLoadOnResume() {
3407 if (mPaused) {
3408 Log.i(TAG, "setLoadOnResume");
3409 mOnResumeNeedsLoad = true;
3410 return true;
3411 } else {
3412 return false;
3413 }
3414 }
3415
3416 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003418 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003420 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003421 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003422 } else {
3423 return SCREEN_COUNT / 2;
3424 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003426
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 /**
3428 * Refreshes the shortcuts shown on the workspace.
3429 *
3430 * Implementation of the method from LauncherModel.Callbacks.
3431 */
3432 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 // If we're starting binding all over again, clear any bind calls we'd postponed in
3434 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3435 // from scratch again
3436 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003437
Michael Jurka7607c2f2013-04-03 14:33:19 -07003438 final Workspace workspace = mWorkspace;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003439 mNewShortcutAnimatePage = -1;
3440 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003441 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 int count = workspace.getChildCount();
3443 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003444 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003445 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3446 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003448 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003449 if (mHotseat != null) {
3450 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003451 }
3452 }
3453
3454 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 * Bind the items start-end from the list.
3456 *
3457 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003459 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end) {
3460 if (waitUntilResume(new Runnable() {
3461 public void run() {
3462 bindItems(shortcuts, start, end);
3463 }
3464 })) {
3465 return;
3466 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003467
Winson Chungf0c6ae02012-03-21 16:10:31 -07003468 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3469 Set<String> newApps = new HashSet<String>();
3470 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3471
3472 Workspace workspace = mWorkspace;
3473 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003475
3476 // Short circuit if we are loading dock items for a configuration which has no dock
3477 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3478 mHotseat == null) {
3479 continue;
3480 }
3481
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 switch (item.itemType) {
3483 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3484 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003485 ShortcutInfo info = (ShortcutInfo) item;
3486 String uri = info.intent.toUri(0).toString();
3487 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003488 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3489 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003490 boolean animateIconUp = false;
3491 synchronized (newApps) {
3492 if (newApps.contains(uri)) {
3493 animateIconUp = newApps.remove(uri);
3494 }
3495 }
3496 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003497 // Prepare the view to be animated up
3498 shortcut.setAlpha(0f);
3499 shortcut.setScaleX(0f);
3500 shortcut.setScaleY(0f);
3501 mNewShortcutAnimatePage = item.screen;
3502 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3503 mNewShortcutAnimateViews.add(shortcut);
3504 }
3505 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003507 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003508 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003509 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003510 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003511 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3512 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003515 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003516
Joe Onorato9c1289c2009-08-17 11:03:03 -04003517 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003518 }
3519
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 /**
3521 * Implementation of the method from LauncherModel.Callbacks.
3522 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003523 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3524 if (waitUntilResume(new Runnable() {
3525 public void run() {
3526 bindFolders(folders);
3527 }
3528 })) {
3529 return;
3530 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003531 sFolders.clear();
3532 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003533 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534
3535 /**
3536 * Add the views for a widget to the workspace.
3537 *
3538 * Implementation of the method from LauncherModel.Callbacks.
3539 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003540 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3541 if (waitUntilResume(new Runnable() {
3542 public void run() {
3543 bindAppWidget(item);
3544 }
3545 })) {
3546 return;
3547 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003548
Daniel Sandler843e8602010-06-07 14:59:01 -04003549 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3550 if (DEBUG_WIDGETS) {
3551 Log.d(TAG, "bindAppWidget: " + item);
3552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 final Workspace workspace = mWorkspace;
3554
3555 final int appWidgetId = item.appWidgetId;
3556 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003557 if (DEBUG_WIDGETS) {
3558 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3559 }
3560
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3562
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003564 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003565
Winson Chung3d503fb2011-07-13 17:25:49 -07003566 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003568 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3569
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 workspace.requestLayout();
3571
Daniel Sandler843e8602010-06-07 14:59:01 -04003572 if (DEBUG_WIDGETS) {
3573 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3574 + (SystemClock.uptimeMillis()-start) + "ms");
3575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 }
3577
Adam Cohen1462de32012-07-24 22:34:36 -07003578 public void onPageBoundSynchronously(int page) {
3579 mSynchronouslyBoundPages.add(page);
3580 }
3581
Joe Onorato9c1289c2009-08-17 11:03:03 -04003582 /**
3583 * Callback saying that there aren't any more items to bind.
3584 *
3585 * Implementation of the method from LauncherModel.Callbacks.
3586 */
Adam Cohene25af792013-06-06 23:08:25 -07003587 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003588 if (waitUntilResume(new Runnable() {
3589 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003590 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 }
3592 })) {
3593 return;
3594 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 if (mSavedState != null) {
3596 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003597 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003599 mSavedState = null;
3600 }
3601
Adam Cohen1462de32012-07-24 22:34:36 -07003602 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003604 // If we received the result of any pending adds while the loader was running (e.g. the
3605 // widget configuration forced an orientation change), process them now.
3606 for (int i = 0; i < sPendingAddList.size(); i++) {
3607 completeAdd(sPendingAddList.get(i));
3608 }
3609 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003610
Winson Chungc51db6a2011-10-05 11:44:49 -07003611 // Update the market app icon as necessary (the other icons will be managed in response to
3612 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003613 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003614
Winson Chungf0c6ae02012-03-21 16:10:31 -07003615 // Animate up any icons as necessary
3616 if (mVisible || mWorkspaceLoading) {
3617 Runnable newAppsRunnable = new Runnable() {
3618 @Override
3619 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003620 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003621 }
3622 };
Winson Chunga2413752012-04-03 14:22:34 -07003623
3624 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3625 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3626 if (canRunNewAppsAnimation()) {
3627 // If the user has not interacted recently, then either snap to the new page to show
3628 // the new-apps animation or just run them if they are to appear on the current page
3629 if (willSnapPage) {
3630 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3631 } else {
3632 runNewAppsAnimation(false);
3633 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003634 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003635 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003636 // are on another page (or just normally if they are added to the current page)
3637 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003638 }
3639 }
3640
3641 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003642 if (upgradePath) {
3643 mWorkspace.saveWorkspaceToDb();
3644
3645 // Run through this twice... a little hackleberry, but the right solution is complex.
3646 mWorkspace.stripDuplicateApps();
3647 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
3648 }
Adam Cohen66a01fd2013-06-11 12:48:00 -07003649 mWorkspace.post(new Runnable() {
3650 @Override
3651 public void run() {
3652 onFinishBindingItems();
3653 }
3654 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003655 }
3656
Winson Chunga2413752012-04-03 14:22:34 -07003657 private boolean canRunNewAppsAnimation() {
3658 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3659 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3660 }
3661
Winson Chungf0c6ae02012-03-21 16:10:31 -07003662 /**
3663 * Runs a new animation that scales up icons that were added while Launcher was in the
3664 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003665 *
3666 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003667 */
Winson Chunga2413752012-04-03 14:22:34 -07003668 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003669 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003670 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003671
3672 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3674 @Override
3675 public int compare(View a, View b) {
3676 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3677 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3678 int cellCountX = LauncherModel.getCellCountX();
3679 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3680 }
3681 });
Winson Chunga2413752012-04-03 14:22:34 -07003682
3683 // Animate each of the views in place (or show them immediately if requested)
3684 if (immediate) {
3685 for (View v : mNewShortcutAnimateViews) {
3686 v.setAlpha(1f);
3687 v.setScaleX(1f);
3688 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 }
Winson Chunga2413752012-04-03 14:22:34 -07003690 } else {
3691 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3692 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003693 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003694 PropertyValuesHolder.ofFloat("alpha", 1f),
3695 PropertyValuesHolder.ofFloat("scaleX", 1f),
3696 PropertyValuesHolder.ofFloat("scaleY", 1f));
3697 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3698 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3699 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3700 bounceAnims.add(bounceAnim);
3701 }
3702 anim.playTogether(bounceAnims);
3703 anim.addListener(new AnimatorListenerAdapter() {
3704 @Override
3705 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003706 if (mWorkspace != null) {
3707 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3708 }
Winson Chunga2413752012-04-03 14:22:34 -07003709 }
3710 });
3711 anim.start();
3712 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003713
3714 // Clean up
3715 mNewShortcutAnimatePage = -1;
3716 mNewShortcutAnimateViews.clear();
3717 new Thread("clearNewAppsThread") {
3718 public void run() {
3719 mSharedPrefs.edit()
3720 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3721 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3722 .commit();
3723 }
3724 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003725 }
3726
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003727 @Override
3728 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003729 boolean searchVisible = updateGlobalSearchIcon();
3730 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003731 if (mSearchDropTargetBar != null) {
3732 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3733 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003734 }
3735
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003736 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003737 * Add the icons for all apps.
3738 *
3739 * Implementation of the method from LauncherModel.Callbacks.
3740 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003741 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003742 Runnable setAllAppsRunnable = new Runnable() {
3743 public void run() {
3744 if (mAppsCustomizeContent != null) {
3745 mAppsCustomizeContent.setApps(apps);
Adam Cohene25af792013-06-06 23:08:25 -07003746
3747 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3748 getHotseat().addAllAppsFolder(mIconCache, apps,
3749 mIntentsOnWorkspaceFromUpgradePath, Launcher.this);
3750 mIntentsOnWorkspaceFromUpgradePath = null;
3751 }
Winson Chungc93e5ae2012-07-23 20:48:26 -07003752 }
3753 }
3754 };
3755
Michael Jurkac57b7a82011-08-09 22:02:20 -07003756 // Remove the progress bar entirely; we could also make it GONE
3757 // but better to remove it since we know it's not going to be used
3758 View progressBar = mAppsCustomizeTabHost.
3759 findViewById(R.id.apps_customize_progress_bar);
3760 if (progressBar != null) {
3761 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003762
3763 // We just post the call to setApps so the user sees the progress bar
3764 // disappear-- otherwise, it just looks like the progress bar froze
3765 // which doesn't look great
3766 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3767 } else {
3768 // If we did not initialize the spinner in onCreate, then we can directly set the
3769 // list of applications without waiting for any progress bars views to be hidden.
3770 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003771 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 }
3773
3774 /**
3775 * A package was installed.
3776 *
3777 * Implementation of the method from LauncherModel.Callbacks.
3778 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003779 public void bindAppsAdded(final ArrayList<ApplicationInfo> apps) {
3780 if (waitUntilResume(new Runnable() {
3781 public void run() {
3782 bindAppsAdded(apps);
3783 }
3784 })) {
3785 return;
3786 }
3787
Winson Chungf0ea4d32011-06-06 14:27:16 -07003788
Winson Chung785d2eb2011-04-14 16:08:02 -07003789 if (mAppsCustomizeContent != null) {
3790 mAppsCustomizeContent.addApps(apps);
3791 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003792 }
3793
3794 /**
3795 * A package was updated.
3796 *
3797 * Implementation of the method from LauncherModel.Callbacks.
3798 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003799 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3800 if (waitUntilResume(new Runnable() {
3801 public void run() {
3802 bindAppsUpdated(apps);
3803 }
3804 })) {
3805 return;
3806 }
3807
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003808 if (mWorkspace != null) {
3809 mWorkspace.updateShortcuts(apps);
3810 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003811
Winson Chung785d2eb2011-04-14 16:08:02 -07003812 if (mAppsCustomizeContent != null) {
3813 mAppsCustomizeContent.updateApps(apps);
3814 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 }
3816
3817 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003818 * A package was uninstalled. We take both the super set of packageNames
3819 * in addition to specific applications to remove, the reason being that
3820 * this can be called when a package is updated as well. In that scenario,
3821 * we only remove specific components from the workspace, where as
3822 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 *
3824 * Implementation of the method from LauncherModel.Callbacks.
3825 */
Winson Chung83892cc2013-05-01 16:53:33 -07003826 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3827 final ArrayList<ApplicationInfo> appInfos,
3828 final boolean matchPackageNamesOnly) {
3829 if (waitUntilResume(new Runnable() {
3830 public void run() {
3831 bindComponentsRemoved(packageNames, appInfos, matchPackageNamesOnly);
3832 }
3833 })) {
3834 return;
3835 }
3836
3837 if (matchPackageNamesOnly) {
3838 mWorkspace.removeItemsByPackageName(packageNames);
3839 } else {
3840 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003841 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003842
Winson Chung785d2eb2011-04-14 16:08:02 -07003843 if (mAppsCustomizeContent != null) {
Winson Chung83892cc2013-05-01 16:53:33 -07003844 mAppsCustomizeContent.removeApps(appInfos);
Winson Chung785d2eb2011-04-14 16:08:02 -07003845 }
Winson Chunga1820962011-10-03 16:31:06 -07003846
3847 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003848 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 }
Joe Onoratobe386092009-11-17 17:32:16 -08003850
3851 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003852 * A number of packages were updated.
3853 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003854
3855 private ArrayList<Object> mWidgetsAndShortcuts;
3856 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003857 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003858 bindPackagesUpdated(mWidgetsAndShortcuts);
3859 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003860 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003861 };
3862
3863 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3864 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3865 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003866 return;
3867 }
3868
Winson Chung785d2eb2011-04-14 16:08:02 -07003869 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003870 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003871 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003872 }
3873
Winson Chung400438b2011-01-16 17:53:48 -08003874 private int mapConfigurationOriActivityInfoOri(int configOri) {
3875 final Display d = getWindowManager().getDefaultDisplay();
3876 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3877 switch (d.getRotation()) {
3878 case Surface.ROTATION_0:
3879 case Surface.ROTATION_180:
3880 // We are currently in the same basic orientation as the natural orientation
3881 naturalOri = configOri;
3882 break;
3883 case Surface.ROTATION_90:
3884 case Surface.ROTATION_270:
3885 // We are currently in the other basic orientation to the natural orientation
3886 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3887 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3888 break;
3889 }
3890
3891 int[] oriMap = {
3892 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3893 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3894 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3895 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3896 };
3897 // Since the map starts at portrait, we need to offset if this device's natural orientation
3898 // is landscape.
3899 int indexOffset = 0;
3900 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3901 indexOffset = 1;
3902 }
3903 return oriMap[(d.getRotation() + indexOffset) % 4];
3904 }
Adam Cohen446e9402011-09-15 18:21:21 -07003905
Winson Chung4b919f82012-05-01 10:44:08 -07003906 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003907 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003908 getResources().getBoolean(R.bool.allow_rotation);
3909 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003910 }
Winson Chung4b919f82012-05-01 10:44:08 -07003911 public void lockScreenOrientation() {
3912 if (isRotationEnabled()) {
3913 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3914 .getConfiguration().orientation));
3915 }
3916 }
3917 public void unlockScreenOrientation(boolean immediate) {
3918 if (isRotationEnabled()) {
3919 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003920 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003921 } else {
3922 mHandler.postDelayed(new Runnable() {
3923 public void run() {
3924 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3925 }
3926 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003927 }
Winson Chung4b919f82012-05-01 10:44:08 -07003928 }
Winson Chung400438b2011-01-16 17:53:48 -08003929 }
3930
Winson Chung82f55532011-08-09 14:14:23 -07003931 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003932 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003933 if (DISABLE_CLINGS) {
3934 return false;
3935 }
3936
Brett Chabot2a9e2282011-08-23 15:03:13 -07003937 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003938 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003939
Michael Jurkae233a8b2013-03-19 13:49:20 +01003940 // Restricted secondary users (child mode) will potentially have very few apps
3941 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003942// boolean supportsLimitedUsers =
3943// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3944// Account[] accounts = AccountManager.get(this).getAccounts();
3945// if (supportsLimitedUsers && accounts.length == 0) {
3946// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3947// Bundle restrictions = um.getUserRestrictions();
3948// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3949// return false;
3950// }
3951// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003952 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003953 }
Michael Jurka22143132012-10-04 17:42:38 +02003954
Winson Chung7d7541e2011-09-16 20:14:36 -07003955 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003956 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003957 if (cling != null) {
3958 cling.init(this, positionData);
3959 cling.setVisibility(View.VISIBLE);
3960 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3961 if (animate) {
3962 cling.buildLayer();
3963 cling.setAlpha(0f);
3964 cling.animate()
3965 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003966 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 .setDuration(SHOW_CLING_DURATION)
3968 .setStartDelay(delay)
3969 .start();
3970 } else {
3971 cling.setAlpha(1f);
3972 }
Michael Jurka22143132012-10-04 17:42:38 +02003973 cling.setFocusableInTouchMode(true);
3974 cling.post(new Runnable() {
3975 public void run() {
3976 cling.setFocusable(true);
3977 cling.requestFocus();
3978 }
3979 });
3980 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3981 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003982 }
3983 return cling;
3984 }
Michael Jurka22143132012-10-04 17:42:38 +02003985
Winson Chung7d7541e2011-09-16 20:14:36 -07003986 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003987 // To catch cases where siblings of top-level views are made invisible, just check whether
3988 // the cling is directly set to GONE before dismissing it.
3989 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003990 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003991 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003992 anim.addListener(new AnimatorListenerAdapter() {
3993 public void onAnimationEnd(Animator animation) {
3994 cling.setVisibility(View.GONE);
3995 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003996 // We should update the shared preferences on a background thread
3997 new Thread("dismissClingThread") {
3998 public void run() {
3999 SharedPreferences.Editor editor = mSharedPrefs.edit();
4000 editor.putBoolean(flag, true);
4001 editor.commit();
4002 }
4003 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004004 };
4005 });
4006 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004007 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004008 }
4009 }
Michael Jurka22143132012-10-04 17:42:38 +02004010
Winson Chung9d9d74f2011-09-19 11:49:12 -07004011 private void removeCling(int id) {
4012 final View cling = findViewById(id);
4013 if (cling != null) {
4014 final ViewGroup parent = (ViewGroup) cling.getParent();
4015 parent.post(new Runnable() {
4016 @Override
4017 public void run() {
4018 parent.removeView(cling);
4019 }
4020 });
Michael Jurka22143132012-10-04 17:42:38 +02004021 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004022 }
4023 }
Michael Jurka974c3862012-05-22 22:00:31 -07004024
4025 private boolean skipCustomClingIfNoAccounts() {
4026 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4027 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4028 if (customCling) {
4029 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004030 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004031 Account[] accounts = am.getAccountsByType("com.google");
4032 return accounts.length == 0;
4033 }
4034 return false;
4035 }
4036
Winson Chung7d7541e2011-09-16 20:14:36 -07004037 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004039 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004040 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4041 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004042 // If we're not using the default workspace layout, replace workspace cling
4043 // with a custom workspace cling (usually specified in an overlay)
4044 // For now, only do this on tablets
4045 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004046 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004047 // Use a custom cling
4048 View cling = findViewById(R.id.workspace_cling);
4049 ViewGroup clingParent = (ViewGroup) cling.getParent();
4050 int clingIndex = clingParent.indexOfChild(cling);
4051 clingParent.removeViewAt(clingIndex);
4052 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4053 clingParent.addView(customCling, clingIndex);
4054 customCling.setId(R.id.workspace_cling);
4055 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004056 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004057 } else {
4058 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 }
4060 }
4061 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004063 if (isClingsEnabled() &&
4064 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004066 } else {
4067 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 }
4069 }
4070 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004071 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004072 if (isClingsEnabled() &&
4073 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4074 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004075 } else {
4076 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004077 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004079 }
4080 public boolean isFolderClingVisible() {
4081 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004082 if (cling != null) {
4083 return cling.getVisibility() == View.VISIBLE;
4084 }
4085 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004086 }
Winson Chung82f55532011-08-09 14:14:23 -07004087 public void dismissWorkspaceCling(View v) {
4088 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004089 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004090 }
4091 public void dismissAllAppsCling(View v) {
4092 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4094 }
4095 public void dismissFolderCling(View v) {
4096 Cling cling = (Cling) findViewById(R.id.folder_cling);
4097 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004098 }
4099
Winson Chung80baf5a2010-08-09 16:03:15 -07004100 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004101 * Prints out out state for debugging.
4102 */
4103 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004104 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004105 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004106 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4107 Log.d(TAG, "mRestoring=" + mRestoring);
4108 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4109 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004110 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004111 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004112
Winson Chung785d2eb2011-04-14 16:08:02 -07004113 if (mAppsCustomizeContent != null) {
4114 mAppsCustomizeContent.dumpState();
4115 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004116 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004117 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004118
4119 @Override
4120 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4121 super.dump(prefix, fd, writer, args);
4122 writer.println(" ");
4123 writer.println("Debug logs: ");
4124 for (int i = 0; i < sDumpLogs.size(); i++) {
4125 writer.println(" " + sDumpLogs.get(i));
4126 }
4127 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004128
4129 public static void dumpDebugLogsToConsole() {
4130 Log.d(TAG, "");
4131 Log.d(TAG, "*********************");
4132 Log.d(TAG, "Launcher debug logs: ");
4133 for (int i = 0; i < sDumpLogs.size(); i++) {
4134 Log.d(TAG, " " + sDumpLogs.get(i));
4135 }
4136 Log.d(TAG, "*********************");
4137 Log.d(TAG, "");
4138 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004139}
Michael Jurka2763be32011-02-24 11:19:57 -08004140
Michael Jurkaabded662011-03-04 12:06:57 -08004141interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004142 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004143 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004144 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004145 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004146 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004147}