blob: 57e0cbf2bc3843466255ed0b730fa88a7554cf12 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700194 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700195 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700196 private State mState = State.WORKSPACE;
197 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800198 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700201 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
202 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700203 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700204 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800207 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
Winson Chunga2413752012-04-03 14:22:34 -0700209 // How long to wait before the new-shortcut animation automatically pans the workspace
210 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700211 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700212
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800213 private final BroadcastReceiver mCloseSystemDialogsReceiver
214 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800215 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private LayoutInflater mInflater;
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800220 private View mQsbDivider;
221 private View mDockDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700222 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private DragLayer mDragLayer;
224 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700225
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700226 private AppWidgetManager mAppWidgetManager;
227 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Michael Jurkac9d95c52011-08-29 14:03:34 -0700229 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700230 private AppWidgetProviderInfo mPendingAddWidgetInfo;
231
Michael Jurka0280c3b2010-09-17 15:00:07 -0700232 private int[] mTmpAddItemCellCoordinates = new int[2];
233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private FolderInfo mFolderInfo;
235
Winson Chung3d503fb2011-07-13 17:25:49 -0700236 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800237 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700238
Winson Chungc51db6a2011-10-05 11:44:49 -0700239 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700240 private AppsCustomizeTabHost mAppsCustomizeTabHost;
241 private AppsCustomizePagedView mAppsCustomizeContent;
242 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700245 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
246 // scroll issues (because the workspace may not have been measured yet) and extra work.
247 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
248 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249
250 private SpannableStringBuilder mDefaultKeySsb = null;
251
Joe Onorato9c1289c2009-08-17 11:03:03 -0400252 private boolean mWorkspaceLoading = true;
253
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800254 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private boolean mRestoring;
256 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700257 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258
Michael Jurka7607c2f2013-04-03 14:33:19 -0700259 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
260
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // Keep track of whether the user has left launcher
262 private static boolean sPausedFromUserAction = false;
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedInstanceState;
265
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800267 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800268 private boolean mUserPresent = true;
269 private boolean mVisible = false;
270 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700271 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700273 private static LocaleConfiguration sLocaleConfiguration = null;
274
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700275 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700276
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700277 private Intent mAppMarketIntent = null;
278
Adam Cohended9f8d2010-11-03 13:25:16 -0700279 // Related to the auto-advancing of widgets
280 private final int ADVANCE_MSG = 1;
281 private final int mAdvanceInterval = 20000;
282 private final int mAdvanceStagger = 250;
283 private long mAutoAdvanceSentTime;
284 private long mAutoAdvanceTimeLeft = -1;
285 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
286 new HashMap<View, AppWidgetProviderInfo>();
287
Winson Chung400438b2011-01-16 17:53:48 -0800288 // Determines how long to wait after a rotation before restoring the screen orientation to
289 // match the sensor state.
290 private final int mRestoreScreenOrientationDelay = 500;
291
Michael Jurka4ef207b2010-11-29 17:05:45 -0800292 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700293 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
294 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
295 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800296
Craig Mautner360310b2012-10-26 15:13:08 -0700297 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700298
Adam Cohen1462de32012-07-24 22:34:36 -0700299 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
300
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700301 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
302
Winson Chung46353de2012-02-16 14:05:10 -0800303 // We only want to get the SharedPreferences once since it does an FS stat each time we get
304 // it from the context.
305 private SharedPreferences mSharedPrefs;
306
Adam Cohene25af792013-06-06 23:08:25 -0700307 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
308
Winson Chungf0c6ae02012-03-21 16:10:31 -0700309 // Holds the page that we need to animate to, and the icon views that we need to animate up
310 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700311 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700313 private ImageView mFolderIconImageView;
314 private Bitmap mFolderIconBitmap;
315 private Canvas mFolderIconCanvas;
316 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700317
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 private BubbleTextView mWaitingForResume;
319
Michael Jurka22143132012-10-04 17:42:38 +0200320 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
321 = new HideFromAccessibilityHelper();
322
Michael Jurkac1f5d262011-09-30 19:32:27 -0700323 private Runnable mBuildLayersRunnable = new Runnable() {
324 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700325 if (mWorkspace != null) {
326 mWorkspace.buildPageHardwareLayers();
327 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700328 }
329 };
330
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800331 private static ArrayList<PendingAddArguments> sPendingAddList
332 = new ArrayList<PendingAddArguments>();
333
Michael Jurka0a457bf2012-11-19 14:05:05 -0800334 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
335
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800336 private static class PendingAddArguments {
337 int requestCode;
338 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700339 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700340 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800341 int cellX;
342 int cellY;
343 }
344
Michael Jurka0a457bf2012-11-19 14:05:05 -0800345 private static boolean isPropertyEnabled(String propertyName) {
346 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700347 }
348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 @Override
350 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700351 if (DEBUG_STRICT_MODE) {
352 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
353 .detectDiskReads()
354 .detectDiskWrites()
355 .detectNetwork() // or .detectAll() for all detectable problems
356 .penaltyLog()
357 .build());
358 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
359 .detectLeakedSqlLiteObjects()
360 .detectLeakedClosableObjects()
361 .penaltyLog()
362 .penaltyDeath()
363 .build());
364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400367
368 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
369 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400370 LauncherAppState app = LauncherAppState.getInstance();
371 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700372 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800373 mModel = app.setLauncher(this);
374 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400375 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700377
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
380 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700381
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700382 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
383 // this also ensures that any synchronous binding below doesn't re-trigger another
384 // LauncherModel load.
385 mPaused = false;
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200388 android.os.Debug.startMethodTracing(
389 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391
392 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 setContentView(R.layout.launcher);
394 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700395 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800397 registerContentObservers();
398
Joe Onorato7c312c12009-08-13 21:36:53 -0700399 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 mSavedState = savedInstanceState;
402 restoreState(mSavedState);
403
Winson Chunga12a2502010-12-20 14:41:35 -0800404 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700405 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200406 mAppsCustomizeContent.onPackagesUpdated(
407 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700408 }
Winson Chunga12a2502010-12-20 14:41:35 -0800409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 if (PROFILE_STARTUP) {
411 android.os.Debug.stopMethodTracing();
412 }
413
414 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700415 if (sPausedFromUserAction) {
416 // If the user leaves launcher, then we should just load items asynchronously when
417 // they return.
418 mModel.startLoader(true, -1);
419 } else {
420 // We only load the page synchronously if the user rotates (or triggers a
421 // configuration change) while launcher is in the foreground
422 mModel.startLoader(true, mWorkspace.getCurrentPage());
423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 // For handling default keys
427 mDefaultKeySsb = new SpannableStringBuilder();
428 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800429
430 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
431 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 updateGlobalIcons();
434
435 // On large interfaces, we want the screen to auto-rotate based on the current orientation
436 unlockScreenOrientation(true);
437 }
438
Winson Chung4a2afa32012-07-19 14:53:05 -0700439 protected void onUserLeaveHint() {
440 super.onUserLeaveHint();
441 sPausedFromUserAction = true;
442 }
443
Adam Cohenf9426d52012-06-04 17:26:21 -0700444 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 boolean searchVisible = false;
446 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800447 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 int coi = getCurrentOrientationIndexForGlobalIcons();
449 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
450 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700451 updateAppMarketIcon();
452 searchVisible = updateGlobalSearchIcon();
453 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700454 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700455 if (sGlobalSearchIcon[coi] != null) {
456 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700457 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700458 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700459 if (sVoiceSearchIcon[coi] != null) {
460 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700461 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700462 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700463 if (sAppMarketIcon[coi] != null) {
464 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800465 }
Winson Chungadf0c182012-08-23 14:59:07 -0700466 if (mSearchDropTargetBar != null) {
467 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
Romain Guycbb89e42009-06-08 15:52:54 -0700470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 if (sLocaleConfiguration == null) {
473 new AsyncTask<Void, Void, LocaleConfiguration>() {
474 @Override
475 protected LocaleConfiguration doInBackground(Void... unused) {
476 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
477 readConfiguration(Launcher.this, localeConfiguration);
478 return localeConfiguration;
479 }
480
481 @Override
482 protected void onPostExecute(LocaleConfiguration result) {
483 sLocaleConfiguration = result;
484 checkForLocaleChange(); // recursive, but now with a locale configuration
485 }
486 }.execute();
487 return;
488 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final Configuration configuration = getResources().getConfiguration();
491
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 final String locale = configuration.locale.toString();
494
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 final int mcc = configuration.mcc;
497
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int mnc = configuration.mnc;
500
Romain Guy84f296c2009-11-04 15:00:44 -0800501 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502
Romain Guy84f296c2009-11-04 15:00:44 -0800503 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 sLocaleConfiguration.locale = locale;
505 sLocaleConfiguration.mcc = mcc;
506 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700507
Joe Onorato0589f0f2010-02-08 13:44:00 -0800508 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700509
510 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
511 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700512 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700513 public void run() {
514 writeConfiguration(Launcher.this, localeConfiguration);
515 }
516 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700517 }
518 }
519
520 private static class LocaleConfiguration {
521 public String locale;
522 public int mcc = -1;
523 public int mnc = -1;
524 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700525
Romain Guy98d01652009-06-30 16:21:04 -0700526 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
527 DataInputStream in = null;
528 try {
529 in = new DataInputStream(context.openFileInput(PREFERENCES));
530 configuration.locale = in.readUTF();
531 configuration.mcc = in.readInt();
532 configuration.mnc = in.readInt();
533 } catch (FileNotFoundException e) {
534 // Ignore
535 } catch (IOException e) {
536 // Ignore
537 } finally {
538 if (in != null) {
539 try {
540 in.close();
541 } catch (IOException e) {
542 // Ignore
543 }
544 }
545 }
546 }
547
548 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
549 DataOutputStream out = null;
550 try {
551 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
552 out.writeUTF(configuration.locale);
553 out.writeInt(configuration.mcc);
554 out.writeInt(configuration.mnc);
555 out.flush();
556 } catch (FileNotFoundException e) {
557 // Ignore
558 } catch (IOException e) {
559 //noinspection ResultOfMethodCallIgnored
560 context.getFileStreamPath(PREFERENCES).delete();
561 } finally {
562 if (out != null) {
563 try {
564 out.close();
565 } catch (IOException e) {
566 // Ignore
567 }
568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 }
570 }
571
Bjorn Bringertc459e522013-06-07 19:36:01 +0100572 public LayoutInflater getInflater() {
573 return mInflater;
574 }
575
Adam Cohen716b51e2011-06-30 12:09:54 -0700576 public DragLayer getDragLayer() {
577 return mDragLayer;
578 }
579
Winson Chung36a62fe2012-05-06 18:04:42 -0700580 boolean isDraggingEnabled() {
581 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
582 // that is subsequently removed from the workspace in startBinding().
583 return !mModel.isLoadingWorkspace();
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 static int getScreen() {
587 synchronized (sLock) {
588 return sScreen;
589 }
590 }
591
592 static void setScreen(int screen) {
593 synchronized (sLock) {
594 sScreen = screen;
595 }
596 }
597
Winson Chung557d6ed2011-07-08 15:34:52 -0700598 /**
599 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
600 * a configuration step, this allows the proper animations to run after other transitions.
601 */
602 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 switch (args.requestCode) {
605 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700606 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700607 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 break;
609 case REQUEST_PICK_SHORTCUT:
610 processShortcut(args.intent);
611 break;
612 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700613 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700614 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 result = true;
616 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_CREATE_APPWIDGET:
618 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700619 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700620 result = true;
621 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 case REQUEST_PICK_WALLPAPER:
623 // We just wanted the activity result here so we can clear mWaitingForResult
624 break;
625 }
Michael Jurka27614d22012-04-02 06:40:22 -0700626 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
627 // if you turned the screen off and then back while in All Apps, Launcher would not
628 // return to the workspace. Clearing mAddInfo.container here fixes this issue
629 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700630 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800631 }
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 protected void onActivityResult(
635 final int requestCode, final int resultCode, final Intent data) {
636 if (requestCode == REQUEST_BIND_APPWIDGET) {
637 int appWidgetId = data != null ?
638 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
639 if (resultCode == RESULT_CANCELED) {
640 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
641 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700642 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 }
644 return;
645 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
648 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700649 mWaitingForResult = false;
650
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 // We have special handling for widgets
652 if (isWidgetDrop) {
653 int appWidgetId = data != null ?
654 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700655 if (appWidgetId < 0) {
656 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
657 "widget configuration activity.");
658 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
659 } else {
660 completeTwoStageWidgetDrop(resultCode, appWidgetId);
661 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 return;
663 }
664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 // The pattern used here is that a user PICKs a specific application,
666 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
669 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700670 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 final PendingAddArguments args = new PendingAddArguments();
672 args.requestCode = requestCode;
673 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700674 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700675 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700676 args.cellX = mPendingAddInfo.cellX;
677 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 if (isWorkspaceLocked()) {
679 sPendingAddList.add(args);
680 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700681 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700685 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
687 null);
688 }
689
690 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700692 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 Runnable onCompleteRunnable = null;
694 int animationType = 0;
695
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700696 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 if (resultCode == RESULT_OK) {
698 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
699 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700700 mPendingAddWidgetInfo);
701 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
705 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
708 null);
709 }
710 };
711 } else if (resultCode == RESULT_CANCELED) {
712 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
713 onCompleteRunnable = new Runnable() {
714 @Override
715 public void run() {
716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
717 null);
718 }
719 };
720 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700721 if (mDragLayer.getAnimatedView() != null) {
722 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
723 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
724 animationType, boundWidget, true);
725 } else {
726 // The animated view may be null in the case of a rotation during widget configuration
727 onCompleteRunnable.run();
728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730
731 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700732 protected void onStop() {
733 super.onStop();
734 FirstFrameAnimatorHelper.setIsVisible(false);
735 }
736
737 @Override
738 protected void onStart() {
739 super.onStart();
740 FirstFrameAnimatorHelper.setIsVisible(true);
741 }
742
743 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200745 long startTime = 0;
746 if (DEBUG_RESUME_TIME) {
747 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400748 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700751
Winson Chung4a2afa32012-07-19 14:53:05 -0700752 // Restore the previous launcher state
753 if (mOnResumeState == State.WORKSPACE) {
754 showWorkspace(false);
755 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
756 showAllApps(false);
757 }
758 mOnResumeState = State.NONE;
759
Craig Mautner360310b2012-10-26 15:13:08 -0700760 // Background was set to gradient in onPause(), restore to black if in all apps.
761 setWorkspaceBackground(mState == State.WORKSPACE);
762
Winson Chungde0fb8f2012-05-08 14:37:08 -0700763 // Process any items that were added while Launcher was away
764 InstallShortcutReceiver.flushInstallQueue(this);
765
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800766 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700767 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700768 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400769 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700770 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700772 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200774 if (mOnResumeCallbacks.size() > 0) {
775 // We might have postponed some bind calls until onResume (see waitUntilResume) --
776 // execute them here
777 long startTimeCallbacks = 0;
778 if (DEBUG_RESUME_TIME) {
779 startTimeCallbacks = System.currentTimeMillis();
780 }
781
782 if (mAppsCustomizeContent != null) {
783 mAppsCustomizeContent.setBulkBind(true);
784 }
785 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
786 mOnResumeCallbacks.get(i).run();
787 }
788 if (mAppsCustomizeContent != null) {
789 mAppsCustomizeContent.setBulkBind(false);
790 }
791 mOnResumeCallbacks.clear();
792 if (DEBUG_RESUME_TIME) {
793 Log.d(TAG, "Time spent processing callbacks in onResume: " +
794 (System.currentTimeMillis() - startTimeCallbacks));
795 }
Michael Jurka447bf842013-05-15 14:52:15 +0200796 }
Winson Chunge4e50662012-01-23 14:45:13 -0800797
798 // Reset the pressed state of icons that were locked in the press state while activities
799 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800800 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800801 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800802 mWaitingForResume.setStayPressed(false);
803 }
Winson Chunge4e50662012-01-23 14:45:13 -0800804 if (mAppsCustomizeContent != null) {
805 // Resets the previous all apps icon press state
806 mAppsCustomizeContent.resetDrawableState();
807 }
Adam Cohen06dff352012-06-01 17:17:08 -0700808 // It is possible that widgets can receive updates while launcher is not in the foreground.
809 // Consequently, the widgets will be inflated in the orientation of the foreground activity
810 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
811 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700812 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700813
814 // Again, as with the above scenario, it's possible that one or more of the global icons
815 // were updated in the wrong orientation.
816 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200817 if (DEBUG_RESUME_TIME) {
818 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
819 }
Adam Cohen06dff352012-06-01 17:17:08 -0700820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700823 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700824 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
825 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
826 // when Launcher resumes and we are still in AllApps.
827 updateWallpaperVisibility(true);
828
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700829 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700830 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800831 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700832 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 }
Romain Guycbb89e42009-06-08 15:52:54 -0700834
Adam Cohen66a01fd2013-06-11 12:48:00 -0700835 protected void onFinishBindingItems() {
836 }
837
838 // Add a fullscreen unpadded view to the workspace to the left all other screens.
839 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700841 }
842
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700843 @Override
844 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400845 // Flag the loader to stop early before switching
846 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700847 if (mAppsCustomizeContent != null) {
848 mAppsCustomizeContent.surrender();
849 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800850 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700851 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800853 // We can't hide the IME if it was forced open. So don't bother
854 /*
855 @Override
856 public void onWindowFocusChanged(boolean hasFocus) {
857 super.onWindowFocusChanged(hasFocus);
858
859 if (hasFocus) {
860 final InputMethodManager inputManager = (InputMethodManager)
861 getSystemService(Context.INPUT_METHOD_SERVICE);
862 WindowManager.LayoutParams lp = getWindow().getAttributes();
863 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
864 android.os.Handler()) {
865 protected void onReceiveResult(int resultCode, Bundle resultData) {
866 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
867 }
868 });
869 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
870 }
871 }
872 */
873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 private boolean acceptFilter() {
875 final InputMethodManager inputManager = (InputMethodManager)
876 getSystemService(Context.INPUT_METHOD_SERVICE);
877 return !inputManager.isFullscreenMode();
878 }
879
880 @Override
881 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700882 final int uniChar = event.getUnicodeChar();
883 final boolean handled = super.onKeyDown(keyCode, event);
884 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
885 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
887 keyCode, event);
888 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700889 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700890 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700891 // showSearchDialog()
892 // If there are multiple keystrokes before the search dialog takes focus,
893 // onSearchRequested() will be called for every keystroke,
894 // but it is idempotent, so it's fine.
895 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897 }
898
Joe Onorato8a9625e2010-01-28 15:55:35 -0800899 // Eat the long press event so the keyboard doesn't come up.
900 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
901 return true;
902 }
903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 return handled;
905 }
906
Karl Rosaen138a0412009-04-23 19:00:21 -0700907 private String getTypedText() {
908 return mDefaultKeySsb.toString();
909 }
910
911 private void clearTypedText() {
912 mDefaultKeySsb.clear();
913 mDefaultKeySsb.clearSpans();
914 Selection.setSelection(mDefaultKeySsb, 0);
915 }
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700918 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
919 * State
920 */
921 private static State intToState(int stateOrdinal) {
922 State state = State.WORKSPACE;
923 final State[] stateValues = State.values();
924 for (int i = 0; i < stateValues.length; i++) {
925 if (stateValues[i].ordinal() == stateOrdinal) {
926 state = stateValues[i];
927 break;
928 }
929 }
930 return state;
931 }
932
933 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 * Restores the previous state, if it exists.
935 *
936 * @param savedState The previous state.
937 */
938 private void restoreState(Bundle savedState) {
939 if (savedState == null) {
940 return;
941 }
942
Michael Jurka883f55b2010-10-21 15:47:14 -0700943 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700944 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700945 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800946 }
947
Winson Chungf0c6ae02012-03-21 16:10:31 -0700948 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700950 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700954 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700955
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
957 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
960 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700961 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
962 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
963 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700964 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 mRestoring = true;
966 }
967
968 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
969 if (renameFolder) {
970 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700971 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 mRestoring = true;
973 }
Winson Chunga12a2502010-12-20 14:41:35 -0800974
Winson Chung785d2eb2011-04-14 16:08:02 -0700975 // Restore the AppsCustomize tab
976 if (mAppsCustomizeTabHost != null) {
977 String curTab = savedState.getString("apps_customize_currentTab");
978 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700979 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700980 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700981 mAppsCustomizeContent.loadAssociatedPages(
982 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700983 }
984
Winson Chung5afbf7b2011-07-25 11:53:08 -0700985 int currentIndex = savedState.getInt("apps_customize_currentIndex");
986 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 }
989
990 /**
991 * Finds all the views we need and configure them properly.
992 */
993 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700994 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400995
Craig Mautner360310b2012-10-26 15:13:08 -0700996 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -0700997 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
998 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -0700999 mQsbDivider = findViewById(R.id.qsb_divider);
1000 mDockDivider = findViewById(R.id.dock_divider);
1001
1002 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1003 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
Winson Chung4c98d922011-05-31 16:50:48 -07001005 // Setup the drag layer
1006 mDragLayer.setup(this, dragController);
1007
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 // Setup the hotseat
1009 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1010 if (mHotseat != null) {
1011 mHotseat.setup(this);
1012 }
1013
Winson Chung4c98d922011-05-31 16:50:48 -07001014 // Setup the workspace
1015 mWorkspace.setHapticFeedbackEnabled(false);
1016 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001017 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001018 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001019
Winson Chungf0ea4d32011-06-06 14:27:16 -07001020 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001021 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001022
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001024 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001025 mAppsCustomizeContent = (AppsCustomizePagedView)
1026 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1027 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001028
Winson Chung3d503fb2011-07-13 17:25:49 -07001029 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001030 dragController.setDragScoller(mWorkspace);
1031 dragController.setScrollView(mDragLayer);
1032 dragController.setMoveTarget(mWorkspace);
1033 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001034 if (mSearchDropTargetBar != null) {
1035 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001036 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001037
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001038 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001039 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001040 final View ww = new WeightWatcher(this);
1041 ww.setAlpha(0.5f);
1042 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001043 new FrameLayout.LayoutParams(
1044 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001045 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001046 Gravity.BOTTOM)
1047 );
1048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050
1051 /**
1052 * Creates a view representing a shortcut.
1053 *
1054 * @param info The data structure describing the shortcut.
1055 *
1056 * @return A View inflated from R.layout.application.
1057 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001058 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001060 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 }
1062
1063 /**
1064 * Creates a view representing a shortcut inflated from the specified resource.
1065 *
1066 * @param layoutResId The id of the XML layout used to create the shortcut.
1067 * @param parent The group the shortcut belongs to.
1068 * @param info The data structure describing the shortcut.
1069 *
1070 * @return A View inflated from layoutResId.
1071 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001072 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001073 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1074 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 return favorite;
1077 }
1078
1079 /**
1080 * Add an application shortcut to the workspace.
1081 *
1082 * @param data The intent describing the application.
1083 * @param cellInfo The position on screen where to create the shortcut.
1084 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001085 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001087 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088
Adam Cohenfbba09b2011-07-18 21:43:05 -07001089 // First we check if we already know the exact location where we want to add this item.
1090 if (cellX >= 0 && cellY >= 0) {
1091 cellXY[0] = cellX;
1092 cellXY[1] = cellY;
1093 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001094 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 return;
1096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001098 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001099
Romain Guy73b979d2009-06-09 12:57:21 -07001100 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001101 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001103 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001104 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001105 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 } else {
1107 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 }
1109 }
Romain Guycbb89e42009-06-08 15:52:54 -07001110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 /**
1112 * Add a shortcut to the workspace.
1113 *
1114 * @param data The intent describing the shortcut.
1115 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001117 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001118 int cellY) {
1119 int[] cellXY = mTmpAddItemCellCoordinates;
1120 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001121 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Michael Jurkad3ef3062010-11-23 16:23:58 -08001123 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001124
Adam Cohen558baaf2011-08-15 15:22:57 -07001125 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001126 if (info == null) {
1127 return;
1128 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001129 final View view = createShortcut(info);
1130
Adam Cohenfbba09b2011-07-18 21:43:05 -07001131 // First we check if we already know the exact location where we want to add this item.
1132 if (cellX >= 0 && cellY >= 0) {
1133 cellXY[0] = cellX;
1134 cellXY[1] = cellY;
1135 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001136
1137 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001138 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001139 true, null,null)) {
1140 return;
1141 }
1142 DragObject dragObject = new DragObject();
1143 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001144 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1145 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001146 return;
1147 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001148 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001151 foundCellSpan = (result != null);
1152 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001153 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 }
1155
1156 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001157 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001158 return;
1159 }
1160
Adam Cohendcd297f2013-06-18 13:13:40 -07001161 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162
1163 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001165 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
1167 }
1168
Adam Cohen2f093b62012-04-30 18:59:53 -07001169 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1170 int minHeight) {
1171 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001172 // We want to account for the extra amount of padding that we are adding to the widget
1173 // to ensure that it gets the full amount of space that it has requested
1174 int requiredWidth = minWidth + padding.left + padding.right;
1175 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001176 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001177 }
1178
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1180 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001181 }
1182
Adam Cohen2f093b62012-04-30 18:59:53 -07001183 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1184 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001185 }
1186
Adam Cohen2f093b62012-04-30 18:59:53 -07001187 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1188 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001189 }
1190
Adam Cohen2f093b62012-04-30 18:59:53 -07001191 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1192 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001193 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001199 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001202 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001203 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1204 if (appWidgetInfo == null) {
1205 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001209 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001210
Adam Cohen2f093b62012-04-30 18:59:53 -07001211 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1212 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001215 // We have saved the position to which the widget was dragged-- this really only matters
1216 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001217 int[] cellXY = mTmpAddItemCellCoordinates;
1218 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001219 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1222 cellXY[0] = mPendingAddInfo.cellX;
1223 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001224 spanXY[0] = mPendingAddInfo.spanX;
1225 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001226 foundCellSpan = true;
1227 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001229 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001230 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1231 spanXY[1], cellXY, finalSpan);
1232 spanXY[0] = finalSpan[0];
1233 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001234 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001236 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001237 }
1238
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001240 if (appWidgetId != -1) {
1241 // Deleting an app widget ID is a void call but writes to disk before returning
1242 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001243 new Thread("deleteAppWidgetId") {
1244 public void run() {
1245 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1246 }
1247 }.start();
1248 }
Winson Chung93eef082012-03-23 15:59:27 -07001249 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001250 return;
1251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001254 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1255 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 launcherInfo.spanX = spanXY[0];
1257 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001258 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1259 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001262 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263
1264 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001265 if (hostView == null) {
1266 // Perform actual inflation because we're live
1267 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1268 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1269 } else {
1270 // The AppWidgetHostView has already been inflated and instantiated
1271 launcherInfo.hostView = hostView;
1272 }
Romain Guycbb89e42009-06-08 15:52:54 -07001273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001275 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001276 launcherInfo.notifyWidgetSizeChanged(this);
1277
Adam Cohendcd297f2013-06-18 13:13:40 -07001278 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001279 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001280
1281 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001283 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
Romain Guycbb89e42009-06-08 15:52:54 -07001285
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1287 @Override
1288 public void onReceive(Context context, Intent intent) {
1289 final String action = intent.getAction();
1290 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1291 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001292 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001293 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001294
Winson Chungc100e8e2011-08-09 16:02:43 -07001295 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001296 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001297 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1298 mAppsCustomizeTabHost.reset();
1299 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001300 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001301 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1302 mUserPresent = true;
1303 updateRunning();
1304 }
1305 }
1306 };
1307
1308 @Override
1309 public void onAttachedToWindow() {
1310 super.onAttachedToWindow();
1311
1312 // Listen for broadcasts related to user-presence
1313 final IntentFilter filter = new IntentFilter();
1314 filter.addAction(Intent.ACTION_SCREEN_OFF);
1315 filter.addAction(Intent.ACTION_USER_PRESENT);
1316 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001317 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001318 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001319 mVisible = true;
1320 }
1321
1322 @Override
1323 public void onDetachedFromWindow() {
1324 super.onDetachedFromWindow();
1325 mVisible = false;
1326
Adam Cohend113e0c2010-11-11 10:48:05 -08001327 if (mAttached) {
1328 unregisterReceiver(mReceiver);
1329 mAttached = false;
1330 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001331 updateRunning();
1332 }
1333
1334 public void onWindowVisibilityChanged(int visibility) {
1335 mVisible = visibility == View.VISIBLE;
1336 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001337 // The following code used to be in onResume, but it turns out onResume is called when
1338 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1339 // is a more appropriate event to handle
1340 if (mVisible) {
1341 mAppsCustomizeTabHost.onWindowVisible();
1342 if (!mWorkspaceLoading) {
1343 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001344 // We want to let Launcher draw itself at least once before we force it to build
1345 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001346 // apps is nice and speedy.
1347 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001348 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001349 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001350 if (mStarted) return;
1351 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001352 // We delay the layer building a bit in order to give
1353 // other message processing a time to run. In particular
1354 // this avoids a delay in hiding the IME if it was
1355 // currently shown, because doing that may involve
1356 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001357 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001358 final ViewTreeObserver.OnDrawListener listener = this;
1359 mWorkspace.post(new Runnable() {
1360 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001361 if (mWorkspace != null &&
1362 mWorkspace.getViewTreeObserver() != null) {
1363 mWorkspace.getViewTreeObserver().
1364 removeOnDrawListener(listener);
1365 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001366 }
1367 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001368 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001369 }
1370 });
1371 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001372 // When Launcher comes back to foreground, a different Activity might be responsible for
1373 // the app market intent, so refresh the icon
1374 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001375 clearTypedText();
1376 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001377 }
1378
1379 private void sendAdvanceMessage(long delay) {
1380 mHandler.removeMessages(ADVANCE_MSG);
1381 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1382 mHandler.sendMessageDelayed(msg, delay);
1383 mAutoAdvanceSentTime = System.currentTimeMillis();
1384 }
1385
1386 private void updateRunning() {
1387 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1388 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1389 mAutoAdvanceRunning = autoAdvanceRunning;
1390 if (autoAdvanceRunning) {
1391 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1392 sendAdvanceMessage(delay);
1393 } else {
1394 if (!mWidgetsToAdvance.isEmpty()) {
1395 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1396 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1397 }
1398 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001399 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001400 }
1401 }
1402 }
1403
1404 private final Handler mHandler = new Handler() {
1405 @Override
1406 public void handleMessage(Message msg) {
1407 if (msg.what == ADVANCE_MSG) {
1408 int i = 0;
1409 for (View key: mWidgetsToAdvance.keySet()) {
1410 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1411 final int delay = mAdvanceStagger * i;
1412 if (v instanceof Advanceable) {
1413 postDelayed(new Runnable() {
1414 public void run() {
1415 ((Advanceable) v).advance();
1416 }
1417 }, delay);
1418 }
1419 i++;
1420 }
1421 sendAdvanceMessage(mAdvanceInterval);
1422 }
1423 }
1424 };
1425
1426 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001427 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001428 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1429 if (v instanceof Advanceable) {
1430 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001431 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001432 updateRunning();
1433 }
1434 }
1435
1436 void removeWidgetToAutoAdvance(View hostView) {
1437 if (mWidgetsToAdvance.containsKey(hostView)) {
1438 mWidgetsToAdvance.remove(hostView);
1439 updateRunning();
1440 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 }
1442
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001444 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001445 launcherInfo.hostView = null;
1446 }
1447
Winson Chung93eef082012-03-23 15:59:27 -07001448 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1449 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1450 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001451 }
1452
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001453 public LauncherAppWidgetHost getAppWidgetHost() {
1454 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
Romain Guycbb89e42009-06-08 15:52:54 -07001456
Winson Chunga9abd0e2010-10-27 17:18:37 -07001457 public LauncherModel getModel() {
1458 return mModel;
1459 }
1460
Bjorn Bringertc459e522013-06-07 19:36:01 +01001461 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001462 getWindow().closeAllPanels();
1463
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001464 // Whatever we were doing is hereby canceled.
1465 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 @Override
1469 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001470 long startTime = 0;
1471 if (DEBUG_RESUME_TIME) {
1472 startTime = System.currentTimeMillis();
1473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 super.onNewIntent(intent);
1475
1476 // Close the menu
1477 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001478 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001479 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001480
Jamie Genniscb222e82012-10-23 15:44:26 -07001481 final boolean alreadyOnHome =
1482 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001483 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001484
Jamie Genniscb222e82012-10-23 15:44:26 -07001485 Runnable processIntent = new Runnable() {
1486 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001487 if (mWorkspace == null) {
1488 // Can be cases where mWorkspace is null, this prevents a NPE
1489 return;
1490 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001491 Folder openFolder = mWorkspace.getOpenFolder();
1492 // In all these cases, only animate if we're already on home
1493 mWorkspace.exitWidgetResizeMode();
1494 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1495 openFolder == null) {
1496 mWorkspace.moveToDefaultScreen(true);
1497 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001498
Jamie Genniscb222e82012-10-23 15:44:26 -07001499 closeFolder();
1500 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001501
Jamie Genniscb222e82012-10-23 15:44:26 -07001502 // If we are already on home, then just animate back to the workspace,
1503 // otherwise, just wait until onResume to set the state back to Workspace
1504 if (alreadyOnHome) {
1505 showWorkspace(true);
1506 } else {
1507 mOnResumeState = State.WORKSPACE;
1508 }
1509
1510 final View v = getWindow().peekDecorView();
1511 if (v != null && v.getWindowToken() != null) {
1512 InputMethodManager imm = (InputMethodManager)getSystemService(
1513 INPUT_METHOD_SERVICE);
1514 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1515 }
1516
1517 // Reset AllApps to its initial state
1518 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1519 mAppsCustomizeTabHost.reset();
1520 }
1521 }
1522 };
1523
1524 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1525 // Delay processing of the intent to allow the status bar animation to finish
1526 // first in order to avoid janky animations.
1527 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001528 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001529 // Process the intent immediately.
1530 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001531 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
Michael Jurka447bf842013-05-15 14:52:15 +02001534 if (DEBUG_RESUME_TIME) {
1535 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
1539 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001540 public void onRestoreInstanceState(Bundle state) {
1541 super.onRestoreInstanceState(state);
1542 for (int page: mSynchronouslyBoundPages) {
1543 mWorkspace.restoreInstanceStateForChild(page);
1544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546
1547 @Override
1548 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001549 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001550 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551
Michael Jurka883f55b2010-10-21 15:47:14 -07001552 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001553 // We close any open folder since it will not be re-opened, and we need to make sure
1554 // this state is reflected.
1555 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
Adam Cohendcd297f2013-06-18 13:13:40 -07001557 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 mWaitingForResult) {
1559 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1562 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001563 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1564 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1565 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
1567
1568 if (mFolderInfo != null && mWaitingForResult) {
1569 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1570 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1571 }
Michael Jurka946ad472010-07-09 18:05:18 -07001572
Winson Chung785d2eb2011-04-14 16:08:02 -07001573 // Save the current AppsCustomize tab
1574 if (mAppsCustomizeTabHost != null) {
1575 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1576 if (currentTabTag != null) {
1577 outState.putString("apps_customize_currentTab", currentTabTag);
1578 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001579 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1580 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
1584 @Override
1585 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001587
Winson Chunge7a03942011-08-05 15:05:12 -07001588 // Remove all pending runnables
1589 mHandler.removeMessages(ADVANCE_MSG);
1590 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001591 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001592
Winson Chungcd2b0142011-06-08 16:02:26 -07001593 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001594 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001595 mModel.stopLoader();
1596 app.setLauncher(null);
1597
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001599 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001601 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001603 mAppWidgetHost = null;
1604
1605 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606
1607 TextKeyListener.getInstance().release();
1608
Winson Chung81b52252012-08-27 15:34:29 -07001609 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1610 // to prevent leaking Launcher activities on orientation change.
1611 if (mModel != null) {
1612 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1613 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001614
1615 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001616 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001617
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001618 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001619 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1620 mWorkspace.removeAllViews();
1621 mWorkspace = null;
1622 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001623
Michael Jurka2ecf9952012-06-18 12:52:28 -07001624 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626
Adam Cohena9cf38f2011-05-02 15:36:58 -07001627 public DragController getDragController() {
1628 return mDragController;
1629 }
1630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 @Override
1632 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001633 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 super.startActivityForResult(intent, requestCode);
1635 }
1636
Winson Chung70d72102011-08-12 11:24:35 -07001637 /**
1638 * Indicates that we want global search for this activity by setting the globalSearch
1639 * argument for {@link #startSearch} to true.
1640 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001642 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001644
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001645 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Karl Rosaen138a0412009-04-23 19:00:21 -07001647 if (initialQuery == null) {
1648 // Use any text typed in the launcher as the initial query
1649 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 if (appSearchData == null) {
1652 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001653 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
Winson Chungadf0c182012-08-23 14:59:07 -07001655 Rect sourceBounds = new Rect();
1656 if (mSearchDropTargetBar != null) {
1657 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1658 }
Romain Guycbb89e42009-06-08 15:52:54 -07001659
Bjorn Bringertc459e522013-06-07 19:36:01 +01001660 startSearch(initialQuery, selectInitialQuery,
1661 appSearchData, sourceBounds);
1662 }
1663
1664 public void startSearch(String initialQuery,
1665 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001666 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001667 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001668 }
1669
1670 /**
1671 * Starts the global search activity. This code is a copied from SearchManager
1672 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001673 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001674 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001675 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001676 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1677 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1678 if (globalSearchActivity == null) {
1679 Log.w(TAG, "No global search activity found.");
1680 return;
1681 }
1682 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1683 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1684 intent.setComponent(globalSearchActivity);
1685 // Make sure that we have a Bundle to put source in
1686 if (appSearchData == null) {
1687 appSearchData = new Bundle();
1688 } else {
1689 appSearchData = new Bundle(appSearchData);
1690 }
1691 // Set source to package name of app that starts global search, if not set already.
1692 if (!appSearchData.containsKey("source")) {
1693 appSearchData.putString("source", getPackageName());
1694 }
1695 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1696 if (!TextUtils.isEmpty(initialQuery)) {
1697 intent.putExtra(SearchManager.QUERY, initialQuery);
1698 }
1699 if (selectInitialQuery) {
1700 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1701 }
1702 intent.setSourceBounds(sourceBounds);
1703 try {
1704 startActivity(intent);
1705 } catch (ActivityNotFoundException ex) {
1706 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
1709
Winson Chung70d72102011-08-12 11:24:35 -07001710 @Override
1711 public boolean onCreateOptionsMenu(Menu menu) {
1712 if (isWorkspaceLocked()) {
1713 return false;
1714 }
1715
1716 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001717
1718 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1719 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1720 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001721 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1722 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1723 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001724 String helpUrl = getString(R.string.help_url);
1725 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001726 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1727 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1728
Winson Chung70d72102011-08-12 11:24:35 -07001729 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1730 .setIcon(android.R.drawable.ic_menu_gallery)
1731 .setAlphabeticShortcut('W');
1732 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1733 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001734 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001735 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001736 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1737 .setIcon(android.R.drawable.ic_menu_preferences)
1738 .setIntent(settings)
1739 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001740 if (!helpUrl.isEmpty()) {
1741 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1742 .setIcon(android.R.drawable.ic_menu_help)
1743 .setIntent(help)
1744 .setAlphabeticShortcut('H');
1745 }
Winson Chung70d72102011-08-12 11:24:35 -07001746 return true;
1747 }
1748
1749 @Override
1750 public boolean onPrepareOptionsMenu(Menu menu) {
1751 super.onPrepareOptionsMenu(menu);
1752
1753 if (mAppsCustomizeTabHost.isTransitioning()) {
1754 return false;
1755 }
1756 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1757 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1758
1759 return true;
1760 }
1761
1762 @Override
1763 public boolean onOptionsItemSelected(MenuItem item) {
1764 switch (item.getItemId()) {
1765 case MENU_WALLPAPER_SETTINGS:
1766 startWallpaper();
1767 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001768 }
1769
1770 return super.onOptionsItemSelected(item);
1771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001773 @Override
1774 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001775 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001776 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001777 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001778 }
1779
Joe Onorato9c1289c2009-08-17 11:03:03 -04001780 public boolean isWorkspaceLocked() {
1781 return mWorkspaceLoading || mWaitingForResult;
1782 }
1783
Michael Jurka0280c3b2010-09-17 15:00:07 -07001784 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001785 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001786 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001787 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1788 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001789 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001790 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001791 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001792
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001793 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1794 AppWidgetProviderInfo appWidgetInfo) {
1795 if (appWidgetInfo.configure != null) {
1796 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001797
Bjorn Bringert7984c942009-12-09 15:38:25 +00001798 // Launch over to configure widget, if needed
1799 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001800 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001801 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001802 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001804 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001805 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001806 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001807 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001808 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810 }
Romain Guycbb89e42009-06-08 15:52:54 -07001811
Adam Cohenfbba09b2011-07-18 21:43:05 -07001812 /**
1813 * Process a shortcut drop.
1814 *
1815 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001816 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001817 * @param cell The cell it should be added to, optional
1818 * @param position The location on the screen where it was dropped, optional
1819 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001820 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001821 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001823 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001825 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001826
1827 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001828 mPendingAddInfo.cellX = cell[0];
1829 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001830 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831
1832 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1833 createShortcutIntent.setComponent(componentName);
1834 processShortcut(createShortcutIntent);
1835 }
1836
Adam Cohenfbba09b2011-07-18 21:43:05 -07001837 /**
1838 * Process a widget drop.
1839 *
1840 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001841 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001842 * @param cell The cell it should be added to, optional
1843 * @param position The location on the screen where it was dropped, optional
1844 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001845 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001846 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001847 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001848 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001849 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001850 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001851 mPendingAddInfo.minSpanX = info.minSpanX;
1852 mPendingAddInfo.minSpanY = info.minSpanY;
1853
Adam Cohenfbba09b2011-07-18 21:43:05 -07001854 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001855 mPendingAddInfo.cellX = cell[0];
1856 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001858 if (span != null) {
1859 mPendingAddInfo.spanX = span[0];
1860 mPendingAddInfo.spanY = span[1];
1861 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001862
Adam Cohened66b2b2012-01-23 17:28:51 -08001863 AppWidgetHostView hostView = info.boundWidget;
1864 int appWidgetId;
1865 if (hostView != null) {
1866 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001867 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001868 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001869 // In this case, we either need to start an activity to get permission to bind
1870 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001871 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001872 Bundle options = info.bindOptions;
1873
1874 boolean success = false;
1875 if (options != null) {
1876 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1877 info.componentName, options);
1878 } else {
1879 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1880 info.componentName);
1881 }
1882 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001883 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001884 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001885 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001886 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1887 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1888 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001889 // TODO: we need to make sure that this accounts for the options bundle.
1890 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001891 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1892 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001893 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001894 }
1895
Joe Onoratodeb98af2010-02-19 14:59:39 -08001896 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001897 // Handle case where user selected "Applications"
1898 String applicationName = getResources().getString(R.string.group_applications);
1899 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001900
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001901 if (applicationName != null && applicationName.equals(shortcutName)) {
1902 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1903 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001905 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1906 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001907 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001908 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001909 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001910 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913
Winson Chung24ab2f12010-09-16 14:10:47 -07001914 void processWallpaper(Intent intent) {
1915 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1916 }
1917
Adam Cohendcd297f2013-06-18 13:13:40 -07001918 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001920 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 folderInfo.title = getText(R.string.folder_name);
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001924 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001925 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001926 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927
1928 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 FolderIcon newFolder =
1930 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001931 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001933 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 }
Romain Guycbb89e42009-06-08 15:52:54 -07001935
Joe Onorato9c1289c2009-08-17 11:03:03 -04001936 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001937 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001938 }
1939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001941 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001943 Intent chooser = Intent.createChooser(pickWallpaper,
1944 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001945 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1946 // Removed in Eclair MR1
1947// WallpaperManager wm = (WallpaperManager)
1948// getSystemService(Context.WALLPAPER_SERVICE);
1949// WallpaperInfo wi = wm.getWallpaperInfo();
1950// if (wi != null && wi.getSettingsActivity() != null) {
1951// LabeledIntent li = new LabeledIntent(getPackageName(),
1952// R.string.configure_wallpaper, 0);
1953// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1954// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1955// }
Mike Clerona0618e42009-10-22 13:55:21 -07001956 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001959 /**
1960 * Registers various content observers. The current implementation registers
1961 * only a favorites observer to keep track of the favorites applications.
1962 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001963 private void registerContentObservers() {
1964 ContentResolver resolver = getContentResolver();
1965 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1966 true, mWidgetObserver);
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
1970 public boolean dispatchKeyEvent(KeyEvent event) {
1971 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1972 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001974 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001975 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001976 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001977 dumpState();
1978 return true;
1979 }
1980 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001981 }
1982 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1983 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 return true;
1986 }
1987 }
1988
1989 return super.dispatchKeyEvent(event);
1990 }
1991
Joe Onorato88ec0992009-11-19 13:16:06 -08001992 @Override
1993 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001994 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001995 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001996 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001997 Folder openFolder = mWorkspace.getOpenFolder();
1998 if (openFolder.isEditingName()) {
1999 openFolder.dismissEditingName();
2000 } else {
2001 closeFolder();
2002 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002003 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002004 mWorkspace.exitWidgetResizeMode();
2005
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002006 // Back button is a no-op here, but give at least some feedback for the button press
2007 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002008 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002009 }
2010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002012 * Re-listen when widgets are reset.
2013 */
2014 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002015 if (mAppWidgetHost != null) {
2016 mAppWidgetHost.startListening();
2017 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002018 }
2019
2020 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 * Launches the intent referred by the clicked shortcut.
2022 *
2023 * @param v The view representing the clicked shortcut.
2024 */
2025 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002026 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2027 // view has detached (it's possible for this to happen if the view is removed mid touch).
2028 if (v.getWindowToken() == null) {
2029 return;
2030 }
2031
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002032 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002033 return;
2034 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002035
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002037 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002039 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002040
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002041 // Check for special shortcuts
2042 if (intent.getComponent() != null) {
2043 final String shortcutClass = intent.getComponent().getClassName();
2044
2045 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2046 showAllApps(true);
2047 return;
2048 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2049 MemoryDumpActivity.startDump(this);
2050 return;
2051 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002052 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002053
2054 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002055 int[] pos = new int[2];
2056 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002057 intent.setSourceBounds(new Rect(pos[0], pos[1],
2058 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002059
2060 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002061
2062 if (success && v instanceof BubbleTextView) {
2063 mWaitingForResume = (BubbleTextView) v;
2064 mWaitingForResume.setStayPressed(true);
2065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002067 if (v instanceof FolderIcon) {
2068 FolderIcon fi = (FolderIcon) v;
2069 handleFolderClick(fi);
2070 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002071 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002072 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002073 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002074 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002075 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078 }
2079
Michael Jurka0e260592010-06-30 17:07:39 -07002080 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002081 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002082 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002083 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2084 showWorkspace(true);
2085 }
Michael Jurka0e260592010-06-30 17:07:39 -07002086 return false;
2087 }
2088
Michael Jurkaaf442092010-06-10 17:01:57 -07002089 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002090 * Event handler for the search button
2091 *
2092 * @param v The view that was clicked.
2093 */
2094 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002095 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2096
Amith Yamasania135ba82011-08-09 17:42:01 -07002097 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002098 }
2099
2100 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002101 * Event handler for the voice button
2102 *
2103 * @param v The view that was clicked.
2104 */
2105 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002106 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002107
Bjorn Bringertc459e522013-06-07 19:36:01 +01002108 startVoice();
2109 }
2110
2111 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002112 try {
2113 final SearchManager searchManager =
2114 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2115 ComponentName activityName = searchManager.getGlobalSearchActivity();
2116 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002117 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002118 if (activityName != null) {
2119 intent.setPackage(activityName.getPackageName());
2120 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002121 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002122 } catch (ActivityNotFoundException e) {
2123 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002124 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002125 startActivitySafely(null, intent, "onClickVoiceButton");
2126 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002127 }
2128
2129 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002130 * Event handler for the "grid" button that appears on the home screen, which
2131 * enters all apps mode.
2132 *
2133 * @param v The view that was clicked.
2134 */
2135 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002136 showAllApps(true);
2137 }
2138
2139 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002140 // Provide the same haptic feedback that the system offers for virtual keys.
2141 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002142 }
2143
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002144 public void onClickAppMarketButton(View v) {
2145 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002146 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002147 } else {
2148 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002149 }
2150 }
2151
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002152 void startApplicationDetailsActivity(ComponentName componentName) {
2153 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002154 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2155 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002156 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002157 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002158 }
2159
Patrick Dubroy5539af72010-09-07 15:22:01 -07002160 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2161 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2162 // System applications cannot be installed. For now, show a toast explaining that.
2163 // We may give them the option of disabling apps this way.
2164 int messageId = R.string.uninstall_system_app_text;
2165 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2166 } else {
2167 String packageName = appInfo.componentName.getPackageName();
2168 String className = appInfo.componentName.getClassName();
2169 Intent intent = new Intent(
2170 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002171 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2172 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002173 startActivity(intent);
2174 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002175 }
2176
Michael Jurka86a720e2012-05-09 11:23:23 -07002177 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002181 // Only launch using the new animation if the shortcut has not opted out (this is a
2182 // private contract between launcher and may be ignored in the future).
2183 boolean useLaunchAnimation = (v != null) &&
2184 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2185 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002186 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2187 v.getMeasuredWidth(), v.getMeasuredHeight());
2188
2189 startActivity(intent, opts.toBundle());
2190 } else {
2191 startActivity(intent);
2192 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002193 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 } catch (SecurityException e) {
2195 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002196 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002198 "or use the exported attribute for this activity. "
2199 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002201 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002203
Michael Jurka86a720e2012-05-09 11:23:23 -07002204 boolean startActivitySafely(View v, Intent intent, Object tag) {
2205 boolean success = false;
2206 try {
2207 success = startActivity(v, intent, tag);
2208 } catch (ActivityNotFoundException e) {
2209 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2210 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2211 }
2212 return success;
2213 }
2214
Romain Guy8e633c52010-03-04 12:51:36 -08002215 void startActivityForResultSafely(Intent intent, int requestCode) {
2216 try {
2217 startActivityForResult(intent, requestCode);
2218 } catch (ActivityNotFoundException e) {
2219 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2220 } catch (SecurityException e) {
2221 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2222 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2223 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2224 "or use the exported attribute for this activity.", e);
2225 }
2226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227
Adam Cohena9cf38f2011-05-02 15:36:58 -07002228 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002229 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002230 Folder openFolder = mWorkspace.getFolderForTag(info);
2231
2232 // If the folder info reports that the associated folder is open, then verify that
2233 // it is actually opened. There have been a few instances where this gets out of sync.
2234 if (info.opened && openFolder == null) {
2235 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002236 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002237 info.opened = false;
2238 }
2239
Adam Cohenfb91f302012-06-11 15:45:18 -07002240 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 // Close any open folder
2242 closeFolder();
2243 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002244 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 } else {
2246 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002247 int folderScreen;
2248 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002249 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 // .. and close it
2251 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002252 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 // Close any folder open on the current screen
2254 closeFolder();
2255 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002256 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 }
2258 }
2259 }
2260 }
2261
Adam Cohen268c4752012-06-06 17:47:33 -07002262 /**
2263 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2264 * in the DragLayer in the exact absolute location of the original FolderIcon.
2265 */
2266 private void copyFolderIconToImage(FolderIcon fi) {
2267 final int width = fi.getMeasuredWidth();
2268 final int height = fi.getMeasuredHeight();
2269
2270 // Lazy load ImageView, Bitmap and Canvas
2271 if (mFolderIconImageView == null) {
2272 mFolderIconImageView = new ImageView(this);
2273 }
2274 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2275 mFolderIconBitmap.getHeight() != height) {
2276 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2277 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2278 }
2279
2280 DragLayer.LayoutParams lp;
2281 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2282 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2283 } else {
2284 lp = new DragLayer.LayoutParams(width, height);
2285 }
2286
Adam Cohen307fe232012-08-16 17:55:58 -07002287 // The layout from which the folder is being opened may be scaled, adjust the starting
2288 // view size by this scale factor.
2289 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002290 lp.customPosition = true;
2291 lp.x = mRectForFolderAnimation.left;
2292 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002293 lp.width = (int) (scale * width);
2294 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002295
2296 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2297 fi.draw(mFolderIconCanvas);
2298 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002299 if (fi.getFolder() != null) {
2300 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2301 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002302 }
Adam Cohen268c4752012-06-06 17:47:33 -07002303 // Just in case this image view is still in the drag layer from a previous animation,
2304 // we remove it and re-add it.
2305 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2306 mDragLayer.removeView(mFolderIconImageView);
2307 }
2308 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002309 if (fi.getFolder() != null) {
2310 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002311 }
Adam Cohen268c4752012-06-06 17:47:33 -07002312 }
2313
Adam Cohen2801caf2011-05-13 20:57:39 -07002314 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002315 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002316 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2317 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2318 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2319
Adam Cohenc51934b2011-07-26 21:07:43 -07002320 FolderInfo info = (FolderInfo) fi.getTag();
2321 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2322 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002323 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2324 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002325 }
2326
Adam Cohen268c4752012-06-06 17:47:33 -07002327 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2328 copyFolderIconToImage(fi);
2329 fi.setVisibility(View.INVISIBLE);
2330
Michael Jurka2ecf9952012-06-18 12:52:28 -07002331 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002332 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002333 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2334 oa.start();
2335 }
2336
Adam Cohen268c4752012-06-06 17:47:33 -07002337 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002338 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002339 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2340 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2341 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2342
Adam Cohen268c4752012-06-06 17:47:33 -07002343 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002344
Adam Cohen268c4752012-06-06 17:47:33 -07002345 // We remove and re-draw the FolderIcon in-case it has changed
2346 mDragLayer.removeView(mFolderIconImageView);
2347 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002348 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002349 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002350 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002351 oa.addListener(new AnimatorListenerAdapter() {
2352 @Override
2353 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002354 if (cl != null) {
2355 cl.clearFolderLeaveBehind();
2356 // Remove the ImageView copy of the FolderIcon and make the original visible.
2357 mDragLayer.removeView(mFolderIconImageView);
2358 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002359 }
2360 }
2361 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002362 oa.start();
2363 }
2364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002366 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 * is animated relative to the specified View. If the View is null, no animation
2368 * is played.
2369 *
2370 * @param folderInfo The FolderInfo describing the folder to open.
2371 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002372 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002373 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002374 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375
Adam Cohena9cf38f2011-05-02 15:36:58 -07002376 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377
Adam Cohen4554ee12011-08-03 16:13:21 -07002378 // Just verify that the folder hasn't already been added to the DragLayer.
2379 // There was a one-off crash where the folder had a parent already.
2380 if (folder.getParent() == null) {
2381 mDragLayer.addView(folder);
2382 mDragController.addDropTarget((DropTarget) folder);
2383 } else {
2384 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2385 folder.getParent() + ").");
2386 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002387 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002388 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002389
2390 // Notify the accessibility manager that this folder "window" has appeared and occluded
2391 // the workspace items
2392 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2393 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002394 }
2395
2396 public void closeFolder() {
2397 Folder folder = mWorkspace.getOpenFolder();
2398 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002399 if (folder.isEditingName()) {
2400 folder.dismissEditingName();
2401 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002402 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002403
2404 // Dismiss the folder cling
2405 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002406 }
2407 }
2408
2409 void closeFolder(Folder folder) {
2410 folder.getInfo().opened = false;
2411
2412 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2413 if (parent != null) {
2414 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2415 shrinkAndFadeInFolderIcon(fi);
2416 }
2417 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002418
2419 // Notify the accessibility manager that this folder "window" has disappeard and no
2420 // longer occludeds the workspace items
2421 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002422 }
2423
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002424 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002425 if (!isDraggingEnabled()) return false;
2426 if (isWorkspaceLocked()) return false;
2427 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428
2429 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002430 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 }
2432
Michael Jurka0280c3b2010-09-17 15:00:07 -07002433 resetAddInfo();
2434 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002436 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002437 return true;
2438 }
2439
Winson Chung3d503fb2011-07-13 17:25:49 -07002440 // The hotseat touch handling does not go through Workspace, and we always allow long press
2441 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002442 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002443 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2444 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 if (itemUnderLongClick == null) {
2446 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002447 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2448 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002449 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002450 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2451 mWorkspace.startReordering();
2452 } else {
2453 startWallpaper();
2454 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002456 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002458 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002459 }
2460 }
2461 }
2462 return true;
2463 }
2464
Winson Chung3d503fb2011-07-13 17:25:49 -07002465 boolean isHotseatLayout(View layout) {
2466 return mHotseat != null && layout != null &&
2467 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2468 }
2469 Hotseat getHotseat() {
2470 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002471 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002472 SearchDropTargetBar getSearchBar() {
2473 return mSearchDropTargetBar;
2474 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002475
Winson Chung3d503fb2011-07-13 17:25:49 -07002476 /**
2477 * Returns the CellLayout of the specified container at the specified screen.
2478 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002479 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002480 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2481 if (mHotseat != null) {
2482 return mHotseat.getLayout();
2483 } else {
2484 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002485 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002486 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002487 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002488 }
2489 }
2490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 Workspace getWorkspace() {
2492 return mWorkspace;
2493 }
2494
Daniel Sandler843e8602010-06-07 14:59:01 -04002495 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002496 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002497 }
2498
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002499 /**
2500 * Helper method for the cameraZoomIn/cameraZoomOut animations
2501 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002502 * @param scaleFactor The scale factor used for the zoom
2503 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002504 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002506 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002507 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002508
Winson Chung18f41f82012-05-09 13:28:10 -07002509 void disableWallpaperIfInAllApps() {
2510 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002511 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002512 if (mAppsCustomizeTabHost != null &&
2513 !mAppsCustomizeTabHost.isTransitioning()) {
2514 updateWallpaperVisibility(false);
2515 }
2516 }
2517 }
2518
Craig Mautner360310b2012-10-26 15:13:08 -07002519 private void setWorkspaceBackground(boolean workspace) {
2520 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002521 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002522 }
2523
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002524 void updateWallpaperVisibility(boolean visible) {
2525 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2526 int curflags = getWindow().getAttributes().flags
2527 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2528 if (wpflags != curflags) {
2529 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2530 }
Craig Mautner360310b2012-10-26 15:13:08 -07002531 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002532 }
2533
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002534 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2535 if (v instanceof LauncherTransitionable) {
2536 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2537 }
2538 }
2539
Michael Jurkabed61d22012-02-14 22:51:29 -08002540 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2541 if (v instanceof LauncherTransitionable) {
2542 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2543 }
Winson Chung70442722012-02-10 15:43:22 -08002544
2545 // Update the workspace transition step as well
2546 dispatchOnLauncherTransitionStep(v, 0f);
2547 }
2548
2549 private void dispatchOnLauncherTransitionStep(View v, float t) {
2550 if (v instanceof LauncherTransitionable) {
2551 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2552 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002553 }
2554
2555 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2556 if (v instanceof LauncherTransitionable) {
2557 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2558 }
Winson Chung70442722012-02-10 15:43:22 -08002559
2560 // Update the workspace transition step as well
2561 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002562 }
2563
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002564 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002565 * Things to test when changing the following seven functions.
2566 * - Home from workspace
2567 * - from center screen
2568 * - from other screens
2569 * - Home from all apps
2570 * - from center screen
2571 * - from other screens
2572 * - Back from all apps
2573 * - from center screen
2574 * - from other screens
2575 * - Launch app from workspace and quit
2576 * - with back
2577 * - with home
2578 * - Launch app from all apps and quit
2579 * - with back
2580 * - with home
2581 * - Go to a screen that's not the default, then all
2582 * apps, and launch and app, and go back
2583 * - with back
2584 * -with home
2585 * - On workspace, long press power and go back
2586 * - with back
2587 * - with home
2588 * - On all apps, long press power and go back
2589 * - with back
2590 * - with home
2591 * - On workspace, power off
2592 * - On all apps, power off
2593 * - Launch an app and turn off the screen while in that app
2594 * - Go back with home key
2595 * - Go back with back key TODO: make this not go to workspace
2596 * - From all apps
2597 * - From workspace
2598 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2599 * - From all apps
2600 * - From the center workspace
2601 * - From another workspace
2602 */
2603
2604 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002605 * Zoom the camera out from the workspace to reveal 'toView'.
2606 * Assumes that the view to show is anchored at either the very top or very bottom
2607 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002608 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002609 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002610 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002611 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002612 mStateAnimation.cancel();
2613 mStateAnimation = null;
2614 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002616
Winson Chungf0ea4d32011-06-06 14:27:16 -07002617 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2618 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2619 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002620 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002621 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002622 final int startDelay =
2623 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002624
Michael Jurkab3e22d92011-10-31 15:58:33 -07002625 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002626
Michael Jurkad74c9842011-07-10 12:44:21 -07002627 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002628 Animator workspaceAnim =
2629 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630
2631 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002632 toView.setScaleX(scale);
2633 toView.setScaleY(scale);
2634 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2635 scaleAnim.
2636 scaleX(1f).scaleY(1f).
2637 setDuration(duration).
2638 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002639
Winson Chungf0ea4d32011-06-06 14:27:16 -07002640 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002641 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002642 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002643 .ofFloat(toView, "alpha", 0f, 1f)
2644 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002645 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002646 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2647 @Override
2648 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002649 if (animation == null) {
2650 throw new RuntimeException("animation is null");
2651 }
Winson Chung70442722012-02-10 15:43:22 -08002652 float t = (Float) animation.getAnimatedValue();
2653 dispatchOnLauncherTransitionStep(fromView, t);
2654 dispatchOnLauncherTransitionStep(toView, t);
2655 }
2656 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002657
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002658 // toView should appear right at the end of the workspace shrink
2659 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002660 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002661 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002662 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002663
2664 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002665 boolean animationCancelled = false;
2666
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002667 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002668 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002669 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 // Prepare the position
2671 toView.setTranslationX(0.0f);
2672 toView.setTranslationY(0.0f);
2673 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002674 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002675 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002676 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002677 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2679 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002680
Daniel Sandlere4f98912013-06-25 15:13:26 -04002681 if (mWorkspace != null
2682 && !springLoaded
2683 && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002684 // Hide the workspace scrollbar
2685 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002686 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002687 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002688 if (!animationCancelled) {
2689 updateWallpaperVisibility(false);
2690 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002691
2692 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002693 if (mSearchDropTargetBar != null) {
2694 mSearchDropTargetBar.hideSearchBar(false);
2695 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002696 }
2697
Adam Cohencff6af82011-09-13 14:51:53 -07002698 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002699 public void onAnimationCancel(Animator animation) {
2700 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002701 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002702 });
2703
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002704 if (workspaceAnim != null) {
2705 mStateAnimation.play(workspaceAnim);
2706 }
Michael Jurka1899a362011-11-03 13:50:45 -07002707
2708 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002710 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2711 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002712
2713 // If any of the objects being animated haven't been measured/laid out
2714 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002715 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002716 (mWorkspace.getMeasuredWidth() == 0) ||
2717 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002718 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002719 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002720
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002721 final AnimatorSet stateAnimation = mStateAnimation;
2722 final Runnable startAnimRunnable = new Runnable() {
2723 public void run() {
2724 // Check that mStateAnimation hasn't changed while
2725 // we waited for a layout/draw pass
2726 if (mStateAnimation != stateAnimation)
2727 return;
2728 setPivotsForZoom(toView, scale);
2729 dispatchOnLauncherTransitionStart(fromView, animated, false);
2730 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002731 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002732 }
2733 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002734 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002735 final ViewTreeObserver observer = toView.getViewTreeObserver();
2736 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2737 public void onGlobalLayout() {
2738 startAnimRunnable.run();
2739 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2740 }
2741 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002742 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002743 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002744 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745 } else {
2746 toView.setTranslationX(0.0f);
2747 toView.setTranslationY(0.0f);
2748 toView.setScaleX(1.0f);
2749 toView.setScaleY(1.0f);
2750 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002751 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002752
Daniel Sandlere4f98912013-06-25 15:13:26 -04002753 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002754 // Hide the workspace scrollbar
2755 mWorkspace.hideScrollingIndicator(true);
2756 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002757
2758 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002759 if (mSearchDropTargetBar != null) {
2760 mSearchDropTargetBar.hideSearchBar(false);
2761 }
Michael Jurkaabded662011-03-04 12:06:57 -08002762 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002763 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002764 dispatchOnLauncherTransitionStart(fromView, animated, false);
2765 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002766 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002767 dispatchOnLauncherTransitionStart(toView, animated, false);
2768 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002769 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 }
2772
2773 /**
2774 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002775 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002776 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002777 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002778 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2779 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002780
Michael Jurkab3e22d92011-10-31 15:58:33 -07002781 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002782 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002783 mStateAnimation.cancel();
2784 mStateAnimation = null;
2785 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002786 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002787
Winson Chungf0ea4d32011-06-06 14:27:16 -07002788 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002789 final int fadeOutDuration =
2790 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002791 final float scaleFactor = (float)
2792 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2793 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002794 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002795 Animator workspaceAnim = null;
2796
2797 if (toState == State.WORKSPACE) {
2798 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2799 workspaceAnim = mWorkspace.getChangeStateAnimation(
2800 Workspace.State.NORMAL, animated, stagger);
2801 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2802 workspaceAnim = mWorkspace.getChangeStateAnimation(
2803 Workspace.State.SPRING_LOADED, animated);
2804 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002805
Michael Jurkab3e22d92011-10-31 15:58:33 -07002806 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002807 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002808 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002810 final LauncherViewPropertyAnimator scaleAnim =
2811 new LauncherViewPropertyAnimator(fromView);
2812 scaleAnim.
2813 scaleX(scaleFactor).scaleY(scaleFactor).
2814 setDuration(duration).
2815 setInterpolator(new Workspace.ZoomInInterpolator());
2816
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002817 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002818 .ofFloat(fromView, "alpha", 1f, 0f)
2819 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002820 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002821 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2822 @Override
2823 public void onAnimationUpdate(ValueAnimator animation) {
2824 float t = 1f - (Float) animation.getAnimatedValue();
2825 dispatchOnLauncherTransitionStep(fromView, t);
2826 dispatchOnLauncherTransitionStep(toView, t);
2827 }
2828 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002829
Michael Jurka2ecf9952012-06-18 12:52:28 -07002830 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002831
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002832 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2833 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002834 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002835
2836 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002837 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002838 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002839 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002841 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2842 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002843 if (mWorkspace != null) {
2844 mWorkspace.hideScrollingIndicator(false);
2845 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002846 if (onCompleteRunnable != null) {
2847 onCompleteRunnable.run();
2848 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002849 mAppsCustomizeContent.updateCurrentPageScroll();
2850 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851 }
2852 });
2853
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002855 if (workspaceAnim != null) {
2856 mStateAnimation.play(workspaceAnim);
2857 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002858 dispatchOnLauncherTransitionStart(fromView, animated, true);
2859 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002860 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 } else {
2862 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002864 dispatchOnLauncherTransitionStart(fromView, animated, true);
2865 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002866 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 dispatchOnLauncherTransitionStart(toView, animated, true);
2868 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002869 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002871 }
2872
Michael Jurkae326f182011-11-21 14:05:46 -08002873 @Override
2874 public void onTrimMemory(int level) {
2875 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002876 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002877 mAppsCustomizeTabHost.onTrimMemory();
2878 }
2879 }
2880
Winson Chung18f41f82012-05-09 13:28:10 -07002881 @Override
2882 public void onWindowFocusChanged(boolean hasFocus) {
2883 if (!hasFocus) {
2884 // When another window occludes launcher (like the notification shade, or recents),
2885 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2886 updateWallpaperVisibility(true);
2887 } else {
2888 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002889 mWorkspace.postDelayed(new Runnable() {
2890 @Override
2891 public void run() {
2892 disableWallpaperIfInAllApps();
2893 }
2894 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002895 }
2896 }
2897
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002898 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002899 showWorkspace(animated, null);
2900 }
2901
2902 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002903 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002904 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002906 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907
Winson Chungc7d2b602012-05-16 17:02:20 -07002908 // Show the search bar (only animate if we were showing the drop target bar in spring
2909 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002910 if (mSearchDropTargetBar != null) {
2911 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2912 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002913
Michael Jurkab737ee62011-11-15 15:57:22 -08002914 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002915 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002916
2917 // Set focus to the AppsCustomize button
2918 if (mAllAppsButton != null) {
2919 mAllAppsButton.requestFocus();
2920 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002921 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002922
Michael Jurkab737ee62011-11-15 15:57:22 -08002923 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002924
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925 // Change the state *after* we've called all the transition code
2926 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002927
Winson Chung5fb63472011-02-02 17:03:37 -08002928 // Resume the auto-advance of widgets
2929 mUserPresent = true;
2930 updateRunning();
2931
alanv1d4fde62012-10-17 13:15:47 -07002932 // Send an accessibility event to announce the context change
2933 getWindow().getDecorView()
2934 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002935
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002936 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002937 }
2938
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002939 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002940 }
2941
Michael Jurkab3e22d92011-10-31 15:58:33 -07002942 void showAllApps(boolean animated) {
2943 if (mState != State.WORKSPACE) return;
2944
2945 showAppsCustomizeHelper(animated, false);
2946 mAppsCustomizeTabHost.requestFocus();
2947
Michael Jurkab3e22d92011-10-31 15:58:33 -07002948 // Change the state *after* we've called all the transition code
2949 mState = State.APPS_CUSTOMIZE;
2950
2951 // Pause the auto-advance of widgets until we are out of AllApps
2952 mUserPresent = false;
2953 updateRunning();
2954 closeFolder();
2955
2956 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002957 getWindow().getDecorView()
2958 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002959 }
2960
Adam Cohen7777d962011-08-18 18:58:38 -07002961 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002962 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002963 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002964 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002965 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002966 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002967 }
Adam Cohen7777d962011-08-18 18:58:38 -07002968
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2970 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002971 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2972
Winson Chunge7a03942011-08-05 15:05:12 -07002973 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002974 @Override
2975 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002976 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002977 // Before we show workspace, hide all apps again because
2978 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2979 // clean up our state transition functions
2980 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002981 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002982 } else {
2983 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002984 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002985 }
2986 }, (extendedDelay ?
2987 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2988 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2989 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990
Michael Jurkad3ef3062010-11-23 16:23:58 -08002991 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002992 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002993 final boolean animated = true;
2994 final boolean springLoaded = true;
2995 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002996 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002997 }
2998 // Otherwise, we are not in spring loaded mode, so don't do anything.
2999 }
3000
Michael Jurkab737ee62011-11-15 15:57:22 -08003001 void hideDockDivider() {
3002 if (mQsbDivider != null && mDockDivider != null) {
3003 mQsbDivider.setVisibility(View.INVISIBLE);
3004 mDockDivider.setVisibility(View.INVISIBLE);
3005 }
3006 }
3007
3008 void showDockDivider(boolean animated) {
3009 if (mQsbDivider != null && mDockDivider != null) {
3010 mQsbDivider.setVisibility(View.VISIBLE);
3011 mDockDivider.setVisibility(View.VISIBLE);
3012 if (mDividerAnimator != null) {
3013 mDividerAnimator.cancel();
3014 mQsbDivider.setAlpha(1f);
3015 mDockDivider.setAlpha(1f);
3016 mDividerAnimator = null;
3017 }
3018 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003019 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
3020 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
3021 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003022 int duration = 0;
3023 if (mSearchDropTargetBar != null) {
3024 duration = mSearchDropTargetBar.getTransitionInDuration();
3025 }
3026 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003027 mDividerAnimator.start();
3028 }
3029 }
3030 }
3031
Michael Jurkab3e22d92011-10-31 15:58:33 -07003032 void lockAllApps() {
3033 // TODO
3034 }
3035
3036 void unlockAllApps() {
3037 // TODO
3038 }
3039
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003041 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003044 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003045 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003046 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003047 int duration = 0;
3048 if (mSearchDropTargetBar != null) {
3049 duration = mSearchDropTargetBar.getTransitionInDuration();
3050 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003051 mHotseat.animate().alpha(1f).setDuration(duration);
3052 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003054 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055 }
3056 }
3057 }
3058
3059 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003060 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003061 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003063 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003064 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003065 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003066 int duration = 0;
3067 if (mSearchDropTargetBar != null) {
3068 duration = mSearchDropTargetBar.getTransitionOutDuration();
3069 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003070 mHotseat.animate().alpha(0f).setDuration(duration);
3071 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003072 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003073 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003074 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003075 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003076 }
3077
Patrick Dubroy5f445422011-02-18 14:35:21 -08003078 /**
3079 * Add an item from all apps or customize onto the given workspace screen.
3080 * If layout is null, add to the current screen.
3081 */
3082 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003083 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003084 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003085 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003086 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003087
Winson Chungdff8ebb2011-09-08 17:25:31 -07003088 /** Maps the current orientation to an index for referencing orientation correct global icons */
3089 private int getCurrentOrientationIndexForGlobalIcons() {
3090 // default - 0, landscape - 1
3091 switch (getResources().getConfiguration().orientation) {
3092 case Configuration.ORIENTATION_LANDSCAPE:
3093 return 1;
3094 default:
3095 return 0;
3096 }
3097 }
3098
Michael Jurkaae65ee32012-04-30 11:45:54 -07003099 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003101 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003102 // Look for the toolbar icon specified in the activity meta-data
3103 Bundle metaData = packageManager.getActivityInfo(
3104 activityName, PackageManager.GET_META_DATA).metaData;
3105 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003106 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107 if (iconResId != 0) {
3108 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003109 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003110 }
3111 }
3112 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003113 // This can happen if the activity defines an invalid drawable
3114 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3115 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003116 } catch (Resources.NotFoundException nfe) {
3117 // This can happen if the activity defines an invalid drawable
3118 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3119 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003120 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003121 return null;
3122 }
3123
3124 // if successful in getting icon, return it; otherwise, set button to use default drawable
3125 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003126 int buttonId, ComponentName activityName, int fallbackDrawableId,
3127 String toolbarResourceName) {
3128 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003129 Resources r = getResources();
3130 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3131 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003133 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003134 // If we were unable to find the icon via the meta-data, use a generic one
3135 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003136 toolbarIcon = r.getDrawable(fallbackDrawableId);
3137 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 null;
3142 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003143 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003144 if (button != null) {
3145 button.setCompoundDrawables(toolbarIcon, null, null, null);
3146 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003147 return toolbarIcon.getConstantState();
3148 }
3149 }
3150
3151 // if successful in getting icon, return it; otherwise, set button to use default drawable
3152 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003153 int buttonId, ComponentName activityName, int fallbackDrawableId,
3154 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003155 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003156 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003157
Michael Jurka19e0fc52011-07-22 18:00:21 -07003158 if (button != null) {
3159 // If we were unable to find the icon via the meta-data, use a
3160 // generic one
3161 if (toolbarIcon == null) {
3162 button.setImageResource(fallbackDrawableId);
3163 } else {
3164 button.setImageDrawable(toolbarIcon);
3165 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003167
3168 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3169
Michael Jurka0423dcf2010-10-05 14:56:18 -07003170 }
3171
Winson Chung1b884092012-06-08 17:13:02 -07003172 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003173 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003174 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003175 }
3176
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003177 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003178 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003179 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003180 }
3181
Winson Chungbb185bd2011-11-21 12:31:42 -08003182 private void invalidatePressedFocusedStates(View container, View button) {
3183 if (container instanceof HolographicLinearLayout) {
3184 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3185 layout.invalidatePressedFocusedStates();
3186 } else if (button instanceof HolographicImageView) {
3187 HolographicImageView view = (HolographicImageView) button;
3188 view.invalidatePressedFocusedStates();
3189 }
3190 }
3191
Winson Chungc51db6a2011-10-05 11:44:49 -07003192 private boolean updateGlobalSearchIcon() {
3193 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003194 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003195 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003196 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003197
Winson Chung1cad91e2011-05-25 17:41:01 -07003198 final SearchManager searchManager =
3199 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3200 ComponentName activityName = searchManager.getGlobalSearchActivity();
3201 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003202 int coi = getCurrentOrientationIndexForGlobalIcons();
3203 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003204 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3205 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3206 if (sGlobalSearchIcon[coi] == null) {
3207 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3208 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3209 TOOLBAR_ICON_METADATA_NAME);
3210 }
3211
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3213 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003214 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003215 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003216 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003217 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003218 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3219 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3220 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003221 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003222 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003224 }
3225 }
3226
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003227 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003228 final View searchButtonContainer = findViewById(R.id.search_button_container);
3229 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003230 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003231 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003232 }
3233
Winson Chungc51db6a2011-10-05 11:44:49 -07003234 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003235 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003236 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003237
Winson Chungc51db6a2011-10-05 11:44:49 -07003238 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003239 final SearchManager searchManager =
3240 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3241 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3242
3243 ComponentName activityName = null;
3244 if (globalSearchActivity != null) {
3245 // Check if the global search activity handles voice search
3246 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3247 intent.setPackage(globalSearchActivity.getPackageName());
3248 activityName = intent.resolveActivity(getPackageManager());
3249 }
3250
3251 if (activityName == null) {
3252 // Fallback: check if an activity other than the global search activity
3253 // resolves this
3254 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3255 activityName = intent.resolveActivity(getPackageManager());
3256 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003257 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003258 int coi = getCurrentOrientationIndexForGlobalIcons();
3259 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003260 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3261 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3262 if (sVoiceSearchIcon[coi] == null) {
3263 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3264 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3265 TOOLBAR_ICON_METADATA_NAME);
3266 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003267 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003268 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003269 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003270 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003272 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003273 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003274 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003275 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003276 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003277 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003278 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003279
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003280 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003281 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3282 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003283 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003284 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003285 }
3286
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003287 public void setVoiceButtonProxyVisible(boolean visible) {
3288 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3289 if (voiceButtonProxy != null) {
3290 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3291 }
3292 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003293 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003294 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003295 */
3296 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003297 final View marketButton = findViewById(R.id.market_button);
3298 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3299 // Find the app market activity by resolving an intent.
3300 // (If multiple app markets are installed, it will return the ResolverActivity.)
3301 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003302 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003303 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003304 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003305 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003306 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3307 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003308 marketButton.setVisibility(View.VISIBLE);
3309 } else {
3310 // We should hide and disable the view so that we don't try and restore the visibility
3311 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3312 marketButton.setVisibility(View.GONE);
3313 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003314 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003315 }
3316
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003317 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003318 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3319 Resources r = getResources();
3320 Drawable marketIconDrawable = d.newDrawable(r);
3321 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3322 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3323 marketIconDrawable.setBounds(0, 0, w, h);
3324
3325 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003326 }
3327
Michael Jurkad7c28052012-04-27 15:43:36 -07003328 @Override
3329 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003330 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003331 final List<CharSequence> text = event.getText();
3332 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003333 // Populate event with a fake title based on the current state.
3334 if (mState == State.APPS_CUSTOMIZE) {
3335 text.add(getString(R.string.all_apps_button_label));
3336 } else {
3337 text.add(getString(R.string.all_apps_home_button_label));
3338 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003339 return result;
3340 }
3341
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003342 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003343 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003344 */
3345 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3346 @Override
3347 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003348 closeSystemDialogs();
3349 }
3350 }
3351
3352 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003353 * Receives notifications whenever the appwidgets are reset.
3354 */
3355 private class AppWidgetResetObserver extends ContentObserver {
3356 public AppWidgetResetObserver() {
3357 super(new Handler());
3358 }
3359
3360 @Override
3361 public void onChange(boolean selfChange) {
3362 onAppWidgetReset();
3363 }
3364 }
3365
3366 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003367 * If the activity is currently paused, signal that we need to run the passed Runnable
3368 * in onResume.
3369 *
3370 * This needs to be called from incoming places where resources might have been loaded
3371 * while we are paused. That is becaues the Configuration might be wrong
3372 * when we're not running, and if it comes back to what it was when we
3373 * were paused, we are not restarted.
3374 *
3375 * Implementation of the method from LauncherModel.Callbacks.
3376 *
3377 * @return true if we are currently paused. The caller might be able to
3378 * skip some work in that case since we will come back again.
3379 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003380 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003381 if (mPaused) {
3382 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003383 if (deletePreviousRunnables) {
3384 while (mOnResumeCallbacks.remove(run)) {
3385 }
3386 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003387 mOnResumeCallbacks.add(run);
3388 return true;
3389 } else {
3390 return false;
3391 }
3392 }
3393
Michael Jurkac402cd92013-05-20 15:49:32 +02003394 private boolean waitUntilResume(Runnable run) {
3395 return waitUntilResume(run, false);
3396 }
3397
Michael Jurka7607c2f2013-04-03 14:33:19 -07003398 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003399 * If the activity is currently paused, signal that we need to re-run the loader
3400 * in onResume.
3401 *
3402 * This needs to be called from incoming places where resources might have been loaded
3403 * while we are paused. That is becaues the Configuration might be wrong
3404 * when we're not running, and if it comes back to what it was when we
3405 * were paused, we are not restarted.
3406 *
3407 * Implementation of the method from LauncherModel.Callbacks.
3408 *
3409 * @return true if we are currently paused. The caller might be able to
3410 * skip some work in that case since we will come back again.
3411 */
3412 public boolean setLoadOnResume() {
3413 if (mPaused) {
3414 Log.i(TAG, "setLoadOnResume");
3415 mOnResumeNeedsLoad = true;
3416 return true;
3417 } else {
3418 return false;
3419 }
3420 }
3421
3422 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003424 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003425 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003426 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003427 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003428 } else {
3429 return SCREEN_COUNT / 2;
3430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003432
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 /**
3434 * Refreshes the shortcuts shown on the workspace.
3435 *
3436 * Implementation of the method from LauncherModel.Callbacks.
3437 */
3438 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003439 // If we're starting binding all over again, clear any bind calls we'd postponed in
3440 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3441 // from scratch again
3442 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003443
Michael Jurka7607c2f2013-04-03 14:33:19 -07003444 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003445 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003446 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003447 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003448 int count = workspace.getChildCount();
3449 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003450 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003451 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3452 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003453 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003454 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003455 if (mHotseat != null) {
3456 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003457 }
3458 }
3459
Adam Cohendcd297f2013-06-18 13:13:40 -07003460 @Override
3461 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003462 bindAddScreens(orderedScreenIds);
3463 mWorkspace.addExtraEmptyScreen();
3464 }
3465
3466 @Override
3467 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003468 int count = orderedScreenIds.size();
3469 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003470 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003471 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003472 }
3473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 * Bind the items start-end from the list.
3476 *
3477 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003478 */
Winson Chung64359a52013-07-08 17:17:08 -07003479 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3480 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003481 if (waitUntilResume(new Runnable() {
3482 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003483 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003484 }
3485 })) {
3486 return;
3487 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003488
Winson Chungf0c6ae02012-03-21 16:10:31 -07003489 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3490 Set<String> newApps = new HashSet<String>();
3491 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3492
Winson Chung64359a52013-07-08 17:17:08 -07003493 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3494 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003495 Workspace workspace = mWorkspace;
3496 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003498
3499 // Short circuit if we are loading dock items for a configuration which has no dock
3500 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3501 mHotseat == null) {
3502 continue;
3503 }
3504
Joe Onorato9c1289c2009-08-17 11:03:03 -04003505 switch (item.itemType) {
3506 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3507 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003508 ShortcutInfo info = (ShortcutInfo) item;
3509 String uri = info.intent.toUri(0).toString();
3510 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003511
Winson Chung64359a52013-07-08 17:17:08 -07003512 /*
3513 * TODO: FIX collision case
3514 */
Winson Chungce376632013-07-11 16:12:41 -07003515 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3516 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3517 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3518 throw new RuntimeException("OCCUPIED");
3519 }
Winson Chung64359a52013-07-08 17:17:08 -07003520 }
3521
Adam Cohendcd297f2013-06-18 13:13:40 -07003522 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3523 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003524 boolean animateIconUp = false;
3525 synchronized (newApps) {
3526 if (newApps.contains(uri)) {
3527 animateIconUp = newApps.remove(uri);
3528 }
3529 }
Winson Chung64359a52013-07-08 17:17:08 -07003530 if (forceAnimateIcons) {
3531 // Animate all the applications up now
3532 shortcut.setAlpha(0f);
3533 shortcut.setScaleX(0f);
3534 shortcut.setScaleY(0f);
3535 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3536 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003537 // Prepare the view to be animated up
3538 shortcut.setAlpha(0f);
3539 shortcut.setScaleX(0f);
3540 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003541 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003542 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3543 mNewShortcutAnimateViews.add(shortcut);
3544 }
3545 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003546 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003547 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003548 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003549 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003550 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003551 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3552 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003553 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003555 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003556
Winson Chung64359a52013-07-08 17:17:08 -07003557 if (forceAnimateIcons) {
3558 // We post the animation slightly delayed to prevent slowdowns when we are loading
3559 // right after we return to launcher.
3560 mWorkspace.postDelayed(new Runnable() {
3561 public void run() {
3562 anim.playTogether(bounceAnims);
3563 anim.start();
3564 }
3565 }, NEW_APPS_ANIMATION_DELAY);
3566 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003567 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003568 }
3569
Joe Onorato9c1289c2009-08-17 11:03:03 -04003570 /**
3571 * Implementation of the method from LauncherModel.Callbacks.
3572 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003573 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3574 if (waitUntilResume(new Runnable() {
3575 public void run() {
3576 bindFolders(folders);
3577 }
3578 })) {
3579 return;
3580 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003581 sFolders.clear();
3582 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003583 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584
3585 /**
3586 * Add the views for a widget to the workspace.
3587 *
3588 * Implementation of the method from LauncherModel.Callbacks.
3589 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003590 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3591 if (waitUntilResume(new Runnable() {
3592 public void run() {
3593 bindAppWidget(item);
3594 }
3595 })) {
3596 return;
3597 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003598
Daniel Sandler843e8602010-06-07 14:59:01 -04003599 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3600 if (DEBUG_WIDGETS) {
3601 Log.d(TAG, "bindAppWidget: " + item);
3602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003603 final Workspace workspace = mWorkspace;
3604
3605 final int appWidgetId = item.appWidgetId;
3606 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003607 if (DEBUG_WIDGETS) {
3608 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3609 }
3610
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3612
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003614 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615
Adam Cohendcd297f2013-06-18 13:13:40 -07003616 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003617 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003618 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3619
Joe Onorato9c1289c2009-08-17 11:03:03 -04003620 workspace.requestLayout();
3621
Daniel Sandler843e8602010-06-07 14:59:01 -04003622 if (DEBUG_WIDGETS) {
3623 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3624 + (SystemClock.uptimeMillis()-start) + "ms");
3625 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003626 }
3627
Adam Cohen1462de32012-07-24 22:34:36 -07003628 public void onPageBoundSynchronously(int page) {
3629 mSynchronouslyBoundPages.add(page);
3630 }
3631
Joe Onorato9c1289c2009-08-17 11:03:03 -04003632 /**
3633 * Callback saying that there aren't any more items to bind.
3634 *
3635 * Implementation of the method from LauncherModel.Callbacks.
3636 */
Adam Cohene25af792013-06-06 23:08:25 -07003637 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003638 if (waitUntilResume(new Runnable() {
3639 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003640 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003641 }
3642 })) {
3643 return;
3644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 if (mSavedState != null) {
3646 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003647 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 mSavedState = null;
3650 }
3651
Adam Cohen1462de32012-07-24 22:34:36 -07003652 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003654 // If we received the result of any pending adds while the loader was running (e.g. the
3655 // widget configuration forced an orientation change), process them now.
3656 for (int i = 0; i < sPendingAddList.size(); i++) {
3657 completeAdd(sPendingAddList.get(i));
3658 }
3659 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003660
Winson Chungc51db6a2011-10-05 11:44:49 -07003661 // Update the market app icon as necessary (the other icons will be managed in response to
3662 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003663 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003664
Winson Chungf0c6ae02012-03-21 16:10:31 -07003665 // Animate up any icons as necessary
3666 if (mVisible || mWorkspaceLoading) {
3667 Runnable newAppsRunnable = new Runnable() {
3668 @Override
3669 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003670 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003671 }
3672 };
Winson Chunga2413752012-04-03 14:22:34 -07003673
Adam Cohendcd297f2013-06-18 13:13:40 -07003674 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3675 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003676 if (canRunNewAppsAnimation()) {
3677 // If the user has not interacted recently, then either snap to the new page to show
3678 // the new-apps animation or just run them if they are to appear on the current page
3679 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003680 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003681 } else {
3682 runNewAppsAnimation(false);
3683 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003684 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003685 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003686 // are on another page (or just normally if they are added to the current page)
3687 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003688 }
3689 }
3690
3691 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003692 if (upgradePath) {
3693 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003694 mWorkspace.stripDuplicateApps();
3695 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003696 }
Adam Cohen99894d92013-06-14 11:22:59 -07003697
Adam Cohen66a01fd2013-06-11 12:48:00 -07003698 mWorkspace.post(new Runnable() {
3699 @Override
3700 public void run() {
3701 onFinishBindingItems();
3702 }
3703 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003704 }
3705
Winson Chunga2413752012-04-03 14:22:34 -07003706 private boolean canRunNewAppsAnimation() {
3707 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3708 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3709 }
3710
Winson Chungc9168342013-06-26 14:54:55 -07003711 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3712 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3713 PropertyValuesHolder.ofFloat("alpha", 1f),
3714 PropertyValuesHolder.ofFloat("scaleX", 1f),
3715 PropertyValuesHolder.ofFloat("scaleY", 1f));
3716 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3717 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3718 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3719 return bounceAnim;
3720 }
3721
Winson Chungf0c6ae02012-03-21 16:10:31 -07003722 /**
3723 * Runs a new animation that scales up icons that were added while Launcher was in the
3724 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003725 *
3726 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003727 */
Winson Chunga2413752012-04-03 14:22:34 -07003728 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003729 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003730 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003731
3732 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003733 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3734 @Override
3735 public int compare(View a, View b) {
3736 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3737 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3738 int cellCountX = LauncherModel.getCellCountX();
3739 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3740 }
3741 });
Winson Chunga2413752012-04-03 14:22:34 -07003742
3743 // Animate each of the views in place (or show them immediately if requested)
3744 if (immediate) {
3745 for (View v : mNewShortcutAnimateViews) {
3746 v.setAlpha(1f);
3747 v.setScaleX(1f);
3748 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 }
Winson Chunga2413752012-04-03 14:22:34 -07003750 } else {
3751 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3752 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003753 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003754 }
3755 anim.playTogether(bounceAnims);
3756 anim.addListener(new AnimatorListenerAdapter() {
3757 @Override
3758 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003759 if (mWorkspace != null) {
3760 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3761 }
Winson Chunga2413752012-04-03 14:22:34 -07003762 }
3763 });
3764 anim.start();
3765 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003766
3767 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003768 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003769 mNewShortcutAnimateViews.clear();
3770 new Thread("clearNewAppsThread") {
3771 public void run() {
3772 mSharedPrefs.edit()
3773 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3774 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3775 .commit();
3776 }
3777 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003778 }
3779
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003780 @Override
3781 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003782 boolean searchVisible = updateGlobalSearchIcon();
3783 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003784 if (mSearchDropTargetBar != null) {
3785 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3786 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003787 }
3788
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003789 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003790 * Add the icons for all apps.
3791 *
3792 * Implementation of the method from LauncherModel.Callbacks.
3793 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003794 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003795 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3796 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003797 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003798 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003800 }
3801
3802 /**
3803 * A package was updated.
3804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003807 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3808 if (waitUntilResume(new Runnable() {
3809 public void run() {
3810 bindAppsUpdated(apps);
3811 }
3812 })) {
3813 return;
3814 }
3815
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003816 if (mWorkspace != null) {
3817 mWorkspace.updateShortcuts(apps);
3818 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003819 }
3820
3821 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003822 * A package was uninstalled. We take both the super set of packageNames
3823 * in addition to specific applications to remove, the reason being that
3824 * this can be called when a package is updated as well. In that scenario,
3825 * we only remove specific components from the workspace, where as
3826 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 *
3828 * Implementation of the method from LauncherModel.Callbacks.
3829 */
Winson Chung83892cc2013-05-01 16:53:33 -07003830 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3831 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003832 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003833 if (waitUntilResume(new Runnable() {
3834 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003835 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003836 }
3837 })) {
3838 return;
3839 }
3840
Winson Chung64359a52013-07-08 17:17:08 -07003841 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003842 mWorkspace.removeItemsByPackageName(packageNames);
3843 } else {
3844 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003845 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003846
Winson Chunga1820962011-10-03 16:31:06 -07003847 // 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 private ArrayList<Object> mWidgetsAndShortcuts;
3855 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003856 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003857 bindPackagesUpdated(mWidgetsAndShortcuts);
3858 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003859 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003860 };
3861
3862 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3863 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3864 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003865 return;
3866 }
3867
Winson Chung64359a52013-07-08 17:17:08 -07003868 // Update the widgets pane
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}