blob: 9a7d42ee0334f20c2fa6a2985ddc97d8bd44444e [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 Jurka1e2f4652013-07-08 18:03:46 -0700258 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700259 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
260
Winson Chung4a2afa32012-07-19 14:53:05 -0700261 // Keep track of whether the user has left launcher
262 private static boolean sPausedFromUserAction = false;
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private Bundle mSavedInstanceState;
265
Joe Onorato9c1289c2009-08-17 11:03:03 -0400266 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800267 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800268 private boolean mUserPresent = true;
269 private boolean mVisible = false;
270 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700271 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700273 private static LocaleConfiguration sLocaleConfiguration = null;
274
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700275 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700276
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700277 private Intent mAppMarketIntent = null;
278
Adam Cohended9f8d2010-11-03 13:25:16 -0700279 // Related to the auto-advancing of widgets
280 private final int ADVANCE_MSG = 1;
281 private final int mAdvanceInterval = 20000;
282 private final int mAdvanceStagger = 250;
283 private long mAutoAdvanceSentTime;
284 private long mAutoAdvanceTimeLeft = -1;
285 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
286 new HashMap<View, AppWidgetProviderInfo>();
287
Winson Chung400438b2011-01-16 17:53:48 -0800288 // Determines how long to wait after a rotation before restoring the screen orientation to
289 // match the sensor state.
290 private final int mRestoreScreenOrientationDelay = 500;
291
Michael Jurka4ef207b2010-11-29 17:05:45 -0800292 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700293 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
294 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
295 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800296
Craig Mautner360310b2012-10-26 15:13:08 -0700297 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700298
Adam Cohen1462de32012-07-24 22:34:36 -0700299 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
300
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700301 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
302
Winson Chung46353de2012-02-16 14:05:10 -0800303 // We only want to get the SharedPreferences once since it does an FS stat each time we get
304 // it from the context.
305 private SharedPreferences mSharedPrefs;
306
Adam Cohene25af792013-06-06 23:08:25 -0700307 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
308
Winson Chungf0c6ae02012-03-21 16:10:31 -0700309 // Holds the page that we need to animate to, and the icon views that we need to animate up
310 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700311 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700312 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700313 private ImageView mFolderIconImageView;
314 private Bitmap mFolderIconBitmap;
315 private Canvas mFolderIconCanvas;
316 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700317
Michael Jurkaddd62e92011-02-16 17:49:14 -0800318 private BubbleTextView mWaitingForResume;
319
Michael Jurka22143132012-10-04 17:42:38 +0200320 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
321 = new HideFromAccessibilityHelper();
322
Michael Jurkac1f5d262011-09-30 19:32:27 -0700323 private Runnable mBuildLayersRunnable = new Runnable() {
324 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700325 if (mWorkspace != null) {
326 mWorkspace.buildPageHardwareLayers();
327 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700328 }
329 };
330
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800331 private static ArrayList<PendingAddArguments> sPendingAddList
332 = new ArrayList<PendingAddArguments>();
333
Michael Jurka0a457bf2012-11-19 14:05:05 -0800334 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
335
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800336 private static class PendingAddArguments {
337 int requestCode;
338 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700339 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700340 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800341 int cellX;
342 int cellY;
343 }
344
Michael Jurka0a457bf2012-11-19 14:05:05 -0800345 private static boolean isPropertyEnabled(String propertyName) {
346 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700347 }
348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 @Override
350 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700351 if (DEBUG_STRICT_MODE) {
352 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
353 .detectDiskReads()
354 .detectDiskWrites()
355 .detectNetwork() // or .detectAll() for all detectable problems
356 .penaltyLog()
357 .build());
358 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
359 .detectLeakedSqlLiteObjects()
360 .detectLeakedClosableObjects()
361 .penaltyLog()
362 .penaltyDeath()
363 .build());
364 }
365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400367
368 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
369 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400370 LauncherAppState app = LauncherAppState.getInstance();
371 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700372 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800373 mModel = app.setLauncher(this);
374 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400375 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700377
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700378 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700379 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
380 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700381
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700382 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
383 // this also ensures that any synchronous binding below doesn't re-trigger another
384 // LauncherModel load.
385 mPaused = false;
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200388 android.os.Debug.startMethodTracing(
389 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391
392 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 setContentView(R.layout.launcher);
394 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700395 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800397 registerContentObservers();
398
Joe Onorato7c312c12009-08-13 21:36:53 -0700399 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 mSavedState = savedInstanceState;
402 restoreState(mSavedState);
403
Winson Chunga12a2502010-12-20 14:41:35 -0800404 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700405 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200406 mAppsCustomizeContent.onPackagesUpdated(
407 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700408 }
Winson Chunga12a2502010-12-20 14:41:35 -0800409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 if (PROFILE_STARTUP) {
411 android.os.Debug.stopMethodTracing();
412 }
413
414 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700415 if (sPausedFromUserAction) {
416 // If the user leaves launcher, then we should just load items asynchronously when
417 // they return.
418 mModel.startLoader(true, -1);
419 } else {
420 // We only load the page synchronously if the user rotates (or triggers a
421 // configuration change) while launcher is in the foreground
422 mModel.startLoader(true, mWorkspace.getCurrentPage());
423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425
426 // For handling default keys
427 mDefaultKeySsb = new SpannableStringBuilder();
428 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800429
430 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
431 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 updateGlobalIcons();
434
435 // On large interfaces, we want the screen to auto-rotate based on the current orientation
436 unlockScreenOrientation(true);
437 }
438
Winson Chung4a2afa32012-07-19 14:53:05 -0700439 protected void onUserLeaveHint() {
440 super.onUserLeaveHint();
441 sPausedFromUserAction = true;
442 }
443
Adam Cohenf9426d52012-06-04 17:26:21 -0700444 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700445 boolean searchVisible = false;
446 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800447 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 int coi = getCurrentOrientationIndexForGlobalIcons();
449 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
450 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700451 updateAppMarketIcon();
452 searchVisible = updateGlobalSearchIcon();
453 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700454 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700455 if (sGlobalSearchIcon[coi] != null) {
456 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700457 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700458 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700459 if (sVoiceSearchIcon[coi] != null) {
460 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700461 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700462 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700463 if (sAppMarketIcon[coi] != null) {
464 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800465 }
Winson Chungadf0c182012-08-23 14:59:07 -0700466 if (mSearchDropTargetBar != null) {
467 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
Romain Guycbb89e42009-06-08 15:52:54 -0700470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 if (sLocaleConfiguration == null) {
473 new AsyncTask<Void, Void, LocaleConfiguration>() {
474 @Override
475 protected LocaleConfiguration doInBackground(Void... unused) {
476 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
477 readConfiguration(Launcher.this, localeConfiguration);
478 return localeConfiguration;
479 }
480
481 @Override
482 protected void onPostExecute(LocaleConfiguration result) {
483 sLocaleConfiguration = result;
484 checkForLocaleChange(); // recursive, but now with a locale configuration
485 }
486 }.execute();
487 return;
488 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 final Configuration configuration = getResources().getConfiguration();
491
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700492 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 final String locale = configuration.locale.toString();
494
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700495 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 final int mcc = configuration.mcc;
497
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 final int mnc = configuration.mnc;
500
Romain Guy84f296c2009-11-04 15:00:44 -0800501 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502
Romain Guy84f296c2009-11-04 15:00:44 -0800503 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700504 sLocaleConfiguration.locale = locale;
505 sLocaleConfiguration.mcc = mcc;
506 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700507
Joe Onorato0589f0f2010-02-08 13:44:00 -0800508 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700509
510 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
511 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700512 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700513 public void run() {
514 writeConfiguration(Launcher.this, localeConfiguration);
515 }
516 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700517 }
518 }
519
520 private static class LocaleConfiguration {
521 public String locale;
522 public int mcc = -1;
523 public int mnc = -1;
524 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700525
Romain Guy98d01652009-06-30 16:21:04 -0700526 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
527 DataInputStream in = null;
528 try {
529 in = new DataInputStream(context.openFileInput(PREFERENCES));
530 configuration.locale = in.readUTF();
531 configuration.mcc = in.readInt();
532 configuration.mnc = in.readInt();
533 } catch (FileNotFoundException e) {
534 // Ignore
535 } catch (IOException e) {
536 // Ignore
537 } finally {
538 if (in != null) {
539 try {
540 in.close();
541 } catch (IOException e) {
542 // Ignore
543 }
544 }
545 }
546 }
547
548 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
549 DataOutputStream out = null;
550 try {
551 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
552 out.writeUTF(configuration.locale);
553 out.writeInt(configuration.mcc);
554 out.writeInt(configuration.mnc);
555 out.flush();
556 } catch (FileNotFoundException e) {
557 // Ignore
558 } catch (IOException e) {
559 //noinspection ResultOfMethodCallIgnored
560 context.getFileStreamPath(PREFERENCES).delete();
561 } finally {
562 if (out != null) {
563 try {
564 out.close();
565 } catch (IOException e) {
566 // Ignore
567 }
568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800569 }
570 }
571
Bjorn Bringertc459e522013-06-07 19:36:01 +0100572 public LayoutInflater getInflater() {
573 return mInflater;
574 }
575
Adam Cohen716b51e2011-06-30 12:09:54 -0700576 public DragLayer getDragLayer() {
577 return mDragLayer;
578 }
579
Winson Chung36a62fe2012-05-06 18:04:42 -0700580 boolean isDraggingEnabled() {
581 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
582 // that is subsequently removed from the workspace in startBinding().
583 return !mModel.isLoadingWorkspace();
584 }
585
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 static int getScreen() {
587 synchronized (sLock) {
588 return sScreen;
589 }
590 }
591
592 static void setScreen(int screen) {
593 synchronized (sLock) {
594 sScreen = screen;
595 }
596 }
597
Winson Chung557d6ed2011-07-08 15:34:52 -0700598 /**
599 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
600 * a configuration step, this allows the proper animations to run after other transitions.
601 */
602 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 switch (args.requestCode) {
605 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700606 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700607 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 break;
609 case REQUEST_PICK_SHORTCUT:
610 processShortcut(args.intent);
611 break;
612 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700613 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700614 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 result = true;
616 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_CREATE_APPWIDGET:
618 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700619 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700620 result = true;
621 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 case REQUEST_PICK_WALLPAPER:
623 // We just wanted the activity result here so we can clear mWaitingForResult
624 break;
625 }
Michael Jurka27614d22012-04-02 06:40:22 -0700626 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
627 // if you turned the screen off and then back while in All Apps, Launcher would not
628 // return to the workspace. Clearing mAddInfo.container here fixes this issue
629 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700630 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800631 }
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700634 protected void onActivityResult(
635 final int requestCode, final int resultCode, final Intent data) {
636 if (requestCode == REQUEST_BIND_APPWIDGET) {
637 int appWidgetId = data != null ?
638 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
639 if (resultCode == RESULT_CANCELED) {
640 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
641 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700642 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700643 }
644 return;
645 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
648 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700649 mWaitingForResult = false;
650
Adam Cohened66b2b2012-01-23 17:28:51 -0800651 // We have special handling for widgets
652 if (isWidgetDrop) {
653 int appWidgetId = data != null ?
654 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700655 if (appWidgetId < 0) {
656 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
657 "widget configuration activity.");
658 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
659 } else {
660 completeTwoStageWidgetDrop(resultCode, appWidgetId);
661 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 return;
663 }
664
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 // The pattern used here is that a user PICKs a specific application,
666 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
669 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700670 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800671 final PendingAddArguments args = new PendingAddArguments();
672 args.requestCode = requestCode;
673 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700674 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700675 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700676 args.cellX = mPendingAddInfo.cellX;
677 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800678 if (isWorkspaceLocked()) {
679 sPendingAddList.add(args);
680 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700681 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800684 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700685 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
687 null);
688 }
689
690 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700691 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700692 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 Runnable onCompleteRunnable = null;
694 int animationType = 0;
695
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700696 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 if (resultCode == RESULT_OK) {
698 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
699 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700700 mPendingAddWidgetInfo);
701 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800702 onCompleteRunnable = new Runnable() {
703 @Override
704 public void run() {
705 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800707 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
708 null);
709 }
710 };
711 } else if (resultCode == RESULT_CANCELED) {
712 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
713 onCompleteRunnable = new Runnable() {
714 @Override
715 public void run() {
716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
717 null);
718 }
719 };
720 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700721 if (mDragLayer.getAnimatedView() != null) {
722 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
723 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
724 animationType, boundWidget, true);
725 } else {
726 // The animated view may be null in the case of a rotation during widget configuration
727 onCompleteRunnable.run();
728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 }
730
731 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700732 protected void onStop() {
733 super.onStop();
734 FirstFrameAnimatorHelper.setIsVisible(false);
735 }
736
737 @Override
738 protected void onStart() {
739 super.onStart();
740 FirstFrameAnimatorHelper.setIsVisible(true);
741 }
742
743 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200745 long startTime = 0;
746 if (DEBUG_RESUME_TIME) {
747 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400748 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700751
Winson Chung4a2afa32012-07-19 14:53:05 -0700752 // Restore the previous launcher state
753 if (mOnResumeState == State.WORKSPACE) {
754 showWorkspace(false);
755 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
756 showAllApps(false);
757 }
758 mOnResumeState = State.NONE;
759
Craig Mautner360310b2012-10-26 15:13:08 -0700760 // Background was set to gradient in onPause(), restore to black if in all apps.
761 setWorkspaceBackground(mState == State.WORKSPACE);
762
Winson Chungde0fb8f2012-05-08 14:37:08 -0700763 // Process any items that were added while Launcher was away
764 InstallShortcutReceiver.flushInstallQueue(this);
765
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800766 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700767 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700768 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400769 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700770 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700772 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700774 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200775 // We might have postponed some bind calls until onResume (see waitUntilResume) --
776 // execute them here
777 long startTimeCallbacks = 0;
778 if (DEBUG_RESUME_TIME) {
779 startTimeCallbacks = System.currentTimeMillis();
780 }
781
782 if (mAppsCustomizeContent != null) {
783 mAppsCustomizeContent.setBulkBind(true);
784 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700785 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
786 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200787 }
788 if (mAppsCustomizeContent != null) {
789 mAppsCustomizeContent.setBulkBind(false);
790 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700791 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200792 if (DEBUG_RESUME_TIME) {
793 Log.d(TAG, "Time spent processing callbacks in onResume: " +
794 (System.currentTimeMillis() - startTimeCallbacks));
795 }
Michael Jurka447bf842013-05-15 14:52:15 +0200796 }
Winson Chunge4e50662012-01-23 14:45:13 -0800797
798 // Reset the pressed state of icons that were locked in the press state while activities
799 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800800 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800801 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800802 mWaitingForResume.setStayPressed(false);
803 }
Winson Chunge4e50662012-01-23 14:45:13 -0800804 if (mAppsCustomizeContent != null) {
805 // Resets the previous all apps icon press state
806 mAppsCustomizeContent.resetDrawableState();
807 }
Adam Cohen06dff352012-06-01 17:17:08 -0700808 // It is possible that widgets can receive updates while launcher is not in the foreground.
809 // Consequently, the widgets will be inflated in the orientation of the foreground activity
810 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
811 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700812 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700813
814 // Again, as with the above scenario, it's possible that one or more of the global icons
815 // were updated in the wrong orientation.
816 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200817 if (DEBUG_RESUME_TIME) {
818 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
819 }
Adam Cohen06dff352012-06-01 17:17:08 -0700820 }
821
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700823 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700824 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
825 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
826 // when Launcher resumes and we are still in AllApps.
827 updateWallpaperVisibility(true);
828
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700829 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700830 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800831 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700832 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 }
Romain Guycbb89e42009-06-08 15:52:54 -0700834
Adam Cohen66a01fd2013-06-11 12:48:00 -0700835 protected void onFinishBindingItems() {
836 }
837
838 // Add a fullscreen unpadded view to the workspace to the left all other screens.
839 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700840 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700841 }
842
Adam Cohenedb40762013-07-18 16:45:45 -0700843 // The custom content needs to offset its content to account for the QSB
844 public int getTopOffsetForCustomContent() {
845 return mWorkspace.getPaddingTop();
846 }
847
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700848 @Override
849 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400850 // Flag the loader to stop early before switching
851 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700852 if (mAppsCustomizeContent != null) {
853 mAppsCustomizeContent.surrender();
854 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800855 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700856 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800858 // We can't hide the IME if it was forced open. So don't bother
859 /*
860 @Override
861 public void onWindowFocusChanged(boolean hasFocus) {
862 super.onWindowFocusChanged(hasFocus);
863
864 if (hasFocus) {
865 final InputMethodManager inputManager = (InputMethodManager)
866 getSystemService(Context.INPUT_METHOD_SERVICE);
867 WindowManager.LayoutParams lp = getWindow().getAttributes();
868 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
869 android.os.Handler()) {
870 protected void onReceiveResult(int resultCode, Bundle resultData) {
871 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
872 }
873 });
874 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
875 }
876 }
877 */
878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 private boolean acceptFilter() {
880 final InputMethodManager inputManager = (InputMethodManager)
881 getSystemService(Context.INPUT_METHOD_SERVICE);
882 return !inputManager.isFullscreenMode();
883 }
884
885 @Override
886 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700887 final int uniChar = event.getUnicodeChar();
888 final boolean handled = super.onKeyDown(keyCode, event);
889 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
890 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
892 keyCode, event);
893 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700894 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700895 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700896 // showSearchDialog()
897 // If there are multiple keystrokes before the search dialog takes focus,
898 // onSearchRequested() will be called for every keystroke,
899 // but it is idempotent, so it's fine.
900 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902 }
903
Joe Onorato8a9625e2010-01-28 15:55:35 -0800904 // Eat the long press event so the keyboard doesn't come up.
905 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
906 return true;
907 }
908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 return handled;
910 }
911
Karl Rosaen138a0412009-04-23 19:00:21 -0700912 private String getTypedText() {
913 return mDefaultKeySsb.toString();
914 }
915
916 private void clearTypedText() {
917 mDefaultKeySsb.clear();
918 mDefaultKeySsb.clearSpans();
919 Selection.setSelection(mDefaultKeySsb, 0);
920 }
921
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700923 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
924 * State
925 */
926 private static State intToState(int stateOrdinal) {
927 State state = State.WORKSPACE;
928 final State[] stateValues = State.values();
929 for (int i = 0; i < stateValues.length; i++) {
930 if (stateValues[i].ordinal() == stateOrdinal) {
931 state = stateValues[i];
932 break;
933 }
934 }
935 return state;
936 }
937
938 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 * Restores the previous state, if it exists.
940 *
941 * @param savedState The previous state.
942 */
943 private void restoreState(Bundle savedState) {
944 if (savedState == null) {
945 return;
946 }
947
Michael Jurka883f55b2010-10-21 15:47:14 -0700948 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700949 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700950 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800951 }
952
Winson Chungf0c6ae02012-03-21 16:10:31 -0700953 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700955 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700959 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700960
Winson Chung3d503fb2011-07-13 17:25:49 -0700961 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
962 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700963 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700964 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
965 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700966 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
967 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
968 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700969 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 mRestoring = true;
971 }
972
973 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
974 if (renameFolder) {
975 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700976 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 mRestoring = true;
978 }
Winson Chunga12a2502010-12-20 14:41:35 -0800979
Winson Chung785d2eb2011-04-14 16:08:02 -0700980 // Restore the AppsCustomize tab
981 if (mAppsCustomizeTabHost != null) {
982 String curTab = savedState.getString("apps_customize_currentTab");
983 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700984 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700985 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700986 mAppsCustomizeContent.loadAssociatedPages(
987 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700988 }
989
Winson Chung5afbf7b2011-07-25 11:53:08 -0700990 int currentIndex = savedState.getInt("apps_customize_currentIndex");
991 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 }
994
995 /**
996 * Finds all the views we need and configure them properly.
997 */
998 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700999 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001000
Craig Mautner360310b2012-10-26 15:13:08 -07001001 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001002 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1003 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001004 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001005
1006 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1007 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008
Winson Chung4c98d922011-05-31 16:50:48 -07001009 // Setup the drag layer
1010 mDragLayer.setup(this, dragController);
1011
Winson Chung3d503fb2011-07-13 17:25:49 -07001012 // Setup the hotseat
1013 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1014 if (mHotseat != null) {
1015 mHotseat.setup(this);
1016 }
1017
Winson Chung4c98d922011-05-31 16:50:48 -07001018 // Setup the workspace
1019 mWorkspace.setHapticFeedbackEnabled(false);
1020 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001021 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001022 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001023
Winson Chungf0ea4d32011-06-06 14:27:16 -07001024 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001025 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001026
Winson Chungf0ea4d32011-06-06 14:27:16 -07001027 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001028 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001029 mAppsCustomizeContent = (AppsCustomizePagedView)
1030 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1031 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001032
Winson Chung3d503fb2011-07-13 17:25:49 -07001033 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001034 dragController.setDragScoller(mWorkspace);
1035 dragController.setScrollView(mDragLayer);
1036 dragController.setMoveTarget(mWorkspace);
1037 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001038 if (mSearchDropTargetBar != null) {
1039 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001040 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001041
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001042 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001043 Log.v(TAG, "adding WeightWatcher");
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001044 final View ww = new WeightWatcher(this);
1045 ww.setAlpha(0.5f);
1046 ((FrameLayout) mLauncherView).addView(ww,
Daniel Sandler924b9932013-06-12 00:38:25 -04001047 new FrameLayout.LayoutParams(
1048 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001049 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001050 Gravity.BOTTOM)
1051 );
1052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054
1055 /**
1056 * Creates a view representing a shortcut.
1057 *
1058 * @param info The data structure describing the shortcut.
1059 *
1060 * @return A View inflated from R.layout.application.
1061 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001062 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001064 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066
1067 /**
1068 * Creates a view representing a shortcut inflated from the specified resource.
1069 *
1070 * @param layoutResId The id of the XML layout used to create the shortcut.
1071 * @param parent The group the shortcut belongs to.
1072 * @param info The data structure describing the shortcut.
1073 *
1074 * @return A View inflated from layoutResId.
1075 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001076 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001077 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1078 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 return favorite;
1081 }
1082
1083 /**
1084 * Add an application shortcut to the workspace.
1085 *
1086 * @param data The intent describing the application.
1087 * @param cellInfo The position on screen where to create the shortcut.
1088 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001089 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001091 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092
Adam Cohenfbba09b2011-07-18 21:43:05 -07001093 // First we check if we already know the exact location where we want to add this item.
1094 if (cellX >= 0 && cellY >= 0) {
1095 cellXY[0] = cellX;
1096 cellXY[1] = cellY;
1097 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001098 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001099 return;
1100 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001102 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001103
Romain Guy73b979d2009-06-09 12:57:21 -07001104 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001105 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001107 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001108 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001109 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001110 } else {
1111 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
1113 }
Romain Guycbb89e42009-06-08 15:52:54 -07001114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 /**
1116 * Add a shortcut to the workspace.
1117 *
1118 * @param data The intent describing the shortcut.
1119 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001121 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001122 int cellY) {
1123 int[] cellXY = mTmpAddItemCellCoordinates;
1124 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001125 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Michael Jurkad3ef3062010-11-23 16:23:58 -08001127 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001128
Adam Cohen558baaf2011-08-15 15:22:57 -07001129 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001130 if (info == null) {
1131 return;
1132 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001133 final View view = createShortcut(info);
1134
Adam Cohenfbba09b2011-07-18 21:43:05 -07001135 // First we check if we already know the exact location where we want to add this item.
1136 if (cellX >= 0 && cellY >= 0) {
1137 cellXY[0] = cellX;
1138 cellXY[1] = cellY;
1139 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001140
1141 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001142 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001143 true, null,null)) {
1144 return;
1145 }
1146 DragObject dragObject = new DragObject();
1147 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001148 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1149 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001150 return;
1151 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001152 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001153 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001154 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001155 foundCellSpan = (result != null);
1156 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001157 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001158 }
1159
1160 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001161 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 return;
1163 }
1164
Adam Cohendcd297f2013-06-18 13:13:40 -07001165 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166
1167 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001168 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001169 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171 }
1172
Adam Cohen2f093b62012-04-30 18:59:53 -07001173 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1174 int minHeight) {
1175 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001176 // We want to account for the extra amount of padding that we are adding to the widget
1177 // to ensure that it gets the full amount of space that it has requested
1178 int requiredWidth = minWidth + padding.left + padding.right;
1179 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001180 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001181 }
1182
Adam Cohen2f093b62012-04-30 18:59:53 -07001183 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1184 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001185 }
1186
Adam Cohen2f093b62012-04-30 18:59:53 -07001187 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1188 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001189 }
1190
Adam Cohen2f093b62012-04-30 18:59:53 -07001191 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1192 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001193 }
1194
Adam Cohen2f093b62012-04-30 18:59:53 -07001195 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1196 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001197 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001198 }
1199
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001201 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001203 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001206 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001207 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1208 if (appWidgetInfo == null) {
1209 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1210 }
Romain Guycbb89e42009-06-08 15:52:54 -07001211
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001212 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001213 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001214
Adam Cohen2f093b62012-04-30 18:59:53 -07001215 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1216 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001219 // We have saved the position to which the widget was dragged-- this really only matters
1220 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 int[] cellXY = mTmpAddItemCellCoordinates;
1222 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001223 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001225 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1226 cellXY[0] = mPendingAddInfo.cellX;
1227 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 spanXY[0] = mPendingAddInfo.spanX;
1229 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001230 foundCellSpan = true;
1231 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001232 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001233 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001234 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1235 spanXY[1], cellXY, finalSpan);
1236 spanXY[0] = finalSpan[0];
1237 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001238 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001240 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001241 }
1242
Michael Jurka0280c3b2010-09-17 15:00:07 -07001243 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001244 if (appWidgetId != -1) {
1245 // Deleting an app widget ID is a void call but writes to disk before returning
1246 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001247 new Thread("deleteAppWidgetId") {
1248 public void run() {
1249 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1250 }
1251 }.start();
1252 }
Winson Chung93eef082012-03-23 15:59:27 -07001253 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001254 return;
1255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001257 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001258 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1259 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001260 launcherInfo.spanX = spanXY[0];
1261 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001262 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1263 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001266 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267
1268 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001269 if (hostView == null) {
1270 // Perform actual inflation because we're live
1271 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1272 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1273 } else {
1274 // The AppWidgetHostView has already been inflated and instantiated
1275 launcherInfo.hostView = hostView;
1276 }
Romain Guycbb89e42009-06-08 15:52:54 -07001277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001279 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001280 launcherInfo.notifyWidgetSizeChanged(this);
1281
Adam Cohendcd297f2013-06-18 13:13:40 -07001282 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001283 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001284
1285 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001287 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 }
Romain Guycbb89e42009-06-08 15:52:54 -07001289
Adam Cohended9f8d2010-11-03 13:25:16 -07001290 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1291 @Override
1292 public void onReceive(Context context, Intent intent) {
1293 final String action = intent.getAction();
1294 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1295 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001296 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001297 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001298
Winson Chungc100e8e2011-08-09 16:02:43 -07001299 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001300 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001301 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1302 mAppsCustomizeTabHost.reset();
1303 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001305 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1306 mUserPresent = true;
1307 updateRunning();
1308 }
1309 }
1310 };
1311
1312 @Override
1313 public void onAttachedToWindow() {
1314 super.onAttachedToWindow();
1315
1316 // Listen for broadcasts related to user-presence
1317 final IntentFilter filter = new IntentFilter();
1318 filter.addAction(Intent.ACTION_SCREEN_OFF);
1319 filter.addAction(Intent.ACTION_USER_PRESENT);
1320 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001321 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001322 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001323 mVisible = true;
1324 }
1325
1326 @Override
1327 public void onDetachedFromWindow() {
1328 super.onDetachedFromWindow();
1329 mVisible = false;
1330
Adam Cohend113e0c2010-11-11 10:48:05 -08001331 if (mAttached) {
1332 unregisterReceiver(mReceiver);
1333 mAttached = false;
1334 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001335 updateRunning();
1336 }
1337
1338 public void onWindowVisibilityChanged(int visibility) {
1339 mVisible = visibility == View.VISIBLE;
1340 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001341 // The following code used to be in onResume, but it turns out onResume is called when
1342 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1343 // is a more appropriate event to handle
1344 if (mVisible) {
1345 mAppsCustomizeTabHost.onWindowVisible();
1346 if (!mWorkspaceLoading) {
1347 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001348 // We want to let Launcher draw itself at least once before we force it to build
1349 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001350 // apps is nice and speedy.
1351 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001352 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001353 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001354 if (mStarted) return;
1355 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001356 // We delay the layer building a bit in order to give
1357 // other message processing a time to run. In particular
1358 // this avoids a delay in hiding the IME if it was
1359 // currently shown, because doing that may involve
1360 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001361 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001362 final ViewTreeObserver.OnDrawListener listener = this;
1363 mWorkspace.post(new Runnable() {
1364 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001365 if (mWorkspace != null &&
1366 mWorkspace.getViewTreeObserver() != null) {
1367 mWorkspace.getViewTreeObserver().
1368 removeOnDrawListener(listener);
1369 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001370 }
1371 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001372 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001373 }
1374 });
1375 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001376 // When Launcher comes back to foreground, a different Activity might be responsible for
1377 // the app market intent, so refresh the icon
1378 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001379 clearTypedText();
1380 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001381 }
1382
1383 private void sendAdvanceMessage(long delay) {
1384 mHandler.removeMessages(ADVANCE_MSG);
1385 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1386 mHandler.sendMessageDelayed(msg, delay);
1387 mAutoAdvanceSentTime = System.currentTimeMillis();
1388 }
1389
1390 private void updateRunning() {
1391 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1392 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1393 mAutoAdvanceRunning = autoAdvanceRunning;
1394 if (autoAdvanceRunning) {
1395 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1396 sendAdvanceMessage(delay);
1397 } else {
1398 if (!mWidgetsToAdvance.isEmpty()) {
1399 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1400 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1401 }
1402 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001403 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001404 }
1405 }
1406 }
1407
1408 private final Handler mHandler = new Handler() {
1409 @Override
1410 public void handleMessage(Message msg) {
1411 if (msg.what == ADVANCE_MSG) {
1412 int i = 0;
1413 for (View key: mWidgetsToAdvance.keySet()) {
1414 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1415 final int delay = mAdvanceStagger * i;
1416 if (v instanceof Advanceable) {
1417 postDelayed(new Runnable() {
1418 public void run() {
1419 ((Advanceable) v).advance();
1420 }
1421 }, delay);
1422 }
1423 i++;
1424 }
1425 sendAdvanceMessage(mAdvanceInterval);
1426 }
1427 }
1428 };
1429
1430 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001431 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001432 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1433 if (v instanceof Advanceable) {
1434 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001435 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001436 updateRunning();
1437 }
1438 }
1439
1440 void removeWidgetToAutoAdvance(View hostView) {
1441 if (mWidgetsToAdvance.containsKey(hostView)) {
1442 mWidgetsToAdvance.remove(hostView);
1443 updateRunning();
1444 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001445 }
1446
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001448 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 launcherInfo.hostView = null;
1450 }
1451
Winson Chung93eef082012-03-23 15:59:27 -07001452 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1453 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1454 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001455 }
1456
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001457 public LauncherAppWidgetHost getAppWidgetHost() {
1458 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
Romain Guycbb89e42009-06-08 15:52:54 -07001460
Winson Chunga9abd0e2010-10-27 17:18:37 -07001461 public LauncherModel getModel() {
1462 return mModel;
1463 }
1464
Bjorn Bringertc459e522013-06-07 19:36:01 +01001465 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001466 getWindow().closeAllPanels();
1467
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001468 // Whatever we were doing is hereby canceled.
1469 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001470 }
1471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 @Override
1473 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001474 long startTime = 0;
1475 if (DEBUG_RESUME_TIME) {
1476 startTime = System.currentTimeMillis();
1477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 super.onNewIntent(intent);
1479
1480 // Close the menu
1481 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001482 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001483 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001484
Jamie Genniscb222e82012-10-23 15:44:26 -07001485 final boolean alreadyOnHome =
1486 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001487 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001488
Jamie Genniscb222e82012-10-23 15:44:26 -07001489 Runnable processIntent = new Runnable() {
1490 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001491 if (mWorkspace == null) {
1492 // Can be cases where mWorkspace is null, this prevents a NPE
1493 return;
1494 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001495 Folder openFolder = mWorkspace.getOpenFolder();
1496 // In all these cases, only animate if we're already on home
1497 mWorkspace.exitWidgetResizeMode();
1498 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1499 openFolder == null) {
1500 mWorkspace.moveToDefaultScreen(true);
1501 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001502
Jamie Genniscb222e82012-10-23 15:44:26 -07001503 closeFolder();
1504 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001505
Jamie Genniscb222e82012-10-23 15:44:26 -07001506 // If we are already on home, then just animate back to the workspace,
1507 // otherwise, just wait until onResume to set the state back to Workspace
1508 if (alreadyOnHome) {
1509 showWorkspace(true);
1510 } else {
1511 mOnResumeState = State.WORKSPACE;
1512 }
1513
1514 final View v = getWindow().peekDecorView();
1515 if (v != null && v.getWindowToken() != null) {
1516 InputMethodManager imm = (InputMethodManager)getSystemService(
1517 INPUT_METHOD_SERVICE);
1518 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1519 }
1520
1521 // Reset AllApps to its initial state
1522 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1523 mAppsCustomizeTabHost.reset();
1524 }
1525 }
1526 };
1527
1528 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1529 // Delay processing of the intent to allow the status bar animation to finish
1530 // first in order to avoid janky animations.
1531 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001532 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001533 // Process the intent immediately.
1534 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001535 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Michael Jurka447bf842013-05-15 14:52:15 +02001538 if (DEBUG_RESUME_TIME) {
1539 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
1543 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001544 public void onRestoreInstanceState(Bundle state) {
1545 super.onRestoreInstanceState(state);
1546 for (int page: mSynchronouslyBoundPages) {
1547 mWorkspace.restoreInstanceStateForChild(page);
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
1551 @Override
1552 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001553 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001554 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555
Michael Jurka883f55b2010-10-21 15:47:14 -07001556 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001557 // We close any open folder since it will not be re-opened, and we need to make sure
1558 // this state is reflected.
1559 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
Adam Cohendcd297f2013-06-18 13:13:40 -07001561 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001562 mWaitingForResult) {
1563 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001564 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001565 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1566 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001567 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1568 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1569 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
1572 if (mFolderInfo != null && mWaitingForResult) {
1573 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1574 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1575 }
Michael Jurka946ad472010-07-09 18:05:18 -07001576
Winson Chung785d2eb2011-04-14 16:08:02 -07001577 // Save the current AppsCustomize tab
1578 if (mAppsCustomizeTabHost != null) {
1579 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1580 if (currentTabTag != null) {
1581 outState.putString("apps_customize_currentTab", currentTabTag);
1582 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001583 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1584 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587
1588 @Override
1589 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001591
Winson Chunge7a03942011-08-05 15:05:12 -07001592 // Remove all pending runnables
1593 mHandler.removeMessages(ADVANCE_MSG);
1594 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001595 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001596
Winson Chungcd2b0142011-06-08 16:02:26 -07001597 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001598 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001599 mModel.stopLoader();
1600 app.setLauncher(null);
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001603 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001605 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001607 mAppWidgetHost = null;
1608
1609 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610
1611 TextKeyListener.getInstance().release();
1612
Winson Chung81b52252012-08-27 15:34:29 -07001613 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1614 // to prevent leaking Launcher activities on orientation change.
1615 if (mModel != null) {
1616 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1617 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001618
1619 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001620 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001621
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001622 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001623 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1624 mWorkspace.removeAllViews();
1625 mWorkspace = null;
1626 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001627
Michael Jurka2ecf9952012-06-18 12:52:28 -07001628 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630
Adam Cohena9cf38f2011-05-02 15:36:58 -07001631 public DragController getDragController() {
1632 return mDragController;
1633 }
1634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 @Override
1636 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001637 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 super.startActivityForResult(intent, requestCode);
1639 }
1640
Winson Chung70d72102011-08-12 11:24:35 -07001641 /**
1642 * Indicates that we want global search for this activity by setting the globalSearch
1643 * argument for {@link #startSearch} to true.
1644 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001646 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001648
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001649 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001650
Karl Rosaen138a0412009-04-23 19:00:21 -07001651 if (initialQuery == null) {
1652 // Use any text typed in the launcher as the initial query
1653 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 if (appSearchData == null) {
1656 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001657 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
Winson Chungadf0c182012-08-23 14:59:07 -07001659 Rect sourceBounds = new Rect();
1660 if (mSearchDropTargetBar != null) {
1661 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1662 }
Romain Guycbb89e42009-06-08 15:52:54 -07001663
Bjorn Bringertc459e522013-06-07 19:36:01 +01001664 startSearch(initialQuery, selectInitialQuery,
1665 appSearchData, sourceBounds);
1666 }
1667
1668 public void startSearch(String initialQuery,
1669 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001670 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001671 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001672 }
1673
1674 /**
1675 * Starts the global search activity. This code is a copied from SearchManager
1676 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001677 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001678 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001679 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001680 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1681 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1682 if (globalSearchActivity == null) {
1683 Log.w(TAG, "No global search activity found.");
1684 return;
1685 }
1686 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1687 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1688 intent.setComponent(globalSearchActivity);
1689 // Make sure that we have a Bundle to put source in
1690 if (appSearchData == null) {
1691 appSearchData = new Bundle();
1692 } else {
1693 appSearchData = new Bundle(appSearchData);
1694 }
1695 // Set source to package name of app that starts global search, if not set already.
1696 if (!appSearchData.containsKey("source")) {
1697 appSearchData.putString("source", getPackageName());
1698 }
1699 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1700 if (!TextUtils.isEmpty(initialQuery)) {
1701 intent.putExtra(SearchManager.QUERY, initialQuery);
1702 }
1703 if (selectInitialQuery) {
1704 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1705 }
1706 intent.setSourceBounds(sourceBounds);
1707 try {
1708 startActivity(intent);
1709 } catch (ActivityNotFoundException ex) {
1710 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 }
1713
Winson Chung70d72102011-08-12 11:24:35 -07001714 @Override
1715 public boolean onCreateOptionsMenu(Menu menu) {
1716 if (isWorkspaceLocked()) {
1717 return false;
1718 }
1719
1720 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001721
1722 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1723 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1724 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001725 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1726 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1727 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001728 String helpUrl = getString(R.string.help_url);
1729 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001730 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1731 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1732
Winson Chung70d72102011-08-12 11:24:35 -07001733 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1734 .setIcon(android.R.drawable.ic_menu_gallery)
1735 .setAlphabeticShortcut('W');
1736 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1737 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001738 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001739 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001740 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1741 .setIcon(android.R.drawable.ic_menu_preferences)
1742 .setIntent(settings)
1743 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001744 if (!helpUrl.isEmpty()) {
1745 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1746 .setIcon(android.R.drawable.ic_menu_help)
1747 .setIntent(help)
1748 .setAlphabeticShortcut('H');
1749 }
Winson Chung70d72102011-08-12 11:24:35 -07001750 return true;
1751 }
1752
1753 @Override
1754 public boolean onPrepareOptionsMenu(Menu menu) {
1755 super.onPrepareOptionsMenu(menu);
1756
1757 if (mAppsCustomizeTabHost.isTransitioning()) {
1758 return false;
1759 }
1760 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1761 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1762
1763 return true;
1764 }
1765
1766 @Override
1767 public boolean onOptionsItemSelected(MenuItem item) {
1768 switch (item.getItemId()) {
1769 case MENU_WALLPAPER_SETTINGS:
1770 startWallpaper();
1771 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001772 }
1773
1774 return super.onOptionsItemSelected(item);
1775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001777 @Override
1778 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001779 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001780 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001781 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001782 }
1783
Joe Onorato9c1289c2009-08-17 11:03:03 -04001784 public boolean isWorkspaceLocked() {
1785 return mWorkspaceLoading || mWaitingForResult;
1786 }
1787
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001789 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001790 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001791 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1792 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001793 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001794 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001795 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001796
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001797 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1798 AppWidgetProviderInfo appWidgetInfo) {
1799 if (appWidgetInfo.configure != null) {
1800 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001801
Bjorn Bringert7984c942009-12-09 15:38:25 +00001802 // Launch over to configure widget, if needed
1803 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001804 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001805 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001806 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001808 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001809 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001810 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001811 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001812 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814 }
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Adam Cohenfbba09b2011-07-18 21:43:05 -07001816 /**
1817 * Process a shortcut drop.
1818 *
1819 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001820 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001821 * @param cell The cell it should be added to, optional
1822 * @param position The location on the screen where it was dropped, optional
1823 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001824 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001825 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001826 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001827 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001828 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001829 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001830
1831 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001832 mPendingAddInfo.cellX = cell[0];
1833 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001834 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835
1836 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1837 createShortcutIntent.setComponent(componentName);
1838 processShortcut(createShortcutIntent);
1839 }
1840
Adam Cohenfbba09b2011-07-18 21:43:05 -07001841 /**
1842 * Process a widget drop.
1843 *
1844 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001845 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001846 * @param cell The cell it should be added to, optional
1847 * @param position The location on the screen where it was dropped, optional
1848 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001849 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001850 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001851 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001852 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001853 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001854 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001855 mPendingAddInfo.minSpanX = info.minSpanX;
1856 mPendingAddInfo.minSpanY = info.minSpanY;
1857
Adam Cohenfbba09b2011-07-18 21:43:05 -07001858 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001859 mPendingAddInfo.cellX = cell[0];
1860 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001861 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001862 if (span != null) {
1863 mPendingAddInfo.spanX = span[0];
1864 mPendingAddInfo.spanY = span[1];
1865 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001866
Adam Cohened66b2b2012-01-23 17:28:51 -08001867 AppWidgetHostView hostView = info.boundWidget;
1868 int appWidgetId;
1869 if (hostView != null) {
1870 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001871 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001872 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001873 // In this case, we either need to start an activity to get permission to bind
1874 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001875 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001876 Bundle options = info.bindOptions;
1877
1878 boolean success = false;
1879 if (options != null) {
1880 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1881 info.componentName, options);
1882 } else {
1883 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1884 info.componentName);
1885 }
1886 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001887 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001888 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001889 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001890 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1891 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1892 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001893 // TODO: we need to make sure that this accounts for the options bundle.
1894 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001895 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1896 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001897 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001898 }
1899
Joe Onoratodeb98af2010-02-19 14:59:39 -08001900 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001901 // Handle case where user selected "Applications"
1902 String applicationName = getResources().getString(R.string.group_applications);
1903 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001904
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001905 if (applicationName != null && applicationName.equals(shortcutName)) {
1906 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1907 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001908
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001909 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1910 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001911 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001912 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001913 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001914 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
Winson Chung24ab2f12010-09-16 14:10:47 -07001918 void processWallpaper(Intent intent) {
1919 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1920 }
1921
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001924 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 folderInfo.title = getText(R.string.folder_name);
1926
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001928 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001930 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931
1932 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001933 FolderIcon newFolder =
1934 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001935 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001936 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001937 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 }
Romain Guycbb89e42009-06-08 15:52:54 -07001939
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001941 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 }
1943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001945 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001947 Intent chooser = Intent.createChooser(pickWallpaper,
1948 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001949 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1950 // Removed in Eclair MR1
1951// WallpaperManager wm = (WallpaperManager)
1952// getSystemService(Context.WALLPAPER_SERVICE);
1953// WallpaperInfo wi = wm.getWallpaperInfo();
1954// if (wi != null && wi.getSettingsActivity() != null) {
1955// LabeledIntent li = new LabeledIntent(getPackageName(),
1956// R.string.configure_wallpaper, 0);
1957// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1958// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1959// }
Mike Clerona0618e42009-10-22 13:55:21 -07001960 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001963 /**
1964 * Registers various content observers. The current implementation registers
1965 * only a favorites observer to keep track of the favorites applications.
1966 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001967 private void registerContentObservers() {
1968 ContentResolver resolver = getContentResolver();
1969 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1970 true, mWidgetObserver);
1971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 @Override
1974 public boolean dispatchKeyEvent(KeyEvent event) {
1975 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1976 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001978 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001979 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001980 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001981 dumpState();
1982 return true;
1983 }
1984 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001985 }
1986 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1987 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001988 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 return true;
1990 }
1991 }
1992
1993 return super.dispatchKeyEvent(event);
1994 }
1995
Joe Onorato88ec0992009-11-19 13:16:06 -08001996 @Override
1997 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001998 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001999 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002000 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002001 Folder openFolder = mWorkspace.getOpenFolder();
2002 if (openFolder.isEditingName()) {
2003 openFolder.dismissEditingName();
2004 } else {
2005 closeFolder();
2006 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002007 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002008 mWorkspace.exitWidgetResizeMode();
2009
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002010 // Back button is a no-op here, but give at least some feedback for the button press
2011 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002012 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002013 }
2014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002016 * Re-listen when widgets are reset.
2017 */
2018 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002019 if (mAppWidgetHost != null) {
2020 mAppWidgetHost.startListening();
2021 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002022 }
2023
2024 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 * Launches the intent referred by the clicked shortcut.
2026 *
2027 * @param v The view representing the clicked shortcut.
2028 */
2029 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002030 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2031 // view has detached (it's possible for this to happen if the view is removed mid touch).
2032 if (v.getWindowToken() == null) {
2033 return;
2034 }
2035
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002036 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002037 return;
2038 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002041 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002043 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002044
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002045 // Check for special shortcuts
2046 if (intent.getComponent() != null) {
2047 final String shortcutClass = intent.getComponent().getClassName();
2048
2049 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2050 showAllApps(true);
2051 return;
2052 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2053 MemoryDumpActivity.startDump(this);
2054 return;
2055 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002056 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002057
2058 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002059 int[] pos = new int[2];
2060 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002061 intent.setSourceBounds(new Rect(pos[0], pos[1],
2062 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002063
2064 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002065
2066 if (success && v instanceof BubbleTextView) {
2067 mWaitingForResume = (BubbleTextView) v;
2068 mWaitingForResume.setStayPressed(true);
2069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002071 if (v instanceof FolderIcon) {
2072 FolderIcon fi = (FolderIcon) v;
2073 handleFolderClick(fi);
2074 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002075 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002076 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002077 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002078 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002079 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
2082 }
2083
Michael Jurka0e260592010-06-30 17:07:39 -07002084 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002085 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002086 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002087 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2088 showWorkspace(true);
2089 }
Michael Jurka0e260592010-06-30 17:07:39 -07002090 return false;
2091 }
2092
Michael Jurkaaf442092010-06-10 17:01:57 -07002093 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002094 * Event handler for the search button
2095 *
2096 * @param v The view that was clicked.
2097 */
2098 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002099 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2100
Amith Yamasania135ba82011-08-09 17:42:01 -07002101 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002102 }
2103
2104 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002105 * Event handler for the voice button
2106 *
2107 * @param v The view that was clicked.
2108 */
2109 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002110 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002111
Bjorn Bringertc459e522013-06-07 19:36:01 +01002112 startVoice();
2113 }
2114
2115 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002116 try {
2117 final SearchManager searchManager =
2118 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2119 ComponentName activityName = searchManager.getGlobalSearchActivity();
2120 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002121 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002122 if (activityName != null) {
2123 intent.setPackage(activityName.getPackageName());
2124 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002125 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002126 } catch (ActivityNotFoundException e) {
2127 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002128 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002129 startActivitySafely(null, intent, "onClickVoiceButton");
2130 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002131 }
2132
2133 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002134 * Event handler for the "grid" button that appears on the home screen, which
2135 * enters all apps mode.
2136 *
2137 * @param v The view that was clicked.
2138 */
2139 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002140 showAllApps(true);
2141 }
2142
2143 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002144 // Provide the same haptic feedback that the system offers for virtual keys.
2145 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002146 }
2147
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002148 public void onClickAppMarketButton(View v) {
2149 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002150 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002151 } else {
2152 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002153 }
2154 }
2155
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002156 void startApplicationDetailsActivity(ComponentName componentName) {
2157 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002158 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2159 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002160 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002161 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002162 }
2163
Michael Jurka1e2f4652013-07-08 18:03:46 -07002164 // returns true if the activity was started
2165 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2166 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002167 // System applications cannot be installed. For now, show a toast explaining that.
2168 // We may give them the option of disabling apps this way.
2169 int messageId = R.string.uninstall_system_app_text;
2170 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002171 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002172 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002173 String packageName = componentName.getPackageName();
2174 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002175 Intent intent = new Intent(
2176 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002177 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2178 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002179 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002180 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002181 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002182 }
2183
Michael Jurka86a720e2012-05-09 11:23:23 -07002184 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002188 // Only launch using the new animation if the shortcut has not opted out (this is a
2189 // private contract between launcher and may be ignored in the future).
2190 boolean useLaunchAnimation = (v != null) &&
2191 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2192 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002193 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2194 v.getMeasuredWidth(), v.getMeasuredHeight());
2195
2196 startActivity(intent, opts.toBundle());
2197 } else {
2198 startActivity(intent);
2199 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002200 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 } catch (SecurityException e) {
2202 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002203 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002205 "or use the exported attribute for this activity. "
2206 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002208 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002210
Michael Jurka86a720e2012-05-09 11:23:23 -07002211 boolean startActivitySafely(View v, Intent intent, Object tag) {
2212 boolean success = false;
2213 try {
2214 success = startActivity(v, intent, tag);
2215 } catch (ActivityNotFoundException e) {
2216 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2217 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2218 }
2219 return success;
2220 }
2221
Romain Guy8e633c52010-03-04 12:51:36 -08002222 void startActivityForResultSafely(Intent intent, int requestCode) {
2223 try {
2224 startActivityForResult(intent, requestCode);
2225 } catch (ActivityNotFoundException e) {
2226 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2227 } catch (SecurityException e) {
2228 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2229 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2230 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2231 "or use the exported attribute for this activity.", e);
2232 }
2233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234
Adam Cohena9cf38f2011-05-02 15:36:58 -07002235 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002236 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002237 Folder openFolder = mWorkspace.getFolderForTag(info);
2238
2239 // If the folder info reports that the associated folder is open, then verify that
2240 // it is actually opened. There have been a few instances where this gets out of sync.
2241 if (info.opened && openFolder == null) {
2242 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002244 info.opened = false;
2245 }
2246
Adam Cohenfb91f302012-06-11 15:45:18 -07002247 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 // Close any open folder
2249 closeFolder();
2250 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002251 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 } else {
2253 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 int folderScreen;
2255 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002256 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 // .. and close it
2258 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002259 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 // Close any folder open on the current screen
2261 closeFolder();
2262 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002263 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 }
2265 }
2266 }
2267 }
2268
Adam Cohen268c4752012-06-06 17:47:33 -07002269 /**
2270 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2271 * in the DragLayer in the exact absolute location of the original FolderIcon.
2272 */
2273 private void copyFolderIconToImage(FolderIcon fi) {
2274 final int width = fi.getMeasuredWidth();
2275 final int height = fi.getMeasuredHeight();
2276
2277 // Lazy load ImageView, Bitmap and Canvas
2278 if (mFolderIconImageView == null) {
2279 mFolderIconImageView = new ImageView(this);
2280 }
2281 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2282 mFolderIconBitmap.getHeight() != height) {
2283 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2284 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2285 }
2286
2287 DragLayer.LayoutParams lp;
2288 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2289 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2290 } else {
2291 lp = new DragLayer.LayoutParams(width, height);
2292 }
2293
Adam Cohen307fe232012-08-16 17:55:58 -07002294 // The layout from which the folder is being opened may be scaled, adjust the starting
2295 // view size by this scale factor.
2296 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002297 lp.customPosition = true;
2298 lp.x = mRectForFolderAnimation.left;
2299 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002300 lp.width = (int) (scale * width);
2301 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002302
2303 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2304 fi.draw(mFolderIconCanvas);
2305 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002306 if (fi.getFolder() != null) {
2307 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2308 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002309 }
Adam Cohen268c4752012-06-06 17:47:33 -07002310 // Just in case this image view is still in the drag layer from a previous animation,
2311 // we remove it and re-add it.
2312 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2313 mDragLayer.removeView(mFolderIconImageView);
2314 }
2315 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002316 if (fi.getFolder() != null) {
2317 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002318 }
Adam Cohen268c4752012-06-06 17:47:33 -07002319 }
2320
Adam Cohen2801caf2011-05-13 20:57:39 -07002321 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002322 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002323 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2324 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2325 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2326
Adam Cohenc51934b2011-07-26 21:07:43 -07002327 FolderInfo info = (FolderInfo) fi.getTag();
2328 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2329 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002330 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2331 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002332 }
2333
Adam Cohen268c4752012-06-06 17:47:33 -07002334 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2335 copyFolderIconToImage(fi);
2336 fi.setVisibility(View.INVISIBLE);
2337
Michael Jurka2ecf9952012-06-18 12:52:28 -07002338 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002339 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002340 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2341 oa.start();
2342 }
2343
Adam Cohen268c4752012-06-06 17:47:33 -07002344 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002345 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002346 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2347 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2348 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2349
Adam Cohen268c4752012-06-06 17:47:33 -07002350 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002351
Adam Cohen268c4752012-06-06 17:47:33 -07002352 // We remove and re-draw the FolderIcon in-case it has changed
2353 mDragLayer.removeView(mFolderIconImageView);
2354 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002355 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002356 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002357 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002358 oa.addListener(new AnimatorListenerAdapter() {
2359 @Override
2360 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002361 if (cl != null) {
2362 cl.clearFolderLeaveBehind();
2363 // Remove the ImageView copy of the FolderIcon and make the original visible.
2364 mDragLayer.removeView(mFolderIconImageView);
2365 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002366 }
2367 }
2368 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002369 oa.start();
2370 }
2371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002373 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 * is animated relative to the specified View. If the View is null, no animation
2375 * is played.
2376 *
2377 * @param folderInfo The FolderInfo describing the folder to open.
2378 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002379 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002380 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002381 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382
Adam Cohena9cf38f2011-05-02 15:36:58 -07002383 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384
Adam Cohen4554ee12011-08-03 16:13:21 -07002385 // Just verify that the folder hasn't already been added to the DragLayer.
2386 // There was a one-off crash where the folder had a parent already.
2387 if (folder.getParent() == null) {
2388 mDragLayer.addView(folder);
2389 mDragController.addDropTarget((DropTarget) folder);
2390 } else {
2391 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2392 folder.getParent() + ").");
2393 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002394 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002395 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002396
2397 // Notify the accessibility manager that this folder "window" has appeared and occluded
2398 // the workspace items
2399 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2400 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002401 }
2402
2403 public void closeFolder() {
2404 Folder folder = mWorkspace.getOpenFolder();
2405 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002406 if (folder.isEditingName()) {
2407 folder.dismissEditingName();
2408 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002409 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002410
2411 // Dismiss the folder cling
2412 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002413 }
2414 }
2415
2416 void closeFolder(Folder folder) {
2417 folder.getInfo().opened = false;
2418
2419 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2420 if (parent != null) {
2421 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2422 shrinkAndFadeInFolderIcon(fi);
2423 }
2424 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002425
2426 // Notify the accessibility manager that this folder "window" has disappeard and no
2427 // longer occludeds the workspace items
2428 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 }
2430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002432 if (!isDraggingEnabled()) return false;
2433 if (isWorkspaceLocked()) return false;
2434 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435
2436 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002437 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
2439
Michael Jurka0280c3b2010-09-17 15:00:07 -07002440 resetAddInfo();
2441 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002443 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 return true;
2445 }
2446
Winson Chung3d503fb2011-07-13 17:25:49 -07002447 // The hotseat touch handling does not go through Workspace, and we always allow long press
2448 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002449 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002450 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2451 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002452 if (itemUnderLongClick == null) {
2453 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002454 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2455 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002456 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002457 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2458 mWorkspace.startReordering();
2459 } else {
2460 startWallpaper();
2461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002462 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002463 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002465 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 }
2467 }
2468 }
2469 return true;
2470 }
2471
Winson Chung3d503fb2011-07-13 17:25:49 -07002472 boolean isHotseatLayout(View layout) {
2473 return mHotseat != null && layout != null &&
2474 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2475 }
2476 Hotseat getHotseat() {
2477 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002478 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002479 SearchDropTargetBar getSearchBar() {
2480 return mSearchDropTargetBar;
2481 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002482
Winson Chung3d503fb2011-07-13 17:25:49 -07002483 /**
2484 * Returns the CellLayout of the specified container at the specified screen.
2485 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002486 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002487 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2488 if (mHotseat != null) {
2489 return mHotseat.getLayout();
2490 } else {
2491 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002492 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002493 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002494 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002495 }
2496 }
2497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 Workspace getWorkspace() {
2499 return mWorkspace;
2500 }
2501
Daniel Sandler843e8602010-06-07 14:59:01 -04002502 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002503 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002504 }
2505
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002506 /**
2507 * Helper method for the cameraZoomIn/cameraZoomOut animations
2508 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002509 * @param scaleFactor The scale factor used for the zoom
2510 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002511 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002512 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002513 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002515
Winson Chung18f41f82012-05-09 13:28:10 -07002516 void disableWallpaperIfInAllApps() {
2517 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002518 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002519 if (mAppsCustomizeTabHost != null &&
2520 !mAppsCustomizeTabHost.isTransitioning()) {
2521 updateWallpaperVisibility(false);
2522 }
2523 }
2524 }
2525
Craig Mautner360310b2012-10-26 15:13:08 -07002526 private void setWorkspaceBackground(boolean workspace) {
2527 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002528 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002529 }
2530
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002531 void updateWallpaperVisibility(boolean visible) {
2532 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2533 int curflags = getWindow().getAttributes().flags
2534 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2535 if (wpflags != curflags) {
2536 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2537 }
Craig Mautner360310b2012-10-26 15:13:08 -07002538 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002539 }
2540
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002541 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2542 if (v instanceof LauncherTransitionable) {
2543 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2544 }
2545 }
2546
Michael Jurkabed61d22012-02-14 22:51:29 -08002547 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2548 if (v instanceof LauncherTransitionable) {
2549 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2550 }
Winson Chung70442722012-02-10 15:43:22 -08002551
2552 // Update the workspace transition step as well
2553 dispatchOnLauncherTransitionStep(v, 0f);
2554 }
2555
2556 private void dispatchOnLauncherTransitionStep(View v, float t) {
2557 if (v instanceof LauncherTransitionable) {
2558 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2559 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002560 }
2561
2562 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2563 if (v instanceof LauncherTransitionable) {
2564 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2565 }
Winson Chung70442722012-02-10 15:43:22 -08002566
2567 // Update the workspace transition step as well
2568 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002569 }
2570
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002571 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002572 * Things to test when changing the following seven functions.
2573 * - Home from workspace
2574 * - from center screen
2575 * - from other screens
2576 * - Home from all apps
2577 * - from center screen
2578 * - from other screens
2579 * - Back from all apps
2580 * - from center screen
2581 * - from other screens
2582 * - Launch app from workspace and quit
2583 * - with back
2584 * - with home
2585 * - Launch app from all apps and quit
2586 * - with back
2587 * - with home
2588 * - Go to a screen that's not the default, then all
2589 * apps, and launch and app, and go back
2590 * - with back
2591 * -with home
2592 * - On workspace, long press power and go back
2593 * - with back
2594 * - with home
2595 * - On all apps, long press power and go back
2596 * - with back
2597 * - with home
2598 * - On workspace, power off
2599 * - On all apps, power off
2600 * - Launch an app and turn off the screen while in that app
2601 * - Go back with home key
2602 * - Go back with back key TODO: make this not go to workspace
2603 * - From all apps
2604 * - From workspace
2605 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2606 * - From all apps
2607 * - From the center workspace
2608 * - From another workspace
2609 */
2610
2611 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002612 * Zoom the camera out from the workspace to reveal 'toView'.
2613 * Assumes that the view to show is anchored at either the very top or very bottom
2614 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002616 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002617 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002618 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002619 mStateAnimation.cancel();
2620 mStateAnimation = null;
2621 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002622 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002623
Winson Chungf0ea4d32011-06-06 14:27:16 -07002624 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2625 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2626 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002627 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002628 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002629 final int startDelay =
2630 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002631
Michael Jurkab3e22d92011-10-31 15:58:33 -07002632 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633
Michael Jurkad74c9842011-07-10 12:44:21 -07002634 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002635 Animator workspaceAnim =
2636 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002637
2638 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002639 toView.setScaleX(scale);
2640 toView.setScaleY(scale);
2641 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2642 scaleAnim.
2643 scaleX(1f).scaleY(1f).
2644 setDuration(duration).
2645 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002646
Winson Chungf0ea4d32011-06-06 14:27:16 -07002647 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002648 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002649 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002650 .ofFloat(toView, "alpha", 0f, 1f)
2651 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002652 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002653 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2654 @Override
2655 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002656 if (animation == null) {
2657 throw new RuntimeException("animation is null");
2658 }
Winson Chung70442722012-02-10 15:43:22 -08002659 float t = (Float) animation.getAnimatedValue();
2660 dispatchOnLauncherTransitionStep(fromView, t);
2661 dispatchOnLauncherTransitionStep(toView, t);
2662 }
2663 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002664
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002665 // toView should appear right at the end of the workspace shrink
2666 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002667 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002668 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002669 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002670
2671 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002672 boolean animationCancelled = false;
2673
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002674 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002675 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002676 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002677 // Prepare the position
2678 toView.setTranslationX(0.0f);
2679 toView.setTranslationY(0.0f);
2680 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002681 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002682 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002683 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002684 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002685 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2686 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002687
Daniel Sandlere4f98912013-06-25 15:13:26 -04002688 if (mWorkspace != null
2689 && !springLoaded
2690 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002691 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002692 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002693 if (!animationCancelled) {
2694 updateWallpaperVisibility(false);
2695 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002696
2697 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002698 if (mSearchDropTargetBar != null) {
2699 mSearchDropTargetBar.hideSearchBar(false);
2700 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002701 }
2702
Adam Cohencff6af82011-09-13 14:51:53 -07002703 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002704 public void onAnimationCancel(Animator animation) {
2705 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002706 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002707 });
2708
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002709 if (workspaceAnim != null) {
2710 mStateAnimation.play(workspaceAnim);
2711 }
Michael Jurka1899a362011-11-03 13:50:45 -07002712
2713 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002715 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2716 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002717
2718 // If any of the objects being animated haven't been measured/laid out
2719 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002720 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002721 (mWorkspace.getMeasuredWidth() == 0) ||
2722 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002723 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002724 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002725
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002726 final AnimatorSet stateAnimation = mStateAnimation;
2727 final Runnable startAnimRunnable = new Runnable() {
2728 public void run() {
2729 // Check that mStateAnimation hasn't changed while
2730 // we waited for a layout/draw pass
2731 if (mStateAnimation != stateAnimation)
2732 return;
2733 setPivotsForZoom(toView, scale);
2734 dispatchOnLauncherTransitionStart(fromView, animated, false);
2735 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002736 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002737 }
2738 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002739 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002740 final ViewTreeObserver observer = toView.getViewTreeObserver();
2741 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2742 public void onGlobalLayout() {
2743 startAnimRunnable.run();
2744 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2745 }
2746 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002747 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002748 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002749 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002750 } else {
2751 toView.setTranslationX(0.0f);
2752 toView.setTranslationY(0.0f);
2753 toView.setScaleX(1.0f);
2754 toView.setScaleY(1.0f);
2755 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002756 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002757
Daniel Sandlere4f98912013-06-25 15:13:26 -04002758 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002759 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002760
2761 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002762 if (mSearchDropTargetBar != null) {
2763 mSearchDropTargetBar.hideSearchBar(false);
2764 }
Michael Jurkaabded662011-03-04 12:06:57 -08002765 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002766 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002767 dispatchOnLauncherTransitionStart(fromView, animated, false);
2768 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002769 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002770 dispatchOnLauncherTransitionStart(toView, animated, false);
2771 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002772 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002774 }
2775
2776 /**
2777 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002778 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002780 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002781 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2782 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002783
Michael Jurkab3e22d92011-10-31 15:58:33 -07002784 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002785 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002786 mStateAnimation.cancel();
2787 mStateAnimation = null;
2788 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002789 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002790
Winson Chungf0ea4d32011-06-06 14:27:16 -07002791 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002792 final int fadeOutDuration =
2793 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002794 final float scaleFactor = (float)
2795 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2796 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002797 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002798 Animator workspaceAnim = null;
2799
2800 if (toState == State.WORKSPACE) {
2801 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2802 workspaceAnim = mWorkspace.getChangeStateAnimation(
2803 Workspace.State.NORMAL, animated, stagger);
2804 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2805 workspaceAnim = mWorkspace.getChangeStateAnimation(
2806 Workspace.State.SPRING_LOADED, animated);
2807 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002808
Michael Jurkab3e22d92011-10-31 15:58:33 -07002809 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002810 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002811 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002813 final LauncherViewPropertyAnimator scaleAnim =
2814 new LauncherViewPropertyAnimator(fromView);
2815 scaleAnim.
2816 scaleX(scaleFactor).scaleY(scaleFactor).
2817 setDuration(duration).
2818 setInterpolator(new Workspace.ZoomInInterpolator());
2819
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002820 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002821 .ofFloat(fromView, "alpha", 1f, 0f)
2822 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002823 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002824 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2825 @Override
2826 public void onAnimationUpdate(ValueAnimator animation) {
2827 float t = 1f - (Float) animation.getAnimatedValue();
2828 dispatchOnLauncherTransitionStep(fromView, t);
2829 dispatchOnLauncherTransitionStep(toView, t);
2830 }
2831 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002832
Michael Jurka2ecf9952012-06-18 12:52:28 -07002833 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002834
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002835 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2836 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002837 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002838
2839 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002840 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002841 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002842 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002843 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002844 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2845 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002846 if (onCompleteRunnable != null) {
2847 onCompleteRunnable.run();
2848 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002849 mAppsCustomizeContent.updateCurrentPageScroll();
2850 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002851 }
2852 });
2853
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002855 if (workspaceAnim != null) {
2856 mStateAnimation.play(workspaceAnim);
2857 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002858 dispatchOnLauncherTransitionStart(fromView, animated, true);
2859 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002860 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 } else {
2862 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002864 dispatchOnLauncherTransitionStart(fromView, animated, true);
2865 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002866 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 dispatchOnLauncherTransitionStart(toView, animated, true);
2868 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002870 }
2871
Michael Jurkae326f182011-11-21 14:05:46 -08002872 @Override
2873 public void onTrimMemory(int level) {
2874 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002875 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002876 mAppsCustomizeTabHost.onTrimMemory();
2877 }
2878 }
2879
Winson Chung18f41f82012-05-09 13:28:10 -07002880 @Override
2881 public void onWindowFocusChanged(boolean hasFocus) {
2882 if (!hasFocus) {
2883 // When another window occludes launcher (like the notification shade, or recents),
2884 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2885 updateWallpaperVisibility(true);
2886 } else {
2887 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002888 mWorkspace.postDelayed(new Runnable() {
2889 @Override
2890 public void run() {
2891 disableWallpaperIfInAllApps();
2892 }
2893 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002894 }
2895 }
2896
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002897 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002898 showWorkspace(animated, null);
2899 }
2900
2901 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002902 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002903 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002904 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002905 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002906
Winson Chungc7d2b602012-05-16 17:02:20 -07002907 // Show the search bar (only animate if we were showing the drop target bar in spring
2908 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002909 if (mSearchDropTargetBar != null) {
2910 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2911 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002912
Michael Jurkab737ee62011-11-15 15:57:22 -08002913 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002914 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002915
2916 // Set focus to the AppsCustomize button
2917 if (mAllAppsButton != null) {
2918 mAllAppsButton.requestFocus();
2919 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002920 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002921
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002922 // Change the state *after* we've called all the transition code
2923 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002924
Winson Chung5fb63472011-02-02 17:03:37 -08002925 // Resume the auto-advance of widgets
2926 mUserPresent = true;
2927 updateRunning();
2928
alanv1d4fde62012-10-17 13:15:47 -07002929 // Send an accessibility event to announce the context change
2930 getWindow().getDecorView()
2931 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002932
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002933 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002934 }
2935
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002936 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002937 }
2938
Michael Jurkab3e22d92011-10-31 15:58:33 -07002939 void showAllApps(boolean animated) {
2940 if (mState != State.WORKSPACE) return;
2941
2942 showAppsCustomizeHelper(animated, false);
2943 mAppsCustomizeTabHost.requestFocus();
2944
Michael Jurkab3e22d92011-10-31 15:58:33 -07002945 // Change the state *after* we've called all the transition code
2946 mState = State.APPS_CUSTOMIZE;
2947
2948 // Pause the auto-advance of widgets until we are out of AllApps
2949 mUserPresent = false;
2950 updateRunning();
2951 closeFolder();
2952
2953 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002954 getWindow().getDecorView()
2955 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002956 }
2957
Adam Cohen7777d962011-08-18 18:58:38 -07002958 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002959 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002960 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002961 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002962 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002963 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002964 }
Adam Cohen7777d962011-08-18 18:58:38 -07002965
Adam Cohened66b2b2012-01-23 17:28:51 -08002966 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2967 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002968 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2969
Winson Chunge7a03942011-08-05 15:05:12 -07002970 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002971 @Override
2972 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002973 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002974 // Before we show workspace, hide all apps again because
2975 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2976 // clean up our state transition functions
2977 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002978 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002979 } else {
2980 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002981 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002982 }
2983 }, (extendedDelay ?
2984 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2985 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2986 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002987
Michael Jurkad3ef3062010-11-23 16:23:58 -08002988 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002989 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002990 final boolean animated = true;
2991 final boolean springLoaded = true;
2992 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002993 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002994 }
2995 // Otherwise, we are not in spring loaded mode, so don't do anything.
2996 }
2997
Michael Jurkab737ee62011-11-15 15:57:22 -08002998 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07002999 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003000 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003001 }
3002 }
3003
3004 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003005 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003006 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003007 if (mDividerAnimator != null) {
3008 mDividerAnimator.cancel();
3009 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003010 mDividerAnimator = null;
3011 }
3012 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003013 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003014 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003015 int duration = 0;
3016 if (mSearchDropTargetBar != null) {
3017 duration = mSearchDropTargetBar.getTransitionInDuration();
3018 }
3019 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003020 mDividerAnimator.start();
3021 }
3022 }
3023 }
3024
Michael Jurkab3e22d92011-10-31 15:58:33 -07003025 void lockAllApps() {
3026 // TODO
3027 }
3028
3029 void unlockAllApps() {
3030 // TODO
3031 }
3032
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003035 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003036 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003037 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003039 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003040 int duration = 0;
3041 if (mSearchDropTargetBar != null) {
3042 duration = mSearchDropTargetBar.getTransitionInDuration();
3043 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003044 mHotseat.animate().alpha(1f).setDuration(duration);
3045 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003046 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003047 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003048 }
3049 }
3050 }
3051
3052 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003053 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003054 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003055 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003056 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003057 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003058 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003059 int duration = 0;
3060 if (mSearchDropTargetBar != null) {
3061 duration = mSearchDropTargetBar.getTransitionOutDuration();
3062 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003063 mHotseat.animate().alpha(0f).setDuration(duration);
3064 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003065 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003066 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003067 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003068 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003069 }
3070
Patrick Dubroy5f445422011-02-18 14:35:21 -08003071 /**
3072 * Add an item from all apps or customize onto the given workspace screen.
3073 * If layout is null, add to the current screen.
3074 */
3075 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003076 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003077 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003078 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003079 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003080
Winson Chungdff8ebb2011-09-08 17:25:31 -07003081 /** Maps the current orientation to an index for referencing orientation correct global icons */
3082 private int getCurrentOrientationIndexForGlobalIcons() {
3083 // default - 0, landscape - 1
3084 switch (getResources().getConfiguration().orientation) {
3085 case Configuration.ORIENTATION_LANDSCAPE:
3086 return 1;
3087 default:
3088 return 0;
3089 }
3090 }
3091
Michael Jurkaae65ee32012-04-30 11:45:54 -07003092 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003093 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003094 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 // Look for the toolbar icon specified in the activity meta-data
3096 Bundle metaData = packageManager.getActivityInfo(
3097 activityName, PackageManager.GET_META_DATA).metaData;
3098 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003099 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 if (iconResId != 0) {
3101 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003102 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003103 }
3104 }
3105 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003106 // This can happen if the activity defines an invalid drawable
3107 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3108 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003109 } catch (Resources.NotFoundException nfe) {
3110 // This can happen if the activity defines an invalid drawable
3111 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3112 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003113 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003114 return null;
3115 }
3116
3117 // if successful in getting icon, return it; otherwise, set button to use default drawable
3118 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003119 int buttonId, ComponentName activityName, int fallbackDrawableId,
3120 String toolbarResourceName) {
3121 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003122 Resources r = getResources();
3123 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3124 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003125
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003126 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003127 // If we were unable to find the icon via the meta-data, use a generic one
3128 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003129 toolbarIcon = r.getDrawable(fallbackDrawableId);
3130 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003131 if (button != null) {
3132 button.setCompoundDrawables(toolbarIcon, null, null, null);
3133 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003134 return null;
3135 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003136 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003137 if (button != null) {
3138 button.setCompoundDrawables(toolbarIcon, null, null, null);
3139 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003140 return toolbarIcon.getConstantState();
3141 }
3142 }
3143
3144 // if successful in getting icon, return it; otherwise, set button to use default drawable
3145 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003146 int buttonId, ComponentName activityName, int fallbackDrawableId,
3147 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003148 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003149 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003150
Michael Jurka19e0fc52011-07-22 18:00:21 -07003151 if (button != null) {
3152 // If we were unable to find the icon via the meta-data, use a
3153 // generic one
3154 if (toolbarIcon == null) {
3155 button.setImageResource(fallbackDrawableId);
3156 } else {
3157 button.setImageDrawable(toolbarIcon);
3158 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003160
3161 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3162
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 }
3164
Winson Chung1b884092012-06-08 17:13:02 -07003165 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003167 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003168 }
3169
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003170 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003171 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003172 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003173 }
3174
Winson Chungbb185bd2011-11-21 12:31:42 -08003175 private void invalidatePressedFocusedStates(View container, View button) {
3176 if (container instanceof HolographicLinearLayout) {
3177 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3178 layout.invalidatePressedFocusedStates();
3179 } else if (button instanceof HolographicImageView) {
3180 HolographicImageView view = (HolographicImageView) button;
3181 view.invalidatePressedFocusedStates();
3182 }
3183 }
3184
Winson Chungc51db6a2011-10-05 11:44:49 -07003185 private boolean updateGlobalSearchIcon() {
3186 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003187 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003188 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003189 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003190
Winson Chung1cad91e2011-05-25 17:41:01 -07003191 final SearchManager searchManager =
3192 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3193 ComponentName activityName = searchManager.getGlobalSearchActivity();
3194 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003195 int coi = getCurrentOrientationIndexForGlobalIcons();
3196 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003197 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3198 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3199 if (sGlobalSearchIcon[coi] == null) {
3200 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3201 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3202 TOOLBAR_ICON_METADATA_NAME);
3203 }
3204
Winson Chungc51db6a2011-10-05 11:44:49 -07003205 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3206 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003207 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003208 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003209 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003210 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003211 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3212 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3213 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003214 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003215 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003216 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003217 }
3218 }
3219
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003220 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003221 final View searchButtonContainer = findViewById(R.id.search_button_container);
3222 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003223 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003224 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003225 }
3226
Winson Chungc51db6a2011-10-05 11:44:49 -07003227 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003228 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003229 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003230
Winson Chungc51db6a2011-10-05 11:44:49 -07003231 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003232 final SearchManager searchManager =
3233 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3234 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3235
3236 ComponentName activityName = null;
3237 if (globalSearchActivity != null) {
3238 // Check if the global search activity handles voice search
3239 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3240 intent.setPackage(globalSearchActivity.getPackageName());
3241 activityName = intent.resolveActivity(getPackageManager());
3242 }
3243
3244 if (activityName == null) {
3245 // Fallback: check if an activity other than the global search activity
3246 // resolves this
3247 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3248 activityName = intent.resolveActivity(getPackageManager());
3249 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003250 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003251 int coi = getCurrentOrientationIndexForGlobalIcons();
3252 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003253 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3254 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3255 if (sVoiceSearchIcon[coi] == null) {
3256 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3257 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3258 TOOLBAR_ICON_METADATA_NAME);
3259 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003260 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003261 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003262 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003263 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003265 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003266 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003267 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003268 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003270 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003271 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003272
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003273 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003274 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3275 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003276 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003277 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003278 }
3279
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003280 public void setVoiceButtonProxyVisible(boolean visible) {
3281 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3282 if (voiceButtonProxy != null) {
3283 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3284 }
3285 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003286 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003287 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003288 */
3289 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003290 final View marketButton = findViewById(R.id.market_button);
3291 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3292 // Find the app market activity by resolving an intent.
3293 // (If multiple app markets are installed, it will return the ResolverActivity.)
3294 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003295 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003296 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003297 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003298 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003299 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3300 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003301 marketButton.setVisibility(View.VISIBLE);
3302 } else {
3303 // We should hide and disable the view so that we don't try and restore the visibility
3304 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3305 marketButton.setVisibility(View.GONE);
3306 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003307 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003308 }
3309
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003310 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003311 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3312 Resources r = getResources();
3313 Drawable marketIconDrawable = d.newDrawable(r);
3314 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3315 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3316 marketIconDrawable.setBounds(0, 0, w, h);
3317
3318 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003319 }
3320
Michael Jurkad7c28052012-04-27 15:43:36 -07003321 @Override
3322 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003323 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003324 final List<CharSequence> text = event.getText();
3325 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003326 // Populate event with a fake title based on the current state.
3327 if (mState == State.APPS_CUSTOMIZE) {
3328 text.add(getString(R.string.all_apps_button_label));
3329 } else {
3330 text.add(getString(R.string.all_apps_home_button_label));
3331 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003332 return result;
3333 }
3334
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003335 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003336 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003337 */
3338 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3339 @Override
3340 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003341 closeSystemDialogs();
3342 }
3343 }
3344
3345 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003346 * Receives notifications whenever the appwidgets are reset.
3347 */
3348 private class AppWidgetResetObserver extends ContentObserver {
3349 public AppWidgetResetObserver() {
3350 super(new Handler());
3351 }
3352
3353 @Override
3354 public void onChange(boolean selfChange) {
3355 onAppWidgetReset();
3356 }
3357 }
3358
3359 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003360 * If the activity is currently paused, signal that we need to run the passed Runnable
3361 * in onResume.
3362 *
3363 * This needs to be called from incoming places where resources might have been loaded
3364 * while we are paused. That is becaues the Configuration might be wrong
3365 * when we're not running, and if it comes back to what it was when we
3366 * were paused, we are not restarted.
3367 *
3368 * Implementation of the method from LauncherModel.Callbacks.
3369 *
3370 * @return true if we are currently paused. The caller might be able to
3371 * skip some work in that case since we will come back again.
3372 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003373 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003374 if (mPaused) {
3375 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003376 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003377 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003378 }
3379 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003380 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003381 return true;
3382 } else {
3383 return false;
3384 }
3385 }
3386
Michael Jurkac402cd92013-05-20 15:49:32 +02003387 private boolean waitUntilResume(Runnable run) {
3388 return waitUntilResume(run, false);
3389 }
3390
Michael Jurka1e2f4652013-07-08 18:03:46 -07003391 public void addOnResumeCallback(Runnable run) {
3392 mBindOnResumeCallbacks.add(run);
3393 }
3394
3395 public void removeOnResumeCallback(Runnable run) {
3396 mBindOnResumeCallbacks.remove(run);
3397 }
3398
Michael Jurka7607c2f2013-04-03 14:33:19 -07003399 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003400 * If the activity is currently paused, signal that we need to re-run the loader
3401 * in onResume.
3402 *
3403 * This needs to be called from incoming places where resources might have been loaded
3404 * while we are paused. That is becaues the Configuration might be wrong
3405 * when we're not running, and if it comes back to what it was when we
3406 * were paused, we are not restarted.
3407 *
3408 * Implementation of the method from LauncherModel.Callbacks.
3409 *
3410 * @return true if we are currently paused. The caller might be able to
3411 * skip some work in that case since we will come back again.
3412 */
3413 public boolean setLoadOnResume() {
3414 if (mPaused) {
3415 Log.i(TAG, "setLoadOnResume");
3416 mOnResumeNeedsLoad = true;
3417 return true;
3418 } else {
3419 return false;
3420 }
3421 }
3422
3423 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003425 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003426 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003427 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003428 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003429 } else {
3430 return SCREEN_COUNT / 2;
3431 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003433
Joe Onorato9c1289c2009-08-17 11:03:03 -04003434 /**
3435 * Refreshes the shortcuts shown on the workspace.
3436 *
3437 * Implementation of the method from LauncherModel.Callbacks.
3438 */
3439 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003440 // If we're starting binding all over again, clear any bind calls we'd postponed in
3441 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3442 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003443 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003444
Michael Jurka7607c2f2013-04-03 14:33:19 -07003445 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003446 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003447 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003448 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003449 int count = workspace.getChildCount();
3450 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003451 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003452 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3453 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003454 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003455 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003456 if (mHotseat != null) {
3457 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003458 }
3459 }
3460
Adam Cohendcd297f2013-06-18 13:13:40 -07003461 @Override
3462 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003463 bindAddScreens(orderedScreenIds);
3464 mWorkspace.addExtraEmptyScreen();
3465 }
3466
3467 @Override
3468 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003469 int count = orderedScreenIds.size();
3470 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003471 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003472 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003473 }
3474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003475 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 * Bind the items start-end from the list.
3477 *
3478 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003479 */
Winson Chung64359a52013-07-08 17:17:08 -07003480 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3481 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003482 if (waitUntilResume(new Runnable() {
3483 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003484 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003485 }
3486 })) {
3487 return;
3488 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003489
Winson Chungf0c6ae02012-03-21 16:10:31 -07003490 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3491 Set<String> newApps = new HashSet<String>();
3492 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3493
Winson Chung64359a52013-07-08 17:17:08 -07003494 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3495 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003496 Workspace workspace = mWorkspace;
3497 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003499
3500 // Short circuit if we are loading dock items for a configuration which has no dock
3501 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3502 mHotseat == null) {
3503 continue;
3504 }
3505
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 switch (item.itemType) {
3507 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3508 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003509 ShortcutInfo info = (ShortcutInfo) item;
3510 String uri = info.intent.toUri(0).toString();
3511 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003512
Winson Chung64359a52013-07-08 17:17:08 -07003513 /*
3514 * TODO: FIX collision case
3515 */
Winson Chungce376632013-07-11 16:12:41 -07003516 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3517 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3518 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3519 throw new RuntimeException("OCCUPIED");
3520 }
Winson Chung64359a52013-07-08 17:17:08 -07003521 }
3522
Adam Cohendcd297f2013-06-18 13:13:40 -07003523 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3524 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003525 boolean animateIconUp = false;
3526 synchronized (newApps) {
3527 if (newApps.contains(uri)) {
3528 animateIconUp = newApps.remove(uri);
3529 }
3530 }
Winson Chung64359a52013-07-08 17:17:08 -07003531 if (forceAnimateIcons) {
3532 // Animate all the applications up now
3533 shortcut.setAlpha(0f);
3534 shortcut.setScaleX(0f);
3535 shortcut.setScaleY(0f);
3536 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3537 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003538 // Prepare the view to be animated up
3539 shortcut.setAlpha(0f);
3540 shortcut.setScaleX(0f);
3541 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003542 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003543 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3544 mNewShortcutAnimateViews.add(shortcut);
3545 }
3546 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003548 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003549 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003550 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003551 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003552 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3553 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003556 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003557
Winson Chung64359a52013-07-08 17:17:08 -07003558 if (forceAnimateIcons) {
3559 // We post the animation slightly delayed to prevent slowdowns when we are loading
3560 // right after we return to launcher.
3561 mWorkspace.postDelayed(new Runnable() {
3562 public void run() {
3563 anim.playTogether(bounceAnims);
3564 anim.start();
3565 }
3566 }, NEW_APPS_ANIMATION_DELAY);
3567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003569 }
3570
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 /**
3572 * Implementation of the method from LauncherModel.Callbacks.
3573 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003574 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3575 if (waitUntilResume(new Runnable() {
3576 public void run() {
3577 bindFolders(folders);
3578 }
3579 })) {
3580 return;
3581 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003582 sFolders.clear();
3583 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003584 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003585
3586 /**
3587 * Add the views for a widget to the workspace.
3588 *
3589 * Implementation of the method from LauncherModel.Callbacks.
3590 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003591 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3592 if (waitUntilResume(new Runnable() {
3593 public void run() {
3594 bindAppWidget(item);
3595 }
3596 })) {
3597 return;
3598 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003599
Daniel Sandler843e8602010-06-07 14:59:01 -04003600 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3601 if (DEBUG_WIDGETS) {
3602 Log.d(TAG, "bindAppWidget: " + item);
3603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003604 final Workspace workspace = mWorkspace;
3605
3606 final int appWidgetId = item.appWidgetId;
3607 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003608 if (DEBUG_WIDGETS) {
3609 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3610 }
3611
Joe Onorato9c1289c2009-08-17 11:03:03 -04003612 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3613
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003615 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003616
Adam Cohendcd297f2013-06-18 13:13:40 -07003617 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003619 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3620
Joe Onorato9c1289c2009-08-17 11:03:03 -04003621 workspace.requestLayout();
3622
Daniel Sandler843e8602010-06-07 14:59:01 -04003623 if (DEBUG_WIDGETS) {
3624 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3625 + (SystemClock.uptimeMillis()-start) + "ms");
3626 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003627 }
3628
Adam Cohen1462de32012-07-24 22:34:36 -07003629 public void onPageBoundSynchronously(int page) {
3630 mSynchronouslyBoundPages.add(page);
3631 }
3632
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 /**
3634 * Callback saying that there aren't any more items to bind.
3635 *
3636 * Implementation of the method from LauncherModel.Callbacks.
3637 */
Adam Cohene25af792013-06-06 23:08:25 -07003638 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003639 if (waitUntilResume(new Runnable() {
3640 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003641 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003642 }
3643 })) {
3644 return;
3645 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 if (mSavedState != null) {
3647 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003648 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 mSavedState = null;
3651 }
3652
Adam Cohen1462de32012-07-24 22:34:36 -07003653 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003655 // If we received the result of any pending adds while the loader was running (e.g. the
3656 // widget configuration forced an orientation change), process them now.
3657 for (int i = 0; i < sPendingAddList.size(); i++) {
3658 completeAdd(sPendingAddList.get(i));
3659 }
3660 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003661
Winson Chungc51db6a2011-10-05 11:44:49 -07003662 // Update the market app icon as necessary (the other icons will be managed in response to
3663 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003664 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003665
Winson Chungf0c6ae02012-03-21 16:10:31 -07003666 // Animate up any icons as necessary
3667 if (mVisible || mWorkspaceLoading) {
3668 Runnable newAppsRunnable = new Runnable() {
3669 @Override
3670 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003671 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003672 }
3673 };
Winson Chunga2413752012-04-03 14:22:34 -07003674
Adam Cohendcd297f2013-06-18 13:13:40 -07003675 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3676 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003677 if (canRunNewAppsAnimation()) {
3678 // If the user has not interacted recently, then either snap to the new page to show
3679 // the new-apps animation or just run them if they are to appear on the current page
3680 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003681 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003682 } else {
3683 runNewAppsAnimation(false);
3684 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003685 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003686 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003687 // are on another page (or just normally if they are added to the current page)
3688 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003689 }
3690 }
3691
3692 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003693 if (upgradePath) {
3694 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003695 mWorkspace.stripDuplicateApps();
3696 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003697 }
Adam Cohen99894d92013-06-14 11:22:59 -07003698
Adam Cohen66a01fd2013-06-11 12:48:00 -07003699 mWorkspace.post(new Runnable() {
3700 @Override
3701 public void run() {
3702 onFinishBindingItems();
3703 }
3704 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003705 }
3706
Winson Chunga2413752012-04-03 14:22:34 -07003707 private boolean canRunNewAppsAnimation() {
3708 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3709 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3710 }
3711
Winson Chungc9168342013-06-26 14:54:55 -07003712 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3713 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3714 PropertyValuesHolder.ofFloat("alpha", 1f),
3715 PropertyValuesHolder.ofFloat("scaleX", 1f),
3716 PropertyValuesHolder.ofFloat("scaleY", 1f));
3717 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3718 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3719 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3720 return bounceAnim;
3721 }
3722
Winson Chungf0c6ae02012-03-21 16:10:31 -07003723 /**
3724 * Runs a new animation that scales up icons that were added while Launcher was in the
3725 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003726 *
3727 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003728 */
Winson Chunga2413752012-04-03 14:22:34 -07003729 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003730 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003731 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003732
3733 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003734 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3735 @Override
3736 public int compare(View a, View b) {
3737 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3738 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3739 int cellCountX = LauncherModel.getCellCountX();
3740 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3741 }
3742 });
Winson Chunga2413752012-04-03 14:22:34 -07003743
3744 // Animate each of the views in place (or show them immediately if requested)
3745 if (immediate) {
3746 for (View v : mNewShortcutAnimateViews) {
3747 v.setAlpha(1f);
3748 v.setScaleX(1f);
3749 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003750 }
Winson Chunga2413752012-04-03 14:22:34 -07003751 } else {
3752 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3753 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003754 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003755 }
3756 anim.playTogether(bounceAnims);
3757 anim.addListener(new AnimatorListenerAdapter() {
3758 @Override
3759 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003760 if (mWorkspace != null) {
3761 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3762 }
Winson Chunga2413752012-04-03 14:22:34 -07003763 }
3764 });
3765 anim.start();
3766 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003767
3768 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003770 mNewShortcutAnimateViews.clear();
3771 new Thread("clearNewAppsThread") {
3772 public void run() {
3773 mSharedPrefs.edit()
3774 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3775 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3776 .commit();
3777 }
3778 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003779 }
3780
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003781 @Override
3782 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003783 boolean searchVisible = updateGlobalSearchIcon();
3784 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003785 if (mSearchDropTargetBar != null) {
3786 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3787 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003788 }
3789
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003790 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003791 * Add the icons for all apps.
3792 *
3793 * Implementation of the method from LauncherModel.Callbacks.
3794 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003795 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003796 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3797 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003798 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003799 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003800 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003801 }
3802
3803 /**
3804 * A package was updated.
3805 *
3806 * Implementation of the method from LauncherModel.Callbacks.
3807 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003808 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3809 if (waitUntilResume(new Runnable() {
3810 public void run() {
3811 bindAppsUpdated(apps);
3812 }
3813 })) {
3814 return;
3815 }
3816
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003817 if (mWorkspace != null) {
3818 mWorkspace.updateShortcuts(apps);
3819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003820 }
3821
3822 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003823 * A package was uninstalled. We take both the super set of packageNames
3824 * in addition to specific applications to remove, the reason being that
3825 * this can be called when a package is updated as well. In that scenario,
3826 * we only remove specific components from the workspace, where as
3827 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003828 *
3829 * Implementation of the method from LauncherModel.Callbacks.
3830 */
Winson Chung83892cc2013-05-01 16:53:33 -07003831 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3832 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003833 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003834 if (waitUntilResume(new Runnable() {
3835 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003836 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003837 }
3838 })) {
3839 return;
3840 }
3841
Winson Chung64359a52013-07-08 17:17:08 -07003842 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003843 mWorkspace.removeItemsByPackageName(packageNames);
3844 } else {
3845 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003846 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003847
Winson Chunga1820962011-10-03 16:31:06 -07003848 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003849 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003850 }
Joe Onoratobe386092009-11-17 17:32:16 -08003851
3852 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003853 * A number of packages were updated.
3854 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003855 private ArrayList<Object> mWidgetsAndShortcuts;
3856 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003857 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003858 bindPackagesUpdated(mWidgetsAndShortcuts);
3859 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003860 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003861 };
3862
3863 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3864 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3865 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003866 return;
3867 }
3868
Winson Chung64359a52013-07-08 17:17:08 -07003869 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003870 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003871 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003872 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003873 }
3874
Winson Chung400438b2011-01-16 17:53:48 -08003875 private int mapConfigurationOriActivityInfoOri(int configOri) {
3876 final Display d = getWindowManager().getDefaultDisplay();
3877 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3878 switch (d.getRotation()) {
3879 case Surface.ROTATION_0:
3880 case Surface.ROTATION_180:
3881 // We are currently in the same basic orientation as the natural orientation
3882 naturalOri = configOri;
3883 break;
3884 case Surface.ROTATION_90:
3885 case Surface.ROTATION_270:
3886 // We are currently in the other basic orientation to the natural orientation
3887 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3888 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3889 break;
3890 }
3891
3892 int[] oriMap = {
3893 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3894 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3895 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3896 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3897 };
3898 // Since the map starts at portrait, we need to offset if this device's natural orientation
3899 // is landscape.
3900 int indexOffset = 0;
3901 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3902 indexOffset = 1;
3903 }
3904 return oriMap[(d.getRotation() + indexOffset) % 4];
3905 }
Adam Cohen446e9402011-09-15 18:21:21 -07003906
Winson Chung4b919f82012-05-01 10:44:08 -07003907 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003908 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003909 getResources().getBoolean(R.bool.allow_rotation);
3910 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003911 }
Winson Chung4b919f82012-05-01 10:44:08 -07003912 public void lockScreenOrientation() {
3913 if (isRotationEnabled()) {
3914 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3915 .getConfiguration().orientation));
3916 }
3917 }
3918 public void unlockScreenOrientation(boolean immediate) {
3919 if (isRotationEnabled()) {
3920 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003921 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003922 } else {
3923 mHandler.postDelayed(new Runnable() {
3924 public void run() {
3925 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3926 }
3927 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003928 }
Winson Chung4b919f82012-05-01 10:44:08 -07003929 }
Winson Chung400438b2011-01-16 17:53:48 -08003930 }
3931
Winson Chung82f55532011-08-09 14:14:23 -07003932 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003933 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003934 if (DISABLE_CLINGS) {
3935 return false;
3936 }
3937
Brett Chabot2a9e2282011-08-23 15:03:13 -07003938 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003939 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003940
Michael Jurkae233a8b2013-03-19 13:49:20 +01003941 // Restricted secondary users (child mode) will potentially have very few apps
3942 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003943// boolean supportsLimitedUsers =
3944// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3945// Account[] accounts = AccountManager.get(this).getAccounts();
3946// if (supportsLimitedUsers && accounts.length == 0) {
3947// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3948// Bundle restrictions = um.getUserRestrictions();
3949// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3950// return false;
3951// }
3952// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003953 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003954 }
Michael Jurka22143132012-10-04 17:42:38 +02003955
Winson Chung7d7541e2011-09-16 20:14:36 -07003956 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003957 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003958 if (cling != null) {
3959 cling.init(this, positionData);
3960 cling.setVisibility(View.VISIBLE);
3961 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3962 if (animate) {
3963 cling.buildLayer();
3964 cling.setAlpha(0f);
3965 cling.animate()
3966 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003967 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003968 .setDuration(SHOW_CLING_DURATION)
3969 .setStartDelay(delay)
3970 .start();
3971 } else {
3972 cling.setAlpha(1f);
3973 }
Michael Jurka22143132012-10-04 17:42:38 +02003974 cling.setFocusableInTouchMode(true);
3975 cling.post(new Runnable() {
3976 public void run() {
3977 cling.setFocusable(true);
3978 cling.requestFocus();
3979 }
3980 });
3981 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3982 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003983 }
3984 return cling;
3985 }
Michael Jurka22143132012-10-04 17:42:38 +02003986
Winson Chung7d7541e2011-09-16 20:14:36 -07003987 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003988 // To catch cases where siblings of top-level views are made invisible, just check whether
3989 // the cling is directly set to GONE before dismissing it.
3990 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003991 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003992 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003993 anim.addListener(new AnimatorListenerAdapter() {
3994 public void onAnimationEnd(Animator animation) {
3995 cling.setVisibility(View.GONE);
3996 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003997 // We should update the shared preferences on a background thread
3998 new Thread("dismissClingThread") {
3999 public void run() {
4000 SharedPreferences.Editor editor = mSharedPrefs.edit();
4001 editor.putBoolean(flag, true);
4002 editor.commit();
4003 }
4004 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004005 };
4006 });
4007 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004008 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004009 }
4010 }
Michael Jurka22143132012-10-04 17:42:38 +02004011
Winson Chung9d9d74f2011-09-19 11:49:12 -07004012 private void removeCling(int id) {
4013 final View cling = findViewById(id);
4014 if (cling != null) {
4015 final ViewGroup parent = (ViewGroup) cling.getParent();
4016 parent.post(new Runnable() {
4017 @Override
4018 public void run() {
4019 parent.removeView(cling);
4020 }
4021 });
Michael Jurka22143132012-10-04 17:42:38 +02004022 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004023 }
4024 }
Michael Jurka974c3862012-05-22 22:00:31 -07004025
4026 private boolean skipCustomClingIfNoAccounts() {
4027 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4028 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4029 if (customCling) {
4030 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004031 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004032 Account[] accounts = am.getAccountsByType("com.google");
4033 return accounts.length == 0;
4034 }
4035 return false;
4036 }
4037
Winson Chung7d7541e2011-09-16 20:14:36 -07004038 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004039 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004040 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004041 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4042 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004043 // If we're not using the default workspace layout, replace workspace cling
4044 // with a custom workspace cling (usually specified in an overlay)
4045 // For now, only do this on tablets
4046 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004047 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004048 // Use a custom cling
4049 View cling = findViewById(R.id.workspace_cling);
4050 ViewGroup clingParent = (ViewGroup) cling.getParent();
4051 int clingIndex = clingParent.indexOfChild(cling);
4052 clingParent.removeViewAt(clingIndex);
4053 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4054 clingParent.addView(customCling, clingIndex);
4055 customCling.setId(R.id.workspace_cling);
4056 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004057 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004058 } else {
4059 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 }
4061 }
4062 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004063 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004064 if (isClingsEnabled() &&
4065 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004066 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004067 } else {
4068 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004069 }
4070 }
4071 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004073 if (isClingsEnabled() &&
4074 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4075 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004076 } else {
4077 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004078 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004079 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004080 }
4081 public boolean isFolderClingVisible() {
4082 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004083 if (cling != null) {
4084 return cling.getVisibility() == View.VISIBLE;
4085 }
4086 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004087 }
Winson Chung82f55532011-08-09 14:14:23 -07004088 public void dismissWorkspaceCling(View v) {
4089 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004090 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004091 }
4092 public void dismissAllAppsCling(View v) {
4093 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4095 }
4096 public void dismissFolderCling(View v) {
4097 Cling cling = (Cling) findViewById(R.id.folder_cling);
4098 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004099 }
4100
Winson Chung80baf5a2010-08-09 16:03:15 -07004101 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004102 * Prints out out state for debugging.
4103 */
4104 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004105 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004106 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004107 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4108 Log.d(TAG, "mRestoring=" + mRestoring);
4109 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4110 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004111 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004112 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004113
Winson Chung785d2eb2011-04-14 16:08:02 -07004114 if (mAppsCustomizeContent != null) {
4115 mAppsCustomizeContent.dumpState();
4116 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004117 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004118 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004119
4120 @Override
4121 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4122 super.dump(prefix, fd, writer, args);
4123 writer.println(" ");
4124 writer.println("Debug logs: ");
4125 for (int i = 0; i < sDumpLogs.size(); i++) {
4126 writer.println(" " + sDumpLogs.get(i));
4127 }
4128 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004129
4130 public static void dumpDebugLogsToConsole() {
4131 Log.d(TAG, "");
4132 Log.d(TAG, "*********************");
4133 Log.d(TAG, "Launcher debug logs: ");
4134 for (int i = 0; i < sDumpLogs.size(); i++) {
4135 Log.d(TAG, " " + sDumpLogs.get(i));
4136 }
4137 Log.d(TAG, "*********************");
4138 Log.d(TAG, "");
4139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004140}
Michael Jurka2763be32011-02-24 11:19:57 -08004141
Michael Jurkaabded662011-03-04 12:06:57 -08004142interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004143 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004144 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004145 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004146 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004147 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004148}