blob: bf9773ad04a4d91133b0216256dcf8218ca6e877 [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;
Craig Mautner360310b2012-10-26 15:13:08 -0700221 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private DragLayer mDragLayer;
223 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700224
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700225 private AppWidgetManager mAppWidgetManager;
226 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Michael Jurkac9d95c52011-08-29 14:03:34 -0700228 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700229 private AppWidgetProviderInfo mPendingAddWidgetInfo;
230
Michael Jurka0280c3b2010-09-17 15:00:07 -0700231 private int[] mTmpAddItemCellCoordinates = new int[2];
232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private FolderInfo mFolderInfo;
234
Winson Chung3d503fb2011-07-13 17:25:49 -0700235 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800236 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700237
Winson Chungc51db6a2011-10-05 11:44:49 -0700238 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700239 private AppsCustomizeTabHost mAppsCustomizeTabHost;
240 private AppsCustomizePagedView mAppsCustomizeContent;
241 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700244 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
245 // scroll issues (because the workspace may not have been measured yet) and extra work.
246 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
247 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
249 private SpannableStringBuilder mDefaultKeySsb = null;
250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private boolean mWorkspaceLoading = true;
252
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800253 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 private boolean mRestoring;
255 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700256 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257
Michael Jurka7607c2f2013-04-03 14:33:19 -0700258 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
259
Winson Chung4a2afa32012-07-19 14:53:05 -0700260 // Keep track of whether the user has left launcher
261 private static boolean sPausedFromUserAction = false;
262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 private Bundle mSavedInstanceState;
264
Joe Onorato9c1289c2009-08-17 11:03:03 -0400265 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800266 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800267 private boolean mUserPresent = true;
268 private boolean mVisible = false;
269 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700270 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400271
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700272 private static LocaleConfiguration sLocaleConfiguration = null;
273
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700274 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700275
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700276 private Intent mAppMarketIntent = null;
277
Adam Cohended9f8d2010-11-03 13:25:16 -0700278 // Related to the auto-advancing of widgets
279 private final int ADVANCE_MSG = 1;
280 private final int mAdvanceInterval = 20000;
281 private final int mAdvanceStagger = 250;
282 private long mAutoAdvanceSentTime;
283 private long mAutoAdvanceTimeLeft = -1;
284 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
285 new HashMap<View, AppWidgetProviderInfo>();
286
Winson Chung400438b2011-01-16 17:53:48 -0800287 // Determines how long to wait after a rotation before restoring the screen orientation to
288 // match the sensor state.
289 private final int mRestoreScreenOrientationDelay = 500;
290
Michael Jurka4ef207b2010-11-29 17:05:45 -0800291 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700292 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
293 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
294 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800295
Craig Mautner360310b2012-10-26 15:13:08 -0700296 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700297
Adam Cohen1462de32012-07-24 22:34:36 -0700298 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
299
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700300 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
301
Winson Chung46353de2012-02-16 14:05:10 -0800302 // We only want to get the SharedPreferences once since it does an FS stat each time we get
303 // it from the context.
304 private SharedPreferences mSharedPrefs;
305
Adam Cohene25af792013-06-06 23:08:25 -0700306 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
307
Winson Chungf0c6ae02012-03-21 16:10:31 -0700308 // Holds the page that we need to animate to, and the icon views that we need to animate up
309 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700310 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700311 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700312 private ImageView mFolderIconImageView;
313 private Bitmap mFolderIconBitmap;
314 private Canvas mFolderIconCanvas;
315 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700316
Michael Jurkaddd62e92011-02-16 17:49:14 -0800317 private BubbleTextView mWaitingForResume;
318
Michael Jurka22143132012-10-04 17:42:38 +0200319 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
320 = new HideFromAccessibilityHelper();
321
Michael Jurkac1f5d262011-09-30 19:32:27 -0700322 private Runnable mBuildLayersRunnable = new Runnable() {
323 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700324 if (mWorkspace != null) {
325 mWorkspace.buildPageHardwareLayers();
326 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700327 }
328 };
329
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800330 private static ArrayList<PendingAddArguments> sPendingAddList
331 = new ArrayList<PendingAddArguments>();
332
Michael Jurka0a457bf2012-11-19 14:05:05 -0800333 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
334
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800335 private static class PendingAddArguments {
336 int requestCode;
337 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700338 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700339 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800340 int cellX;
341 int cellY;
342 }
343
Michael Jurka0a457bf2012-11-19 14:05:05 -0800344 private static boolean isPropertyEnabled(String propertyName) {
345 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700346 }
347
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 @Override
349 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700350 if (DEBUG_STRICT_MODE) {
351 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
352 .detectDiskReads()
353 .detectDiskWrites()
354 .detectNetwork() // or .detectAll() for all detectable problems
355 .penaltyLog()
356 .build());
357 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
358 .detectLeakedSqlLiteObjects()
359 .detectLeakedClosableObjects()
360 .penaltyLog()
361 .penaltyDeath()
362 .build());
363 }
364
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400366
367 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
368 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400369 LauncherAppState app = LauncherAppState.getInstance();
370 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700371 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800372 mModel = app.setLauncher(this);
373 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400374 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700376
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700377 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
379 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700380
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700381 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
382 // this also ensures that any synchronous binding below doesn't re-trigger another
383 // LauncherModel load.
384 mPaused = false;
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200387 android.os.Debug.startMethodTracing(
388 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
390
391 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 setContentView(R.layout.launcher);
393 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700394 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800396 registerContentObservers();
397
Joe Onorato7c312c12009-08-13 21:36:53 -0700398 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 mSavedState = savedInstanceState;
401 restoreState(mSavedState);
402
Winson Chunga12a2502010-12-20 14:41:35 -0800403 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700404 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200405 mAppsCustomizeContent.onPackagesUpdated(
406 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700407 }
Winson Chunga12a2502010-12-20 14:41:35 -0800408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 if (PROFILE_STARTUP) {
410 android.os.Debug.stopMethodTracing();
411 }
412
413 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700414 if (sPausedFromUserAction) {
415 // If the user leaves launcher, then we should just load items asynchronously when
416 // they return.
417 mModel.startLoader(true, -1);
418 } else {
419 // We only load the page synchronously if the user rotates (or triggers a
420 // configuration change) while launcher is in the foreground
421 mModel.startLoader(true, mWorkspace.getCurrentPage());
422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424
425 // For handling default keys
426 mDefaultKeySsb = new SpannableStringBuilder();
427 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800428
429 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
430 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800431
Adam Cohenf9426d52012-06-04 17:26:21 -0700432 updateGlobalIcons();
433
434 // On large interfaces, we want the screen to auto-rotate based on the current orientation
435 unlockScreenOrientation(true);
436 }
437
Winson Chung4a2afa32012-07-19 14:53:05 -0700438 protected void onUserLeaveHint() {
439 super.onUserLeaveHint();
440 sPausedFromUserAction = true;
441 }
442
Adam Cohenf9426d52012-06-04 17:26:21 -0700443 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700444 boolean searchVisible = false;
445 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800446 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 int coi = getCurrentOrientationIndexForGlobalIcons();
448 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
449 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 updateAppMarketIcon();
451 searchVisible = updateGlobalSearchIcon();
452 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700453 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700454 if (sGlobalSearchIcon[coi] != null) {
455 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700456 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700457 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700458 if (sVoiceSearchIcon[coi] != null) {
459 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700460 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700461 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700462 if (sAppMarketIcon[coi] != null) {
463 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800464 }
Winson Chungadf0c182012-08-23 14:59:07 -0700465 if (mSearchDropTargetBar != null) {
466 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 }
Romain Guycbb89e42009-06-08 15:52:54 -0700469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700471 if (sLocaleConfiguration == null) {
472 new AsyncTask<Void, Void, LocaleConfiguration>() {
473 @Override
474 protected LocaleConfiguration doInBackground(Void... unused) {
475 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
476 readConfiguration(Launcher.this, localeConfiguration);
477 return localeConfiguration;
478 }
479
480 @Override
481 protected void onPostExecute(LocaleConfiguration result) {
482 sLocaleConfiguration = result;
483 checkForLocaleChange(); // recursive, but now with a locale configuration
484 }
485 }.execute();
486 return;
487 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 final Configuration configuration = getResources().getConfiguration();
490
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700491 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 final String locale = configuration.locale.toString();
493
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 final int mcc = configuration.mcc;
496
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 final int mnc = configuration.mnc;
499
Romain Guy84f296c2009-11-04 15:00:44 -0800500 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501
Romain Guy84f296c2009-11-04 15:00:44 -0800502 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700503 sLocaleConfiguration.locale = locale;
504 sLocaleConfiguration.mcc = mcc;
505 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700506
Joe Onorato0589f0f2010-02-08 13:44:00 -0800507 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700508
509 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
510 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700511 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700512 public void run() {
513 writeConfiguration(Launcher.this, localeConfiguration);
514 }
515 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700516 }
517 }
518
519 private static class LocaleConfiguration {
520 public String locale;
521 public int mcc = -1;
522 public int mnc = -1;
523 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700524
Romain Guy98d01652009-06-30 16:21:04 -0700525 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
526 DataInputStream in = null;
527 try {
528 in = new DataInputStream(context.openFileInput(PREFERENCES));
529 configuration.locale = in.readUTF();
530 configuration.mcc = in.readInt();
531 configuration.mnc = in.readInt();
532 } catch (FileNotFoundException e) {
533 // Ignore
534 } catch (IOException e) {
535 // Ignore
536 } finally {
537 if (in != null) {
538 try {
539 in.close();
540 } catch (IOException e) {
541 // Ignore
542 }
543 }
544 }
545 }
546
547 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
548 DataOutputStream out = null;
549 try {
550 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
551 out.writeUTF(configuration.locale);
552 out.writeInt(configuration.mcc);
553 out.writeInt(configuration.mnc);
554 out.flush();
555 } catch (FileNotFoundException e) {
556 // Ignore
557 } catch (IOException e) {
558 //noinspection ResultOfMethodCallIgnored
559 context.getFileStreamPath(PREFERENCES).delete();
560 } finally {
561 if (out != null) {
562 try {
563 out.close();
564 } catch (IOException e) {
565 // Ignore
566 }
567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 }
569 }
570
Bjorn Bringertc459e522013-06-07 19:36:01 +0100571 public LayoutInflater getInflater() {
572 return mInflater;
573 }
574
Adam Cohen716b51e2011-06-30 12:09:54 -0700575 public DragLayer getDragLayer() {
576 return mDragLayer;
577 }
578
Winson Chung36a62fe2012-05-06 18:04:42 -0700579 boolean isDraggingEnabled() {
580 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
581 // that is subsequently removed from the workspace in startBinding().
582 return !mModel.isLoadingWorkspace();
583 }
584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 static int getScreen() {
586 synchronized (sLock) {
587 return sScreen;
588 }
589 }
590
591 static void setScreen(int screen) {
592 synchronized (sLock) {
593 sScreen = screen;
594 }
595 }
596
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 /**
598 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
599 * a configuration step, this allows the proper animations to run after other transitions.
600 */
601 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700602 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800603 switch (args.requestCode) {
604 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700605 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700606 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 break;
608 case REQUEST_PICK_SHORTCUT:
609 processShortcut(args.intent);
610 break;
611 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700612 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700613 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700614 result = true;
615 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 case REQUEST_CREATE_APPWIDGET:
617 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700618 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700619 result = true;
620 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800621 case REQUEST_PICK_WALLPAPER:
622 // We just wanted the activity result here so we can clear mWaitingForResult
623 break;
624 }
Michael Jurka27614d22012-04-02 06:40:22 -0700625 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
626 // if you turned the screen off and then back while in All Apps, Launcher would not
627 // return to the workspace. Clearing mAddInfo.container here fixes this issue
628 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700629 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700633 protected void onActivityResult(
634 final int requestCode, final int resultCode, final Intent data) {
635 if (requestCode == REQUEST_BIND_APPWIDGET) {
636 int appWidgetId = data != null ?
637 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
638 if (resultCode == RESULT_CANCELED) {
639 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
640 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700641 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700642 }
643 return;
644 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
647 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700648 mWaitingForResult = false;
649
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 // We have special handling for widgets
651 if (isWidgetDrop) {
652 int appWidgetId = data != null ?
653 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700654 if (appWidgetId < 0) {
655 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
656 "widget configuration activity.");
657 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
658 } else {
659 completeTwoStageWidgetDrop(resultCode, appWidgetId);
660 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800661 return;
662 }
663
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 // The pattern used here is that a user PICKs a specific application,
665 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
668 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700669 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800670 final PendingAddArguments args = new PendingAddArguments();
671 args.requestCode = requestCode;
672 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700673 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700674 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 args.cellX = mPendingAddInfo.cellX;
676 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800677 if (isWorkspaceLocked()) {
678 sPendingAddList.add(args);
679 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700680 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800683 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700684 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
686 null);
687 }
688
689 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700690 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700691 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 Runnable onCompleteRunnable = null;
693 int animationType = 0;
694
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700695 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800696 if (resultCode == RESULT_OK) {
697 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
698 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700699 mPendingAddWidgetInfo);
700 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800706 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
707 null);
708 }
709 };
710 } else if (resultCode == RESULT_CANCELED) {
711 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
712 onCompleteRunnable = new Runnable() {
713 @Override
714 public void run() {
715 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
716 null);
717 }
718 };
719 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700720 if (mDragLayer.getAnimatedView() != null) {
721 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
722 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
723 animationType, boundWidget, true);
724 } else {
725 // The animated view may be null in the case of a rotation during widget configuration
726 onCompleteRunnable.run();
727 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
729
730 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700731 protected void onStop() {
732 super.onStop();
733 FirstFrameAnimatorHelper.setIsVisible(false);
734 }
735
736 @Override
737 protected void onStart() {
738 super.onStart();
739 FirstFrameAnimatorHelper.setIsVisible(true);
740 }
741
742 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200744 long startTime = 0;
745 if (DEBUG_RESUME_TIME) {
746 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400747 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700750
Winson Chung4a2afa32012-07-19 14:53:05 -0700751 // Restore the previous launcher state
752 if (mOnResumeState == State.WORKSPACE) {
753 showWorkspace(false);
754 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
755 showAllApps(false);
756 }
757 mOnResumeState = State.NONE;
758
Craig Mautner360310b2012-10-26 15:13:08 -0700759 // Background was set to gradient in onPause(), restore to black if in all apps.
760 setWorkspaceBackground(mState == State.WORKSPACE);
761
Winson Chungde0fb8f2012-05-08 14:37:08 -0700762 // Process any items that were added while Launcher was away
763 InstallShortcutReceiver.flushInstallQueue(this);
764
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800765 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700766 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700767 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400768 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700769 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400770 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700771 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200773 if (mOnResumeCallbacks.size() > 0) {
774 // We might have postponed some bind calls until onResume (see waitUntilResume) --
775 // execute them here
776 long startTimeCallbacks = 0;
777 if (DEBUG_RESUME_TIME) {
778 startTimeCallbacks = System.currentTimeMillis();
779 }
780
781 if (mAppsCustomizeContent != null) {
782 mAppsCustomizeContent.setBulkBind(true);
783 }
784 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
785 mOnResumeCallbacks.get(i).run();
786 }
787 if (mAppsCustomizeContent != null) {
788 mAppsCustomizeContent.setBulkBind(false);
789 }
790 mOnResumeCallbacks.clear();
791 if (DEBUG_RESUME_TIME) {
792 Log.d(TAG, "Time spent processing callbacks in onResume: " +
793 (System.currentTimeMillis() - startTimeCallbacks));
794 }
Michael Jurka447bf842013-05-15 14:52:15 +0200795 }
Winson Chunge4e50662012-01-23 14:45:13 -0800796
797 // Reset the pressed state of icons that were locked in the press state while activities
798 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800799 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800800 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800801 mWaitingForResume.setStayPressed(false);
802 }
Winson Chunge4e50662012-01-23 14:45:13 -0800803 if (mAppsCustomizeContent != null) {
804 // Resets the previous all apps icon press state
805 mAppsCustomizeContent.resetDrawableState();
806 }
Adam Cohen06dff352012-06-01 17:17:08 -0700807 // It is possible that widgets can receive updates while launcher is not in the foreground.
808 // Consequently, the widgets will be inflated in the orientation of the foreground activity
809 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
810 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700811 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700812
813 // Again, as with the above scenario, it's possible that one or more of the global icons
814 // were updated in the wrong orientation.
815 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200816 if (DEBUG_RESUME_TIME) {
817 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
818 }
Adam Cohen06dff352012-06-01 17:17:08 -0700819 }
820
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700822 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700823 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
824 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
825 // when Launcher resumes and we are still in AllApps.
826 updateWallpaperVisibility(true);
827
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700828 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700829 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800830 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700831 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 }
Romain Guycbb89e42009-06-08 15:52:54 -0700833
Adam Cohen66a01fd2013-06-11 12:48:00 -0700834 protected void onFinishBindingItems() {
835 }
836
837 // Add a fullscreen unpadded view to the workspace to the left all other screens.
838 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700839 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700840 }
841
Adam Cohenedb40762013-07-18 16:45:45 -0700842 // The custom content needs to offset its content to account for the QSB
843 public int getTopOffsetForCustomContent() {
844 return mWorkspace.getPaddingTop();
845 }
846
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700847 @Override
848 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400849 // Flag the loader to stop early before switching
850 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700851 if (mAppsCustomizeContent != null) {
852 mAppsCustomizeContent.surrender();
853 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800854 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700855 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700856
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800857 // We can't hide the IME if it was forced open. So don't bother
858 /*
859 @Override
860 public void onWindowFocusChanged(boolean hasFocus) {
861 super.onWindowFocusChanged(hasFocus);
862
863 if (hasFocus) {
864 final InputMethodManager inputManager = (InputMethodManager)
865 getSystemService(Context.INPUT_METHOD_SERVICE);
866 WindowManager.LayoutParams lp = getWindow().getAttributes();
867 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
868 android.os.Handler()) {
869 protected void onReceiveResult(int resultCode, Bundle resultData) {
870 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
871 }
872 });
873 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
874 }
875 }
876 */
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 private boolean acceptFilter() {
879 final InputMethodManager inputManager = (InputMethodManager)
880 getSystemService(Context.INPUT_METHOD_SERVICE);
881 return !inputManager.isFullscreenMode();
882 }
883
884 @Override
885 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700886 final int uniChar = event.getUnicodeChar();
887 final boolean handled = super.onKeyDown(keyCode, event);
888 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
889 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
891 keyCode, event);
892 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700893 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700894 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700895 // showSearchDialog()
896 // If there are multiple keystrokes before the search dialog takes focus,
897 // onSearchRequested() will be called for every keystroke,
898 // but it is idempotent, so it's fine.
899 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901 }
902
Joe Onorato8a9625e2010-01-28 15:55:35 -0800903 // Eat the long press event so the keyboard doesn't come up.
904 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
905 return true;
906 }
907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 return handled;
909 }
910
Karl Rosaen138a0412009-04-23 19:00:21 -0700911 private String getTypedText() {
912 return mDefaultKeySsb.toString();
913 }
914
915 private void clearTypedText() {
916 mDefaultKeySsb.clear();
917 mDefaultKeySsb.clearSpans();
918 Selection.setSelection(mDefaultKeySsb, 0);
919 }
920
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700922 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
923 * State
924 */
925 private static State intToState(int stateOrdinal) {
926 State state = State.WORKSPACE;
927 final State[] stateValues = State.values();
928 for (int i = 0; i < stateValues.length; i++) {
929 if (stateValues[i].ordinal() == stateOrdinal) {
930 state = stateValues[i];
931 break;
932 }
933 }
934 return state;
935 }
936
937 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 * Restores the previous state, if it exists.
939 *
940 * @param savedState The previous state.
941 */
942 private void restoreState(Bundle savedState) {
943 if (savedState == null) {
944 return;
945 }
946
Michael Jurka883f55b2010-10-21 15:47:14 -0700947 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700948 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700949 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800950 }
951
Winson Chungf0c6ae02012-03-21 16:10:31 -0700952 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700954 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700958 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700959
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
961 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700962 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
964 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700965 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
966 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
967 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700968 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 mRestoring = true;
970 }
971
972 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
973 if (renameFolder) {
974 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700975 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 mRestoring = true;
977 }
Winson Chunga12a2502010-12-20 14:41:35 -0800978
Winson Chung785d2eb2011-04-14 16:08:02 -0700979 // Restore the AppsCustomize tab
980 if (mAppsCustomizeTabHost != null) {
981 String curTab = savedState.getString("apps_customize_currentTab");
982 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700983 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700984 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700985 mAppsCustomizeContent.loadAssociatedPages(
986 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 }
988
Winson Chung5afbf7b2011-07-25 11:53:08 -0700989 int currentIndex = savedState.getInt("apps_customize_currentIndex");
990 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 }
993
994 /**
995 * Finds all the views we need and configure them properly.
996 */
997 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700998 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400999
Craig Mautner360310b2012-10-26 15:13:08 -07001000 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001001 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1002 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001003 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001004
1005 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1006 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007
Winson Chung4c98d922011-05-31 16:50:48 -07001008 // Setup the drag layer
1009 mDragLayer.setup(this, dragController);
1010
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 // Setup the hotseat
1012 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1013 if (mHotseat != null) {
1014 mHotseat.setup(this);
1015 }
1016
Winson Chung4c98d922011-05-31 16:50:48 -07001017 // Setup the workspace
1018 mWorkspace.setHapticFeedbackEnabled(false);
1019 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001020 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001021 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001022
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001024 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001025
Winson Chungf0ea4d32011-06-06 14:27:16 -07001026 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001027 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001028 mAppsCustomizeContent = (AppsCustomizePagedView)
1029 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1030 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001031
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001033 dragController.setDragScoller(mWorkspace);
1034 dragController.setScrollView(mDragLayer);
1035 dragController.setMoveTarget(mWorkspace);
1036 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001037 if (mSearchDropTargetBar != null) {
1038 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001039 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001040
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001041 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001042 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001043 final View ww = new WeightWatcher(this);
1044 ww.setAlpha(0.5f);
1045 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001046 new FrameLayout.LayoutParams(
1047 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001048 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001049 Gravity.BOTTOM)
1050 );
1051 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053
1054 /**
1055 * Creates a view representing a shortcut.
1056 *
1057 * @param info The data structure describing the shortcut.
1058 *
1059 * @return A View inflated from R.layout.application.
1060 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001061 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001063 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065
1066 /**
1067 * Creates a view representing a shortcut inflated from the specified resource.
1068 *
1069 * @param layoutResId The id of the XML layout used to create the shortcut.
1070 * @param parent The group the shortcut belongs to.
1071 * @param info The data structure describing the shortcut.
1072 *
1073 * @return A View inflated from layoutResId.
1074 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001075 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001076 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1077 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 return favorite;
1080 }
1081
1082 /**
1083 * Add an application shortcut to the workspace.
1084 *
1085 * @param data The intent describing the application.
1086 * @param cellInfo The position on screen where to create the shortcut.
1087 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001088 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001090 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091
Adam Cohenfbba09b2011-07-18 21:43:05 -07001092 // First we check if we already know the exact location where we want to add this item.
1093 if (cellX >= 0 && cellY >= 0) {
1094 cellXY[0] = cellX;
1095 cellXY[1] = cellY;
1096 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001097 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 return;
1099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001101 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001102
Romain Guy73b979d2009-06-09 12:57:21 -07001103 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001104 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001106 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001107 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001108 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001109 } else {
1110 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
Romain Guycbb89e42009-06-08 15:52:54 -07001113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 /**
1115 * Add a shortcut to the workspace.
1116 *
1117 * @param data The intent describing the shortcut.
1118 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001120 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 int cellY) {
1122 int[] cellXY = mTmpAddItemCellCoordinates;
1123 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001124 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001125
Michael Jurkad3ef3062010-11-23 16:23:58 -08001126 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001127
Adam Cohen558baaf2011-08-15 15:22:57 -07001128 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001129 if (info == null) {
1130 return;
1131 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001132 final View view = createShortcut(info);
1133
Adam Cohenfbba09b2011-07-18 21:43:05 -07001134 // First we check if we already know the exact location where we want to add this item.
1135 if (cellX >= 0 && cellY >= 0) {
1136 cellXY[0] = cellX;
1137 cellXY[1] = cellY;
1138 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001139
1140 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001141 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001142 true, null,null)) {
1143 return;
1144 }
1145 DragObject dragObject = new DragObject();
1146 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001147 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1148 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001149 return;
1150 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001151 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001154 foundCellSpan = (result != null);
1155 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001156 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001157 }
1158
1159 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001160 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001161 return;
1162 }
1163
Adam Cohendcd297f2013-06-18 13:13:40 -07001164 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
1166 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001167 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001168 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
1171
Adam Cohen2f093b62012-04-30 18:59:53 -07001172 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1173 int minHeight) {
1174 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001175 // We want to account for the extra amount of padding that we are adding to the widget
1176 // to ensure that it gets the full amount of space that it has requested
1177 int requiredWidth = minWidth + padding.left + padding.right;
1178 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001179 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001180 }
1181
Adam Cohen2f093b62012-04-30 18:59:53 -07001182 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1183 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001184 }
1185
Adam Cohen2f093b62012-04-30 18:59:53 -07001186 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1187 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001188 }
1189
Adam Cohen2f093b62012-04-30 18:59:53 -07001190 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1191 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001192 }
1193
Adam Cohen2f093b62012-04-30 18:59:53 -07001194 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1195 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001196 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001197 }
1198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001202 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001205 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001206 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1207 if (appWidgetInfo == null) {
1208 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1209 }
Romain Guycbb89e42009-06-08 15:52:54 -07001210
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001211 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001213
Adam Cohen2f093b62012-04-30 18:59:53 -07001214 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1215 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001218 // We have saved the position to which the widget was dragged-- this really only matters
1219 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001220 int[] cellXY = mTmpAddItemCellCoordinates;
1221 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001222 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1225 cellXY[0] = mPendingAddInfo.cellX;
1226 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001227 spanXY[0] = mPendingAddInfo.spanX;
1228 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001229 foundCellSpan = true;
1230 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001232 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1234 spanXY[1], cellXY, finalSpan);
1235 spanXY[0] = finalSpan[0];
1236 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001237 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001240 }
1241
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001243 if (appWidgetId != -1) {
1244 // Deleting an app widget ID is a void call but writes to disk before returning
1245 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001246 new Thread("deleteAppWidgetId") {
1247 public void run() {
1248 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1249 }
1250 }.start();
1251 }
Winson Chung93eef082012-03-23 15:59:27 -07001252 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001253 return;
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001257 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1258 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 launcherInfo.spanX = spanXY[0];
1260 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001261 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1262 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001265 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
1267 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001268 if (hostView == null) {
1269 // Perform actual inflation because we're live
1270 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1271 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1272 } else {
1273 // The AppWidgetHostView has already been inflated and instantiated
1274 launcherInfo.hostView = hostView;
1275 }
Romain Guycbb89e42009-06-08 15:52:54 -07001276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001278 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001279 launcherInfo.notifyWidgetSizeChanged(this);
1280
Adam Cohendcd297f2013-06-18 13:13:40 -07001281 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001282 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001283
1284 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001286 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
Romain Guycbb89e42009-06-08 15:52:54 -07001288
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1290 @Override
1291 public void onReceive(Context context, Intent intent) {
1292 final String action = intent.getAction();
1293 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1294 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001295 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001296 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001297
Winson Chungc100e8e2011-08-09 16:02:43 -07001298 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001299 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001300 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1301 mAppsCustomizeTabHost.reset();
1302 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001303 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1305 mUserPresent = true;
1306 updateRunning();
1307 }
1308 }
1309 };
1310
1311 @Override
1312 public void onAttachedToWindow() {
1313 super.onAttachedToWindow();
1314
1315 // Listen for broadcasts related to user-presence
1316 final IntentFilter filter = new IntentFilter();
1317 filter.addAction(Intent.ACTION_SCREEN_OFF);
1318 filter.addAction(Intent.ACTION_USER_PRESENT);
1319 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001320 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001321 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001322 mVisible = true;
1323 }
1324
1325 @Override
1326 public void onDetachedFromWindow() {
1327 super.onDetachedFromWindow();
1328 mVisible = false;
1329
Adam Cohend113e0c2010-11-11 10:48:05 -08001330 if (mAttached) {
1331 unregisterReceiver(mReceiver);
1332 mAttached = false;
1333 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 updateRunning();
1335 }
1336
1337 public void onWindowVisibilityChanged(int visibility) {
1338 mVisible = visibility == View.VISIBLE;
1339 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001340 // The following code used to be in onResume, but it turns out onResume is called when
1341 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1342 // is a more appropriate event to handle
1343 if (mVisible) {
1344 mAppsCustomizeTabHost.onWindowVisible();
1345 if (!mWorkspaceLoading) {
1346 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001347 // We want to let Launcher draw itself at least once before we force it to build
1348 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001349 // apps is nice and speedy.
1350 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001351 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001352 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001353 if (mStarted) return;
1354 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001355 // We delay the layer building a bit in order to give
1356 // other message processing a time to run. In particular
1357 // this avoids a delay in hiding the IME if it was
1358 // currently shown, because doing that may involve
1359 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001360 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001361 final ViewTreeObserver.OnDrawListener listener = this;
1362 mWorkspace.post(new Runnable() {
1363 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001364 if (mWorkspace != null &&
1365 mWorkspace.getViewTreeObserver() != null) {
1366 mWorkspace.getViewTreeObserver().
1367 removeOnDrawListener(listener);
1368 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001369 }
1370 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001371 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001372 }
1373 });
1374 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001375 // When Launcher comes back to foreground, a different Activity might be responsible for
1376 // the app market intent, so refresh the icon
1377 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001378 clearTypedText();
1379 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 }
1381
1382 private void sendAdvanceMessage(long delay) {
1383 mHandler.removeMessages(ADVANCE_MSG);
1384 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1385 mHandler.sendMessageDelayed(msg, delay);
1386 mAutoAdvanceSentTime = System.currentTimeMillis();
1387 }
1388
1389 private void updateRunning() {
1390 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1391 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1392 mAutoAdvanceRunning = autoAdvanceRunning;
1393 if (autoAdvanceRunning) {
1394 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1395 sendAdvanceMessage(delay);
1396 } else {
1397 if (!mWidgetsToAdvance.isEmpty()) {
1398 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1399 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1400 }
1401 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001402 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001403 }
1404 }
1405 }
1406
1407 private final Handler mHandler = new Handler() {
1408 @Override
1409 public void handleMessage(Message msg) {
1410 if (msg.what == ADVANCE_MSG) {
1411 int i = 0;
1412 for (View key: mWidgetsToAdvance.keySet()) {
1413 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1414 final int delay = mAdvanceStagger * i;
1415 if (v instanceof Advanceable) {
1416 postDelayed(new Runnable() {
1417 public void run() {
1418 ((Advanceable) v).advance();
1419 }
1420 }, delay);
1421 }
1422 i++;
1423 }
1424 sendAdvanceMessage(mAdvanceInterval);
1425 }
1426 }
1427 };
1428
1429 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001430 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001431 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1432 if (v instanceof Advanceable) {
1433 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001434 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001435 updateRunning();
1436 }
1437 }
1438
1439 void removeWidgetToAutoAdvance(View hostView) {
1440 if (mWidgetsToAdvance.containsKey(hostView)) {
1441 mWidgetsToAdvance.remove(hostView);
1442 updateRunning();
1443 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001444 }
1445
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001447 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 launcherInfo.hostView = null;
1449 }
1450
Winson Chung93eef082012-03-23 15:59:27 -07001451 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1452 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1453 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001454 }
1455
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001456 public LauncherAppWidgetHost getAppWidgetHost() {
1457 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Winson Chunga9abd0e2010-10-27 17:18:37 -07001460 public LauncherModel getModel() {
1461 return mModel;
1462 }
1463
Bjorn Bringertc459e522013-06-07 19:36:01 +01001464 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001465 getWindow().closeAllPanels();
1466
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001467 // Whatever we were doing is hereby canceled.
1468 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001469 }
1470
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 @Override
1472 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001473 long startTime = 0;
1474 if (DEBUG_RESUME_TIME) {
1475 startTime = System.currentTimeMillis();
1476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 super.onNewIntent(intent);
1478
1479 // Close the menu
1480 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001481 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001482 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001483
Jamie Genniscb222e82012-10-23 15:44:26 -07001484 final boolean alreadyOnHome =
1485 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001486 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001487
Jamie Genniscb222e82012-10-23 15:44:26 -07001488 Runnable processIntent = new Runnable() {
1489 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001490 if (mWorkspace == null) {
1491 // Can be cases where mWorkspace is null, this prevents a NPE
1492 return;
1493 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001494 Folder openFolder = mWorkspace.getOpenFolder();
1495 // In all these cases, only animate if we're already on home
1496 mWorkspace.exitWidgetResizeMode();
1497 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1498 openFolder == null) {
1499 mWorkspace.moveToDefaultScreen(true);
1500 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001501
Jamie Genniscb222e82012-10-23 15:44:26 -07001502 closeFolder();
1503 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001504
Jamie Genniscb222e82012-10-23 15:44:26 -07001505 // If we are already on home, then just animate back to the workspace,
1506 // otherwise, just wait until onResume to set the state back to Workspace
1507 if (alreadyOnHome) {
1508 showWorkspace(true);
1509 } else {
1510 mOnResumeState = State.WORKSPACE;
1511 }
1512
1513 final View v = getWindow().peekDecorView();
1514 if (v != null && v.getWindowToken() != null) {
1515 InputMethodManager imm = (InputMethodManager)getSystemService(
1516 INPUT_METHOD_SERVICE);
1517 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1518 }
1519
1520 // Reset AllApps to its initial state
1521 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1522 mAppsCustomizeTabHost.reset();
1523 }
1524 }
1525 };
1526
1527 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1528 // Delay processing of the intent to allow the status bar animation to finish
1529 // first in order to avoid janky animations.
1530 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001531 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001532 // Process the intent immediately.
1533 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001534 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Michael Jurka447bf842013-05-15 14:52:15 +02001537 if (DEBUG_RESUME_TIME) {
1538 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1539 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
1542 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001543 public void onRestoreInstanceState(Bundle state) {
1544 super.onRestoreInstanceState(state);
1545 for (int page: mSynchronouslyBoundPages) {
1546 mWorkspace.restoreInstanceStateForChild(page);
1547 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549
1550 @Override
1551 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001552 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001553 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554
Michael Jurka883f55b2010-10-21 15:47:14 -07001555 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001556 // We close any open folder since it will not be re-opened, and we need to make sure
1557 // this state is reflected.
1558 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
Adam Cohendcd297f2013-06-18 13:13:40 -07001560 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 mWaitingForResult) {
1562 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001563 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1565 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001566 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1567 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1568 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 if (mFolderInfo != null && mWaitingForResult) {
1572 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1573 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1574 }
Michael Jurka946ad472010-07-09 18:05:18 -07001575
Winson Chung785d2eb2011-04-14 16:08:02 -07001576 // Save the current AppsCustomize tab
1577 if (mAppsCustomizeTabHost != null) {
1578 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1579 if (currentTabTag != null) {
1580 outState.putString("apps_customize_currentTab", currentTabTag);
1581 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001582 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1583 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
1587 @Override
1588 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001590
Winson Chunge7a03942011-08-05 15:05:12 -07001591 // Remove all pending runnables
1592 mHandler.removeMessages(ADVANCE_MSG);
1593 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001594 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001595
Winson Chungcd2b0142011-06-08 16:02:26 -07001596 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001597 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001598 mModel.stopLoader();
1599 app.setLauncher(null);
1600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001602 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001604 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001606 mAppWidgetHost = null;
1607
1608 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
1610 TextKeyListener.getInstance().release();
1611
Winson Chung81b52252012-08-27 15:34:29 -07001612 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1613 // to prevent leaking Launcher activities on orientation change.
1614 if (mModel != null) {
1615 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1616 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001617
1618 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001619 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001620
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001621 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001622 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1623 mWorkspace.removeAllViews();
1624 mWorkspace = null;
1625 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001626
Michael Jurka2ecf9952012-06-18 12:52:28 -07001627 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629
Adam Cohena9cf38f2011-05-02 15:36:58 -07001630 public DragController getDragController() {
1631 return mDragController;
1632 }
1633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 @Override
1635 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001636 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 super.startActivityForResult(intent, requestCode);
1638 }
1639
Winson Chung70d72102011-08-12 11:24:35 -07001640 /**
1641 * Indicates that we want global search for this activity by setting the globalSearch
1642 * argument for {@link #startSearch} to true.
1643 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001645 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001647
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 if (initialQuery == null) {
1651 // Use any text typed in the launcher as the initial query
1652 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 if (appSearchData == null) {
1655 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001656 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
Winson Chungadf0c182012-08-23 14:59:07 -07001658 Rect sourceBounds = new Rect();
1659 if (mSearchDropTargetBar != null) {
1660 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Bjorn Bringertc459e522013-06-07 19:36:01 +01001663 startSearch(initialQuery, selectInitialQuery,
1664 appSearchData, sourceBounds);
1665 }
1666
1667 public void startSearch(String initialQuery,
1668 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001669 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001670 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001671 }
1672
1673 /**
1674 * Starts the global search activity. This code is a copied from SearchManager
1675 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001676 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001677 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001678 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001679 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1680 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1681 if (globalSearchActivity == null) {
1682 Log.w(TAG, "No global search activity found.");
1683 return;
1684 }
1685 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1686 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1687 intent.setComponent(globalSearchActivity);
1688 // Make sure that we have a Bundle to put source in
1689 if (appSearchData == null) {
1690 appSearchData = new Bundle();
1691 } else {
1692 appSearchData = new Bundle(appSearchData);
1693 }
1694 // Set source to package name of app that starts global search, if not set already.
1695 if (!appSearchData.containsKey("source")) {
1696 appSearchData.putString("source", getPackageName());
1697 }
1698 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1699 if (!TextUtils.isEmpty(initialQuery)) {
1700 intent.putExtra(SearchManager.QUERY, initialQuery);
1701 }
1702 if (selectInitialQuery) {
1703 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1704 }
1705 intent.setSourceBounds(sourceBounds);
1706 try {
1707 startActivity(intent);
1708 } catch (ActivityNotFoundException ex) {
1709 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1710 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 }
1712
Winson Chung70d72102011-08-12 11:24:35 -07001713 @Override
1714 public boolean onCreateOptionsMenu(Menu menu) {
1715 if (isWorkspaceLocked()) {
1716 return false;
1717 }
1718
1719 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001720
1721 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1722 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1723 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001724 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1725 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1726 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001727 String helpUrl = getString(R.string.help_url);
1728 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001729 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1730 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1731
Winson Chung70d72102011-08-12 11:24:35 -07001732 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1733 .setIcon(android.R.drawable.ic_menu_gallery)
1734 .setAlphabeticShortcut('W');
1735 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1736 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001737 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001738 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001739 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1740 .setIcon(android.R.drawable.ic_menu_preferences)
1741 .setIntent(settings)
1742 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001743 if (!helpUrl.isEmpty()) {
1744 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1745 .setIcon(android.R.drawable.ic_menu_help)
1746 .setIntent(help)
1747 .setAlphabeticShortcut('H');
1748 }
Winson Chung70d72102011-08-12 11:24:35 -07001749 return true;
1750 }
1751
1752 @Override
1753 public boolean onPrepareOptionsMenu(Menu menu) {
1754 super.onPrepareOptionsMenu(menu);
1755
1756 if (mAppsCustomizeTabHost.isTransitioning()) {
1757 return false;
1758 }
1759 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1760 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1761
1762 return true;
1763 }
1764
1765 @Override
1766 public boolean onOptionsItemSelected(MenuItem item) {
1767 switch (item.getItemId()) {
1768 case MENU_WALLPAPER_SETTINGS:
1769 startWallpaper();
1770 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001771 }
1772
1773 return super.onOptionsItemSelected(item);
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001776 @Override
1777 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001778 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001779 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001780 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001781 }
1782
Joe Onorato9c1289c2009-08-17 11:03:03 -04001783 public boolean isWorkspaceLocked() {
1784 return mWorkspaceLoading || mWaitingForResult;
1785 }
1786
Michael Jurka0280c3b2010-09-17 15:00:07 -07001787 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001788 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001789 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001790 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1791 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001792 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001793 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001794 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001795
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001796 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1797 AppWidgetProviderInfo appWidgetInfo) {
1798 if (appWidgetInfo.configure != null) {
1799 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001800
Bjorn Bringert7984c942009-12-09 15:38:25 +00001801 // Launch over to configure widget, if needed
1802 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001803 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001804 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001805 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001807 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001808 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001809 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001810 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001811 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Adam Cohenfbba09b2011-07-18 21:43:05 -07001815 /**
1816 * Process a shortcut drop.
1817 *
1818 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001819 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001820 * @param cell The cell it should be added to, optional
1821 * @param position The location on the screen where it was dropped, optional
1822 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001823 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001824 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001826 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001827 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001828 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001829
1830 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001831 mPendingAddInfo.cellX = cell[0];
1832 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001833 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834
1835 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1836 createShortcutIntent.setComponent(componentName);
1837 processShortcut(createShortcutIntent);
1838 }
1839
Adam Cohenfbba09b2011-07-18 21:43:05 -07001840 /**
1841 * Process a widget drop.
1842 *
1843 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001844 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001845 * @param cell The cell it should be added to, optional
1846 * @param position The location on the screen where it was dropped, optional
1847 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001848 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001849 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001850 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001851 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001852 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001853 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001854 mPendingAddInfo.minSpanX = info.minSpanX;
1855 mPendingAddInfo.minSpanY = info.minSpanY;
1856
Adam Cohenfbba09b2011-07-18 21:43:05 -07001857 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001858 mPendingAddInfo.cellX = cell[0];
1859 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001860 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001861 if (span != null) {
1862 mPendingAddInfo.spanX = span[0];
1863 mPendingAddInfo.spanY = span[1];
1864 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001865
Adam Cohened66b2b2012-01-23 17:28:51 -08001866 AppWidgetHostView hostView = info.boundWidget;
1867 int appWidgetId;
1868 if (hostView != null) {
1869 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001870 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001871 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001872 // In this case, we either need to start an activity to get permission to bind
1873 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001874 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001875 Bundle options = info.bindOptions;
1876
1877 boolean success = false;
1878 if (options != null) {
1879 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1880 info.componentName, options);
1881 } else {
1882 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1883 info.componentName);
1884 }
1885 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001886 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001887 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001888 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001889 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1890 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1891 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001892 // TODO: we need to make sure that this accounts for the options bundle.
1893 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001894 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1895 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001896 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001897 }
1898
Joe Onoratodeb98af2010-02-19 14:59:39 -08001899 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001900 // Handle case where user selected "Applications"
1901 String applicationName = getResources().getString(R.string.group_applications);
1902 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001903
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001904 if (applicationName != null && applicationName.equals(shortcutName)) {
1905 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1906 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001907
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001908 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1909 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001910 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001911 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001912 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001913 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001914 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916
Winson Chung24ab2f12010-09-16 14:10:47 -07001917 void processWallpaper(Intent intent) {
1918 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1919 }
1920
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001922 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001923 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 folderInfo.title = getText(R.string.folder_name);
1925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001929 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930
1931 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 FolderIcon newFolder =
1933 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001934 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001936 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 }
Romain Guycbb89e42009-06-08 15:52:54 -07001938
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001940 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001944 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001946 Intent chooser = Intent.createChooser(pickWallpaper,
1947 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001948 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1949 // Removed in Eclair MR1
1950// WallpaperManager wm = (WallpaperManager)
1951// getSystemService(Context.WALLPAPER_SERVICE);
1952// WallpaperInfo wi = wm.getWallpaperInfo();
1953// if (wi != null && wi.getSettingsActivity() != null) {
1954// LabeledIntent li = new LabeledIntent(getPackageName(),
1955// R.string.configure_wallpaper, 0);
1956// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1957// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1958// }
Mike Clerona0618e42009-10-22 13:55:21 -07001959 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001962 /**
1963 * Registers various content observers. The current implementation registers
1964 * only a favorites observer to keep track of the favorites applications.
1965 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001966 private void registerContentObservers() {
1967 ContentResolver resolver = getContentResolver();
1968 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1969 true, mWidgetObserver);
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 @Override
1973 public boolean dispatchKeyEvent(KeyEvent event) {
1974 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1975 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001977 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001978 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001979 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001980 dumpState();
1981 return true;
1982 }
1983 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001984 }
1985 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1986 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001987 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 return true;
1989 }
1990 }
1991
1992 return super.dispatchKeyEvent(event);
1993 }
1994
Joe Onorato88ec0992009-11-19 13:16:06 -08001995 @Override
1996 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001997 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001998 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001999 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002000 Folder openFolder = mWorkspace.getOpenFolder();
2001 if (openFolder.isEditingName()) {
2002 openFolder.dismissEditingName();
2003 } else {
2004 closeFolder();
2005 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002006 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002007 mWorkspace.exitWidgetResizeMode();
2008
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002009 // Back button is a no-op here, but give at least some feedback for the button press
2010 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002011 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002012 }
2013
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002015 * Re-listen when widgets are reset.
2016 */
2017 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002018 if (mAppWidgetHost != null) {
2019 mAppWidgetHost.startListening();
2020 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002021 }
2022
2023 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 * Launches the intent referred by the clicked shortcut.
2025 *
2026 * @param v The view representing the clicked shortcut.
2027 */
2028 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002029 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2030 // view has detached (it's possible for this to happen if the view is removed mid touch).
2031 if (v.getWindowToken() == null) {
2032 return;
2033 }
2034
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002035 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002036 return;
2037 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002040 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002042 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002043
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002044 // Check for special shortcuts
2045 if (intent.getComponent() != null) {
2046 final String shortcutClass = intent.getComponent().getClassName();
2047
2048 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2049 showAllApps(true);
2050 return;
2051 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2052 MemoryDumpActivity.startDump(this);
2053 return;
2054 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002055 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002056
2057 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002058 int[] pos = new int[2];
2059 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002060 intent.setSourceBounds(new Rect(pos[0], pos[1],
2061 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002062
2063 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002064
2065 if (success && v instanceof BubbleTextView) {
2066 mWaitingForResume = (BubbleTextView) v;
2067 mWaitingForResume.setStayPressed(true);
2068 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002070 if (v instanceof FolderIcon) {
2071 FolderIcon fi = (FolderIcon) v;
2072 handleFolderClick(fi);
2073 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002074 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002075 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002076 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002077 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002078 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081 }
2082
Michael Jurka0e260592010-06-30 17:07:39 -07002083 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002084 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002086 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2087 showWorkspace(true);
2088 }
Michael Jurka0e260592010-06-30 17:07:39 -07002089 return false;
2090 }
2091
Michael Jurkaaf442092010-06-10 17:01:57 -07002092 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002093 * Event handler for the search button
2094 *
2095 * @param v The view that was clicked.
2096 */
2097 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002098 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2099
Amith Yamasania135ba82011-08-09 17:42:01 -07002100 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002101 }
2102
2103 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002104 * Event handler for the voice button
2105 *
2106 * @param v The view that was clicked.
2107 */
2108 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002109 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002110
Bjorn Bringertc459e522013-06-07 19:36:01 +01002111 startVoice();
2112 }
2113
2114 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002115 try {
2116 final SearchManager searchManager =
2117 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2118 ComponentName activityName = searchManager.getGlobalSearchActivity();
2119 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002120 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002121 if (activityName != null) {
2122 intent.setPackage(activityName.getPackageName());
2123 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002124 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002125 } catch (ActivityNotFoundException e) {
2126 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002127 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002128 startActivitySafely(null, intent, "onClickVoiceButton");
2129 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002130 }
2131
2132 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002133 * Event handler for the "grid" button that appears on the home screen, which
2134 * enters all apps mode.
2135 *
2136 * @param v The view that was clicked.
2137 */
2138 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002139 showAllApps(true);
2140 }
2141
2142 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002143 // Provide the same haptic feedback that the system offers for virtual keys.
2144 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002145 }
2146
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002147 public void onClickAppMarketButton(View v) {
2148 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002149 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002150 } else {
2151 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002152 }
2153 }
2154
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002155 void startApplicationDetailsActivity(ComponentName componentName) {
2156 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002157 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2158 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002159 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002160 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002161 }
2162
Patrick Dubroy5539af72010-09-07 15:22:01 -07002163 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2164 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2165 // System applications cannot be installed. For now, show a toast explaining that.
2166 // We may give them the option of disabling apps this way.
2167 int messageId = R.string.uninstall_system_app_text;
2168 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2169 } else {
2170 String packageName = appInfo.componentName.getPackageName();
2171 String className = appInfo.componentName.getClassName();
2172 Intent intent = new Intent(
2173 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002174 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2175 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002176 startActivity(intent);
2177 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002178 }
2179
Michael Jurka86a720e2012-05-09 11:23:23 -07002180 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002184 // Only launch using the new animation if the shortcut has not opted out (this is a
2185 // private contract between launcher and may be ignored in the future).
2186 boolean useLaunchAnimation = (v != null) &&
2187 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2188 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002189 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2190 v.getMeasuredWidth(), v.getMeasuredHeight());
2191
2192 startActivity(intent, opts.toBundle());
2193 } else {
2194 startActivity(intent);
2195 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002196 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 } catch (SecurityException e) {
2198 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002199 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002201 "or use the exported attribute for this activity. "
2202 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002204 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002206
Michael Jurka86a720e2012-05-09 11:23:23 -07002207 boolean startActivitySafely(View v, Intent intent, Object tag) {
2208 boolean success = false;
2209 try {
2210 success = startActivity(v, intent, tag);
2211 } catch (ActivityNotFoundException e) {
2212 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2213 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2214 }
2215 return success;
2216 }
2217
Romain Guy8e633c52010-03-04 12:51:36 -08002218 void startActivityForResultSafely(Intent intent, int requestCode) {
2219 try {
2220 startActivityForResult(intent, requestCode);
2221 } catch (ActivityNotFoundException e) {
2222 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2223 } catch (SecurityException e) {
2224 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2225 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2226 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2227 "or use the exported attribute for this activity.", e);
2228 }
2229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002230
Adam Cohena9cf38f2011-05-02 15:36:58 -07002231 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002232 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002233 Folder openFolder = mWorkspace.getFolderForTag(info);
2234
2235 // If the folder info reports that the associated folder is open, then verify that
2236 // it is actually opened. There have been a few instances where this gets out of sync.
2237 if (info.opened && openFolder == null) {
2238 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002239 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002240 info.opened = false;
2241 }
2242
Adam Cohenfb91f302012-06-11 15:45:18 -07002243 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 // Close any open folder
2245 closeFolder();
2246 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002247 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 } else {
2249 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 int folderScreen;
2251 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002252 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 // .. and close it
2254 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002255 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 // Close any folder open on the current screen
2257 closeFolder();
2258 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002259 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 }
2261 }
2262 }
2263 }
2264
Adam Cohen268c4752012-06-06 17:47:33 -07002265 /**
2266 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2267 * in the DragLayer in the exact absolute location of the original FolderIcon.
2268 */
2269 private void copyFolderIconToImage(FolderIcon fi) {
2270 final int width = fi.getMeasuredWidth();
2271 final int height = fi.getMeasuredHeight();
2272
2273 // Lazy load ImageView, Bitmap and Canvas
2274 if (mFolderIconImageView == null) {
2275 mFolderIconImageView = new ImageView(this);
2276 }
2277 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2278 mFolderIconBitmap.getHeight() != height) {
2279 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2280 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2281 }
2282
2283 DragLayer.LayoutParams lp;
2284 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2285 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2286 } else {
2287 lp = new DragLayer.LayoutParams(width, height);
2288 }
2289
Adam Cohen307fe232012-08-16 17:55:58 -07002290 // The layout from which the folder is being opened may be scaled, adjust the starting
2291 // view size by this scale factor.
2292 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002293 lp.customPosition = true;
2294 lp.x = mRectForFolderAnimation.left;
2295 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002296 lp.width = (int) (scale * width);
2297 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002298
2299 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2300 fi.draw(mFolderIconCanvas);
2301 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002302 if (fi.getFolder() != null) {
2303 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2304 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002305 }
Adam Cohen268c4752012-06-06 17:47:33 -07002306 // Just in case this image view is still in the drag layer from a previous animation,
2307 // we remove it and re-add it.
2308 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2309 mDragLayer.removeView(mFolderIconImageView);
2310 }
2311 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002312 if (fi.getFolder() != null) {
2313 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002314 }
Adam Cohen268c4752012-06-06 17:47:33 -07002315 }
2316
Adam Cohen2801caf2011-05-13 20:57:39 -07002317 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002318 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002319 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2320 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2321 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2322
Adam Cohenc51934b2011-07-26 21:07:43 -07002323 FolderInfo info = (FolderInfo) fi.getTag();
2324 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2325 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002326 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2327 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002328 }
2329
Adam Cohen268c4752012-06-06 17:47:33 -07002330 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2331 copyFolderIconToImage(fi);
2332 fi.setVisibility(View.INVISIBLE);
2333
Michael Jurka2ecf9952012-06-18 12:52:28 -07002334 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002335 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002336 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2337 oa.start();
2338 }
2339
Adam Cohen268c4752012-06-06 17:47:33 -07002340 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002341 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002342 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2343 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2344 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2345
Adam Cohen268c4752012-06-06 17:47:33 -07002346 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002347
Adam Cohen268c4752012-06-06 17:47:33 -07002348 // We remove and re-draw the FolderIcon in-case it has changed
2349 mDragLayer.removeView(mFolderIconImageView);
2350 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002351 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002352 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002353 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002354 oa.addListener(new AnimatorListenerAdapter() {
2355 @Override
2356 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002357 if (cl != null) {
2358 cl.clearFolderLeaveBehind();
2359 // Remove the ImageView copy of the FolderIcon and make the original visible.
2360 mDragLayer.removeView(mFolderIconImageView);
2361 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002362 }
2363 }
2364 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002365 oa.start();
2366 }
2367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002369 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 * is animated relative to the specified View. If the View is null, no animation
2371 * is played.
2372 *
2373 * @param folderInfo The FolderInfo describing the folder to open.
2374 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002375 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002376 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002377 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378
Adam Cohena9cf38f2011-05-02 15:36:58 -07002379 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380
Adam Cohen4554ee12011-08-03 16:13:21 -07002381 // Just verify that the folder hasn't already been added to the DragLayer.
2382 // There was a one-off crash where the folder had a parent already.
2383 if (folder.getParent() == null) {
2384 mDragLayer.addView(folder);
2385 mDragController.addDropTarget((DropTarget) folder);
2386 } else {
2387 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2388 folder.getParent() + ").");
2389 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002390 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002391 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002392
2393 // Notify the accessibility manager that this folder "window" has appeared and occluded
2394 // the workspace items
2395 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2396 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002397 }
2398
2399 public void closeFolder() {
2400 Folder folder = mWorkspace.getOpenFolder();
2401 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002402 if (folder.isEditingName()) {
2403 folder.dismissEditingName();
2404 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002405 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002406
2407 // Dismiss the folder cling
2408 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002409 }
2410 }
2411
2412 void closeFolder(Folder folder) {
2413 folder.getInfo().opened = false;
2414
2415 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2416 if (parent != null) {
2417 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2418 shrinkAndFadeInFolderIcon(fi);
2419 }
2420 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002421
2422 // Notify the accessibility manager that this folder "window" has disappeard and no
2423 // longer occludeds the workspace items
2424 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 }
2426
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002427 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002428 if (!isDraggingEnabled()) return false;
2429 if (isWorkspaceLocked()) return false;
2430 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431
2432 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002433 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 }
2435
Michael Jurka0280c3b2010-09-17 15:00:07 -07002436 resetAddInfo();
2437 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002439 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 return true;
2441 }
2442
Winson Chung3d503fb2011-07-13 17:25:49 -07002443 // The hotseat touch handling does not go through Workspace, and we always allow long press
2444 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002446 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2447 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002448 if (itemUnderLongClick == null) {
2449 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002450 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2451 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002452 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002453 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2454 mWorkspace.startReordering();
2455 } else {
2456 startWallpaper();
2457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002459 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002461 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 }
2463 }
2464 }
2465 return true;
2466 }
2467
Winson Chung3d503fb2011-07-13 17:25:49 -07002468 boolean isHotseatLayout(View layout) {
2469 return mHotseat != null && layout != null &&
2470 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2471 }
2472 Hotseat getHotseat() {
2473 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002474 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002475 SearchDropTargetBar getSearchBar() {
2476 return mSearchDropTargetBar;
2477 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002478
Winson Chung3d503fb2011-07-13 17:25:49 -07002479 /**
2480 * Returns the CellLayout of the specified container at the specified screen.
2481 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002482 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002483 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2484 if (mHotseat != null) {
2485 return mHotseat.getLayout();
2486 } else {
2487 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002488 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002489 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002490 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002491 }
2492 }
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 Workspace getWorkspace() {
2495 return mWorkspace;
2496 }
2497
Daniel Sandler843e8602010-06-07 14:59:01 -04002498 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002499 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002500 }
2501
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002502 /**
2503 * Helper method for the cameraZoomIn/cameraZoomOut animations
2504 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 * @param scaleFactor The scale factor used for the zoom
2506 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002507 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002508 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002509 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002511
Winson Chung18f41f82012-05-09 13:28:10 -07002512 void disableWallpaperIfInAllApps() {
2513 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002514 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002515 if (mAppsCustomizeTabHost != null &&
2516 !mAppsCustomizeTabHost.isTransitioning()) {
2517 updateWallpaperVisibility(false);
2518 }
2519 }
2520 }
2521
Craig Mautner360310b2012-10-26 15:13:08 -07002522 private void setWorkspaceBackground(boolean workspace) {
2523 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002524 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002525 }
2526
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002527 void updateWallpaperVisibility(boolean visible) {
2528 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2529 int curflags = getWindow().getAttributes().flags
2530 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2531 if (wpflags != curflags) {
2532 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2533 }
Craig Mautner360310b2012-10-26 15:13:08 -07002534 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002535 }
2536
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002537 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2538 if (v instanceof LauncherTransitionable) {
2539 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2540 }
2541 }
2542
Michael Jurkabed61d22012-02-14 22:51:29 -08002543 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2544 if (v instanceof LauncherTransitionable) {
2545 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2546 }
Winson Chung70442722012-02-10 15:43:22 -08002547
2548 // Update the workspace transition step as well
2549 dispatchOnLauncherTransitionStep(v, 0f);
2550 }
2551
2552 private void dispatchOnLauncherTransitionStep(View v, float t) {
2553 if (v instanceof LauncherTransitionable) {
2554 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2555 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002556 }
2557
2558 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2559 if (v instanceof LauncherTransitionable) {
2560 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2561 }
Winson Chung70442722012-02-10 15:43:22 -08002562
2563 // Update the workspace transition step as well
2564 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002565 }
2566
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002567 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002568 * Things to test when changing the following seven functions.
2569 * - Home from workspace
2570 * - from center screen
2571 * - from other screens
2572 * - Home from all apps
2573 * - from center screen
2574 * - from other screens
2575 * - Back from all apps
2576 * - from center screen
2577 * - from other screens
2578 * - Launch app from workspace and quit
2579 * - with back
2580 * - with home
2581 * - Launch app from all apps and quit
2582 * - with back
2583 * - with home
2584 * - Go to a screen that's not the default, then all
2585 * apps, and launch and app, and go back
2586 * - with back
2587 * -with home
2588 * - On workspace, long press power and go back
2589 * - with back
2590 * - with home
2591 * - On all apps, long press power and go back
2592 * - with back
2593 * - with home
2594 * - On workspace, power off
2595 * - On all apps, power off
2596 * - Launch an app and turn off the screen while in that app
2597 * - Go back with home key
2598 * - Go back with back key TODO: make this not go to workspace
2599 * - From all apps
2600 * - From workspace
2601 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2602 * - From all apps
2603 * - From the center workspace
2604 * - From another workspace
2605 */
2606
2607 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002608 * Zoom the camera out from the workspace to reveal 'toView'.
2609 * Assumes that the view to show is anchored at either the very top or very bottom
2610 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002611 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002612 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002613 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002614 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002615 mStateAnimation.cancel();
2616 mStateAnimation = null;
2617 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002618 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002619
Winson Chungf0ea4d32011-06-06 14:27:16 -07002620 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2621 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2622 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002623 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002624 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002625 final int startDelay =
2626 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002627
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629
Michael Jurkad74c9842011-07-10 12:44:21 -07002630 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002631 Animator workspaceAnim =
2632 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633
2634 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002635 toView.setScaleX(scale);
2636 toView.setScaleY(scale);
2637 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2638 scaleAnim.
2639 scaleX(1f).scaleY(1f).
2640 setDuration(duration).
2641 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002642
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002644 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002645 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002646 .ofFloat(toView, "alpha", 0f, 1f)
2647 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002648 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002649 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2650 @Override
2651 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002652 if (animation == null) {
2653 throw new RuntimeException("animation is null");
2654 }
Winson Chung70442722012-02-10 15:43:22 -08002655 float t = (Float) animation.getAnimatedValue();
2656 dispatchOnLauncherTransitionStep(fromView, t);
2657 dispatchOnLauncherTransitionStep(toView, t);
2658 }
2659 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002660
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002661 // toView should appear right at the end of the workspace shrink
2662 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002663 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002664 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002665 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002666
2667 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002668 boolean animationCancelled = false;
2669
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002670 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002671 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002672 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 // Prepare the position
2674 toView.setTranslationX(0.0f);
2675 toView.setTranslationY(0.0f);
2676 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002677 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002679 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002680 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002681 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2682 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002683
Daniel Sandlere4f98912013-06-25 15:13:26 -04002684 if (mWorkspace != null
2685 && !springLoaded
2686 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002687 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002688 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002689 if (!animationCancelled) {
2690 updateWallpaperVisibility(false);
2691 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002692
2693 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002694 if (mSearchDropTargetBar != null) {
2695 mSearchDropTargetBar.hideSearchBar(false);
2696 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002697 }
2698
Adam Cohencff6af82011-09-13 14:51:53 -07002699 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002700 public void onAnimationCancel(Animator animation) {
2701 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002702 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 });
2704
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705 if (workspaceAnim != null) {
2706 mStateAnimation.play(workspaceAnim);
2707 }
Michael Jurka1899a362011-11-03 13:50:45 -07002708
2709 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002710
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002711 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2712 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002713
2714 // If any of the objects being animated haven't been measured/laid out
2715 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002716 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002717 (mWorkspace.getMeasuredWidth() == 0) ||
2718 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002719 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002720 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002721
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002722 final AnimatorSet stateAnimation = mStateAnimation;
2723 final Runnable startAnimRunnable = new Runnable() {
2724 public void run() {
2725 // Check that mStateAnimation hasn't changed while
2726 // we waited for a layout/draw pass
2727 if (mStateAnimation != stateAnimation)
2728 return;
2729 setPivotsForZoom(toView, scale);
2730 dispatchOnLauncherTransitionStart(fromView, animated, false);
2731 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002732 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002733 }
2734 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002735 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002736 final ViewTreeObserver observer = toView.getViewTreeObserver();
2737 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2738 public void onGlobalLayout() {
2739 startAnimRunnable.run();
2740 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2741 }
2742 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002743 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002744 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002745 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 } else {
2747 toView.setTranslationX(0.0f);
2748 toView.setTranslationY(0.0f);
2749 toView.setScaleX(1.0f);
2750 toView.setScaleY(1.0f);
2751 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002752 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002753
Daniel Sandlere4f98912013-06-25 15:13:26 -04002754 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002755 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002756
2757 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002758 if (mSearchDropTargetBar != null) {
2759 mSearchDropTargetBar.hideSearchBar(false);
2760 }
Michael Jurkaabded662011-03-04 12:06:57 -08002761 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002762 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002763 dispatchOnLauncherTransitionStart(fromView, animated, false);
2764 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002765 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002766 dispatchOnLauncherTransitionStart(toView, animated, false);
2767 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002768 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002770 }
2771
2772 /**
2773 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002774 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002775 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002776 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002777 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2778 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002779
Michael Jurkab3e22d92011-10-31 15:58:33 -07002780 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002781 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002782 mStateAnimation.cancel();
2783 mStateAnimation = null;
2784 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002785 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002786
Winson Chungf0ea4d32011-06-06 14:27:16 -07002787 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002788 final int fadeOutDuration =
2789 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002790 final float scaleFactor = (float)
2791 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2792 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002793 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002794 Animator workspaceAnim = null;
2795
2796 if (toState == State.WORKSPACE) {
2797 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2798 workspaceAnim = mWorkspace.getChangeStateAnimation(
2799 Workspace.State.NORMAL, animated, stagger);
2800 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2801 workspaceAnim = mWorkspace.getChangeStateAnimation(
2802 Workspace.State.SPRING_LOADED, animated);
2803 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002804
Michael Jurkab3e22d92011-10-31 15:58:33 -07002805 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002806 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002807 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002808 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002809 final LauncherViewPropertyAnimator scaleAnim =
2810 new LauncherViewPropertyAnimator(fromView);
2811 scaleAnim.
2812 scaleX(scaleFactor).scaleY(scaleFactor).
2813 setDuration(duration).
2814 setInterpolator(new Workspace.ZoomInInterpolator());
2815
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002816 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002817 .ofFloat(fromView, "alpha", 1f, 0f)
2818 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002819 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002820 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2821 @Override
2822 public void onAnimationUpdate(ValueAnimator animation) {
2823 float t = 1f - (Float) animation.getAnimatedValue();
2824 dispatchOnLauncherTransitionStep(fromView, t);
2825 dispatchOnLauncherTransitionStep(toView, t);
2826 }
2827 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002828
Michael Jurka2ecf9952012-06-18 12:52:28 -07002829 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002830
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002831 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2832 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002833 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002834
2835 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002836 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002837 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002838 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002839 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002840 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2841 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002842 if (onCompleteRunnable != null) {
2843 onCompleteRunnable.run();
2844 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002845 mAppsCustomizeContent.updateCurrentPageScroll();
2846 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 }
2848 });
2849
Winson Chungf0ea4d32011-06-06 14:27:16 -07002850 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002851 if (workspaceAnim != null) {
2852 mStateAnimation.play(workspaceAnim);
2853 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002854 dispatchOnLauncherTransitionStart(fromView, animated, true);
2855 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002856 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002857 } else {
2858 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002859 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002860 dispatchOnLauncherTransitionStart(fromView, animated, true);
2861 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002862 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002863 dispatchOnLauncherTransitionStart(toView, animated, true);
2864 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002865 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 }
2867
Michael Jurkae326f182011-11-21 14:05:46 -08002868 @Override
2869 public void onTrimMemory(int level) {
2870 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002871 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002872 mAppsCustomizeTabHost.onTrimMemory();
2873 }
2874 }
2875
Winson Chung18f41f82012-05-09 13:28:10 -07002876 @Override
2877 public void onWindowFocusChanged(boolean hasFocus) {
2878 if (!hasFocus) {
2879 // When another window occludes launcher (like the notification shade, or recents),
2880 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2881 updateWallpaperVisibility(true);
2882 } else {
2883 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002884 mWorkspace.postDelayed(new Runnable() {
2885 @Override
2886 public void run() {
2887 disableWallpaperIfInAllApps();
2888 }
2889 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002890 }
2891 }
2892
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002893 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002894 showWorkspace(animated, null);
2895 }
2896
2897 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002898 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002899 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002900 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002901 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002902
Winson Chungc7d2b602012-05-16 17:02:20 -07002903 // Show the search bar (only animate if we were showing the drop target bar in spring
2904 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002905 if (mSearchDropTargetBar != null) {
2906 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2907 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002908
Michael Jurkab737ee62011-11-15 15:57:22 -08002909 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002910 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002911
2912 // Set focus to the AppsCustomize button
2913 if (mAllAppsButton != null) {
2914 mAllAppsButton.requestFocus();
2915 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002916 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002917
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918 // Change the state *after* we've called all the transition code
2919 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002920
Winson Chung5fb63472011-02-02 17:03:37 -08002921 // Resume the auto-advance of widgets
2922 mUserPresent = true;
2923 updateRunning();
2924
alanv1d4fde62012-10-17 13:15:47 -07002925 // Send an accessibility event to announce the context change
2926 getWindow().getDecorView()
2927 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002928
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002929 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002930 }
2931
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002932 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002933 }
2934
Michael Jurkab3e22d92011-10-31 15:58:33 -07002935 void showAllApps(boolean animated) {
2936 if (mState != State.WORKSPACE) return;
2937
2938 showAppsCustomizeHelper(animated, false);
2939 mAppsCustomizeTabHost.requestFocus();
2940
Michael Jurkab3e22d92011-10-31 15:58:33 -07002941 // Change the state *after* we've called all the transition code
2942 mState = State.APPS_CUSTOMIZE;
2943
2944 // Pause the auto-advance of widgets until we are out of AllApps
2945 mUserPresent = false;
2946 updateRunning();
2947 closeFolder();
2948
2949 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002950 getWindow().getDecorView()
2951 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002952 }
2953
Adam Cohen7777d962011-08-18 18:58:38 -07002954 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002955 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002956 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002957 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002958 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002959 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002960 }
Adam Cohen7777d962011-08-18 18:58:38 -07002961
Adam Cohened66b2b2012-01-23 17:28:51 -08002962 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2963 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002964 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2965
Winson Chunge7a03942011-08-05 15:05:12 -07002966 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002967 @Override
2968 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002969 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002970 // Before we show workspace, hide all apps again because
2971 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2972 // clean up our state transition functions
2973 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002974 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002975 } else {
2976 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002977 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002978 }
2979 }, (extendedDelay ?
2980 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2981 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2982 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002983
Michael Jurkad3ef3062010-11-23 16:23:58 -08002984 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002985 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002986 final boolean animated = true;
2987 final boolean springLoaded = true;
2988 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002989 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002990 }
2991 // Otherwise, we are not in spring loaded mode, so don't do anything.
2992 }
2993
Michael Jurkab737ee62011-11-15 15:57:22 -08002994 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07002995 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002996 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002997 }
2998 }
2999
3000 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003001 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003002 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003003 if (mDividerAnimator != null) {
3004 mDividerAnimator.cancel();
3005 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003006 mDividerAnimator = null;
3007 }
3008 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003009 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003010 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003011 int duration = 0;
3012 if (mSearchDropTargetBar != null) {
3013 duration = mSearchDropTargetBar.getTransitionInDuration();
3014 }
3015 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003016 mDividerAnimator.start();
3017 }
3018 }
3019 }
3020
Michael Jurkab3e22d92011-10-31 15:58:33 -07003021 void lockAllApps() {
3022 // TODO
3023 }
3024
3025 void unlockAllApps() {
3026 // TODO
3027 }
3028
Winson Chungf0ea4d32011-06-06 14:27:16 -07003029 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003030 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003031 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003032 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003033 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003034 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003035 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003036 int duration = 0;
3037 if (mSearchDropTargetBar != null) {
3038 duration = mSearchDropTargetBar.getTransitionInDuration();
3039 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003040 mHotseat.animate().alpha(1f).setDuration(duration);
3041 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003043 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044 }
3045 }
3046 }
3047
3048 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003049 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003050 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003051 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003052 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003054 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003055 int duration = 0;
3056 if (mSearchDropTargetBar != null) {
3057 duration = mSearchDropTargetBar.getTransitionOutDuration();
3058 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003059 mHotseat.animate().alpha(0f).setDuration(duration);
3060 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003061 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003062 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003063 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003064 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003065 }
3066
Patrick Dubroy5f445422011-02-18 14:35:21 -08003067 /**
3068 * Add an item from all apps or customize onto the given workspace screen.
3069 * If layout is null, add to the current screen.
3070 */
3071 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003072 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003073 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003074 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003075 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003076
Winson Chungdff8ebb2011-09-08 17:25:31 -07003077 /** Maps the current orientation to an index for referencing orientation correct global icons */
3078 private int getCurrentOrientationIndexForGlobalIcons() {
3079 // default - 0, landscape - 1
3080 switch (getResources().getConfiguration().orientation) {
3081 case Configuration.ORIENTATION_LANDSCAPE:
3082 return 1;
3083 default:
3084 return 0;
3085 }
3086 }
3087
Michael Jurkaae65ee32012-04-30 11:45:54 -07003088 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003090 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003091 // Look for the toolbar icon specified in the activity meta-data
3092 Bundle metaData = packageManager.getActivityInfo(
3093 activityName, PackageManager.GET_META_DATA).metaData;
3094 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003095 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003096 if (iconResId != 0) {
3097 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003098 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003099 }
3100 }
3101 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003102 // This can happen if the activity defines an invalid drawable
3103 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3104 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003105 } catch (Resources.NotFoundException nfe) {
3106 // This can happen if the activity defines an invalid drawable
3107 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3108 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003110 return null;
3111 }
3112
3113 // if successful in getting icon, return it; otherwise, set button to use default drawable
3114 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003115 int buttonId, ComponentName activityName, int fallbackDrawableId,
3116 String toolbarResourceName) {
3117 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003118 Resources r = getResources();
3119 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3120 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003121
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003122 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003123 // If we were unable to find the icon via the meta-data, use a generic one
3124 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003125 toolbarIcon = r.getDrawable(fallbackDrawableId);
3126 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003127 if (button != null) {
3128 button.setCompoundDrawables(toolbarIcon, null, null, null);
3129 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003130 return null;
3131 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003132 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003133 if (button != null) {
3134 button.setCompoundDrawables(toolbarIcon, null, null, null);
3135 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003136 return toolbarIcon.getConstantState();
3137 }
3138 }
3139
3140 // if successful in getting icon, return it; otherwise, set button to use default drawable
3141 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003142 int buttonId, ComponentName activityName, int fallbackDrawableId,
3143 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003144 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003145 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003146
Michael Jurka19e0fc52011-07-22 18:00:21 -07003147 if (button != null) {
3148 // If we were unable to find the icon via the meta-data, use a
3149 // generic one
3150 if (toolbarIcon == null) {
3151 button.setImageResource(fallbackDrawableId);
3152 } else {
3153 button.setImageDrawable(toolbarIcon);
3154 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003155 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003156
3157 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3158
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 }
3160
Winson Chung1b884092012-06-08 17:13:02 -07003161 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003162 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003163 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003164 }
3165
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003166 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003167 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003168 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003169 }
3170
Winson Chungbb185bd2011-11-21 12:31:42 -08003171 private void invalidatePressedFocusedStates(View container, View button) {
3172 if (container instanceof HolographicLinearLayout) {
3173 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3174 layout.invalidatePressedFocusedStates();
3175 } else if (button instanceof HolographicImageView) {
3176 HolographicImageView view = (HolographicImageView) button;
3177 view.invalidatePressedFocusedStates();
3178 }
3179 }
3180
Winson Chungc51db6a2011-10-05 11:44:49 -07003181 private boolean updateGlobalSearchIcon() {
3182 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003183 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003184 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003185 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003186
Winson Chung1cad91e2011-05-25 17:41:01 -07003187 final SearchManager searchManager =
3188 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3189 ComponentName activityName = searchManager.getGlobalSearchActivity();
3190 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003191 int coi = getCurrentOrientationIndexForGlobalIcons();
3192 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003193 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3194 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3195 if (sGlobalSearchIcon[coi] == null) {
3196 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3197 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3198 TOOLBAR_ICON_METADATA_NAME);
3199 }
3200
Winson Chungc51db6a2011-10-05 11:44:49 -07003201 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3202 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003203 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003204 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003205 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003206 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003207 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3208 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3209 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003210 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003211 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003212 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003213 }
3214 }
3215
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003216 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003217 final View searchButtonContainer = findViewById(R.id.search_button_container);
3218 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003219 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003220 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003221 }
3222
Winson Chungc51db6a2011-10-05 11:44:49 -07003223 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003224 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003225 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003226
Winson Chungc51db6a2011-10-05 11:44:49 -07003227 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003228 final SearchManager searchManager =
3229 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3230 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3231
3232 ComponentName activityName = null;
3233 if (globalSearchActivity != null) {
3234 // Check if the global search activity handles voice search
3235 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3236 intent.setPackage(globalSearchActivity.getPackageName());
3237 activityName = intent.resolveActivity(getPackageManager());
3238 }
3239
3240 if (activityName == null) {
3241 // Fallback: check if an activity other than the global search activity
3242 // resolves this
3243 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3244 activityName = intent.resolveActivity(getPackageManager());
3245 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003246 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003247 int coi = getCurrentOrientationIndexForGlobalIcons();
3248 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003249 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3250 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3251 if (sVoiceSearchIcon[coi] == null) {
3252 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3253 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3254 TOOLBAR_ICON_METADATA_NAME);
3255 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003257 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003258 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003259 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003261 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003262 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003263 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003264 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003266 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003267 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003268
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003269 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003270 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3271 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003272 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003273 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003274 }
3275
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003276 public void setVoiceButtonProxyVisible(boolean visible) {
3277 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3278 if (voiceButtonProxy != null) {
3279 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3280 }
3281 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003282 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003283 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003284 */
3285 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003286 final View marketButton = findViewById(R.id.market_button);
3287 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3288 // Find the app market activity by resolving an intent.
3289 // (If multiple app markets are installed, it will return the ResolverActivity.)
3290 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003291 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003292 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003293 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003294 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003295 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3296 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003297 marketButton.setVisibility(View.VISIBLE);
3298 } else {
3299 // We should hide and disable the view so that we don't try and restore the visibility
3300 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3301 marketButton.setVisibility(View.GONE);
3302 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003303 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003304 }
3305
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003306 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003307 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3308 Resources r = getResources();
3309 Drawable marketIconDrawable = d.newDrawable(r);
3310 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3311 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3312 marketIconDrawable.setBounds(0, 0, w, h);
3313
3314 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003315 }
3316
Michael Jurkad7c28052012-04-27 15:43:36 -07003317 @Override
3318 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003319 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003320 final List<CharSequence> text = event.getText();
3321 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003322 // Populate event with a fake title based on the current state.
3323 if (mState == State.APPS_CUSTOMIZE) {
3324 text.add(getString(R.string.all_apps_button_label));
3325 } else {
3326 text.add(getString(R.string.all_apps_home_button_label));
3327 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003328 return result;
3329 }
3330
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003331 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003332 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003333 */
3334 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3335 @Override
3336 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003337 closeSystemDialogs();
3338 }
3339 }
3340
3341 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003342 * Receives notifications whenever the appwidgets are reset.
3343 */
3344 private class AppWidgetResetObserver extends ContentObserver {
3345 public AppWidgetResetObserver() {
3346 super(new Handler());
3347 }
3348
3349 @Override
3350 public void onChange(boolean selfChange) {
3351 onAppWidgetReset();
3352 }
3353 }
3354
3355 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003356 * If the activity is currently paused, signal that we need to run the passed Runnable
3357 * in onResume.
3358 *
3359 * This needs to be called from incoming places where resources might have been loaded
3360 * while we are paused. That is becaues the Configuration might be wrong
3361 * when we're not running, and if it comes back to what it was when we
3362 * were paused, we are not restarted.
3363 *
3364 * Implementation of the method from LauncherModel.Callbacks.
3365 *
3366 * @return true if we are currently paused. The caller might be able to
3367 * skip some work in that case since we will come back again.
3368 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003369 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003370 if (mPaused) {
3371 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003372 if (deletePreviousRunnables) {
3373 while (mOnResumeCallbacks.remove(run)) {
3374 }
3375 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003376 mOnResumeCallbacks.add(run);
3377 return true;
3378 } else {
3379 return false;
3380 }
3381 }
3382
Michael Jurkac402cd92013-05-20 15:49:32 +02003383 private boolean waitUntilResume(Runnable run) {
3384 return waitUntilResume(run, false);
3385 }
3386
Michael Jurka7607c2f2013-04-03 14:33:19 -07003387 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003388 * If the activity is currently paused, signal that we need to re-run the loader
3389 * in onResume.
3390 *
3391 * This needs to be called from incoming places where resources might have been loaded
3392 * while we are paused. That is becaues the Configuration might be wrong
3393 * when we're not running, and if it comes back to what it was when we
3394 * were paused, we are not restarted.
3395 *
3396 * Implementation of the method from LauncherModel.Callbacks.
3397 *
3398 * @return true if we are currently paused. The caller might be able to
3399 * skip some work in that case since we will come back again.
3400 */
3401 public boolean setLoadOnResume() {
3402 if (mPaused) {
3403 Log.i(TAG, "setLoadOnResume");
3404 mOnResumeNeedsLoad = true;
3405 return true;
3406 } else {
3407 return false;
3408 }
3409 }
3410
3411 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003412 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003413 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003415 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003416 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003417 } else {
3418 return SCREEN_COUNT / 2;
3419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003421
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 /**
3423 * Refreshes the shortcuts shown on the workspace.
3424 *
3425 * Implementation of the method from LauncherModel.Callbacks.
3426 */
3427 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003428 // If we're starting binding all over again, clear any bind calls we'd postponed in
3429 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3430 // from scratch again
3431 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003432
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003434 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003435 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003436 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 int count = workspace.getChildCount();
3438 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003439 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003440 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3441 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003442 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003443 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003444 if (mHotseat != null) {
3445 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003446 }
3447 }
3448
Adam Cohendcd297f2013-06-18 13:13:40 -07003449 @Override
3450 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003451 bindAddScreens(orderedScreenIds);
3452 mWorkspace.addExtraEmptyScreen();
3453 }
3454
3455 @Override
3456 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003457 int count = orderedScreenIds.size();
3458 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003459 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003460 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003461 }
3462
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003463 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 * Bind the items start-end from the list.
3465 *
3466 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003467 */
Winson Chung64359a52013-07-08 17:17:08 -07003468 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3469 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003470 if (waitUntilResume(new Runnable() {
3471 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003472 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003473 }
3474 })) {
3475 return;
3476 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003477
Winson Chungf0c6ae02012-03-21 16:10:31 -07003478 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3479 Set<String> newApps = new HashSet<String>();
3480 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3481
Winson Chung64359a52013-07-08 17:17:08 -07003482 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3483 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003484 Workspace workspace = mWorkspace;
3485 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003487
3488 // Short circuit if we are loading dock items for a configuration which has no dock
3489 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3490 mHotseat == null) {
3491 continue;
3492 }
3493
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 switch (item.itemType) {
3495 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3496 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003497 ShortcutInfo info = (ShortcutInfo) item;
3498 String uri = info.intent.toUri(0).toString();
3499 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003500
Winson Chung64359a52013-07-08 17:17:08 -07003501 /*
3502 * TODO: FIX collision case
3503 */
Winson Chungce376632013-07-11 16:12:41 -07003504 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3505 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3506 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3507 throw new RuntimeException("OCCUPIED");
3508 }
Winson Chung64359a52013-07-08 17:17:08 -07003509 }
3510
Adam Cohendcd297f2013-06-18 13:13:40 -07003511 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3512 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003513 boolean animateIconUp = false;
3514 synchronized (newApps) {
3515 if (newApps.contains(uri)) {
3516 animateIconUp = newApps.remove(uri);
3517 }
3518 }
Winson Chung64359a52013-07-08 17:17:08 -07003519 if (forceAnimateIcons) {
3520 // Animate all the applications up now
3521 shortcut.setAlpha(0f);
3522 shortcut.setScaleX(0f);
3523 shortcut.setScaleY(0f);
3524 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3525 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003526 // Prepare the view to be animated up
3527 shortcut.setAlpha(0f);
3528 shortcut.setScaleX(0f);
3529 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003530 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003531 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3532 mNewShortcutAnimateViews.add(shortcut);
3533 }
3534 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003535 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003536 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003537 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003538 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003539 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003540 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3541 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003544 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003545
Winson Chung64359a52013-07-08 17:17:08 -07003546 if (forceAnimateIcons) {
3547 // We post the animation slightly delayed to prevent slowdowns when we are loading
3548 // right after we return to launcher.
3549 mWorkspace.postDelayed(new Runnable() {
3550 public void run() {
3551 anim.playTogether(bounceAnims);
3552 anim.start();
3553 }
3554 }, NEW_APPS_ANIMATION_DELAY);
3555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003556 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003557 }
3558
Joe Onorato9c1289c2009-08-17 11:03:03 -04003559 /**
3560 * Implementation of the method from LauncherModel.Callbacks.
3561 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003562 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3563 if (waitUntilResume(new Runnable() {
3564 public void run() {
3565 bindFolders(folders);
3566 }
3567 })) {
3568 return;
3569 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003570 sFolders.clear();
3571 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573
3574 /**
3575 * Add the views for a widget to the workspace.
3576 *
3577 * Implementation of the method from LauncherModel.Callbacks.
3578 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003579 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3580 if (waitUntilResume(new Runnable() {
3581 public void run() {
3582 bindAppWidget(item);
3583 }
3584 })) {
3585 return;
3586 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003587
Daniel Sandler843e8602010-06-07 14:59:01 -04003588 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3589 if (DEBUG_WIDGETS) {
3590 Log.d(TAG, "bindAppWidget: " + item);
3591 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 final Workspace workspace = mWorkspace;
3593
3594 final int appWidgetId = item.appWidgetId;
3595 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003596 if (DEBUG_WIDGETS) {
3597 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3598 }
3599
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3601
Joe Onorato9c1289c2009-08-17 11:03:03 -04003602 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003603 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604
Adam Cohendcd297f2013-06-18 13:13:40 -07003605 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003607 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3608
Joe Onorato9c1289c2009-08-17 11:03:03 -04003609 workspace.requestLayout();
3610
Daniel Sandler843e8602010-06-07 14:59:01 -04003611 if (DEBUG_WIDGETS) {
3612 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3613 + (SystemClock.uptimeMillis()-start) + "ms");
3614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 }
3616
Adam Cohen1462de32012-07-24 22:34:36 -07003617 public void onPageBoundSynchronously(int page) {
3618 mSynchronouslyBoundPages.add(page);
3619 }
3620
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 /**
3622 * Callback saying that there aren't any more items to bind.
3623 *
3624 * Implementation of the method from LauncherModel.Callbacks.
3625 */
Adam Cohene25af792013-06-06 23:08:25 -07003626 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003627 if (waitUntilResume(new Runnable() {
3628 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003629 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003630 }
3631 })) {
3632 return;
3633 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003634 if (mSavedState != null) {
3635 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003636 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003638 mSavedState = null;
3639 }
3640
Adam Cohen1462de32012-07-24 22:34:36 -07003641 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003642
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003643 // If we received the result of any pending adds while the loader was running (e.g. the
3644 // widget configuration forced an orientation change), process them now.
3645 for (int i = 0; i < sPendingAddList.size(); i++) {
3646 completeAdd(sPendingAddList.get(i));
3647 }
3648 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649
Winson Chungc51db6a2011-10-05 11:44:49 -07003650 // Update the market app icon as necessary (the other icons will be managed in response to
3651 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003652 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003653
Winson Chungf0c6ae02012-03-21 16:10:31 -07003654 // Animate up any icons as necessary
3655 if (mVisible || mWorkspaceLoading) {
3656 Runnable newAppsRunnable = new Runnable() {
3657 @Override
3658 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003659 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003660 }
3661 };
Winson Chunga2413752012-04-03 14:22:34 -07003662
Adam Cohendcd297f2013-06-18 13:13:40 -07003663 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3664 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003665 if (canRunNewAppsAnimation()) {
3666 // If the user has not interacted recently, then either snap to the new page to show
3667 // the new-apps animation or just run them if they are to appear on the current page
3668 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003669 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003670 } else {
3671 runNewAppsAnimation(false);
3672 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003673 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003674 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003675 // are on another page (or just normally if they are added to the current page)
3676 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003677 }
3678 }
3679
3680 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003681 if (upgradePath) {
3682 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003683 mWorkspace.stripDuplicateApps();
3684 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003685 }
Adam Cohen99894d92013-06-14 11:22:59 -07003686
Adam Cohen66a01fd2013-06-11 12:48:00 -07003687 mWorkspace.post(new Runnable() {
3688 @Override
3689 public void run() {
3690 onFinishBindingItems();
3691 }
3692 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 }
3694
Winson Chunga2413752012-04-03 14:22:34 -07003695 private boolean canRunNewAppsAnimation() {
3696 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3697 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3698 }
3699
Winson Chungc9168342013-06-26 14:54:55 -07003700 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3701 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3702 PropertyValuesHolder.ofFloat("alpha", 1f),
3703 PropertyValuesHolder.ofFloat("scaleX", 1f),
3704 PropertyValuesHolder.ofFloat("scaleY", 1f));
3705 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3706 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3707 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3708 return bounceAnim;
3709 }
3710
Winson Chungf0c6ae02012-03-21 16:10:31 -07003711 /**
3712 * Runs a new animation that scales up icons that were added while Launcher was in the
3713 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003714 *
3715 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003716 */
Winson Chunga2413752012-04-03 14:22:34 -07003717 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003718 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003719 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003720
3721 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003722 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3723 @Override
3724 public int compare(View a, View b) {
3725 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3726 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3727 int cellCountX = LauncherModel.getCellCountX();
3728 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3729 }
3730 });
Winson Chunga2413752012-04-03 14:22:34 -07003731
3732 // Animate each of the views in place (or show them immediately if requested)
3733 if (immediate) {
3734 for (View v : mNewShortcutAnimateViews) {
3735 v.setAlpha(1f);
3736 v.setScaleX(1f);
3737 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003738 }
Winson Chunga2413752012-04-03 14:22:34 -07003739 } else {
3740 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3741 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003742 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003743 }
3744 anim.playTogether(bounceAnims);
3745 anim.addListener(new AnimatorListenerAdapter() {
3746 @Override
3747 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003748 if (mWorkspace != null) {
3749 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3750 }
Winson Chunga2413752012-04-03 14:22:34 -07003751 }
3752 });
3753 anim.start();
3754 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003755
3756 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003757 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003758 mNewShortcutAnimateViews.clear();
3759 new Thread("clearNewAppsThread") {
3760 public void run() {
3761 mSharedPrefs.edit()
3762 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3763 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3764 .commit();
3765 }
3766 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003767 }
3768
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003769 @Override
3770 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003771 boolean searchVisible = updateGlobalSearchIcon();
3772 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003773 if (mSearchDropTargetBar != null) {
3774 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3775 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003776 }
3777
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003778 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 * Add the icons for all apps.
3780 *
3781 * Implementation of the method from LauncherModel.Callbacks.
3782 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003783 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003784 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3785 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003786 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003787 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003788 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789 }
3790
3791 /**
3792 * A package was updated.
3793 *
3794 * Implementation of the method from LauncherModel.Callbacks.
3795 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3797 if (waitUntilResume(new Runnable() {
3798 public void run() {
3799 bindAppsUpdated(apps);
3800 }
3801 })) {
3802 return;
3803 }
3804
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003805 if (mWorkspace != null) {
3806 mWorkspace.updateShortcuts(apps);
3807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003808 }
3809
3810 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003811 * A package was uninstalled. We take both the super set of packageNames
3812 * in addition to specific applications to remove, the reason being that
3813 * this can be called when a package is updated as well. In that scenario,
3814 * we only remove specific components from the workspace, where as
3815 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003816 *
3817 * Implementation of the method from LauncherModel.Callbacks.
3818 */
Winson Chung83892cc2013-05-01 16:53:33 -07003819 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3820 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003821 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003822 if (waitUntilResume(new Runnable() {
3823 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003824 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003825 }
3826 })) {
3827 return;
3828 }
3829
Winson Chung64359a52013-07-08 17:17:08 -07003830 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003831 mWorkspace.removeItemsByPackageName(packageNames);
3832 } else {
3833 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003834 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003835
Winson Chunga1820962011-10-03 16:31:06 -07003836 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003837 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003838 }
Joe Onoratobe386092009-11-17 17:32:16 -08003839
3840 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003841 * A number of packages were updated.
3842 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003843 private ArrayList<Object> mWidgetsAndShortcuts;
3844 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003845 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003846 bindPackagesUpdated(mWidgetsAndShortcuts);
3847 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003848 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003849 };
3850
3851 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3852 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3853 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003854 return;
3855 }
3856
Winson Chung64359a52013-07-08 17:17:08 -07003857 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003858 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003859 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003860 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003861 }
3862
Winson Chung400438b2011-01-16 17:53:48 -08003863 private int mapConfigurationOriActivityInfoOri(int configOri) {
3864 final Display d = getWindowManager().getDefaultDisplay();
3865 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3866 switch (d.getRotation()) {
3867 case Surface.ROTATION_0:
3868 case Surface.ROTATION_180:
3869 // We are currently in the same basic orientation as the natural orientation
3870 naturalOri = configOri;
3871 break;
3872 case Surface.ROTATION_90:
3873 case Surface.ROTATION_270:
3874 // We are currently in the other basic orientation to the natural orientation
3875 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3876 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3877 break;
3878 }
3879
3880 int[] oriMap = {
3881 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3882 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3883 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3884 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3885 };
3886 // Since the map starts at portrait, we need to offset if this device's natural orientation
3887 // is landscape.
3888 int indexOffset = 0;
3889 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3890 indexOffset = 1;
3891 }
3892 return oriMap[(d.getRotation() + indexOffset) % 4];
3893 }
Adam Cohen446e9402011-09-15 18:21:21 -07003894
Winson Chung4b919f82012-05-01 10:44:08 -07003895 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003896 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003897 getResources().getBoolean(R.bool.allow_rotation);
3898 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003899 }
Winson Chung4b919f82012-05-01 10:44:08 -07003900 public void lockScreenOrientation() {
3901 if (isRotationEnabled()) {
3902 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3903 .getConfiguration().orientation));
3904 }
3905 }
3906 public void unlockScreenOrientation(boolean immediate) {
3907 if (isRotationEnabled()) {
3908 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003909 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003910 } else {
3911 mHandler.postDelayed(new Runnable() {
3912 public void run() {
3913 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3914 }
3915 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003916 }
Winson Chung4b919f82012-05-01 10:44:08 -07003917 }
Winson Chung400438b2011-01-16 17:53:48 -08003918 }
3919
Winson Chung82f55532011-08-09 14:14:23 -07003920 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003921 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003922 if (DISABLE_CLINGS) {
3923 return false;
3924 }
3925
Brett Chabot2a9e2282011-08-23 15:03:13 -07003926 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003927 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003928
Michael Jurkae233a8b2013-03-19 13:49:20 +01003929 // Restricted secondary users (child mode) will potentially have very few apps
3930 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003931// boolean supportsLimitedUsers =
3932// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3933// Account[] accounts = AccountManager.get(this).getAccounts();
3934// if (supportsLimitedUsers && accounts.length == 0) {
3935// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3936// Bundle restrictions = um.getUserRestrictions();
3937// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3938// return false;
3939// }
3940// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003941 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003942 }
Michael Jurka22143132012-10-04 17:42:38 +02003943
Winson Chung7d7541e2011-09-16 20:14:36 -07003944 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003945 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003946 if (cling != null) {
3947 cling.init(this, positionData);
3948 cling.setVisibility(View.VISIBLE);
3949 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3950 if (animate) {
3951 cling.buildLayer();
3952 cling.setAlpha(0f);
3953 cling.animate()
3954 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003955 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003956 .setDuration(SHOW_CLING_DURATION)
3957 .setStartDelay(delay)
3958 .start();
3959 } else {
3960 cling.setAlpha(1f);
3961 }
Michael Jurka22143132012-10-04 17:42:38 +02003962 cling.setFocusableInTouchMode(true);
3963 cling.post(new Runnable() {
3964 public void run() {
3965 cling.setFocusable(true);
3966 cling.requestFocus();
3967 }
3968 });
3969 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3970 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003971 }
3972 return cling;
3973 }
Michael Jurka22143132012-10-04 17:42:38 +02003974
Winson Chung7d7541e2011-09-16 20:14:36 -07003975 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003976 // To catch cases where siblings of top-level views are made invisible, just check whether
3977 // the cling is directly set to GONE before dismissing it.
3978 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003979 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003980 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003981 anim.addListener(new AnimatorListenerAdapter() {
3982 public void onAnimationEnd(Animator animation) {
3983 cling.setVisibility(View.GONE);
3984 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003985 // We should update the shared preferences on a background thread
3986 new Thread("dismissClingThread") {
3987 public void run() {
3988 SharedPreferences.Editor editor = mSharedPrefs.edit();
3989 editor.putBoolean(flag, true);
3990 editor.commit();
3991 }
3992 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003993 };
3994 });
3995 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003996 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003997 }
3998 }
Michael Jurka22143132012-10-04 17:42:38 +02003999
Winson Chung9d9d74f2011-09-19 11:49:12 -07004000 private void removeCling(int id) {
4001 final View cling = findViewById(id);
4002 if (cling != null) {
4003 final ViewGroup parent = (ViewGroup) cling.getParent();
4004 parent.post(new Runnable() {
4005 @Override
4006 public void run() {
4007 parent.removeView(cling);
4008 }
4009 });
Michael Jurka22143132012-10-04 17:42:38 +02004010 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004011 }
4012 }
Michael Jurka974c3862012-05-22 22:00:31 -07004013
4014 private boolean skipCustomClingIfNoAccounts() {
4015 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4016 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4017 if (customCling) {
4018 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004019 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004020 Account[] accounts = am.getAccountsByType("com.google");
4021 return accounts.length == 0;
4022 }
4023 return false;
4024 }
4025
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004027 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004028 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004029 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4030 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004031 // If we're not using the default workspace layout, replace workspace cling
4032 // with a custom workspace cling (usually specified in an overlay)
4033 // For now, only do this on tablets
4034 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004035 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004036 // Use a custom cling
4037 View cling = findViewById(R.id.workspace_cling);
4038 ViewGroup clingParent = (ViewGroup) cling.getParent();
4039 int clingIndex = clingParent.indexOfChild(cling);
4040 clingParent.removeViewAt(clingIndex);
4041 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4042 clingParent.addView(customCling, clingIndex);
4043 customCling.setId(R.id.workspace_cling);
4044 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004045 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004046 } else {
4047 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004048 }
4049 }
4050 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004051 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004052 if (isClingsEnabled() &&
4053 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004054 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004055 } else {
4056 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004057 }
4058 }
4059 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004061 if (isClingsEnabled() &&
4062 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4063 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004064 } else {
4065 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004066 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004067 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 }
4069 public boolean isFolderClingVisible() {
4070 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004071 if (cling != null) {
4072 return cling.getVisibility() == View.VISIBLE;
4073 }
4074 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004075 }
Winson Chung82f55532011-08-09 14:14:23 -07004076 public void dismissWorkspaceCling(View v) {
4077 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004079 }
4080 public void dismissAllAppsCling(View v) {
4081 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004082 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4083 }
4084 public void dismissFolderCling(View v) {
4085 Cling cling = (Cling) findViewById(R.id.folder_cling);
4086 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004087 }
4088
Winson Chung80baf5a2010-08-09 16:03:15 -07004089 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004090 * Prints out out state for debugging.
4091 */
4092 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004093 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004094 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004095 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4096 Log.d(TAG, "mRestoring=" + mRestoring);
4097 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4098 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004099 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004100 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004101
Winson Chung785d2eb2011-04-14 16:08:02 -07004102 if (mAppsCustomizeContent != null) {
4103 mAppsCustomizeContent.dumpState();
4104 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004105 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004106 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004107
4108 @Override
4109 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4110 super.dump(prefix, fd, writer, args);
4111 writer.println(" ");
4112 writer.println("Debug logs: ");
4113 for (int i = 0; i < sDumpLogs.size(); i++) {
4114 writer.println(" " + sDumpLogs.get(i));
4115 }
4116 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004117
4118 public static void dumpDebugLogsToConsole() {
4119 Log.d(TAG, "");
4120 Log.d(TAG, "*********************");
4121 Log.d(TAG, "Launcher debug logs: ");
4122 for (int i = 0; i < sDumpLogs.size(); i++) {
4123 Log.d(TAG, " " + sDumpLogs.get(i));
4124 }
4125 Log.d(TAG, "*********************");
4126 Log.d(TAG, "");
4127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004128}
Michael Jurka2763be32011-02-24 11:19:57 -08004129
Michael Jurkaabded662011-03-04 12:06:57 -08004130interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004131 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004132 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004133 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004134 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004135 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004136}