blob: 2881a91e149af954c411e56675532c744ec94cb1 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
195
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700196 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700197 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700198 private State mState = State.WORKSPACE;
199 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800200 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700203 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
204 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700205 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700206 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800209 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210
Winson Chunga2413752012-04-03 14:22:34 -0700211 // How long to wait before the new-shortcut animation automatically pans the workspace
212 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700213 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700214
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800215 private final BroadcastReceiver mCloseSystemDialogsReceiver
216 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800217 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
218
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219 private LayoutInflater mInflater;
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800222 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700223 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800224 private DragLayer mDragLayer;
225 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700226 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700227
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700228 private AppWidgetManager mAppWidgetManager;
229 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Michael Jurkac9d95c52011-08-29 14:03:34 -0700231 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700232 private AppWidgetProviderInfo mPendingAddWidgetInfo;
233
Michael Jurka0280c3b2010-09-17 15:00:07 -0700234 private int[] mTmpAddItemCellCoordinates = new int[2];
235
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private FolderInfo mFolderInfo;
237
Winson Chung3d503fb2011-07-13 17:25:49 -0700238 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800239 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240
Winson Chungc51db6a2011-10-05 11:44:49 -0700241 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700242 private AppsCustomizeTabHost mAppsCustomizeTabHost;
243 private AppsCustomizePagedView mAppsCustomizeContent;
244 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700247 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
248 // scroll issues (because the workspace may not have been measured yet) and extra work.
249 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
250 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251
252 private SpannableStringBuilder mDefaultKeySsb = null;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private boolean mWorkspaceLoading = true;
255
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800256 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private boolean mRestoring;
258 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700259 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260
Michael Jurka7607c2f2013-04-03 14:33:19 -0700261 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
262
Winson Chung4a2afa32012-07-19 14:53:05 -0700263 // Keep track of whether the user has left launcher
264 private static boolean sPausedFromUserAction = false;
265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private Bundle mSavedInstanceState;
267
Joe Onorato9c1289c2009-08-17 11:03:03 -0400268 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800269 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800270 private boolean mUserPresent = true;
271 private boolean mVisible = false;
272 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700273 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400274
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700275 private static LocaleConfiguration sLocaleConfiguration = null;
276
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700277 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700278
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700279 private Intent mAppMarketIntent = null;
280
Adam Cohended9f8d2010-11-03 13:25:16 -0700281 // Related to the auto-advancing of widgets
282 private final int ADVANCE_MSG = 1;
283 private final int mAdvanceInterval = 20000;
284 private final int mAdvanceStagger = 250;
285 private long mAutoAdvanceSentTime;
286 private long mAutoAdvanceTimeLeft = -1;
287 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
288 new HashMap<View, AppWidgetProviderInfo>();
289
Winson Chung400438b2011-01-16 17:53:48 -0800290 // Determines how long to wait after a rotation before restoring the screen orientation to
291 // match the sensor state.
292 private final int mRestoreScreenOrientationDelay = 500;
293
Michael Jurka4ef207b2010-11-29 17:05:45 -0800294 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700295 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
296 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
297 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800298
Craig Mautner360310b2012-10-26 15:13:08 -0700299 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700300
Adam Cohen1462de32012-07-24 22:34:36 -0700301 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
302
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700303 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
304
Winson Chung46353de2012-02-16 14:05:10 -0800305 // We only want to get the SharedPreferences once since it does an FS stat each time we get
306 // it from the context.
307 private SharedPreferences mSharedPrefs;
308
Adam Cohene25af792013-06-06 23:08:25 -0700309 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
310
Winson Chungf0c6ae02012-03-21 16:10:31 -0700311 // Holds the page that we need to animate to, and the icon views that we need to animate up
312 // when we scroll to that page on resume.
Adam Cohendcd297f2013-06-18 13:13:40 -0700313 private long mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700314 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700315 private ImageView mFolderIconImageView;
316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Michael Jurkaddd62e92011-02-16 17:49:14 -0800320 private BubbleTextView mWaitingForResume;
321
Michael Jurka22143132012-10-04 17:42:38 +0200322 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
323 = new HideFromAccessibilityHelper();
324
Michael Jurkac1f5d262011-09-30 19:32:27 -0700325 private Runnable mBuildLayersRunnable = new Runnable() {
326 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700327 if (mWorkspace != null) {
328 mWorkspace.buildPageHardwareLayers();
329 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700330 }
331 };
332
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800333 private static ArrayList<PendingAddArguments> sPendingAddList
334 = new ArrayList<PendingAddArguments>();
335
Michael Jurka0a457bf2012-11-19 14:05:05 -0800336 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
337
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800338 private static class PendingAddArguments {
339 int requestCode;
340 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700341 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700342 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800343 int cellX;
344 int cellY;
345 }
346
Michael Jurka0a457bf2012-11-19 14:05:05 -0800347 private static boolean isPropertyEnabled(String propertyName) {
348 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 @Override
352 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700353 if (DEBUG_STRICT_MODE) {
354 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
355 .detectDiskReads()
356 .detectDiskWrites()
357 .detectNetwork() // or .detectAll() for all detectable problems
358 .penaltyLog()
359 .build());
360 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
361 .detectLeakedSqlLiteObjects()
362 .detectLeakedClosableObjects()
363 .penaltyLog()
364 .penaltyDeath()
365 .build());
366 }
367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400369
370 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
371 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400372 LauncherAppState app = LauncherAppState.getInstance();
373 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700374 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800375 mModel = app.setLauncher(this);
376 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400377 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700379
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700380 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700381 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
382 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700383
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700384 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
385 // this also ensures that any synchronous binding below doesn't re-trigger another
386 // LauncherModel load.
387 mPaused = false;
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200390 android.os.Debug.startMethodTracing(
391 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 }
393
394 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800395 setContentView(R.layout.launcher);
396 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700397 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800399 registerContentObservers();
400
Joe Onorato7c312c12009-08-13 21:36:53 -0700401 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 mSavedState = savedInstanceState;
404 restoreState(mSavedState);
405
Winson Chunga12a2502010-12-20 14:41:35 -0800406 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700407 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200408 mAppsCustomizeContent.onPackagesUpdated(
409 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700410 }
Winson Chunga12a2502010-12-20 14:41:35 -0800411
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 if (PROFILE_STARTUP) {
413 android.os.Debug.stopMethodTracing();
414 }
415
416 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700417 if (sPausedFromUserAction) {
418 // If the user leaves launcher, then we should just load items asynchronously when
419 // they return.
420 mModel.startLoader(true, -1);
421 } else {
422 // We only load the page synchronously if the user rotates (or triggers a
423 // configuration change) while launcher is in the foreground
424 mModel.startLoader(true, mWorkspace.getCurrentPage());
425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 }
427
428 // For handling default keys
429 mDefaultKeySsb = new SpannableStringBuilder();
430 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800431
432 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
433 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800434
Adam Cohenf9426d52012-06-04 17:26:21 -0700435 updateGlobalIcons();
436
437 // On large interfaces, we want the screen to auto-rotate based on the current orientation
438 unlockScreenOrientation(true);
439 }
440
Winson Chung4a2afa32012-07-19 14:53:05 -0700441 protected void onUserLeaveHint() {
442 super.onUserLeaveHint();
443 sPausedFromUserAction = true;
444 }
445
Adam Cohenf9426d52012-06-04 17:26:21 -0700446 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700447 boolean searchVisible = false;
448 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800449 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700450 int coi = getCurrentOrientationIndexForGlobalIcons();
451 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
452 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700453 updateAppMarketIcon();
454 searchVisible = updateGlobalSearchIcon();
455 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700456 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700457 if (sGlobalSearchIcon[coi] != null) {
458 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700459 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700460 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700461 if (sVoiceSearchIcon[coi] != null) {
462 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700463 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700464 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700465 if (sAppMarketIcon[coi] != null) {
466 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800467 }
Winson Chungadf0c182012-08-23 14:59:07 -0700468 if (mSearchDropTargetBar != null) {
469 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
Romain Guycbb89e42009-06-08 15:52:54 -0700472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474 if (sLocaleConfiguration == null) {
475 new AsyncTask<Void, Void, LocaleConfiguration>() {
476 @Override
477 protected LocaleConfiguration doInBackground(Void... unused) {
478 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
479 readConfiguration(Launcher.this, localeConfiguration);
480 return localeConfiguration;
481 }
482
483 @Override
484 protected void onPostExecute(LocaleConfiguration result) {
485 sLocaleConfiguration = result;
486 checkForLocaleChange(); // recursive, but now with a locale configuration
487 }
488 }.execute();
489 return;
490 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 final Configuration configuration = getResources().getConfiguration();
493
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700494 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 final String locale = configuration.locale.toString();
496
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700497 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 final int mcc = configuration.mcc;
499
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 final int mnc = configuration.mnc;
502
Romain Guy84f296c2009-11-04 15:00:44 -0800503 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504
Romain Guy84f296c2009-11-04 15:00:44 -0800505 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700506 sLocaleConfiguration.locale = locale;
507 sLocaleConfiguration.mcc = mcc;
508 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700509
Joe Onorato0589f0f2010-02-08 13:44:00 -0800510 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700511
512 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
513 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700514 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700515 public void run() {
516 writeConfiguration(Launcher.this, localeConfiguration);
517 }
518 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700519 }
520 }
521
522 private static class LocaleConfiguration {
523 public String locale;
524 public int mcc = -1;
525 public int mnc = -1;
526 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700527
Romain Guy98d01652009-06-30 16:21:04 -0700528 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
529 DataInputStream in = null;
530 try {
531 in = new DataInputStream(context.openFileInput(PREFERENCES));
532 configuration.locale = in.readUTF();
533 configuration.mcc = in.readInt();
534 configuration.mnc = in.readInt();
535 } catch (FileNotFoundException e) {
536 // Ignore
537 } catch (IOException e) {
538 // Ignore
539 } finally {
540 if (in != null) {
541 try {
542 in.close();
543 } catch (IOException e) {
544 // Ignore
545 }
546 }
547 }
548 }
549
550 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
551 DataOutputStream out = null;
552 try {
553 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
554 out.writeUTF(configuration.locale);
555 out.writeInt(configuration.mcc);
556 out.writeInt(configuration.mnc);
557 out.flush();
558 } catch (FileNotFoundException e) {
559 // Ignore
560 } catch (IOException e) {
561 //noinspection ResultOfMethodCallIgnored
562 context.getFileStreamPath(PREFERENCES).delete();
563 } finally {
564 if (out != null) {
565 try {
566 out.close();
567 } catch (IOException e) {
568 // Ignore
569 }
570 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 }
572 }
573
Bjorn Bringertc459e522013-06-07 19:36:01 +0100574 public LayoutInflater getInflater() {
575 return mInflater;
576 }
577
Adam Cohen716b51e2011-06-30 12:09:54 -0700578 public DragLayer getDragLayer() {
579 return mDragLayer;
580 }
581
Winson Chung36a62fe2012-05-06 18:04:42 -0700582 boolean isDraggingEnabled() {
583 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
584 // that is subsequently removed from the workspace in startBinding().
585 return !mModel.isLoadingWorkspace();
586 }
587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 static int getScreen() {
589 synchronized (sLock) {
590 return sScreen;
591 }
592 }
593
594 static void setScreen(int screen) {
595 synchronized (sLock) {
596 sScreen = screen;
597 }
598 }
599
Winson Chung557d6ed2011-07-08 15:34:52 -0700600 /**
601 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
602 * a configuration step, this allows the proper animations to run after other transitions.
603 */
604 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700605 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800606 switch (args.requestCode) {
607 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700608 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700609 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 break;
611 case REQUEST_PICK_SHORTCUT:
612 processShortcut(args.intent);
613 break;
614 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700615 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700616 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700617 result = true;
618 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 case REQUEST_CREATE_APPWIDGET:
620 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700621 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700622 result = true;
623 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800624 case REQUEST_PICK_WALLPAPER:
625 // We just wanted the activity result here so we can clear mWaitingForResult
626 break;
627 }
Michael Jurka27614d22012-04-02 06:40:22 -0700628 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
629 // if you turned the screen off and then back while in All Apps, Launcher would not
630 // return to the workspace. Clearing mAddInfo.container here fixes this issue
631 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700632 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800633 }
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700636 protected void onActivityResult(
637 final int requestCode, final int resultCode, final Intent data) {
638 if (requestCode == REQUEST_BIND_APPWIDGET) {
639 int appWidgetId = data != null ?
640 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
641 if (resultCode == RESULT_CANCELED) {
642 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
643 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700644 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700645 }
646 return;
647 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700648 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800649 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
650 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700651 mWaitingForResult = false;
652
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 // We have special handling for widgets
654 if (isWidgetDrop) {
655 int appWidgetId = data != null ?
656 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700657 if (appWidgetId < 0) {
658 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
659 "widget configuration activity.");
660 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
661 } else {
662 completeTwoStageWidgetDrop(resultCode, appWidgetId);
663 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800664 return;
665 }
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 // The pattern used here is that a user PICKs a specific application,
668 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
671 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700672 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800673 final PendingAddArguments args = new PendingAddArguments();
674 args.requestCode = requestCode;
675 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700676 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700677 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700678 args.cellX = mPendingAddInfo.cellX;
679 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800680 if (isWorkspaceLocked()) {
681 sPendingAddList.add(args);
682 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700683 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800686 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700687 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800688 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
689 null);
690 }
691
692 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700693 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700694 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 Runnable onCompleteRunnable = null;
696 int animationType = 0;
697
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700698 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800699 if (resultCode == RESULT_OK) {
700 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700702 mPendingAddWidgetInfo);
703 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800704 onCompleteRunnable = new Runnable() {
705 @Override
706 public void run() {
707 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700708 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800709 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
710 null);
711 }
712 };
713 } else if (resultCode == RESULT_CANCELED) {
714 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
715 onCompleteRunnable = new Runnable() {
716 @Override
717 public void run() {
718 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
719 null);
720 }
721 };
722 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700723 if (mDragLayer.getAnimatedView() != null) {
724 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
725 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
726 animationType, boundWidget, true);
727 } else {
728 // The animated view may be null in the case of a rotation during widget configuration
729 onCompleteRunnable.run();
730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
732
733 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700734 protected void onStop() {
735 super.onStop();
736 FirstFrameAnimatorHelper.setIsVisible(false);
737 }
738
739 @Override
740 protected void onStart() {
741 super.onStart();
742 FirstFrameAnimatorHelper.setIsVisible(true);
743 }
744
745 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200747 long startTime = 0;
748 if (DEBUG_RESUME_TIME) {
749 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400750 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700753
Winson Chung4a2afa32012-07-19 14:53:05 -0700754 // Restore the previous launcher state
755 if (mOnResumeState == State.WORKSPACE) {
756 showWorkspace(false);
757 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
758 showAllApps(false);
759 }
760 mOnResumeState = State.NONE;
761
Craig Mautner360310b2012-10-26 15:13:08 -0700762 // Background was set to gradient in onPause(), restore to black if in all apps.
763 setWorkspaceBackground(mState == State.WORKSPACE);
764
Winson Chungde0fb8f2012-05-08 14:37:08 -0700765 // Process any items that were added while Launcher was away
766 InstallShortcutReceiver.flushInstallQueue(this);
767
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800768 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700769 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700770 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700772 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400773 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700774 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
Michael Jurkac402cd92013-05-20 15:49:32 +0200776 if (mOnResumeCallbacks.size() > 0) {
777 // We might have postponed some bind calls until onResume (see waitUntilResume) --
778 // execute them here
779 long startTimeCallbacks = 0;
780 if (DEBUG_RESUME_TIME) {
781 startTimeCallbacks = System.currentTimeMillis();
782 }
783
784 if (mAppsCustomizeContent != null) {
785 mAppsCustomizeContent.setBulkBind(true);
786 }
787 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
788 mOnResumeCallbacks.get(i).run();
789 }
790 if (mAppsCustomizeContent != null) {
791 mAppsCustomizeContent.setBulkBind(false);
792 }
793 mOnResumeCallbacks.clear();
794 if (DEBUG_RESUME_TIME) {
795 Log.d(TAG, "Time spent processing callbacks in onResume: " +
796 (System.currentTimeMillis() - startTimeCallbacks));
797 }
Michael Jurka447bf842013-05-15 14:52:15 +0200798 }
Winson Chunge4e50662012-01-23 14:45:13 -0800799
800 // Reset the pressed state of icons that were locked in the press state while activities
801 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800802 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800803 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800804 mWaitingForResume.setStayPressed(false);
805 }
Winson Chunge4e50662012-01-23 14:45:13 -0800806 if (mAppsCustomizeContent != null) {
807 // Resets the previous all apps icon press state
808 mAppsCustomizeContent.resetDrawableState();
809 }
Adam Cohen06dff352012-06-01 17:17:08 -0700810 // It is possible that widgets can receive updates while launcher is not in the foreground.
811 // Consequently, the widgets will be inflated in the orientation of the foreground activity
812 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
813 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700814 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700815
816 // Again, as with the above scenario, it's possible that one or more of the global icons
817 // were updated in the wrong orientation.
818 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200819 if (DEBUG_RESUME_TIME) {
820 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
821 }
Adam Cohen06dff352012-06-01 17:17:08 -0700822 }
823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700825 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700826 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
827 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
828 // when Launcher resumes and we are still in AllApps.
829 updateWallpaperVisibility(true);
830
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700831 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700832 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800833 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700834 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700835 }
Romain Guycbb89e42009-06-08 15:52:54 -0700836
Adam Cohen66a01fd2013-06-11 12:48:00 -0700837 protected void onFinishBindingItems() {
838 }
839
840 // Add a fullscreen unpadded view to the workspace to the left all other screens.
841 public void addCustomContentToLeft(View customContent) {
Adam Cohendcd297f2013-06-18 13:13:40 -0700842 mWorkspace.addCustomContentToLeft(customContent);
Adam Cohen66a01fd2013-06-11 12:48:00 -0700843 }
844
Adam Cohenedb40762013-07-18 16:45:45 -0700845 // The custom content needs to offset its content to account for the QSB
846 public int getTopOffsetForCustomContent() {
847 return mWorkspace.getPaddingTop();
848 }
849
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700850 @Override
851 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400852 // Flag the loader to stop early before switching
853 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700854 if (mAppsCustomizeContent != null) {
855 mAppsCustomizeContent.surrender();
856 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800857 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700858 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800860 // We can't hide the IME if it was forced open. So don't bother
861 /*
862 @Override
863 public void onWindowFocusChanged(boolean hasFocus) {
864 super.onWindowFocusChanged(hasFocus);
865
866 if (hasFocus) {
867 final InputMethodManager inputManager = (InputMethodManager)
868 getSystemService(Context.INPUT_METHOD_SERVICE);
869 WindowManager.LayoutParams lp = getWindow().getAttributes();
870 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
871 android.os.Handler()) {
872 protected void onReceiveResult(int resultCode, Bundle resultData) {
873 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
874 }
875 });
876 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
877 }
878 }
879 */
880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 private boolean acceptFilter() {
882 final InputMethodManager inputManager = (InputMethodManager)
883 getSystemService(Context.INPUT_METHOD_SERVICE);
884 return !inputManager.isFullscreenMode();
885 }
886
887 @Override
888 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700889 final int uniChar = event.getUnicodeChar();
890 final boolean handled = super.onKeyDown(keyCode, event);
891 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
892 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800893 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
894 keyCode, event);
895 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700896 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700897 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700898 // showSearchDialog()
899 // If there are multiple keystrokes before the search dialog takes focus,
900 // onSearchRequested() will be called for every keystroke,
901 // but it is idempotent, so it's fine.
902 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 }
904 }
905
Joe Onorato8a9625e2010-01-28 15:55:35 -0800906 // Eat the long press event so the keyboard doesn't come up.
907 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
908 return true;
909 }
910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 return handled;
912 }
913
Karl Rosaen138a0412009-04-23 19:00:21 -0700914 private String getTypedText() {
915 return mDefaultKeySsb.toString();
916 }
917
918 private void clearTypedText() {
919 mDefaultKeySsb.clear();
920 mDefaultKeySsb.clearSpans();
921 Selection.setSelection(mDefaultKeySsb, 0);
922 }
923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700925 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
926 * State
927 */
928 private static State intToState(int stateOrdinal) {
929 State state = State.WORKSPACE;
930 final State[] stateValues = State.values();
931 for (int i = 0; i < stateValues.length; i++) {
932 if (stateValues[i].ordinal() == stateOrdinal) {
933 state = stateValues[i];
934 break;
935 }
936 }
937 return state;
938 }
939
940 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 * Restores the previous state, if it exists.
942 *
943 * @param savedState The previous state.
944 */
945 private void restoreState(Bundle savedState) {
946 if (savedState == null) {
947 return;
948 }
949
Michael Jurka883f55b2010-10-21 15:47:14 -0700950 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700951 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700952 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800953 }
954
Winson Chungf0c6ae02012-03-21 16:10:31 -0700955 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700957 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 }
959
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700961 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
964 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
967 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700968 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
969 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
970 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700971 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 mRestoring = true;
973 }
974
975 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
976 if (renameFolder) {
977 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700978 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 mRestoring = true;
980 }
Winson Chunga12a2502010-12-20 14:41:35 -0800981
Winson Chung785d2eb2011-04-14 16:08:02 -0700982 // Restore the AppsCustomize tab
983 if (mAppsCustomizeTabHost != null) {
984 String curTab = savedState.getString("apps_customize_currentTab");
985 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700986 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700988 mAppsCustomizeContent.loadAssociatedPages(
989 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700990 }
991
Winson Chung5afbf7b2011-07-25 11:53:08 -0700992 int currentIndex = savedState.getInt("apps_customize_currentIndex");
993 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996
997 /**
998 * Finds all the views we need and configure them properly.
999 */
1000 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001001 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001002
Craig Mautner360310b2012-10-26 15:13:08 -07001003 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001004 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1005 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001006 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001007
1008 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1009 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010
Winson Chung4c98d922011-05-31 16:50:48 -07001011 // Setup the drag layer
1012 mDragLayer.setup(this, dragController);
1013
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 // Setup the hotseat
1015 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1016 if (mHotseat != null) {
1017 mHotseat.setup(this);
1018 }
1019
Winson Chung4c98d922011-05-31 16:50:48 -07001020 // Setup the workspace
1021 mWorkspace.setHapticFeedbackEnabled(false);
1022 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001023 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001024 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001025
Winson Chungf0ea4d32011-06-06 14:27:16 -07001026 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001027 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001028
Winson Chungf0ea4d32011-06-06 14:27:16 -07001029 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001030 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001031 mAppsCustomizeContent = (AppsCustomizePagedView)
1032 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1033 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001034
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001036 dragController.setDragScoller(mWorkspace);
1037 dragController.setScrollView(mDragLayer);
1038 dragController.setMoveTarget(mWorkspace);
1039 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001040 if (mSearchDropTargetBar != null) {
1041 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001042 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001043
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001044 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001045 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001046 mWeightWatcher = new WeightWatcher(this);
1047 mWeightWatcher.setAlpha(0.5f);
1048 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001049 new FrameLayout.LayoutParams(
1050 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001051 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001052 Gravity.BOTTOM)
1053 );
Adam Cohen39a06042013-07-19 14:30:12 -07001054
1055 boolean show = shouldShowWeightWatcher();
1056 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 }
1059
1060 /**
1061 * Creates a view representing a shortcut.
1062 *
1063 * @param info The data structure describing the shortcut.
1064 *
1065 * @return A View inflated from R.layout.application.
1066 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001067 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001069 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 }
1071
1072 /**
1073 * Creates a view representing a shortcut inflated from the specified resource.
1074 *
1075 * @param layoutResId The id of the XML layout used to create the shortcut.
1076 * @param parent The group the shortcut belongs to.
1077 * @param info The data structure describing the shortcut.
1078 *
1079 * @return A View inflated from layoutResId.
1080 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001081 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001082 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1083 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 return favorite;
1086 }
1087
1088 /**
1089 * Add an application shortcut to the workspace.
1090 *
1091 * @param data The intent describing the application.
1092 * @param cellInfo The position on screen where to create the shortcut.
1093 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001094 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001096 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097
Adam Cohenfbba09b2011-07-18 21:43:05 -07001098 // First we check if we already know the exact location where we want to add this item.
1099 if (cellX >= 0 && cellY >= 0) {
1100 cellXY[0] = cellX;
1101 cellXY[1] = cellY;
1102 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001103 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001104 return;
1105 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001106
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001107 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001108
Romain Guy73b979d2009-06-09 12:57:21 -07001109 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001110 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001112 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001113 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001114 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001115 } else {
1116 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118 }
Romain Guycbb89e42009-06-08 15:52:54 -07001119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 /**
1121 * Add a shortcut to the workspace.
1122 *
1123 * @param data The intent describing the shortcut.
1124 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001126 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001127 int cellY) {
1128 int[] cellXY = mTmpAddItemCellCoordinates;
1129 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001130 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
Michael Jurkad3ef3062010-11-23 16:23:58 -08001132 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001133
Adam Cohen558baaf2011-08-15 15:22:57 -07001134 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001135 if (info == null) {
1136 return;
1137 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001138 final View view = createShortcut(info);
1139
Adam Cohenfbba09b2011-07-18 21:43:05 -07001140 // First we check if we already know the exact location where we want to add this item.
1141 if (cellX >= 0 && cellY >= 0) {
1142 cellXY[0] = cellX;
1143 cellXY[1] = cellY;
1144 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001145
1146 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001147 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001148 true, null,null)) {
1149 return;
1150 }
1151 DragObject dragObject = new DragObject();
1152 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001153 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1154 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001155 return;
1156 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001157 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001158 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001159 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001160 foundCellSpan = (result != null);
1161 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001162 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001163 }
1164
1165 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001166 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001167 return;
1168 }
1169
Adam Cohendcd297f2013-06-18 13:13:40 -07001170 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171
1172 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001173 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001174 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176 }
1177
Adam Cohen2f093b62012-04-30 18:59:53 -07001178 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1179 int minHeight) {
1180 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001181 // We want to account for the extra amount of padding that we are adding to the widget
1182 // to ensure that it gets the full amount of space that it has requested
1183 int requiredWidth = minWidth + padding.left + padding.right;
1184 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001185 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001186 }
1187
Adam Cohen2f093b62012-04-30 18:59:53 -07001188 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1189 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001190 }
1191
Adam Cohen2f093b62012-04-30 18:59:53 -07001192 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1193 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001194 }
1195
Adam Cohen2f093b62012-04-30 18:59:53 -07001196 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1197 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001198 }
1199
Adam Cohen2f093b62012-04-30 18:59:53 -07001200 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1201 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001202 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001203 }
1204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001208 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001209 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001211 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001212 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1213 if (appWidgetInfo == null) {
1214 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1215 }
Romain Guycbb89e42009-06-08 15:52:54 -07001216
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001217 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001218 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001219
Adam Cohen2f093b62012-04-30 18:59:53 -07001220 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1221 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001224 // We have saved the position to which the widget was dragged-- this really only matters
1225 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001226 int[] cellXY = mTmpAddItemCellCoordinates;
1227 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001228 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001229 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001230 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1231 cellXY[0] = mPendingAddInfo.cellX;
1232 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001233 spanXY[0] = mPendingAddInfo.spanX;
1234 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001235 foundCellSpan = true;
1236 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001237 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001239 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1240 spanXY[1], cellXY, finalSpan);
1241 spanXY[0] = finalSpan[0];
1242 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001243 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001245 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001246 }
1247
Michael Jurka0280c3b2010-09-17 15:00:07 -07001248 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001249 if (appWidgetId != -1) {
1250 // Deleting an app widget ID is a void call but writes to disk before returning
1251 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001252 new Thread("deleteAppWidgetId") {
1253 public void run() {
1254 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1255 }
1256 }.start();
1257 }
Winson Chung93eef082012-03-23 15:59:27 -07001258 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001259 return;
1260 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001263 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1264 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265 launcherInfo.spanX = spanXY[0];
1266 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001267 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1268 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001269
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001271 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
1273 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001274 if (hostView == null) {
1275 // Perform actual inflation because we're live
1276 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1277 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1278 } else {
1279 // The AppWidgetHostView has already been inflated and instantiated
1280 launcherInfo.hostView = hostView;
1281 }
Romain Guycbb89e42009-06-08 15:52:54 -07001282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001284 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001285 launcherInfo.notifyWidgetSizeChanged(this);
1286
Adam Cohendcd297f2013-06-18 13:13:40 -07001287 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001288 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001289
1290 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001292 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
Romain Guycbb89e42009-06-08 15:52:54 -07001294
Adam Cohended9f8d2010-11-03 13:25:16 -07001295 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1296 @Override
1297 public void onReceive(Context context, Intent intent) {
1298 final String action = intent.getAction();
1299 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1300 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001301 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001302 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001303
Winson Chungc100e8e2011-08-09 16:02:43 -07001304 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001305 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001306 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1307 mAppsCustomizeTabHost.reset();
1308 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001309 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001310 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1311 mUserPresent = true;
1312 updateRunning();
1313 }
1314 }
1315 };
1316
1317 @Override
1318 public void onAttachedToWindow() {
1319 super.onAttachedToWindow();
1320
1321 // Listen for broadcasts related to user-presence
1322 final IntentFilter filter = new IntentFilter();
1323 filter.addAction(Intent.ACTION_SCREEN_OFF);
1324 filter.addAction(Intent.ACTION_USER_PRESENT);
1325 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001326 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001327 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001328 mVisible = true;
1329 }
1330
1331 @Override
1332 public void onDetachedFromWindow() {
1333 super.onDetachedFromWindow();
1334 mVisible = false;
1335
Adam Cohend113e0c2010-11-11 10:48:05 -08001336 if (mAttached) {
1337 unregisterReceiver(mReceiver);
1338 mAttached = false;
1339 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001340 updateRunning();
1341 }
1342
1343 public void onWindowVisibilityChanged(int visibility) {
1344 mVisible = visibility == View.VISIBLE;
1345 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001346 // The following code used to be in onResume, but it turns out onResume is called when
1347 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1348 // is a more appropriate event to handle
1349 if (mVisible) {
1350 mAppsCustomizeTabHost.onWindowVisible();
1351 if (!mWorkspaceLoading) {
1352 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001353 // We want to let Launcher draw itself at least once before we force it to build
1354 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001355 // apps is nice and speedy.
1356 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001357 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001358 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001359 if (mStarted) return;
1360 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001361 // We delay the layer building a bit in order to give
1362 // other message processing a time to run. In particular
1363 // this avoids a delay in hiding the IME if it was
1364 // currently shown, because doing that may involve
1365 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001366 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001367 final ViewTreeObserver.OnDrawListener listener = this;
1368 mWorkspace.post(new Runnable() {
1369 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001370 if (mWorkspace != null &&
1371 mWorkspace.getViewTreeObserver() != null) {
1372 mWorkspace.getViewTreeObserver().
1373 removeOnDrawListener(listener);
1374 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001375 }
1376 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001377 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001378 }
1379 });
1380 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001381 // When Launcher comes back to foreground, a different Activity might be responsible for
1382 // the app market intent, so refresh the icon
1383 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001384 clearTypedText();
1385 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001386 }
1387
1388 private void sendAdvanceMessage(long delay) {
1389 mHandler.removeMessages(ADVANCE_MSG);
1390 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1391 mHandler.sendMessageDelayed(msg, delay);
1392 mAutoAdvanceSentTime = System.currentTimeMillis();
1393 }
1394
1395 private void updateRunning() {
1396 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1397 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1398 mAutoAdvanceRunning = autoAdvanceRunning;
1399 if (autoAdvanceRunning) {
1400 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1401 sendAdvanceMessage(delay);
1402 } else {
1403 if (!mWidgetsToAdvance.isEmpty()) {
1404 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1405 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1406 }
1407 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001408 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001409 }
1410 }
1411 }
1412
1413 private final Handler mHandler = new Handler() {
1414 @Override
1415 public void handleMessage(Message msg) {
1416 if (msg.what == ADVANCE_MSG) {
1417 int i = 0;
1418 for (View key: mWidgetsToAdvance.keySet()) {
1419 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1420 final int delay = mAdvanceStagger * i;
1421 if (v instanceof Advanceable) {
1422 postDelayed(new Runnable() {
1423 public void run() {
1424 ((Advanceable) v).advance();
1425 }
1426 }, delay);
1427 }
1428 i++;
1429 }
1430 sendAdvanceMessage(mAdvanceInterval);
1431 }
1432 }
1433 };
1434
1435 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001436 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001437 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1438 if (v instanceof Advanceable) {
1439 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001440 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001441 updateRunning();
1442 }
1443 }
1444
1445 void removeWidgetToAutoAdvance(View hostView) {
1446 if (mWidgetsToAdvance.containsKey(hostView)) {
1447 mWidgetsToAdvance.remove(hostView);
1448 updateRunning();
1449 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001450 }
1451
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001453 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001454 launcherInfo.hostView = null;
1455 }
1456
Winson Chung93eef082012-03-23 15:59:27 -07001457 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1458 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1459 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001460 }
1461
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001462 public LauncherAppWidgetHost getAppWidgetHost() {
1463 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Winson Chunga9abd0e2010-10-27 17:18:37 -07001466 public LauncherModel getModel() {
1467 return mModel;
1468 }
1469
Bjorn Bringertc459e522013-06-07 19:36:01 +01001470 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001471 getWindow().closeAllPanels();
1472
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001473 // Whatever we were doing is hereby canceled.
1474 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001475 }
1476
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 @Override
1478 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001479 long startTime = 0;
1480 if (DEBUG_RESUME_TIME) {
1481 startTime = System.currentTimeMillis();
1482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 super.onNewIntent(intent);
1484
1485 // Close the menu
1486 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001487 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001488 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001489
Jamie Genniscb222e82012-10-23 15:44:26 -07001490 final boolean alreadyOnHome =
1491 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001492 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001493
Jamie Genniscb222e82012-10-23 15:44:26 -07001494 Runnable processIntent = new Runnable() {
1495 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001496 if (mWorkspace == null) {
1497 // Can be cases where mWorkspace is null, this prevents a NPE
1498 return;
1499 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001500 Folder openFolder = mWorkspace.getOpenFolder();
1501 // In all these cases, only animate if we're already on home
1502 mWorkspace.exitWidgetResizeMode();
1503 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1504 openFolder == null) {
1505 mWorkspace.moveToDefaultScreen(true);
1506 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001507
Jamie Genniscb222e82012-10-23 15:44:26 -07001508 closeFolder();
1509 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001510
Jamie Genniscb222e82012-10-23 15:44:26 -07001511 // If we are already on home, then just animate back to the workspace,
1512 // otherwise, just wait until onResume to set the state back to Workspace
1513 if (alreadyOnHome) {
1514 showWorkspace(true);
1515 } else {
1516 mOnResumeState = State.WORKSPACE;
1517 }
1518
1519 final View v = getWindow().peekDecorView();
1520 if (v != null && v.getWindowToken() != null) {
1521 InputMethodManager imm = (InputMethodManager)getSystemService(
1522 INPUT_METHOD_SERVICE);
1523 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1524 }
1525
1526 // Reset AllApps to its initial state
1527 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1528 mAppsCustomizeTabHost.reset();
1529 }
1530 }
1531 };
1532
1533 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1534 // Delay processing of the intent to allow the status bar animation to finish
1535 // first in order to avoid janky animations.
1536 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001537 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001538 // Process the intent immediately.
1539 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001540 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
Michael Jurka447bf842013-05-15 14:52:15 +02001543 if (DEBUG_RESUME_TIME) {
1544 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1545 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
1547
1548 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001549 public void onRestoreInstanceState(Bundle state) {
1550 super.onRestoreInstanceState(state);
1551 for (int page: mSynchronouslyBoundPages) {
1552 mWorkspace.restoreInstanceStateForChild(page);
1553 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001554 }
1555
1556 @Override
1557 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001558 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001559 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
Michael Jurka883f55b2010-10-21 15:47:14 -07001561 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001562 // We close any open folder since it will not be re-opened, and we need to make sure
1563 // this state is reflected.
1564 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
Adam Cohendcd297f2013-06-18 13:13:40 -07001566 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001567 mWaitingForResult) {
1568 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001569 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001570 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1571 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001572 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1573 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1574 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576
1577 if (mFolderInfo != null && mWaitingForResult) {
1578 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1579 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1580 }
Michael Jurka946ad472010-07-09 18:05:18 -07001581
Winson Chung785d2eb2011-04-14 16:08:02 -07001582 // Save the current AppsCustomize tab
1583 if (mAppsCustomizeTabHost != null) {
1584 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1585 if (currentTabTag != null) {
1586 outState.putString("apps_customize_currentTab", currentTabTag);
1587 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001588 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1589 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 }
1592
1593 @Override
1594 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Winson Chunge7a03942011-08-05 15:05:12 -07001597 // Remove all pending runnables
1598 mHandler.removeMessages(ADVANCE_MSG);
1599 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001600 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001601
Winson Chungcd2b0142011-06-08 16:02:26 -07001602 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001603 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001604 mModel.stopLoader();
1605 app.setLauncher(null);
1606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001608 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001610 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001612 mAppWidgetHost = null;
1613
1614 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 TextKeyListener.getInstance().release();
1617
Winson Chung81b52252012-08-27 15:34:29 -07001618 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1619 // to prevent leaking Launcher activities on orientation change.
1620 if (mModel != null) {
1621 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1622 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001623
1624 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001625 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001626
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001627 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001628 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1629 mWorkspace.removeAllViews();
1630 mWorkspace = null;
1631 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001632
Michael Jurka2ecf9952012-06-18 12:52:28 -07001633 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
Adam Cohena9cf38f2011-05-02 15:36:58 -07001636 public DragController getDragController() {
1637 return mDragController;
1638 }
1639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 @Override
1641 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001642 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 super.startActivityForResult(intent, requestCode);
1644 }
1645
Winson Chung70d72102011-08-12 11:24:35 -07001646 /**
1647 * Indicates that we want global search for this activity by setting the globalSearch
1648 * argument for {@link #startSearch} to true.
1649 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001651 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001653
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001654 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001655
Karl Rosaen138a0412009-04-23 19:00:21 -07001656 if (initialQuery == null) {
1657 // Use any text typed in the launcher as the initial query
1658 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 if (appSearchData == null) {
1661 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001662 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
Winson Chungadf0c182012-08-23 14:59:07 -07001664 Rect sourceBounds = new Rect();
1665 if (mSearchDropTargetBar != null) {
1666 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1667 }
Romain Guycbb89e42009-06-08 15:52:54 -07001668
Bjorn Bringertc459e522013-06-07 19:36:01 +01001669 startSearch(initialQuery, selectInitialQuery,
1670 appSearchData, sourceBounds);
1671 }
1672
1673 public void startSearch(String initialQuery,
1674 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001675 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001676 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001677 }
1678
1679 /**
1680 * Starts the global search activity. This code is a copied from SearchManager
1681 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001682 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001683 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001684 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001685 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1686 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1687 if (globalSearchActivity == null) {
1688 Log.w(TAG, "No global search activity found.");
1689 return;
1690 }
1691 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1692 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1693 intent.setComponent(globalSearchActivity);
1694 // Make sure that we have a Bundle to put source in
1695 if (appSearchData == null) {
1696 appSearchData = new Bundle();
1697 } else {
1698 appSearchData = new Bundle(appSearchData);
1699 }
1700 // Set source to package name of app that starts global search, if not set already.
1701 if (!appSearchData.containsKey("source")) {
1702 appSearchData.putString("source", getPackageName());
1703 }
1704 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1705 if (!TextUtils.isEmpty(initialQuery)) {
1706 intent.putExtra(SearchManager.QUERY, initialQuery);
1707 }
1708 if (selectInitialQuery) {
1709 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1710 }
1711 intent.setSourceBounds(sourceBounds);
1712 try {
1713 startActivity(intent);
1714 } catch (ActivityNotFoundException ex) {
1715 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
1718
Winson Chung70d72102011-08-12 11:24:35 -07001719 @Override
1720 public boolean onCreateOptionsMenu(Menu menu) {
1721 if (isWorkspaceLocked()) {
1722 return false;
1723 }
1724
1725 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001726
1727 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1728 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1729 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001730 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1731 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1732 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001733 String helpUrl = getString(R.string.help_url);
1734 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001735 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1736 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1737
Winson Chung70d72102011-08-12 11:24:35 -07001738 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1739 .setIcon(android.R.drawable.ic_menu_gallery)
1740 .setAlphabeticShortcut('W');
1741 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1742 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001743 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001744 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001745 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1746 .setIcon(android.R.drawable.ic_menu_preferences)
1747 .setIntent(settings)
1748 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001749 if (!helpUrl.isEmpty()) {
1750 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1751 .setIcon(android.R.drawable.ic_menu_help)
1752 .setIntent(help)
1753 .setAlphabeticShortcut('H');
1754 }
Winson Chung70d72102011-08-12 11:24:35 -07001755 return true;
1756 }
1757
1758 @Override
1759 public boolean onPrepareOptionsMenu(Menu menu) {
1760 super.onPrepareOptionsMenu(menu);
1761
1762 if (mAppsCustomizeTabHost.isTransitioning()) {
1763 return false;
1764 }
1765 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1766 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1767
1768 return true;
1769 }
1770
1771 @Override
1772 public boolean onOptionsItemSelected(MenuItem item) {
1773 switch (item.getItemId()) {
1774 case MENU_WALLPAPER_SETTINGS:
1775 startWallpaper();
1776 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001777 }
1778
1779 return super.onOptionsItemSelected(item);
1780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001782 @Override
1783 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001784 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001785 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001786 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001787 }
1788
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 public boolean isWorkspaceLocked() {
1790 return mWorkspaceLoading || mWaitingForResult;
1791 }
1792
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001794 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001795 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001796 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1797 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001798 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001799 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001801
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001802 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1803 AppWidgetProviderInfo appWidgetInfo) {
1804 if (appWidgetInfo.configure != null) {
1805 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001806
Bjorn Bringert7984c942009-12-09 15:38:25 +00001807 // Launch over to configure widget, if needed
1808 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001809 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001810 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001811 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001813 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001814 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001815 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001816 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001817 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 }
1819 }
Romain Guycbb89e42009-06-08 15:52:54 -07001820
Adam Cohenfbba09b2011-07-18 21:43:05 -07001821 /**
1822 * Process a shortcut drop.
1823 *
1824 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001825 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001826 * @param cell The cell it should be added to, optional
1827 * @param position The location on the screen where it was dropped, optional
1828 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001829 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001830 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001832 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001833 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001834 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001835
1836 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 mPendingAddInfo.cellX = cell[0];
1838 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840
1841 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1842 createShortcutIntent.setComponent(componentName);
1843 processShortcut(createShortcutIntent);
1844 }
1845
Adam Cohenfbba09b2011-07-18 21:43:05 -07001846 /**
1847 * Process a widget drop.
1848 *
1849 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001850 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001851 * @param cell The cell it should be added to, optional
1852 * @param position The location on the screen where it was dropped, optional
1853 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001854 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001855 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001856 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001857 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001858 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001859 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001860 mPendingAddInfo.minSpanX = info.minSpanX;
1861 mPendingAddInfo.minSpanY = info.minSpanY;
1862
Adam Cohenfbba09b2011-07-18 21:43:05 -07001863 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001864 mPendingAddInfo.cellX = cell[0];
1865 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001866 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001867 if (span != null) {
1868 mPendingAddInfo.spanX = span[0];
1869 mPendingAddInfo.spanY = span[1];
1870 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001871
Adam Cohened66b2b2012-01-23 17:28:51 -08001872 AppWidgetHostView hostView = info.boundWidget;
1873 int appWidgetId;
1874 if (hostView != null) {
1875 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001876 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001877 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001878 // In this case, we either need to start an activity to get permission to bind
1879 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001880 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001881 Bundle options = info.bindOptions;
1882
1883 boolean success = false;
1884 if (options != null) {
1885 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1886 info.componentName, options);
1887 } else {
1888 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1889 info.componentName);
1890 }
1891 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001892 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001893 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001894 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001895 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1896 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1897 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001898 // TODO: we need to make sure that this accounts for the options bundle.
1899 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001900 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1901 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001902 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001903 }
1904
Joe Onoratodeb98af2010-02-19 14:59:39 -08001905 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001906 // Handle case where user selected "Applications"
1907 String applicationName = getResources().getString(R.string.group_applications);
1908 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001910 if (applicationName != null && applicationName.equals(shortcutName)) {
1911 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1912 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001913
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001914 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1915 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001916 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001917 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001918 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001919 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922
Winson Chung24ab2f12010-09-16 14:10:47 -07001923 void processWallpaper(Intent intent) {
1924 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1925 }
1926
Adam Cohendcd297f2013-06-18 13:13:40 -07001927 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001929 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 folderInfo.title = getText(R.string.folder_name);
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001933 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001935 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936
1937 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001938 FolderIcon newFolder =
1939 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001940 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001941 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001942 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 }
Romain Guycbb89e42009-06-08 15:52:54 -07001944
Joe Onorato9c1289c2009-08-17 11:03:03 -04001945 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001946 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001950 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001952 Intent chooser = Intent.createChooser(pickWallpaper,
1953 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001954 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1955 // Removed in Eclair MR1
1956// WallpaperManager wm = (WallpaperManager)
1957// getSystemService(Context.WALLPAPER_SERVICE);
1958// WallpaperInfo wi = wm.getWallpaperInfo();
1959// if (wi != null && wi.getSettingsActivity() != null) {
1960// LabeledIntent li = new LabeledIntent(getPackageName(),
1961// R.string.configure_wallpaper, 0);
1962// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1963// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1964// }
Mike Clerona0618e42009-10-22 13:55:21 -07001965 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
1967
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001968 /**
1969 * Registers various content observers. The current implementation registers
1970 * only a favorites observer to keep track of the favorites applications.
1971 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001972 private void registerContentObservers() {
1973 ContentResolver resolver = getContentResolver();
1974 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1975 true, mWidgetObserver);
1976 }
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 @Override
1979 public boolean dispatchKeyEvent(KeyEvent event) {
1980 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1981 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001983 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001984 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08001985 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001986 dumpState();
1987 return true;
1988 }
1989 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001990 }
1991 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1992 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001993 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 return true;
1995 }
1996 }
1997
1998 return super.dispatchKeyEvent(event);
1999 }
2000
Joe Onorato88ec0992009-11-19 13:16:06 -08002001 @Override
2002 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002003 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002004 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002005 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002006 Folder openFolder = mWorkspace.getOpenFolder();
2007 if (openFolder.isEditingName()) {
2008 openFolder.dismissEditingName();
2009 } else {
2010 closeFolder();
2011 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002012 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002013 mWorkspace.exitWidgetResizeMode();
2014
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002015 // Back button is a no-op here, but give at least some feedback for the button press
2016 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002017 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002018 }
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002021 * Re-listen when widgets are reset.
2022 */
2023 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002024 if (mAppWidgetHost != null) {
2025 mAppWidgetHost.startListening();
2026 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002027 }
2028
2029 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 * Launches the intent referred by the clicked shortcut.
2031 *
2032 * @param v The view representing the clicked shortcut.
2033 */
2034 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002035 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2036 // view has detached (it's possible for this to happen if the view is removed mid touch).
2037 if (v.getWindowToken() == null) {
2038 return;
2039 }
2040
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002041 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002042 return;
2043 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002046 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002048 final Intent intent = ((ShortcutInfo) tag).intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002049
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002050 // Check for special shortcuts
2051 if (intent.getComponent() != null) {
2052 final String shortcutClass = intent.getComponent().getClassName();
2053
2054 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2055 showAllApps(true);
2056 return;
2057 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2058 MemoryDumpActivity.startDump(this);
2059 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002060 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2061 toggleShowWeightWatcher();
2062 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002063 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002064 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002065
2066 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002067 int[] pos = new int[2];
2068 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002069 intent.setSourceBounds(new Rect(pos[0], pos[1],
2070 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002071
2072 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002073
2074 if (success && v instanceof BubbleTextView) {
2075 mWaitingForResume = (BubbleTextView) v;
2076 mWaitingForResume.setStayPressed(true);
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002079 if (v instanceof FolderIcon) {
2080 FolderIcon fi = (FolderIcon) v;
2081 handleFolderClick(fi);
2082 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002083 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002084 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002086 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002087 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090 }
2091
Michael Jurka0e260592010-06-30 17:07:39 -07002092 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002093 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002094 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002095 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2096 showWorkspace(true);
2097 }
Michael Jurka0e260592010-06-30 17:07:39 -07002098 return false;
2099 }
2100
Michael Jurkaaf442092010-06-10 17:01:57 -07002101 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002102 * Event handler for the search button
2103 *
2104 * @param v The view that was clicked.
2105 */
2106 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002107 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2108
Amith Yamasania135ba82011-08-09 17:42:01 -07002109 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002110 }
2111
2112 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002113 * Event handler for the voice button
2114 *
2115 * @param v The view that was clicked.
2116 */
2117 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002118 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002119
Bjorn Bringertc459e522013-06-07 19:36:01 +01002120 startVoice();
2121 }
2122
2123 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002124 try {
2125 final SearchManager searchManager =
2126 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2127 ComponentName activityName = searchManager.getGlobalSearchActivity();
2128 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002129 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002130 if (activityName != null) {
2131 intent.setPackage(activityName.getPackageName());
2132 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002133 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002134 } catch (ActivityNotFoundException e) {
2135 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002136 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002137 startActivitySafely(null, intent, "onClickVoiceButton");
2138 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002139 }
2140
2141 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002142 * Event handler for the "grid" button that appears on the home screen, which
2143 * enters all apps mode.
2144 *
2145 * @param v The view that was clicked.
2146 */
2147 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002148 showAllApps(true);
2149 }
2150
2151 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002152 // Provide the same haptic feedback that the system offers for virtual keys.
2153 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002154 }
2155
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002156 public void onClickAppMarketButton(View v) {
2157 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002158 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002159 } else {
2160 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002161 }
2162 }
2163
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002164 void startApplicationDetailsActivity(ComponentName componentName) {
2165 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002166 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2167 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002168 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002169 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002170 }
2171
Patrick Dubroy5539af72010-09-07 15:22:01 -07002172 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2173 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2174 // System applications cannot be installed. For now, show a toast explaining that.
2175 // We may give them the option of disabling apps this way.
2176 int messageId = R.string.uninstall_system_app_text;
2177 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2178 } else {
2179 String packageName = appInfo.componentName.getPackageName();
2180 String className = appInfo.componentName.getClassName();
2181 Intent intent = new Intent(
2182 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002183 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2184 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002185 startActivity(intent);
2186 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002187 }
2188
Michael Jurka86a720e2012-05-09 11:23:23 -07002189 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002193 // Only launch using the new animation if the shortcut has not opted out (this is a
2194 // private contract between launcher and may be ignored in the future).
2195 boolean useLaunchAnimation = (v != null) &&
2196 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2197 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002198 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2199 v.getMeasuredWidth(), v.getMeasuredHeight());
2200
2201 startActivity(intent, opts.toBundle());
2202 } else {
2203 startActivity(intent);
2204 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002205 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 } catch (SecurityException e) {
2207 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002208 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002210 "or use the exported attribute for this activity. "
2211 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002213 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002215
Michael Jurka86a720e2012-05-09 11:23:23 -07002216 boolean startActivitySafely(View v, Intent intent, Object tag) {
2217 boolean success = false;
2218 try {
2219 success = startActivity(v, intent, tag);
2220 } catch (ActivityNotFoundException e) {
2221 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2222 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2223 }
2224 return success;
2225 }
2226
Romain Guy8e633c52010-03-04 12:51:36 -08002227 void startActivityForResultSafely(Intent intent, int requestCode) {
2228 try {
2229 startActivityForResult(intent, requestCode);
2230 } catch (ActivityNotFoundException e) {
2231 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2232 } catch (SecurityException e) {
2233 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2234 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2235 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2236 "or use the exported attribute for this activity.", e);
2237 }
2238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239
Adam Cohena9cf38f2011-05-02 15:36:58 -07002240 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002241 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002242 Folder openFolder = mWorkspace.getFolderForTag(info);
2243
2244 // If the folder info reports that the associated folder is open, then verify that
2245 // it is actually opened. There have been a few instances where this gets out of sync.
2246 if (info.opened && openFolder == null) {
2247 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002249 info.opened = false;
2250 }
2251
Adam Cohenfb91f302012-06-11 15:45:18 -07002252 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 // Close any open folder
2254 closeFolder();
2255 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002256 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 } else {
2258 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 int folderScreen;
2260 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002261 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002262 // .. and close it
2263 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002264 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 // Close any folder open on the current screen
2266 closeFolder();
2267 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002268 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 }
2270 }
2271 }
2272 }
2273
Adam Cohen268c4752012-06-06 17:47:33 -07002274 /**
2275 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2276 * in the DragLayer in the exact absolute location of the original FolderIcon.
2277 */
2278 private void copyFolderIconToImage(FolderIcon fi) {
2279 final int width = fi.getMeasuredWidth();
2280 final int height = fi.getMeasuredHeight();
2281
2282 // Lazy load ImageView, Bitmap and Canvas
2283 if (mFolderIconImageView == null) {
2284 mFolderIconImageView = new ImageView(this);
2285 }
2286 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2287 mFolderIconBitmap.getHeight() != height) {
2288 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2289 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2290 }
2291
2292 DragLayer.LayoutParams lp;
2293 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2294 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2295 } else {
2296 lp = new DragLayer.LayoutParams(width, height);
2297 }
2298
Adam Cohen307fe232012-08-16 17:55:58 -07002299 // The layout from which the folder is being opened may be scaled, adjust the starting
2300 // view size by this scale factor.
2301 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002302 lp.customPosition = true;
2303 lp.x = mRectForFolderAnimation.left;
2304 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002305 lp.width = (int) (scale * width);
2306 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002307
2308 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2309 fi.draw(mFolderIconCanvas);
2310 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002311 if (fi.getFolder() != null) {
2312 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2313 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002314 }
Adam Cohen268c4752012-06-06 17:47:33 -07002315 // Just in case this image view is still in the drag layer from a previous animation,
2316 // we remove it and re-add it.
2317 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2318 mDragLayer.removeView(mFolderIconImageView);
2319 }
2320 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002321 if (fi.getFolder() != null) {
2322 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002323 }
Adam Cohen268c4752012-06-06 17:47:33 -07002324 }
2325
Adam Cohen2801caf2011-05-13 20:57:39 -07002326 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002327 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002328 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2329 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2330 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2331
Adam Cohenc51934b2011-07-26 21:07:43 -07002332 FolderInfo info = (FolderInfo) fi.getTag();
2333 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2334 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002335 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2336 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002337 }
2338
Adam Cohen268c4752012-06-06 17:47:33 -07002339 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2340 copyFolderIconToImage(fi);
2341 fi.setVisibility(View.INVISIBLE);
2342
Michael Jurka2ecf9952012-06-18 12:52:28 -07002343 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002344 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002345 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2346 oa.start();
2347 }
2348
Adam Cohen268c4752012-06-06 17:47:33 -07002349 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002350 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002351 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2352 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2353 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2354
Adam Cohen268c4752012-06-06 17:47:33 -07002355 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002356
Adam Cohen268c4752012-06-06 17:47:33 -07002357 // We remove and re-draw the FolderIcon in-case it has changed
2358 mDragLayer.removeView(mFolderIconImageView);
2359 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002360 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002361 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002362 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002363 oa.addListener(new AnimatorListenerAdapter() {
2364 @Override
2365 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002366 if (cl != null) {
2367 cl.clearFolderLeaveBehind();
2368 // Remove the ImageView copy of the FolderIcon and make the original visible.
2369 mDragLayer.removeView(mFolderIconImageView);
2370 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002371 }
2372 }
2373 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002374 oa.start();
2375 }
2376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002378 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 * is animated relative to the specified View. If the View is null, no animation
2380 * is played.
2381 *
2382 * @param folderInfo The FolderInfo describing the folder to open.
2383 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002384 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002385 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002386 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002387
Adam Cohena9cf38f2011-05-02 15:36:58 -07002388 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002389
Adam Cohen4554ee12011-08-03 16:13:21 -07002390 // Just verify that the folder hasn't already been added to the DragLayer.
2391 // There was a one-off crash where the folder had a parent already.
2392 if (folder.getParent() == null) {
2393 mDragLayer.addView(folder);
2394 mDragController.addDropTarget((DropTarget) folder);
2395 } else {
2396 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2397 folder.getParent() + ").");
2398 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002399 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002400 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002401
2402 // Notify the accessibility manager that this folder "window" has appeared and occluded
2403 // the workspace items
2404 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2405 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002406 }
2407
2408 public void closeFolder() {
2409 Folder folder = mWorkspace.getOpenFolder();
2410 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002411 if (folder.isEditingName()) {
2412 folder.dismissEditingName();
2413 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002414 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002415
2416 // Dismiss the folder cling
2417 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002418 }
2419 }
2420
2421 void closeFolder(Folder folder) {
2422 folder.getInfo().opened = false;
2423
2424 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2425 if (parent != null) {
2426 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2427 shrinkAndFadeInFolderIcon(fi);
2428 }
2429 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002430
2431 // Notify the accessibility manager that this folder "window" has disappeard and no
2432 // longer occludeds the workspace items
2433 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 }
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002437 if (!isDraggingEnabled()) return false;
2438 if (isWorkspaceLocked()) return false;
2439 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440
2441 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002442 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 }
2444
Michael Jurka0280c3b2010-09-17 15:00:07 -07002445 resetAddInfo();
2446 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002448 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002449 return true;
2450 }
2451
Winson Chung3d503fb2011-07-13 17:25:49 -07002452 // The hotseat touch handling does not go through Workspace, and we always allow long press
2453 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002454 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002455 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2456 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002457 if (itemUnderLongClick == null) {
2458 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002459 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2460 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002461 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002462 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2463 mWorkspace.startReordering();
2464 } else {
2465 startWallpaper();
2466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002468 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002470 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 }
2472 }
2473 }
2474 return true;
2475 }
2476
Winson Chung3d503fb2011-07-13 17:25:49 -07002477 boolean isHotseatLayout(View layout) {
2478 return mHotseat != null && layout != null &&
2479 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2480 }
2481 Hotseat getHotseat() {
2482 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002483 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002484 SearchDropTargetBar getSearchBar() {
2485 return mSearchDropTargetBar;
2486 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002487
Winson Chung3d503fb2011-07-13 17:25:49 -07002488 /**
2489 * Returns the CellLayout of the specified container at the specified screen.
2490 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002491 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002492 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2493 if (mHotseat != null) {
2494 return mHotseat.getLayout();
2495 } else {
2496 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002497 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002498 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002499 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002500 }
2501 }
2502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 Workspace getWorkspace() {
2504 return mWorkspace;
2505 }
2506
Daniel Sandler843e8602010-06-07 14:59:01 -04002507 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002508 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002509 }
2510
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002511 /**
2512 * Helper method for the cameraZoomIn/cameraZoomOut animations
2513 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 * @param scaleFactor The scale factor used for the zoom
2515 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002516 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002517 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002518 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002519 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002520
Winson Chung18f41f82012-05-09 13:28:10 -07002521 void disableWallpaperIfInAllApps() {
2522 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002523 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002524 if (mAppsCustomizeTabHost != null &&
2525 !mAppsCustomizeTabHost.isTransitioning()) {
2526 updateWallpaperVisibility(false);
2527 }
2528 }
2529 }
2530
Craig Mautner360310b2012-10-26 15:13:08 -07002531 private void setWorkspaceBackground(boolean workspace) {
2532 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002533 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002534 }
2535
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002536 void updateWallpaperVisibility(boolean visible) {
2537 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2538 int curflags = getWindow().getAttributes().flags
2539 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2540 if (wpflags != curflags) {
2541 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2542 }
Craig Mautner360310b2012-10-26 15:13:08 -07002543 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002544 }
2545
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002546 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2547 if (v instanceof LauncherTransitionable) {
2548 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2549 }
2550 }
2551
Michael Jurkabed61d22012-02-14 22:51:29 -08002552 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2553 if (v instanceof LauncherTransitionable) {
2554 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2555 }
Winson Chung70442722012-02-10 15:43:22 -08002556
2557 // Update the workspace transition step as well
2558 dispatchOnLauncherTransitionStep(v, 0f);
2559 }
2560
2561 private void dispatchOnLauncherTransitionStep(View v, float t) {
2562 if (v instanceof LauncherTransitionable) {
2563 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2564 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002565 }
2566
2567 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2568 if (v instanceof LauncherTransitionable) {
2569 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2570 }
Winson Chung70442722012-02-10 15:43:22 -08002571
2572 // Update the workspace transition step as well
2573 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002574 }
2575
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002576 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002577 * Things to test when changing the following seven functions.
2578 * - Home from workspace
2579 * - from center screen
2580 * - from other screens
2581 * - Home from all apps
2582 * - from center screen
2583 * - from other screens
2584 * - Back from all apps
2585 * - from center screen
2586 * - from other screens
2587 * - Launch app from workspace and quit
2588 * - with back
2589 * - with home
2590 * - Launch app from all apps and quit
2591 * - with back
2592 * - with home
2593 * - Go to a screen that's not the default, then all
2594 * apps, and launch and app, and go back
2595 * - with back
2596 * -with home
2597 * - On workspace, long press power and go back
2598 * - with back
2599 * - with home
2600 * - On all apps, long press power and go back
2601 * - with back
2602 * - with home
2603 * - On workspace, power off
2604 * - On all apps, power off
2605 * - Launch an app and turn off the screen while in that app
2606 * - Go back with home key
2607 * - Go back with back key TODO: make this not go to workspace
2608 * - From all apps
2609 * - From workspace
2610 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2611 * - From all apps
2612 * - From the center workspace
2613 * - From another workspace
2614 */
2615
2616 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002617 * Zoom the camera out from the workspace to reveal 'toView'.
2618 * Assumes that the view to show is anchored at either the very top or very bottom
2619 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002620 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002621 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002622 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002623 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002624 mStateAnimation.cancel();
2625 mStateAnimation = null;
2626 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002627 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002628
Winson Chungf0ea4d32011-06-06 14:27:16 -07002629 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2630 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2631 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002632 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002633 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002634 final int startDelay =
2635 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002636
Michael Jurkab3e22d92011-10-31 15:58:33 -07002637 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638
Michael Jurkad74c9842011-07-10 12:44:21 -07002639 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002640 Animator workspaceAnim =
2641 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642
2643 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002644 toView.setScaleX(scale);
2645 toView.setScaleY(scale);
2646 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2647 scaleAnim.
2648 scaleX(1f).scaleY(1f).
2649 setDuration(duration).
2650 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002651
Winson Chungf0ea4d32011-06-06 14:27:16 -07002652 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002653 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002654 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002655 .ofFloat(toView, "alpha", 0f, 1f)
2656 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002657 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002658 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2659 @Override
2660 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002661 if (animation == null) {
2662 throw new RuntimeException("animation is null");
2663 }
Winson Chung70442722012-02-10 15:43:22 -08002664 float t = (Float) animation.getAnimatedValue();
2665 dispatchOnLauncherTransitionStep(fromView, t);
2666 dispatchOnLauncherTransitionStep(toView, t);
2667 }
2668 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002669
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002670 // toView should appear right at the end of the workspace shrink
2671 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002672 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002673 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002674 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002675
2676 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002677 boolean animationCancelled = false;
2678
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002679 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002680 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002681 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002682 // Prepare the position
2683 toView.setTranslationX(0.0f);
2684 toView.setTranslationY(0.0f);
2685 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002686 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002688 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002689 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002690 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2691 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002692
Daniel Sandlere4f98912013-06-25 15:13:26 -04002693 if (mWorkspace != null
2694 && !springLoaded
2695 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002696 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002697 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002698 if (!animationCancelled) {
2699 updateWallpaperVisibility(false);
2700 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002701
2702 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002703 if (mSearchDropTargetBar != null) {
2704 mSearchDropTargetBar.hideSearchBar(false);
2705 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002706 }
2707
Adam Cohencff6af82011-09-13 14:51:53 -07002708 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002709 public void onAnimationCancel(Animator animation) {
2710 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 });
2713
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002714 if (workspaceAnim != null) {
2715 mStateAnimation.play(workspaceAnim);
2716 }
Michael Jurka1899a362011-11-03 13:50:45 -07002717
2718 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002719
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002720 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2721 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002722
2723 // If any of the objects being animated haven't been measured/laid out
2724 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002725 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002726 (mWorkspace.getMeasuredWidth() == 0) ||
2727 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002728 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002729 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002730
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002731 final AnimatorSet stateAnimation = mStateAnimation;
2732 final Runnable startAnimRunnable = new Runnable() {
2733 public void run() {
2734 // Check that mStateAnimation hasn't changed while
2735 // we waited for a layout/draw pass
2736 if (mStateAnimation != stateAnimation)
2737 return;
2738 setPivotsForZoom(toView, scale);
2739 dispatchOnLauncherTransitionStart(fromView, animated, false);
2740 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002741 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002742 }
2743 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002744 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002745 final ViewTreeObserver observer = toView.getViewTreeObserver();
2746 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2747 public void onGlobalLayout() {
2748 startAnimRunnable.run();
2749 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2750 }
2751 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002752 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002753 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002754 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 } else {
2756 toView.setTranslationX(0.0f);
2757 toView.setTranslationY(0.0f);
2758 toView.setScaleX(1.0f);
2759 toView.setScaleY(1.0f);
2760 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002761 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002762
Daniel Sandlere4f98912013-06-25 15:13:26 -04002763 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002764 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002765
2766 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002767 if (mSearchDropTargetBar != null) {
2768 mSearchDropTargetBar.hideSearchBar(false);
2769 }
Michael Jurkaabded662011-03-04 12:06:57 -08002770 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002771 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002772 dispatchOnLauncherTransitionStart(fromView, animated, false);
2773 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002774 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002775 dispatchOnLauncherTransitionStart(toView, animated, false);
2776 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002777 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002778 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002779 }
2780
2781 /**
2782 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002783 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002784 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002785 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002786 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2787 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002788
Michael Jurkab3e22d92011-10-31 15:58:33 -07002789 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002790 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002791 mStateAnimation.cancel();
2792 mStateAnimation = null;
2793 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002794 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002795
Winson Chungf0ea4d32011-06-06 14:27:16 -07002796 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002797 final int fadeOutDuration =
2798 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002799 final float scaleFactor = (float)
2800 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2801 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002802 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002803 Animator workspaceAnim = null;
2804
2805 if (toState == State.WORKSPACE) {
2806 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2807 workspaceAnim = mWorkspace.getChangeStateAnimation(
2808 Workspace.State.NORMAL, animated, stagger);
2809 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2810 workspaceAnim = mWorkspace.getChangeStateAnimation(
2811 Workspace.State.SPRING_LOADED, animated);
2812 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002813
Michael Jurkab3e22d92011-10-31 15:58:33 -07002814 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002815 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002816 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002818 final LauncherViewPropertyAnimator scaleAnim =
2819 new LauncherViewPropertyAnimator(fromView);
2820 scaleAnim.
2821 scaleX(scaleFactor).scaleY(scaleFactor).
2822 setDuration(duration).
2823 setInterpolator(new Workspace.ZoomInInterpolator());
2824
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002825 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002826 .ofFloat(fromView, "alpha", 1f, 0f)
2827 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002828 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002829 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2830 @Override
2831 public void onAnimationUpdate(ValueAnimator animation) {
2832 float t = 1f - (Float) animation.getAnimatedValue();
2833 dispatchOnLauncherTransitionStep(fromView, t);
2834 dispatchOnLauncherTransitionStep(toView, t);
2835 }
2836 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002837
Michael Jurka2ecf9952012-06-18 12:52:28 -07002838 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002839
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002840 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2841 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002842 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002843
2844 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002845 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002846 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002847 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002848 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002849 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2850 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002851 if (onCompleteRunnable != null) {
2852 onCompleteRunnable.run();
2853 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002854 mAppsCustomizeContent.updateCurrentPageScroll();
2855 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002856 }
2857 });
2858
Winson Chungf0ea4d32011-06-06 14:27:16 -07002859 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002860 if (workspaceAnim != null) {
2861 mStateAnimation.play(workspaceAnim);
2862 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionStart(fromView, animated, true);
2864 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002865 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002866 } else {
2867 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002868 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002869 dispatchOnLauncherTransitionStart(fromView, animated, true);
2870 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002871 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002872 dispatchOnLauncherTransitionStart(toView, animated, true);
2873 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002874 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002875 }
2876
Michael Jurkae326f182011-11-21 14:05:46 -08002877 @Override
2878 public void onTrimMemory(int level) {
2879 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002880 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002881 mAppsCustomizeTabHost.onTrimMemory();
2882 }
2883 }
2884
Winson Chung18f41f82012-05-09 13:28:10 -07002885 @Override
2886 public void onWindowFocusChanged(boolean hasFocus) {
2887 if (!hasFocus) {
2888 // When another window occludes launcher (like the notification shade, or recents),
2889 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2890 updateWallpaperVisibility(true);
2891 } else {
2892 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002893 mWorkspace.postDelayed(new Runnable() {
2894 @Override
2895 public void run() {
2896 disableWallpaperIfInAllApps();
2897 }
2898 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002899 }
2900 }
2901
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002902 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002903 showWorkspace(animated, null);
2904 }
2905
2906 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002907 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002908 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002909 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002910 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002911
Winson Chungc7d2b602012-05-16 17:02:20 -07002912 // Show the search bar (only animate if we were showing the drop target bar in spring
2913 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002914 if (mSearchDropTargetBar != null) {
2915 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2916 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002917
Michael Jurkab737ee62011-11-15 15:57:22 -08002918 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002919 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002920
2921 // Set focus to the AppsCustomize button
2922 if (mAllAppsButton != null) {
2923 mAllAppsButton.requestFocus();
2924 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002926
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002927 // Change the state *after* we've called all the transition code
2928 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002929
Winson Chung5fb63472011-02-02 17:03:37 -08002930 // Resume the auto-advance of widgets
2931 mUserPresent = true;
2932 updateRunning();
2933
alanv1d4fde62012-10-17 13:15:47 -07002934 // Send an accessibility event to announce the context change
2935 getWindow().getDecorView()
2936 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002937
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002938 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002939 }
2940
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002941 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04002942 }
2943
Michael Jurkab3e22d92011-10-31 15:58:33 -07002944 void showAllApps(boolean animated) {
2945 if (mState != State.WORKSPACE) return;
2946
2947 showAppsCustomizeHelper(animated, false);
2948 mAppsCustomizeTabHost.requestFocus();
2949
Michael Jurkab3e22d92011-10-31 15:58:33 -07002950 // Change the state *after* we've called all the transition code
2951 mState = State.APPS_CUSTOMIZE;
2952
2953 // Pause the auto-advance of widgets until we are out of AllApps
2954 mUserPresent = false;
2955 updateRunning();
2956 closeFolder();
2957
2958 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07002959 getWindow().getDecorView()
2960 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002961 }
2962
Adam Cohen7777d962011-08-18 18:58:38 -07002963 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002964 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002965 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002966 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002967 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002968 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002969 }
Adam Cohen7777d962011-08-18 18:58:38 -07002970
Adam Cohened66b2b2012-01-23 17:28:51 -08002971 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2972 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002973 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2974
Winson Chunge7a03942011-08-05 15:05:12 -07002975 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002976 @Override
2977 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002978 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002979 // Before we show workspace, hide all apps again because
2980 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2981 // clean up our state transition functions
2982 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002983 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002984 } else {
2985 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002986 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002987 }
2988 }, (extendedDelay ?
2989 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2990 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2991 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002992
Michael Jurkad3ef3062010-11-23 16:23:58 -08002993 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002994 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002995 final boolean animated = true;
2996 final boolean springLoaded = true;
2997 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002998 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002999 }
3000 // Otherwise, we are not in spring loaded mode, so don't do anything.
3001 }
3002
Michael Jurkab737ee62011-11-15 15:57:22 -08003003 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003004 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003005 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003006 }
3007 }
3008
3009 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003010 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003011 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003012 if (mDividerAnimator != null) {
3013 mDividerAnimator.cancel();
3014 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003015 mDividerAnimator = null;
3016 }
3017 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003018 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003019 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003020 int duration = 0;
3021 if (mSearchDropTargetBar != null) {
3022 duration = mSearchDropTargetBar.getTransitionInDuration();
3023 }
3024 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003025 mDividerAnimator.start();
3026 }
3027 }
3028 }
3029
Michael Jurkab3e22d92011-10-31 15:58:33 -07003030 void lockAllApps() {
3031 // TODO
3032 }
3033
3034 void unlockAllApps() {
3035 // TODO
3036 }
3037
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003039 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003041 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003042 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003043 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003044 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003045 int duration = 0;
3046 if (mSearchDropTargetBar != null) {
3047 duration = mSearchDropTargetBar.getTransitionInDuration();
3048 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003049 mHotseat.animate().alpha(1f).setDuration(duration);
3050 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003051 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003052 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053 }
3054 }
3055 }
3056
3057 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003058 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003059 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003060 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003061 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003062 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003063 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003064 int duration = 0;
3065 if (mSearchDropTargetBar != null) {
3066 duration = mSearchDropTargetBar.getTransitionOutDuration();
3067 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003068 mHotseat.animate().alpha(0f).setDuration(duration);
3069 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003070 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003071 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003072 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003073 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003074 }
3075
Patrick Dubroy5f445422011-02-18 14:35:21 -08003076 /**
3077 * Add an item from all apps or customize onto the given workspace screen.
3078 * If layout is null, add to the current screen.
3079 */
3080 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003081 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003082 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003083 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003084 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003085
Winson Chungdff8ebb2011-09-08 17:25:31 -07003086 /** Maps the current orientation to an index for referencing orientation correct global icons */
3087 private int getCurrentOrientationIndexForGlobalIcons() {
3088 // default - 0, landscape - 1
3089 switch (getResources().getConfiguration().orientation) {
3090 case Configuration.ORIENTATION_LANDSCAPE:
3091 return 1;
3092 default:
3093 return 0;
3094 }
3095 }
3096
Michael Jurkaae65ee32012-04-30 11:45:54 -07003097 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003098 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003099 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 // Look for the toolbar icon specified in the activity meta-data
3101 Bundle metaData = packageManager.getActivityInfo(
3102 activityName, PackageManager.GET_META_DATA).metaData;
3103 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003104 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003105 if (iconResId != 0) {
3106 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003107 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003108 }
3109 }
3110 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003111 // This can happen if the activity defines an invalid drawable
3112 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3113 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003114 } catch (Resources.NotFoundException nfe) {
3115 // This can happen if the activity defines an invalid drawable
3116 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3117 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003118 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003119 return null;
3120 }
3121
3122 // if successful in getting icon, return it; otherwise, set button to use default drawable
3123 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003124 int buttonId, ComponentName activityName, int fallbackDrawableId,
3125 String toolbarResourceName) {
3126 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003127 Resources r = getResources();
3128 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3129 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003130
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003131 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 // If we were unable to find the icon via the meta-data, use a generic one
3133 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003134 toolbarIcon = r.getDrawable(fallbackDrawableId);
3135 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003136 if (button != null) {
3137 button.setCompoundDrawables(toolbarIcon, null, null, null);
3138 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003139 return null;
3140 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003141 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003142 if (button != null) {
3143 button.setCompoundDrawables(toolbarIcon, null, null, null);
3144 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003145 return toolbarIcon.getConstantState();
3146 }
3147 }
3148
3149 // if successful in getting icon, return it; otherwise, set button to use default drawable
3150 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003151 int buttonId, ComponentName activityName, int fallbackDrawableId,
3152 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003153 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003154 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003155
Michael Jurka19e0fc52011-07-22 18:00:21 -07003156 if (button != null) {
3157 // If we were unable to find the icon via the meta-data, use a
3158 // generic one
3159 if (toolbarIcon == null) {
3160 button.setImageResource(fallbackDrawableId);
3161 } else {
3162 button.setImageDrawable(toolbarIcon);
3163 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003165
3166 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3167
Michael Jurka0423dcf2010-10-05 14:56:18 -07003168 }
3169
Winson Chung1b884092012-06-08 17:13:02 -07003170 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003171 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003172 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003173 }
3174
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003175 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003176 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003177 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003178 }
3179
Winson Chungbb185bd2011-11-21 12:31:42 -08003180 private void invalidatePressedFocusedStates(View container, View button) {
3181 if (container instanceof HolographicLinearLayout) {
3182 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3183 layout.invalidatePressedFocusedStates();
3184 } else if (button instanceof HolographicImageView) {
3185 HolographicImageView view = (HolographicImageView) button;
3186 view.invalidatePressedFocusedStates();
3187 }
3188 }
3189
Winson Chungc51db6a2011-10-05 11:44:49 -07003190 private boolean updateGlobalSearchIcon() {
3191 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003192 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003193 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003194 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003195
Winson Chung1cad91e2011-05-25 17:41:01 -07003196 final SearchManager searchManager =
3197 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3198 ComponentName activityName = searchManager.getGlobalSearchActivity();
3199 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003200 int coi = getCurrentOrientationIndexForGlobalIcons();
3201 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003202 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3203 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3204 if (sGlobalSearchIcon[coi] == null) {
3205 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3206 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3207 TOOLBAR_ICON_METADATA_NAME);
3208 }
3209
Winson Chungc51db6a2011-10-05 11:44:49 -07003210 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3211 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003212 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003213 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003214 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003215 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003216 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3217 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3218 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003219 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003220 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003221 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003222 }
3223 }
3224
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003225 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003226 final View searchButtonContainer = findViewById(R.id.search_button_container);
3227 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003228 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003229 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003230 }
3231
Winson Chungc51db6a2011-10-05 11:44:49 -07003232 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003233 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003234 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003235
Winson Chungc51db6a2011-10-05 11:44:49 -07003236 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003237 final SearchManager searchManager =
3238 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3239 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3240
3241 ComponentName activityName = null;
3242 if (globalSearchActivity != null) {
3243 // Check if the global search activity handles voice search
3244 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3245 intent.setPackage(globalSearchActivity.getPackageName());
3246 activityName = intent.resolveActivity(getPackageManager());
3247 }
3248
3249 if (activityName == null) {
3250 // Fallback: check if an activity other than the global search activity
3251 // resolves this
3252 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3253 activityName = intent.resolveActivity(getPackageManager());
3254 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003255 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003256 int coi = getCurrentOrientationIndexForGlobalIcons();
3257 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003258 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3259 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3260 if (sVoiceSearchIcon[coi] == null) {
3261 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3262 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3263 TOOLBAR_ICON_METADATA_NAME);
3264 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003265 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003266 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003267 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003268 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003270 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003271 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003272 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003273 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003274 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003275 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003276 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003277
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003278 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003279 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3280 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003281 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003282 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003283 }
3284
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003285 public void setVoiceButtonProxyVisible(boolean visible) {
3286 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3287 if (voiceButtonProxy != null) {
3288 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3289 }
3290 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003291 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003292 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003293 */
3294 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003295 final View marketButton = findViewById(R.id.market_button);
3296 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3297 // Find the app market activity by resolving an intent.
3298 // (If multiple app markets are installed, it will return the ResolverActivity.)
3299 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003300 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003301 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003302 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003303 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003304 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3305 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003306 marketButton.setVisibility(View.VISIBLE);
3307 } else {
3308 // We should hide and disable the view so that we don't try and restore the visibility
3309 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3310 marketButton.setVisibility(View.GONE);
3311 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003312 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003313 }
3314
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003315 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003316 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3317 Resources r = getResources();
3318 Drawable marketIconDrawable = d.newDrawable(r);
3319 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3320 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3321 marketIconDrawable.setBounds(0, 0, w, h);
3322
3323 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003324 }
3325
Michael Jurkad7c28052012-04-27 15:43:36 -07003326 @Override
3327 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003328 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003329 final List<CharSequence> text = event.getText();
3330 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003331 // Populate event with a fake title based on the current state.
3332 if (mState == State.APPS_CUSTOMIZE) {
3333 text.add(getString(R.string.all_apps_button_label));
3334 } else {
3335 text.add(getString(R.string.all_apps_home_button_label));
3336 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003337 return result;
3338 }
3339
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003340 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003341 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003342 */
3343 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3344 @Override
3345 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003346 closeSystemDialogs();
3347 }
3348 }
3349
3350 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003351 * Receives notifications whenever the appwidgets are reset.
3352 */
3353 private class AppWidgetResetObserver extends ContentObserver {
3354 public AppWidgetResetObserver() {
3355 super(new Handler());
3356 }
3357
3358 @Override
3359 public void onChange(boolean selfChange) {
3360 onAppWidgetReset();
3361 }
3362 }
3363
3364 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003365 * If the activity is currently paused, signal that we need to run the passed Runnable
3366 * in onResume.
3367 *
3368 * This needs to be called from incoming places where resources might have been loaded
3369 * while we are paused. That is becaues the Configuration might be wrong
3370 * when we're not running, and if it comes back to what it was when we
3371 * were paused, we are not restarted.
3372 *
3373 * Implementation of the method from LauncherModel.Callbacks.
3374 *
3375 * @return true if we are currently paused. The caller might be able to
3376 * skip some work in that case since we will come back again.
3377 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003378 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003379 if (mPaused) {
3380 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003381 if (deletePreviousRunnables) {
3382 while (mOnResumeCallbacks.remove(run)) {
3383 }
3384 }
Michael Jurka7607c2f2013-04-03 14:33:19 -07003385 mOnResumeCallbacks.add(run);
3386 return true;
3387 } else {
3388 return false;
3389 }
3390 }
3391
Michael Jurkac402cd92013-05-20 15:49:32 +02003392 private boolean waitUntilResume(Runnable run) {
3393 return waitUntilResume(run, false);
3394 }
3395
Michael Jurka7607c2f2013-04-03 14:33:19 -07003396 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003397 * If the activity is currently paused, signal that we need to re-run the loader
3398 * in onResume.
3399 *
3400 * This needs to be called from incoming places where resources might have been loaded
3401 * while we are paused. That is becaues the Configuration might be wrong
3402 * when we're not running, and if it comes back to what it was when we
3403 * were paused, we are not restarted.
3404 *
3405 * Implementation of the method from LauncherModel.Callbacks.
3406 *
3407 * @return true if we are currently paused. The caller might be able to
3408 * skip some work in that case since we will come back again.
3409 */
3410 public boolean setLoadOnResume() {
3411 if (mPaused) {
3412 Log.i(TAG, "setLoadOnResume");
3413 mOnResumeNeedsLoad = true;
3414 return true;
3415 } else {
3416 return false;
3417 }
3418 }
3419
3420 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003421 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003422 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003424 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003425 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003426 } else {
3427 return SCREEN_COUNT / 2;
3428 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003430
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 /**
3432 * Refreshes the shortcuts shown on the workspace.
3433 *
3434 * Implementation of the method from LauncherModel.Callbacks.
3435 */
3436 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003437 // If we're starting binding all over again, clear any bind calls we'd postponed in
3438 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3439 // from scratch again
3440 mOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003441
Michael Jurka7607c2f2013-04-03 14:33:19 -07003442 final Workspace workspace = mWorkspace;
Adam Cohendcd297f2013-06-18 13:13:40 -07003443 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003444 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003445 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 int count = workspace.getChildCount();
3447 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003448 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003449 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3450 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003452 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003453 if (mHotseat != null) {
3454 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003455 }
3456 }
3457
Adam Cohendcd297f2013-06-18 13:13:40 -07003458 @Override
3459 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003460 bindAddScreens(orderedScreenIds);
3461 mWorkspace.addExtraEmptyScreen();
3462 }
3463
3464 @Override
3465 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003466 int count = orderedScreenIds.size();
3467 for (int i = 0; i < count; i++) {
Winson Chung64359a52013-07-08 17:17:08 -07003468 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003469 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003470 }
3471
Adam Cohen39a06042013-07-19 14:30:12 -07003472 private boolean shouldShowWeightWatcher() {
3473 String spKey = LauncherAppState.getSharedPreferencesKey();
3474 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3475 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3476
3477 return show;
3478 }
3479
3480 private void toggleShowWeightWatcher() {
3481 String spKey = LauncherAppState.getSharedPreferencesKey();
3482 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3483 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3484
3485 show = !show;
3486
3487 SharedPreferences.Editor editor = sp.edit();
3488 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3489 editor.commit();
3490
3491 if (mWeightWatcher != null) {
3492 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3493 }
3494 }
3495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003496 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 * Bind the items start-end from the list.
3498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003500 */
Winson Chung64359a52013-07-08 17:17:08 -07003501 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3502 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003503 if (waitUntilResume(new Runnable() {
3504 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003505 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003506 }
3507 })) {
3508 return;
3509 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003510
Winson Chungf0c6ae02012-03-21 16:10:31 -07003511 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3512 Set<String> newApps = new HashSet<String>();
3513 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3514
Winson Chung64359a52013-07-08 17:17:08 -07003515 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3516 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003517 Workspace workspace = mWorkspace;
3518 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003519 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003520
3521 // Short circuit if we are loading dock items for a configuration which has no dock
3522 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3523 mHotseat == null) {
3524 continue;
3525 }
3526
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 switch (item.itemType) {
3528 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3529 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003530 ShortcutInfo info = (ShortcutInfo) item;
3531 String uri = info.intent.toUri(0).toString();
3532 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003533
Winson Chung64359a52013-07-08 17:17:08 -07003534 /*
3535 * TODO: FIX collision case
3536 */
Winson Chungce376632013-07-11 16:12:41 -07003537 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3538 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3539 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3540 throw new RuntimeException("OCCUPIED");
3541 }
Winson Chung64359a52013-07-08 17:17:08 -07003542 }
3543
Adam Cohendcd297f2013-06-18 13:13:40 -07003544 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3545 item.cellY, 1, 1);
Winson Chungbfeac062012-06-06 15:56:08 -07003546 boolean animateIconUp = false;
3547 synchronized (newApps) {
3548 if (newApps.contains(uri)) {
3549 animateIconUp = newApps.remove(uri);
3550 }
3551 }
Winson Chung64359a52013-07-08 17:17:08 -07003552 if (forceAnimateIcons) {
3553 // Animate all the applications up now
3554 shortcut.setAlpha(0f);
3555 shortcut.setScaleX(0f);
3556 shortcut.setScaleY(0f);
3557 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
3558 } else if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003559 // Prepare the view to be animated up
3560 shortcut.setAlpha(0f);
3561 shortcut.setScaleX(0f);
3562 shortcut.setScaleY(0f);
Adam Cohendcd297f2013-06-18 13:13:40 -07003563 mNewShortcutAnimateScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003564 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3565 mNewShortcutAnimateViews.add(shortcut);
3566 }
3567 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003568 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003569 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003570 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003571 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003572 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003573 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3574 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003575 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003576 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003577 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003578
Winson Chung64359a52013-07-08 17:17:08 -07003579 if (forceAnimateIcons) {
3580 // We post the animation slightly delayed to prevent slowdowns when we are loading
3581 // right after we return to launcher.
3582 mWorkspace.postDelayed(new Runnable() {
3583 public void run() {
3584 anim.playTogether(bounceAnims);
3585 anim.start();
3586 }
3587 }, NEW_APPS_ANIMATION_DELAY);
3588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003590 }
3591
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 /**
3593 * Implementation of the method from LauncherModel.Callbacks.
3594 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003595 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3596 if (waitUntilResume(new Runnable() {
3597 public void run() {
3598 bindFolders(folders);
3599 }
3600 })) {
3601 return;
3602 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003603 sFolders.clear();
3604 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003605 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606
3607 /**
3608 * Add the views for a widget to the workspace.
3609 *
3610 * Implementation of the method from LauncherModel.Callbacks.
3611 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003612 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3613 if (waitUntilResume(new Runnable() {
3614 public void run() {
3615 bindAppWidget(item);
3616 }
3617 })) {
3618 return;
3619 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003620
Daniel Sandler843e8602010-06-07 14:59:01 -04003621 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3622 if (DEBUG_WIDGETS) {
3623 Log.d(TAG, "bindAppWidget: " + item);
3624 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 final Workspace workspace = mWorkspace;
3626
3627 final int appWidgetId = item.appWidgetId;
3628 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003629 if (DEBUG_WIDGETS) {
3630 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3631 }
3632
Joe Onorato9c1289c2009-08-17 11:03:03 -04003633 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3634
Joe Onorato9c1289c2009-08-17 11:03:03 -04003635 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003636 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637
Adam Cohendcd297f2013-06-18 13:13:40 -07003638 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003639 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003640 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3641
Joe Onorato9c1289c2009-08-17 11:03:03 -04003642 workspace.requestLayout();
3643
Daniel Sandler843e8602010-06-07 14:59:01 -04003644 if (DEBUG_WIDGETS) {
3645 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3646 + (SystemClock.uptimeMillis()-start) + "ms");
3647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003648 }
3649
Adam Cohen1462de32012-07-24 22:34:36 -07003650 public void onPageBoundSynchronously(int page) {
3651 mSynchronouslyBoundPages.add(page);
3652 }
3653
Joe Onorato9c1289c2009-08-17 11:03:03 -04003654 /**
3655 * Callback saying that there aren't any more items to bind.
3656 *
3657 * Implementation of the method from LauncherModel.Callbacks.
3658 */
Adam Cohene25af792013-06-06 23:08:25 -07003659 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003660 if (waitUntilResume(new Runnable() {
3661 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003662 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003663 }
3664 })) {
3665 return;
3666 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667 if (mSavedState != null) {
3668 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003669 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003670 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003671 mSavedState = null;
3672 }
3673
Adam Cohen1462de32012-07-24 22:34:36 -07003674 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003675
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003676 // If we received the result of any pending adds while the loader was running (e.g. the
3677 // widget configuration forced an orientation change), process them now.
3678 for (int i = 0; i < sPendingAddList.size(); i++) {
3679 completeAdd(sPendingAddList.get(i));
3680 }
3681 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003682
Winson Chungc51db6a2011-10-05 11:44:49 -07003683 // Update the market app icon as necessary (the other icons will be managed in response to
3684 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003685 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003686
Winson Chungf0c6ae02012-03-21 16:10:31 -07003687 // Animate up any icons as necessary
3688 if (mVisible || mWorkspaceLoading) {
3689 Runnable newAppsRunnable = new Runnable() {
3690 @Override
3691 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003692 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003693 }
3694 };
Winson Chunga2413752012-04-03 14:22:34 -07003695
Adam Cohendcd297f2013-06-18 13:13:40 -07003696 boolean willSnapPage = mNewShortcutAnimateScreenId > -1 &&
3697 mNewShortcutAnimateScreenId != mWorkspace.getCurrentPage();
Winson Chunga2413752012-04-03 14:22:34 -07003698 if (canRunNewAppsAnimation()) {
3699 // If the user has not interacted recently, then either snap to the new page to show
3700 // the new-apps animation or just run them if they are to appear on the current page
3701 if (willSnapPage) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003702 mWorkspace.snapToScreenId(mNewShortcutAnimateScreenId, newAppsRunnable);
Winson Chunga2413752012-04-03 14:22:34 -07003703 } else {
3704 runNewAppsAnimation(false);
3705 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003706 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003707 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003708 // are on another page (or just normally if they are added to the current page)
3709 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003710 }
3711 }
3712
3713 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003714 if (upgradePath) {
3715 mWorkspace.saveWorkspaceToDb();
Adam Cohena0b57492013-06-14 15:33:35 -07003716 mWorkspace.stripDuplicateApps();
3717 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003718 }
Adam Cohen99894d92013-06-14 11:22:59 -07003719
Adam Cohen66a01fd2013-06-11 12:48:00 -07003720 mWorkspace.post(new Runnable() {
3721 @Override
3722 public void run() {
3723 onFinishBindingItems();
3724 }
3725 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003726 }
3727
Winson Chunga2413752012-04-03 14:22:34 -07003728 private boolean canRunNewAppsAnimation() {
3729 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3730 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3731 }
3732
Winson Chungc9168342013-06-26 14:54:55 -07003733 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3734 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3735 PropertyValuesHolder.ofFloat("alpha", 1f),
3736 PropertyValuesHolder.ofFloat("scaleX", 1f),
3737 PropertyValuesHolder.ofFloat("scaleY", 1f));
3738 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3739 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3740 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3741 return bounceAnim;
3742 }
3743
Winson Chungf0c6ae02012-03-21 16:10:31 -07003744 /**
3745 * Runs a new animation that scales up icons that were added while Launcher was in the
3746 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003747 *
3748 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003749 */
Winson Chunga2413752012-04-03 14:22:34 -07003750 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003751 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003752 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003753
3754 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003755 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3756 @Override
3757 public int compare(View a, View b) {
3758 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3759 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3760 int cellCountX = LauncherModel.getCellCountX();
3761 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3762 }
3763 });
Winson Chunga2413752012-04-03 14:22:34 -07003764
3765 // Animate each of the views in place (or show them immediately if requested)
3766 if (immediate) {
3767 for (View v : mNewShortcutAnimateViews) {
3768 v.setAlpha(1f);
3769 v.setScaleX(1f);
3770 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003771 }
Winson Chunga2413752012-04-03 14:22:34 -07003772 } else {
3773 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3774 View v = mNewShortcutAnimateViews.get(i);
Winson Chungc9168342013-06-26 14:54:55 -07003775 bounceAnims.add(createNewAppBounceAnimation(v, i));
Winson Chunga2413752012-04-03 14:22:34 -07003776 }
3777 anim.playTogether(bounceAnims);
3778 anim.addListener(new AnimatorListenerAdapter() {
3779 @Override
3780 public void onAnimationEnd(Animator animation) {
Michael Jurkad6dd7c82012-10-19 17:15:16 +02003781 if (mWorkspace != null) {
3782 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3783 }
Winson Chunga2413752012-04-03 14:22:34 -07003784 }
3785 });
3786 anim.start();
3787 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003788
3789 // Clean up
Adam Cohendcd297f2013-06-18 13:13:40 -07003790 mNewShortcutAnimateScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003791 mNewShortcutAnimateViews.clear();
3792 new Thread("clearNewAppsThread") {
3793 public void run() {
3794 mSharedPrefs.edit()
3795 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3796 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3797 .commit();
3798 }
3799 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003800 }
3801
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003802 @Override
3803 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003804 boolean searchVisible = updateGlobalSearchIcon();
3805 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003806 if (mSearchDropTargetBar != null) {
3807 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3808 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003809 }
3810
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003811 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003812 * Add the icons for all apps.
3813 *
3814 * Implementation of the method from LauncherModel.Callbacks.
3815 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003816 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003817 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3818 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003819 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003820 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 }
3823
3824 /**
3825 * A package was updated.
3826 *
3827 * Implementation of the method from LauncherModel.Callbacks.
3828 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003829 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3830 if (waitUntilResume(new Runnable() {
3831 public void run() {
3832 bindAppsUpdated(apps);
3833 }
3834 })) {
3835 return;
3836 }
3837
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003838 if (mWorkspace != null) {
3839 mWorkspace.updateShortcuts(apps);
3840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003841 }
3842
3843 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003844 * A package was uninstalled. We take both the super set of packageNames
3845 * in addition to specific applications to remove, the reason being that
3846 * this can be called when a package is updated as well. In that scenario,
3847 * we only remove specific components from the workspace, where as
3848 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003849 *
3850 * Implementation of the method from LauncherModel.Callbacks.
3851 */
Winson Chung83892cc2013-05-01 16:53:33 -07003852 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3853 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003854 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003855 if (waitUntilResume(new Runnable() {
3856 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003857 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003858 }
3859 })) {
3860 return;
3861 }
3862
Winson Chung64359a52013-07-08 17:17:08 -07003863 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003864 mWorkspace.removeItemsByPackageName(packageNames);
3865 } else {
3866 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003867 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003868
Winson Chunga1820962011-10-03 16:31:06 -07003869 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003870 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003871 }
Joe Onoratobe386092009-11-17 17:32:16 -08003872
3873 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003874 * A number of packages were updated.
3875 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003876 private ArrayList<Object> mWidgetsAndShortcuts;
3877 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003878 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003879 bindPackagesUpdated(mWidgetsAndShortcuts);
3880 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003881 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003882 };
3883
3884 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3885 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3886 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003887 return;
3888 }
3889
Winson Chung64359a52013-07-08 17:17:08 -07003890 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003891 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003893 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003894 }
3895
Winson Chung400438b2011-01-16 17:53:48 -08003896 private int mapConfigurationOriActivityInfoOri(int configOri) {
3897 final Display d = getWindowManager().getDefaultDisplay();
3898 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3899 switch (d.getRotation()) {
3900 case Surface.ROTATION_0:
3901 case Surface.ROTATION_180:
3902 // We are currently in the same basic orientation as the natural orientation
3903 naturalOri = configOri;
3904 break;
3905 case Surface.ROTATION_90:
3906 case Surface.ROTATION_270:
3907 // We are currently in the other basic orientation to the natural orientation
3908 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3909 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3910 break;
3911 }
3912
3913 int[] oriMap = {
3914 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3915 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3916 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3917 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3918 };
3919 // Since the map starts at portrait, we need to offset if this device's natural orientation
3920 // is landscape.
3921 int indexOffset = 0;
3922 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3923 indexOffset = 1;
3924 }
3925 return oriMap[(d.getRotation() + indexOffset) % 4];
3926 }
Adam Cohen446e9402011-09-15 18:21:21 -07003927
Winson Chung4b919f82012-05-01 10:44:08 -07003928 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003929 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003930 getResources().getBoolean(R.bool.allow_rotation);
3931 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003932 }
Winson Chung4b919f82012-05-01 10:44:08 -07003933 public void lockScreenOrientation() {
3934 if (isRotationEnabled()) {
3935 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3936 .getConfiguration().orientation));
3937 }
3938 }
3939 public void unlockScreenOrientation(boolean immediate) {
3940 if (isRotationEnabled()) {
3941 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003942 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003943 } else {
3944 mHandler.postDelayed(new Runnable() {
3945 public void run() {
3946 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3947 }
3948 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003949 }
Winson Chung4b919f82012-05-01 10:44:08 -07003950 }
Winson Chung400438b2011-01-16 17:53:48 -08003951 }
3952
Winson Chung82f55532011-08-09 14:14:23 -07003953 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003954 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003955 if (DISABLE_CLINGS) {
3956 return false;
3957 }
3958
Brett Chabot2a9e2282011-08-23 15:03:13 -07003959 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003960 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003961
Michael Jurkae233a8b2013-03-19 13:49:20 +01003962 // Restricted secondary users (child mode) will potentially have very few apps
3963 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003964// boolean supportsLimitedUsers =
3965// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3966// Account[] accounts = AccountManager.get(this).getAccounts();
3967// if (supportsLimitedUsers && accounts.length == 0) {
3968// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3969// Bundle restrictions = um.getUserRestrictions();
3970// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3971// return false;
3972// }
3973// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003974 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003975 }
Michael Jurka22143132012-10-04 17:42:38 +02003976
Winson Chung7d7541e2011-09-16 20:14:36 -07003977 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003978 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003979 if (cling != null) {
3980 cling.init(this, positionData);
3981 cling.setVisibility(View.VISIBLE);
3982 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3983 if (animate) {
3984 cling.buildLayer();
3985 cling.setAlpha(0f);
3986 cling.animate()
3987 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003988 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003989 .setDuration(SHOW_CLING_DURATION)
3990 .setStartDelay(delay)
3991 .start();
3992 } else {
3993 cling.setAlpha(1f);
3994 }
Michael Jurka22143132012-10-04 17:42:38 +02003995 cling.setFocusableInTouchMode(true);
3996 cling.post(new Runnable() {
3997 public void run() {
3998 cling.setFocusable(true);
3999 cling.requestFocus();
4000 }
4001 });
4002 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4003 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004004 }
4005 return cling;
4006 }
Michael Jurka22143132012-10-04 17:42:38 +02004007
Winson Chung7d7541e2011-09-16 20:14:36 -07004008 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004009 // To catch cases where siblings of top-level views are made invisible, just check whether
4010 // the cling is directly set to GONE before dismissing it.
4011 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004012 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004013 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004014 anim.addListener(new AnimatorListenerAdapter() {
4015 public void onAnimationEnd(Animator animation) {
4016 cling.setVisibility(View.GONE);
4017 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004018 // We should update the shared preferences on a background thread
4019 new Thread("dismissClingThread") {
4020 public void run() {
4021 SharedPreferences.Editor editor = mSharedPrefs.edit();
4022 editor.putBoolean(flag, true);
4023 editor.commit();
4024 }
4025 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004026 };
4027 });
4028 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004029 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004030 }
4031 }
Michael Jurka22143132012-10-04 17:42:38 +02004032
Winson Chung9d9d74f2011-09-19 11:49:12 -07004033 private void removeCling(int id) {
4034 final View cling = findViewById(id);
4035 if (cling != null) {
4036 final ViewGroup parent = (ViewGroup) cling.getParent();
4037 parent.post(new Runnable() {
4038 @Override
4039 public void run() {
4040 parent.removeView(cling);
4041 }
4042 });
Michael Jurka22143132012-10-04 17:42:38 +02004043 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004044 }
4045 }
Michael Jurka974c3862012-05-22 22:00:31 -07004046
4047 private boolean skipCustomClingIfNoAccounts() {
4048 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4049 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4050 if (customCling) {
4051 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004052 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004053 Account[] accounts = am.getAccountsByType("com.google");
4054 return accounts.length == 0;
4055 }
4056 return false;
4057 }
4058
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004060 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004061 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004062 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4063 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004064 // If we're not using the default workspace layout, replace workspace cling
4065 // with a custom workspace cling (usually specified in an overlay)
4066 // For now, only do this on tablets
4067 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004068 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004069 // Use a custom cling
4070 View cling = findViewById(R.id.workspace_cling);
4071 ViewGroup clingParent = (ViewGroup) cling.getParent();
4072 int clingIndex = clingParent.indexOfChild(cling);
4073 clingParent.removeViewAt(clingIndex);
4074 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4075 clingParent.addView(customCling, clingIndex);
4076 customCling.setId(R.id.workspace_cling);
4077 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004079 } else {
4080 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 }
4082 }
4083 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004084 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004085 if (isClingsEnabled() &&
4086 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004087 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004088 } else {
4089 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004090 }
4091 }
4092 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004093 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004094 if (isClingsEnabled() &&
4095 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4096 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004097 } else {
4098 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004099 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004101 }
4102 public boolean isFolderClingVisible() {
4103 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004104 if (cling != null) {
4105 return cling.getVisibility() == View.VISIBLE;
4106 }
4107 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004108 }
Winson Chung82f55532011-08-09 14:14:23 -07004109 public void dismissWorkspaceCling(View v) {
4110 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004112 }
4113 public void dismissAllAppsCling(View v) {
4114 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004115 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4116 }
4117 public void dismissFolderCling(View v) {
4118 Cling cling = (Cling) findViewById(R.id.folder_cling);
4119 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004120 }
4121
Winson Chung80baf5a2010-08-09 16:03:15 -07004122 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004123 * Prints out out state for debugging.
4124 */
4125 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004126 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004127 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004128 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4129 Log.d(TAG, "mRestoring=" + mRestoring);
4130 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4131 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004132 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004133 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004134
Winson Chung785d2eb2011-04-14 16:08:02 -07004135 if (mAppsCustomizeContent != null) {
4136 mAppsCustomizeContent.dumpState();
4137 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004138 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004139 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004140
4141 @Override
4142 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4143 super.dump(prefix, fd, writer, args);
4144 writer.println(" ");
4145 writer.println("Debug logs: ");
4146 for (int i = 0; i < sDumpLogs.size(); i++) {
4147 writer.println(" " + sDumpLogs.get(i));
4148 }
4149 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004150
4151 public static void dumpDebugLogsToConsole() {
4152 Log.d(TAG, "");
4153 Log.d(TAG, "*********************");
4154 Log.d(TAG, "Launcher debug logs: ");
4155 for (int i = 0; i < sDumpLogs.size(); i++) {
4156 Log.d(TAG, " " + sDumpLogs.get(i));
4157 }
4158 Log.d(TAG, "*********************");
4159 Log.d(TAG, "");
4160 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004161}
Michael Jurka2763be32011-02-24 11:19:57 -08004162
Michael Jurkaabded662011-03-04 12:06:57 -08004163interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004164 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004165 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004166 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004167 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004168 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004169}