blob: a16a33e5871750e670c6fa343c04182bd800687c [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;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
80import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
110import java.util.Collections;
111import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700130
Winson Chung70d72102011-08-12 11:24:35 -0700131 private static final int MENU_GROUP_WALLPAPER = 1;
132 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
133 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700134 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
135 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final int REQUEST_PICK_APPLICATION = 6;
140 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700142 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Michael Jurka8b805b12012-04-18 14:23:14 -0700144 private static final int REQUEST_BIND_APPWIDGET = 11;
145
Mathew Inwood876a8462013-06-14 14:12:41 +0100146 /**
147 * IntentStarter uses request codes starting with this. This must be greater than all activity
148 * request codes used internally.
149 */
150 protected static final int REQUEST_LAST = 100;
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
153
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800154 static final int SCREEN_COUNT = 5;
155 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800158 // To turn on these properties, type
159 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
160 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
161 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chung2672ff92012-05-04 16:22:30 -0700163 // The Intent extra that defines whether to ignore the launch animation
164 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400165 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
168 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700169 // Type: int
170 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700172 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
173 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: boolean
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
181 // Type: long
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
187 // Type: parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700193 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700195
Adam Cohen39a06042013-07-19 14:30:12 -0700196 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b8002013-08-15 15:44:26 -0700197 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700200 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700201 private State mState = State.WORKSPACE;
202 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700205 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
206 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700207 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700208 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800211 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
214 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700215 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800217 private final BroadcastReceiver mCloseSystemDialogsReceiver
218 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800219 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private LayoutInflater mInflater;
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700224 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800225 private DragLayer mDragLayer;
226 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700227 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 private AppWidgetManager mAppWidgetManager;
230 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Michael Jurkac9d95c52011-08-29 14:03:34 -0700232 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700233 private AppWidgetProviderInfo mPendingAddWidgetInfo;
234
Michael Jurka0280c3b2010-09-17 15:00:07 -0700235 private int[] mTmpAddItemCellCoordinates = new int[2];
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private FolderInfo mFolderInfo;
238
Winson Chung3d503fb2011-07-13 17:25:49 -0700239 private Hotseat mHotseat;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700240 private View mOverviewPanel;
241
Michael Jurka838a4ca2011-02-07 13:33:06 -0800242 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700243
Winson Chungc51db6a2011-10-05 11:44:49 -0700244 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700245 private AppsCustomizeTabHost mAppsCustomizeTabHost;
246 private AppsCustomizePagedView mAppsCustomizeContent;
247 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700250 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
251 // scroll issues (because the workspace may not have been measured yet) and extra work.
252 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
253 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254
255 private SpannableStringBuilder mDefaultKeySsb = null;
256
Joe Onorato9c1289c2009-08-17 11:03:03 -0400257 private boolean mWorkspaceLoading = true;
258
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800259 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 private boolean mRestoring;
261 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700262 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263
Michael Jurka1e2f4652013-07-08 18:03:46 -0700264 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700265 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
266
Winson Chung4a2afa32012-07-19 14:53:05 -0700267 // Keep track of whether the user has left launcher
268 private static boolean sPausedFromUserAction = false;
269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 private Bundle mSavedInstanceState;
271
Joe Onorato9c1289c2009-08-17 11:03:03 -0400272 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800273 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800274 private boolean mUserPresent = true;
275 private boolean mVisible = false;
276 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700277 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400278
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700279 private static LocaleConfiguration sLocaleConfiguration = null;
280
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700281 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700282
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700283 private Intent mAppMarketIntent = null;
284
Adam Cohended9f8d2010-11-03 13:25:16 -0700285 // Related to the auto-advancing of widgets
286 private final int ADVANCE_MSG = 1;
287 private final int mAdvanceInterval = 20000;
288 private final int mAdvanceStagger = 250;
289 private long mAutoAdvanceSentTime;
290 private long mAutoAdvanceTimeLeft = -1;
291 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
292 new HashMap<View, AppWidgetProviderInfo>();
293
Winson Chung400438b2011-01-16 17:53:48 -0800294 // Determines how long to wait after a rotation before restoring the screen orientation to
295 // match the sensor state.
296 private final int mRestoreScreenOrientationDelay = 500;
297
Michael Jurka4ef207b2010-11-29 17:05:45 -0800298 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700299 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
300 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
301 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800302
Craig Mautner360310b2012-10-26 15:13:08 -0700303 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700304
Adam Cohen1462de32012-07-24 22:34:36 -0700305 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
306
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700307 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
308
Winson Chung46353de2012-02-16 14:05:10 -0800309 // We only want to get the SharedPreferences once since it does an FS stat each time we get
310 // it from the context.
311 private SharedPreferences mSharedPrefs;
312
Adam Cohene25af792013-06-06 23:08:25 -0700313 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
314
Winson Chungf0c6ae02012-03-21 16:10:31 -0700315 // Holds the page that we need to animate to, and the icon views that we need to animate up
316 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700317 private ImageView mFolderIconImageView;
318 private Bitmap mFolderIconBitmap;
319 private Canvas mFolderIconCanvas;
320 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700321
Michael Jurkaddd62e92011-02-16 17:49:14 -0800322 private BubbleTextView mWaitingForResume;
323
Michael Jurka22143132012-10-04 17:42:38 +0200324 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
325 = new HideFromAccessibilityHelper();
326
Michael Jurkac1f5d262011-09-30 19:32:27 -0700327 private Runnable mBuildLayersRunnable = new Runnable() {
328 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700329 if (mWorkspace != null) {
330 mWorkspace.buildPageHardwareLayers();
331 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700332 }
333 };
334
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800335 private static ArrayList<PendingAddArguments> sPendingAddList
336 = new ArrayList<PendingAddArguments>();
337
Michael Jurka0a457bf2012-11-19 14:05:05 -0800338 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
339
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800340 private static class PendingAddArguments {
341 int requestCode;
342 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700343 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700344 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800345 int cellX;
346 int cellY;
347 }
348
Daniel Sandlerff02d492013-08-05 02:12:05 -0400349 private Stats mStats;
350
Michael Jurka0a457bf2012-11-19 14:05:05 -0800351 private static boolean isPropertyEnabled(String propertyName) {
352 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700353 }
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 @Override
356 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700357 if (DEBUG_STRICT_MODE) {
358 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
359 .detectDiskReads()
360 .detectDiskWrites()
361 .detectNetwork() // or .detectAll() for all detectable problems
362 .penaltyLog()
363 .build());
364 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
365 .detectLeakedSqlLiteObjects()
366 .detectLeakedClosableObjects()
367 .penaltyLog()
368 .penaltyDeath()
369 .build());
370 }
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400373
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400374 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400375 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700376
377 // Determine the dynamic grid properties
378 Point smallestSize = new Point();
379 Point largestSize = new Point();
380 Point realSize = new Point();
381 Display display = getWindowManager().getDefaultDisplay();
382 display.getCurrentSizeRange(smallestSize, largestSize);
383 display.getRealSize(realSize);
384 // Lazy-initialize the dynamic grid
385 DeviceProfile grid = app.initDynamicGrid(this,
386 Math.min(smallestSize.x, smallestSize.y),
387 Math.min(largestSize.x, largestSize.y),
388 realSize.x, realSize.y);
389
390 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400391 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700392 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800393 mModel = app.setLauncher(this);
394 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400395 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700397
Daniel Sandlerff02d492013-08-05 02:12:05 -0400398 mStats = new Stats(this);
399
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700400 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700401 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
402 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700403
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700404 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
405 // this also ensures that any synchronous binding below doesn't re-trigger another
406 // LauncherModel load.
407 mPaused = false;
408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200410 android.os.Debug.startMethodTracing(
411 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 }
413
414 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 setContentView(R.layout.launcher);
Winson Chung5f8afe62013-08-12 16:19:28 -0700416 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700418 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800420 registerContentObservers();
421
Joe Onorato7c312c12009-08-13 21:36:53 -0700422 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700423
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 mSavedState = savedInstanceState;
425 restoreState(mSavedState);
426
Winson Chunga12a2502010-12-20 14:41:35 -0800427 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700428 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200429 mAppsCustomizeContent.onPackagesUpdated(
430 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700431 }
Winson Chunga12a2502010-12-20 14:41:35 -0800432
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 if (PROFILE_STARTUP) {
434 android.os.Debug.stopMethodTracing();
435 }
436
437 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700438 if (sPausedFromUserAction) {
439 // If the user leaves launcher, then we should just load items asynchronously when
440 // they return.
441 mModel.startLoader(true, -1);
442 } else {
443 // We only load the page synchronously if the user rotates (or triggers a
444 // configuration change) while launcher is in the foreground
445 mModel.startLoader(true, mWorkspace.getCurrentPage());
446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448
449 // For handling default keys
450 mDefaultKeySsb = new SpannableStringBuilder();
451 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800452
453 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
454 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800455
Adam Cohenf9426d52012-06-04 17:26:21 -0700456 updateGlobalIcons();
457
458 // On large interfaces, we want the screen to auto-rotate based on the current orientation
459 unlockScreenOrientation(true);
460 }
461
Winson Chung4a2afa32012-07-19 14:53:05 -0700462 protected void onUserLeaveHint() {
463 super.onUserLeaveHint();
464 sPausedFromUserAction = true;
465 }
466
Winson Chung98ca0f72013-07-29 12:58:51 -0700467 /** To be overriden by subclasses to hint to Launcher that we have custom content */
468 protected boolean hasCustomContentToLeft() {
469 return false;
470 }
471
Adam Cohenf9426d52012-06-04 17:26:21 -0700472 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700473 boolean searchVisible = false;
474 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800475 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700476 int coi = getCurrentOrientationIndexForGlobalIcons();
477 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
478 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700479 updateAppMarketIcon();
480 searchVisible = updateGlobalSearchIcon();
481 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700482 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700483 if (sGlobalSearchIcon[coi] != null) {
484 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700485 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700486 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700487 if (sVoiceSearchIcon[coi] != null) {
488 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700489 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700490 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700491 if (sAppMarketIcon[coi] != null) {
492 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800493 }
Winson Chungadf0c182012-08-23 14:59:07 -0700494 if (mSearchDropTargetBar != null) {
495 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 }
Romain Guycbb89e42009-06-08 15:52:54 -0700498
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700500 if (sLocaleConfiguration == null) {
501 new AsyncTask<Void, Void, LocaleConfiguration>() {
502 @Override
503 protected LocaleConfiguration doInBackground(Void... unused) {
504 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
505 readConfiguration(Launcher.this, localeConfiguration);
506 return localeConfiguration;
507 }
508
509 @Override
510 protected void onPostExecute(LocaleConfiguration result) {
511 sLocaleConfiguration = result;
512 checkForLocaleChange(); // recursive, but now with a locale configuration
513 }
514 }.execute();
515 return;
516 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 final Configuration configuration = getResources().getConfiguration();
519
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700520 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 final String locale = configuration.locale.toString();
522
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700523 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 final int mcc = configuration.mcc;
525
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700526 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 final int mnc = configuration.mnc;
528
Romain Guy84f296c2009-11-04 15:00:44 -0800529 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530
Romain Guy84f296c2009-11-04 15:00:44 -0800531 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700532 sLocaleConfiguration.locale = locale;
533 sLocaleConfiguration.mcc = mcc;
534 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700535
Joe Onorato0589f0f2010-02-08 13:44:00 -0800536 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700537
538 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
539 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700540 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700541 public void run() {
542 writeConfiguration(Launcher.this, localeConfiguration);
543 }
544 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700545 }
546 }
547
548 private static class LocaleConfiguration {
549 public String locale;
550 public int mcc = -1;
551 public int mnc = -1;
552 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700553
Romain Guy98d01652009-06-30 16:21:04 -0700554 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
555 DataInputStream in = null;
556 try {
557 in = new DataInputStream(context.openFileInput(PREFERENCES));
558 configuration.locale = in.readUTF();
559 configuration.mcc = in.readInt();
560 configuration.mnc = in.readInt();
561 } catch (FileNotFoundException e) {
562 // Ignore
563 } catch (IOException e) {
564 // Ignore
565 } finally {
566 if (in != null) {
567 try {
568 in.close();
569 } catch (IOException e) {
570 // Ignore
571 }
572 }
573 }
574 }
575
576 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
577 DataOutputStream out = null;
578 try {
579 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
580 out.writeUTF(configuration.locale);
581 out.writeInt(configuration.mcc);
582 out.writeInt(configuration.mnc);
583 out.flush();
584 } catch (FileNotFoundException e) {
585 // Ignore
586 } catch (IOException e) {
587 //noinspection ResultOfMethodCallIgnored
588 context.getFileStreamPath(PREFERENCES).delete();
589 } finally {
590 if (out != null) {
591 try {
592 out.close();
593 } catch (IOException e) {
594 // Ignore
595 }
596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598 }
599
Bjorn Bringertc459e522013-06-07 19:36:01 +0100600 public LayoutInflater getInflater() {
601 return mInflater;
602 }
603
Adam Cohen716b51e2011-06-30 12:09:54 -0700604 public DragLayer getDragLayer() {
605 return mDragLayer;
606 }
607
Winson Chung36a62fe2012-05-06 18:04:42 -0700608 boolean isDraggingEnabled() {
609 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
610 // that is subsequently removed from the workspace in startBinding().
611 return !mModel.isLoadingWorkspace();
612 }
613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 static int getScreen() {
615 synchronized (sLock) {
616 return sScreen;
617 }
618 }
619
620 static void setScreen(int screen) {
621 synchronized (sLock) {
622 sScreen = screen;
623 }
624 }
625
Winson Chung557d6ed2011-07-08 15:34:52 -0700626 /**
627 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
628 * a configuration step, this allows the proper animations to run after other transitions.
629 */
630 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700631 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 switch (args.requestCode) {
633 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700634 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700635 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800636 break;
637 case REQUEST_PICK_SHORTCUT:
638 processShortcut(args.intent);
639 break;
640 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700641 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700643 result = true;
644 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800645 case REQUEST_CREATE_APPWIDGET:
646 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700647 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700648 result = true;
649 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800650 case REQUEST_PICK_WALLPAPER:
651 // We just wanted the activity result here so we can clear mWaitingForResult
652 break;
653 }
Michael Jurka27614d22012-04-02 06:40:22 -0700654 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
655 // if you turned the screen off and then back while in All Apps, Launcher would not
656 // return to the workspace. Clearing mAddInfo.container here fixes this issue
657 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700658 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 }
660
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700662 protected void onActivityResult(
663 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700664 // Reset the startActivity waiting flag
665 mWaitingForResult = false;
666
Michael Jurka8b805b12012-04-18 14:23:14 -0700667 if (requestCode == REQUEST_BIND_APPWIDGET) {
668 int appWidgetId = data != null ?
669 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
670 if (resultCode == RESULT_CANCELED) {
671 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
672 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700673 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700674 }
675 return;
676 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700677 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
679 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700680
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 // We have special handling for widgets
682 if (isWidgetDrop) {
683 int appWidgetId = data != null ?
684 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700685 if (appWidgetId < 0) {
686 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
687 "widget configuration activity.");
688 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
689 } else {
690 completeTwoStageWidgetDrop(resultCode, appWidgetId);
691 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800692 return;
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 // The pattern used here is that a user PICKs a specific application,
696 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
699 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700700 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800701 final PendingAddArguments args = new PendingAddArguments();
702 args.requestCode = requestCode;
703 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700705 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 args.cellX = mPendingAddInfo.cellX;
707 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 if (isWorkspaceLocked()) {
709 sPendingAddList.add(args);
710 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700711 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800714 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700715 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800716 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
717 null);
718 }
719
720 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700721 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700722 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800723 Runnable onCompleteRunnable = null;
724 int animationType = 0;
725
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700726 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800727 if (resultCode == RESULT_OK) {
728 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
729 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700730 mPendingAddWidgetInfo);
731 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800732 onCompleteRunnable = new Runnable() {
733 @Override
734 public void run() {
735 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700736 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800737 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
738 null);
739 }
740 };
741 } else if (resultCode == RESULT_CANCELED) {
742 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
743 onCompleteRunnable = new Runnable() {
744 @Override
745 public void run() {
746 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
747 null);
748 }
749 };
750 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700751 if (mDragLayer.getAnimatedView() != null) {
752 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
753 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
754 animationType, boundWidget, true);
755 } else {
756 // The animated view may be null in the case of a rotation during widget configuration
757 onCompleteRunnable.run();
758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 }
760
761 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700762 protected void onStop() {
763 super.onStop();
764 FirstFrameAnimatorHelper.setIsVisible(false);
765 }
766
767 @Override
768 protected void onStart() {
769 super.onStart();
770 FirstFrameAnimatorHelper.setIsVisible(true);
771 }
772
773 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200775 long startTime = 0;
776 if (DEBUG_RESUME_TIME) {
777 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400778 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700781
Winson Chung4a2afa32012-07-19 14:53:05 -0700782 // Restore the previous launcher state
783 if (mOnResumeState == State.WORKSPACE) {
784 showWorkspace(false);
785 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
786 showAllApps(false);
787 }
788 mOnResumeState = State.NONE;
789
Craig Mautner360310b2012-10-26 15:13:08 -0700790 // Background was set to gradient in onPause(), restore to black if in all apps.
791 setWorkspaceBackground(mState == State.WORKSPACE);
792
Winson Chungde0fb8f2012-05-08 14:37:08 -0700793 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700794 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700795
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800796 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700797 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700798 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400799 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700800 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400801 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700802 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700804 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200805 // We might have postponed some bind calls until onResume (see waitUntilResume) --
806 // execute them here
807 long startTimeCallbacks = 0;
808 if (DEBUG_RESUME_TIME) {
809 startTimeCallbacks = System.currentTimeMillis();
810 }
811
812 if (mAppsCustomizeContent != null) {
813 mAppsCustomizeContent.setBulkBind(true);
814 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700815 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
816 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200817 }
818 if (mAppsCustomizeContent != null) {
819 mAppsCustomizeContent.setBulkBind(false);
820 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700821 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200822 if (DEBUG_RESUME_TIME) {
823 Log.d(TAG, "Time spent processing callbacks in onResume: " +
824 (System.currentTimeMillis() - startTimeCallbacks));
825 }
Michael Jurka447bf842013-05-15 14:52:15 +0200826 }
Michael Jurka54554252013-08-01 12:52:23 +0200827 if (mOnResumeCallbacks.size() > 0) {
828 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
829 mOnResumeCallbacks.get(i).run();
830 }
831 mOnResumeCallbacks.clear();
832 }
Winson Chunge4e50662012-01-23 14:45:13 -0800833
834 // Reset the pressed state of icons that were locked in the press state while activities
835 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800836 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800837 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800838 mWaitingForResume.setStayPressed(false);
839 }
Winson Chunge4e50662012-01-23 14:45:13 -0800840 if (mAppsCustomizeContent != null) {
841 // Resets the previous all apps icon press state
842 mAppsCustomizeContent.resetDrawableState();
843 }
Adam Cohen06dff352012-06-01 17:17:08 -0700844 // It is possible that widgets can receive updates while launcher is not in the foreground.
845 // Consequently, the widgets will be inflated in the orientation of the foreground activity
846 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
847 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700848 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700849
850 // Again, as with the above scenario, it's possible that one or more of the global icons
851 // were updated in the wrong orientation.
852 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200853 if (DEBUG_RESUME_TIME) {
854 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
855 }
Adam Cohen06dff352012-06-01 17:17:08 -0700856 }
857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700860 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
861 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
862 // when Launcher resumes and we are still in AllApps.
863 updateWallpaperVisibility(true);
864
Winson Chung997a9232013-07-24 15:33:46 -0700865 // Ensure that items added to Launcher are queued until Launcher returns
866 InstallShortcutReceiver.enableInstallQueue();
867
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700868 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700869 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800870 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700871 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700872 }
Romain Guycbb89e42009-06-08 15:52:54 -0700873
Adam Cohen66a01fd2013-06-11 12:48:00 -0700874 protected void onFinishBindingItems() {
875 }
876
Adam Cohenbffe7452013-07-22 18:21:45 -0700877 QSBScroller mQsbScroller = new QSBScroller() {
878 int scrollY = 0;
879
880 @Override
881 public void setScrollY(int scroll) {
882 scrollY = scroll;
883
884 if (mWorkspace.isOnOrMovingToCustomContent()) {
885 mSearchDropTargetBar.setTranslationY(- scrollY);
886 }
887 }
888 };
889
890 public void resetQSBScroll() {
891 mSearchDropTargetBar.animate().translationY(0).start();
892 }
893
894 public interface CustomContentCallbacks {
895 // Custom content is completely shown
896 public void onShow();
897
898 // Custom content is completely hidden
899 public void onHide();
900 }
901
902 public interface QSBScroller {
903 public void setScrollY(int scrollY);
904 }
905
Adam Cohen66a01fd2013-06-11 12:48:00 -0700906 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700907 public QSBScroller addToCustomContentPage(View customContent) {
908 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700909 }
910
Winson Chung98ca0f72013-07-29 12:58:51 -0700911 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700912 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700913 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700914 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700915 }
916
Adam Cohenedb40762013-07-18 16:45:45 -0700917 // The custom content needs to offset its content to account for the QSB
918 public int getTopOffsetForCustomContent() {
919 return mWorkspace.getPaddingTop();
920 }
921
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700922 @Override
923 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 // Flag the loader to stop early before switching
925 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700926 if (mAppsCustomizeContent != null) {
927 mAppsCustomizeContent.surrender();
928 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800929 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700930 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700931
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800932 // We can't hide the IME if it was forced open. So don't bother
933 /*
934 @Override
935 public void onWindowFocusChanged(boolean hasFocus) {
936 super.onWindowFocusChanged(hasFocus);
937
938 if (hasFocus) {
939 final InputMethodManager inputManager = (InputMethodManager)
940 getSystemService(Context.INPUT_METHOD_SERVICE);
941 WindowManager.LayoutParams lp = getWindow().getAttributes();
942 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
943 android.os.Handler()) {
944 protected void onReceiveResult(int resultCode, Bundle resultData) {
945 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
946 }
947 });
948 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
949 }
950 }
951 */
952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 private boolean acceptFilter() {
954 final InputMethodManager inputManager = (InputMethodManager)
955 getSystemService(Context.INPUT_METHOD_SERVICE);
956 return !inputManager.isFullscreenMode();
957 }
958
959 @Override
960 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700961 final int uniChar = event.getUnicodeChar();
962 final boolean handled = super.onKeyDown(keyCode, event);
963 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
964 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
966 keyCode, event);
967 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700968 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700969 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700970 // showSearchDialog()
971 // If there are multiple keystrokes before the search dialog takes focus,
972 // onSearchRequested() will be called for every keystroke,
973 // but it is idempotent, so it's fine.
974 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976 }
977
Joe Onorato8a9625e2010-01-28 15:55:35 -0800978 // Eat the long press event so the keyboard doesn't come up.
979 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
980 return true;
981 }
982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 return handled;
984 }
985
Karl Rosaen138a0412009-04-23 19:00:21 -0700986 private String getTypedText() {
987 return mDefaultKeySsb.toString();
988 }
989
990 private void clearTypedText() {
991 mDefaultKeySsb.clear();
992 mDefaultKeySsb.clearSpans();
993 Selection.setSelection(mDefaultKeySsb, 0);
994 }
995
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700997 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
998 * State
999 */
1000 private static State intToState(int stateOrdinal) {
1001 State state = State.WORKSPACE;
1002 final State[] stateValues = State.values();
1003 for (int i = 0; i < stateValues.length; i++) {
1004 if (stateValues[i].ordinal() == stateOrdinal) {
1005 state = stateValues[i];
1006 break;
1007 }
1008 }
1009 return state;
1010 }
1011
1012 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 * Restores the previous state, if it exists.
1014 *
1015 * @param savedState The previous state.
1016 */
1017 private void restoreState(Bundle savedState) {
1018 if (savedState == null) {
1019 return;
1020 }
1021
Michael Jurka883f55b2010-10-21 15:47:14 -07001022 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001023 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001024 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001025 }
1026
Winson Chungf0c6ae02012-03-21 16:10:31 -07001027 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001029 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 }
1031
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001033 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1036 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001037 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1039 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001040 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1041 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1042 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001043 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 mRestoring = true;
1045 }
1046
1047 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1048 if (renameFolder) {
1049 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001050 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 mRestoring = true;
1052 }
Winson Chunga12a2502010-12-20 14:41:35 -08001053
Winson Chung785d2eb2011-04-14 16:08:02 -07001054 // Restore the AppsCustomize tab
1055 if (mAppsCustomizeTabHost != null) {
1056 String curTab = savedState.getString("apps_customize_currentTab");
1057 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001058 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001059 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001060 mAppsCustomizeContent.loadAssociatedPages(
1061 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001062 }
1063
Winson Chung5afbf7b2011-07-25 11:53:08 -07001064 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1065 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 }
1068
1069 /**
1070 * Finds all the views we need and configure them properly.
1071 */
1072 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001074
Craig Mautner360310b2012-10-26 15:13:08 -07001075 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001076 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1077 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001078
1079 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1080 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
Winson Chung4c98d922011-05-31 16:50:48 -07001082 // Setup the drag layer
1083 mDragLayer.setup(this, dragController);
1084
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 // Setup the hotseat
1086 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1087 if (mHotseat != null) {
1088 mHotseat.setup(this);
1089 }
1090
Adam Cohenf358a4b2013-07-23 16:47:31 -07001091 mOverviewPanel = findViewById(R.id.overview_panel);
1092 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1093 @Override
1094 public void onClick(View arg0) {
1095 showAllApps(true);
1096 }
1097 });
1098 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1099 @Override
1100 public void onClick(View arg0) {
1101 startWallpaper();
1102 }
1103 });
1104
Winson Chung4c98d922011-05-31 16:50:48 -07001105 // Setup the workspace
1106 mWorkspace.setHapticFeedbackEnabled(false);
1107 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001108 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001109 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001110
Winson Chungf0ea4d32011-06-06 14:27:16 -07001111 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001112 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001113
Winson Chungf0ea4d32011-06-06 14:27:16 -07001114 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001115 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001116 mAppsCustomizeContent = (AppsCustomizePagedView)
1117 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1118 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001119
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001121 dragController.setDragScoller(mWorkspace);
1122 dragController.setScrollView(mDragLayer);
1123 dragController.setMoveTarget(mWorkspace);
1124 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001125 if (mSearchDropTargetBar != null) {
1126 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001127 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001128
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001129 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001130 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001131 mWeightWatcher = new WeightWatcher(this);
1132 mWeightWatcher.setAlpha(0.5f);
1133 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001134 new FrameLayout.LayoutParams(
1135 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001136 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001137 Gravity.BOTTOM)
1138 );
Adam Cohen39a06042013-07-19 14:30:12 -07001139
1140 boolean show = shouldShowWeightWatcher();
1141 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
1145 /**
1146 * Creates a view representing a shortcut.
1147 *
1148 * @param info The data structure describing the shortcut.
1149 *
1150 * @return A View inflated from R.layout.application.
1151 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001152 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001154 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156
1157 /**
1158 * Creates a view representing a shortcut inflated from the specified resource.
1159 *
1160 * @param layoutResId The id of the XML layout used to create the shortcut.
1161 * @param parent The group the shortcut belongs to.
1162 * @param info The data structure describing the shortcut.
1163 *
1164 * @return A View inflated from layoutResId.
1165 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001166 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001167 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1168 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 return favorite;
1171 }
1172
1173 /**
1174 * Add an application shortcut to the workspace.
1175 *
1176 * @param data The intent describing the application.
1177 * @param cellInfo The position on screen where to create the shortcut.
1178 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001179 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001180 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001181 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182
Adam Cohenfbba09b2011-07-18 21:43:05 -07001183 // First we check if we already know the exact location where we want to add this item.
1184 if (cellX >= 0 && cellY >= 0) {
1185 cellXY[0] = cellX;
1186 cellXY[1] = cellY;
1187 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001188 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 return;
1190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001192 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001193
Romain Guy73b979d2009-06-09 12:57:21 -07001194 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001195 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001197 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001198 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001199 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001200 } else {
1201 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203 }
Romain Guycbb89e42009-06-08 15:52:54 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 /**
1206 * Add a shortcut to the workspace.
1207 *
1208 * @param data The intent describing the shortcut.
1209 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001211 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 int cellY) {
1213 int[] cellXY = mTmpAddItemCellCoordinates;
1214 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001215 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Michael Jurkad3ef3062010-11-23 16:23:58 -08001217 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001218
Adam Cohen558baaf2011-08-15 15:22:57 -07001219 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001220 if (info == null) {
1221 return;
1222 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001223 final View view = createShortcut(info);
1224
Adam Cohenfbba09b2011-07-18 21:43:05 -07001225 // First we check if we already know the exact location where we want to add this item.
1226 if (cellX >= 0 && cellY >= 0) {
1227 cellXY[0] = cellX;
1228 cellXY[1] = cellY;
1229 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001230
1231 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001232 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001233 true, null,null)) {
1234 return;
1235 }
1236 DragObject dragObject = new DragObject();
1237 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001238 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1239 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001240 return;
1241 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001242 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001243 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001244 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001245 foundCellSpan = (result != null);
1246 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001247 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001248 }
1249
1250 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001251 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 return;
1253 }
1254
Adam Cohendcd297f2013-06-18 13:13:40 -07001255 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
1257 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001258 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001259 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
1262
Adam Cohen2f093b62012-04-30 18:59:53 -07001263 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1264 int minHeight) {
1265 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001266 // We want to account for the extra amount of padding that we are adding to the widget
1267 // to ensure that it gets the full amount of space that it has requested
1268 int requiredWidth = minWidth + padding.left + padding.right;
1269 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001270 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001271 }
1272
Adam Cohen2f093b62012-04-30 18:59:53 -07001273 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1274 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001275 }
1276
Adam Cohen2f093b62012-04-30 18:59:53 -07001277 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1278 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001279 }
1280
Adam Cohen2f093b62012-04-30 18:59:53 -07001281 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1282 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001283 }
1284
Adam Cohen2f093b62012-04-30 18:59:53 -07001285 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1286 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001287 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001288 }
1289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001291 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001293 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001294 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001296 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001297 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1298 if (appWidgetInfo == null) {
1299 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1300 }
Romain Guycbb89e42009-06-08 15:52:54 -07001301
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001302 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001303 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001304
Adam Cohen2f093b62012-04-30 18:59:53 -07001305 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1306 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001309 // We have saved the position to which the widget was dragged-- this really only matters
1310 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001311 int[] cellXY = mTmpAddItemCellCoordinates;
1312 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001313 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1316 cellXY[0] = mPendingAddInfo.cellX;
1317 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001318 spanXY[0] = mPendingAddInfo.spanX;
1319 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001320 foundCellSpan = true;
1321 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001323 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001324 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1325 spanXY[1], cellXY, finalSpan);
1326 spanXY[0] = finalSpan[0];
1327 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001328 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001330 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001331 }
1332
Michael Jurka0280c3b2010-09-17 15:00:07 -07001333 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001334 if (appWidgetId != -1) {
1335 // Deleting an app widget ID is a void call but writes to disk before returning
1336 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001337 new Thread("deleteAppWidgetId") {
1338 public void run() {
1339 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1340 }
1341 }.start();
1342 }
Winson Chung93eef082012-03-23 15:59:27 -07001343 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001344 return;
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001347 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001348 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1349 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 launcherInfo.spanX = spanXY[0];
1351 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001352 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1353 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001356 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357
1358 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001359 if (hostView == null) {
1360 // Perform actual inflation because we're live
1361 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1362 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1363 } else {
1364 // The AppWidgetHostView has already been inflated and instantiated
1365 launcherInfo.hostView = hostView;
1366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001369 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001370 launcherInfo.notifyWidgetSizeChanged(this);
1371
Adam Cohendcd297f2013-06-18 13:13:40 -07001372 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001373 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001374
1375 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001377 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
Romain Guycbb89e42009-06-08 15:52:54 -07001379
Adam Cohended9f8d2010-11-03 13:25:16 -07001380 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1381 @Override
1382 public void onReceive(Context context, Intent intent) {
1383 final String action = intent.getAction();
1384 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1385 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001386 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001387 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001388
Winson Chungc100e8e2011-08-09 16:02:43 -07001389 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001390 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001391 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1392 mAppsCustomizeTabHost.reset();
1393 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001394 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001395 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1396 mUserPresent = true;
1397 updateRunning();
1398 }
1399 }
1400 };
1401
1402 @Override
1403 public void onAttachedToWindow() {
1404 super.onAttachedToWindow();
1405
1406 // Listen for broadcasts related to user-presence
1407 final IntentFilter filter = new IntentFilter();
1408 filter.addAction(Intent.ACTION_SCREEN_OFF);
1409 filter.addAction(Intent.ACTION_USER_PRESENT);
1410 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001411 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001412 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001413 mVisible = true;
1414 }
1415
1416 @Override
1417 public void onDetachedFromWindow() {
1418 super.onDetachedFromWindow();
1419 mVisible = false;
1420
Adam Cohend113e0c2010-11-11 10:48:05 -08001421 if (mAttached) {
1422 unregisterReceiver(mReceiver);
1423 mAttached = false;
1424 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001425 updateRunning();
1426 }
1427
1428 public void onWindowVisibilityChanged(int visibility) {
1429 mVisible = visibility == View.VISIBLE;
1430 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001431 // The following code used to be in onResume, but it turns out onResume is called when
1432 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1433 // is a more appropriate event to handle
1434 if (mVisible) {
1435 mAppsCustomizeTabHost.onWindowVisible();
1436 if (!mWorkspaceLoading) {
1437 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001438 // We want to let Launcher draw itself at least once before we force it to build
1439 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001440 // apps is nice and speedy.
1441 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001442 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001443 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001444 if (mStarted) return;
1445 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001446 // We delay the layer building a bit in order to give
1447 // other message processing a time to run. In particular
1448 // this avoids a delay in hiding the IME if it was
1449 // currently shown, because doing that may involve
1450 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001451 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001452 final ViewTreeObserver.OnDrawListener listener = this;
1453 mWorkspace.post(new Runnable() {
1454 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001455 if (mWorkspace != null &&
1456 mWorkspace.getViewTreeObserver() != null) {
1457 mWorkspace.getViewTreeObserver().
1458 removeOnDrawListener(listener);
1459 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001460 }
1461 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001462 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001463 }
1464 });
1465 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001466 // When Launcher comes back to foreground, a different Activity might be responsible for
1467 // the app market intent, so refresh the icon
1468 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001469 clearTypedText();
1470 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001471 }
1472
1473 private void sendAdvanceMessage(long delay) {
1474 mHandler.removeMessages(ADVANCE_MSG);
1475 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1476 mHandler.sendMessageDelayed(msg, delay);
1477 mAutoAdvanceSentTime = System.currentTimeMillis();
1478 }
1479
1480 private void updateRunning() {
1481 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1482 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1483 mAutoAdvanceRunning = autoAdvanceRunning;
1484 if (autoAdvanceRunning) {
1485 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1486 sendAdvanceMessage(delay);
1487 } else {
1488 if (!mWidgetsToAdvance.isEmpty()) {
1489 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1490 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1491 }
1492 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001493 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001494 }
1495 }
1496 }
1497
1498 private final Handler mHandler = new Handler() {
1499 @Override
1500 public void handleMessage(Message msg) {
1501 if (msg.what == ADVANCE_MSG) {
1502 int i = 0;
1503 for (View key: mWidgetsToAdvance.keySet()) {
1504 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1505 final int delay = mAdvanceStagger * i;
1506 if (v instanceof Advanceable) {
1507 postDelayed(new Runnable() {
1508 public void run() {
1509 ((Advanceable) v).advance();
1510 }
1511 }, delay);
1512 }
1513 i++;
1514 }
1515 sendAdvanceMessage(mAdvanceInterval);
1516 }
1517 }
1518 };
1519
1520 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001521 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001522 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1523 if (v instanceof Advanceable) {
1524 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001525 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001526 updateRunning();
1527 }
1528 }
1529
1530 void removeWidgetToAutoAdvance(View hostView) {
1531 if (mWidgetsToAdvance.containsKey(hostView)) {
1532 mWidgetsToAdvance.remove(hostView);
1533 updateRunning();
1534 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 }
1536
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001538 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 launcherInfo.hostView = null;
1540 }
1541
Winson Chung93eef082012-03-23 15:59:27 -07001542 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1543 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1544 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001545 }
1546
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001547 public LauncherAppWidgetHost getAppWidgetHost() {
1548 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Winson Chunga9abd0e2010-10-27 17:18:37 -07001551 public LauncherModel getModel() {
1552 return mModel;
1553 }
1554
Bjorn Bringertc459e522013-06-07 19:36:01 +01001555 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001556 getWindow().closeAllPanels();
1557
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001558 // Whatever we were doing is hereby canceled.
1559 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001560 }
1561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 @Override
1563 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001564 long startTime = 0;
1565 if (DEBUG_RESUME_TIME) {
1566 startTime = System.currentTimeMillis();
1567 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 super.onNewIntent(intent);
1569
1570 // Close the menu
1571 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001572 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001573 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001574
Jamie Genniscb222e82012-10-23 15:44:26 -07001575 final boolean alreadyOnHome =
1576 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001577 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001578
Jamie Genniscb222e82012-10-23 15:44:26 -07001579 Runnable processIntent = new Runnable() {
1580 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001581 if (mWorkspace == null) {
1582 // Can be cases where mWorkspace is null, this prevents a NPE
1583 return;
1584 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001585 Folder openFolder = mWorkspace.getOpenFolder();
1586 // In all these cases, only animate if we're already on home
1587 mWorkspace.exitWidgetResizeMode();
1588 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1589 openFolder == null) {
1590 mWorkspace.moveToDefaultScreen(true);
1591 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001592
Jamie Genniscb222e82012-10-23 15:44:26 -07001593 closeFolder();
1594 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001595
Jamie Genniscb222e82012-10-23 15:44:26 -07001596 // If we are already on home, then just animate back to the workspace,
1597 // otherwise, just wait until onResume to set the state back to Workspace
1598 if (alreadyOnHome) {
1599 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001600 if (mWorkspace.isInOverviewMode()) {
1601 mWorkspace.exitOverviewMode();
1602 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001603 } else {
1604 mOnResumeState = State.WORKSPACE;
1605 }
1606
1607 final View v = getWindow().peekDecorView();
1608 if (v != null && v.getWindowToken() != null) {
1609 InputMethodManager imm = (InputMethodManager)getSystemService(
1610 INPUT_METHOD_SERVICE);
1611 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1612 }
1613
1614 // Reset AllApps to its initial state
1615 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1616 mAppsCustomizeTabHost.reset();
1617 }
1618 }
1619 };
1620
1621 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1622 // Delay processing of the intent to allow the status bar animation to finish
1623 // first in order to avoid janky animations.
1624 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001625 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001626 // Process the intent immediately.
1627 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001628 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Michael Jurka447bf842013-05-15 14:52:15 +02001631 if (DEBUG_RESUME_TIME) {
1632 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1633 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
1635
1636 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001637 public void onRestoreInstanceState(Bundle state) {
1638 super.onRestoreInstanceState(state);
1639 for (int page: mSynchronouslyBoundPages) {
1640 mWorkspace.restoreInstanceStateForChild(page);
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 }
1643
1644 @Override
1645 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001646 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001647 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
Michael Jurka883f55b2010-10-21 15:47:14 -07001649 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001650 // We close any open folder since it will not be re-opened, and we need to make sure
1651 // this state is reflected.
1652 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
Adam Cohendcd297f2013-06-18 13:13:40 -07001654 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 mWaitingForResult) {
1656 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001657 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001658 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1659 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001660 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1661 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1662 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
1664
1665 if (mFolderInfo != null && mWaitingForResult) {
1666 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1667 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1668 }
Michael Jurka946ad472010-07-09 18:05:18 -07001669
Winson Chung785d2eb2011-04-14 16:08:02 -07001670 // Save the current AppsCustomize tab
1671 if (mAppsCustomizeTabHost != null) {
1672 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1673 if (currentTabTag != null) {
1674 outState.putString("apps_customize_currentTab", currentTabTag);
1675 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001676 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1677 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 }
1680
1681 @Override
1682 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001684
Winson Chunge7a03942011-08-05 15:05:12 -07001685 // Remove all pending runnables
1686 mHandler.removeMessages(ADVANCE_MSG);
1687 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001688 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001689
Winson Chungcd2b0142011-06-08 16:02:26 -07001690 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001691 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001692 mModel.stopLoader();
1693 app.setLauncher(null);
1694
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001696 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001698 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001700 mAppWidgetHost = null;
1701
1702 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703
1704 TextKeyListener.getInstance().release();
1705
Winson Chung81b52252012-08-27 15:34:29 -07001706 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1707 // to prevent leaking Launcher activities on orientation change.
1708 if (mModel != null) {
1709 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1710 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001711
1712 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001713 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001714
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001715 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001716 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1717 mWorkspace.removeAllViews();
1718 mWorkspace = null;
1719 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001720
Michael Jurka2ecf9952012-06-18 12:52:28 -07001721 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723
Adam Cohena9cf38f2011-05-02 15:36:58 -07001724 public DragController getDragController() {
1725 return mDragController;
1726 }
1727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 @Override
1729 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001730 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 super.startActivityForResult(intent, requestCode);
1732 }
1733
Winson Chung70d72102011-08-12 11:24:35 -07001734 /**
1735 * Indicates that we want global search for this activity by setting the globalSearch
1736 * argument for {@link #startSearch} to true.
1737 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001739 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001741
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001742 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Karl Rosaen138a0412009-04-23 19:00:21 -07001744 if (initialQuery == null) {
1745 // Use any text typed in the launcher as the initial query
1746 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 if (appSearchData == null) {
1749 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001750 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
Winson Chungadf0c182012-08-23 14:59:07 -07001752 Rect sourceBounds = new Rect();
1753 if (mSearchDropTargetBar != null) {
1754 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1755 }
Romain Guycbb89e42009-06-08 15:52:54 -07001756
Bjorn Bringertc459e522013-06-07 19:36:01 +01001757 startSearch(initialQuery, selectInitialQuery,
1758 appSearchData, sourceBounds);
1759 }
1760
1761 public void startSearch(String initialQuery,
1762 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001763 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001764 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001765 }
1766
1767 /**
1768 * Starts the global search activity. This code is a copied from SearchManager
1769 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001770 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001771 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001772 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001773 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1774 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1775 if (globalSearchActivity == null) {
1776 Log.w(TAG, "No global search activity found.");
1777 return;
1778 }
1779 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1780 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1781 intent.setComponent(globalSearchActivity);
1782 // Make sure that we have a Bundle to put source in
1783 if (appSearchData == null) {
1784 appSearchData = new Bundle();
1785 } else {
1786 appSearchData = new Bundle(appSearchData);
1787 }
1788 // Set source to package name of app that starts global search, if not set already.
1789 if (!appSearchData.containsKey("source")) {
1790 appSearchData.putString("source", getPackageName());
1791 }
1792 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1793 if (!TextUtils.isEmpty(initialQuery)) {
1794 intent.putExtra(SearchManager.QUERY, initialQuery);
1795 }
1796 if (selectInitialQuery) {
1797 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1798 }
1799 intent.setSourceBounds(sourceBounds);
1800 try {
1801 startActivity(intent);
1802 } catch (ActivityNotFoundException ex) {
1803 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1804 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 }
1806
Winson Chung70d72102011-08-12 11:24:35 -07001807 @Override
1808 public boolean onCreateOptionsMenu(Menu menu) {
1809 if (isWorkspaceLocked()) {
1810 return false;
1811 }
1812
1813 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001814
1815 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1816 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1817 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001818 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1819 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1820 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001821 String helpUrl = getString(R.string.help_url);
1822 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001823 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1824 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1825
Winson Chung70d72102011-08-12 11:24:35 -07001826 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1827 .setIcon(android.R.drawable.ic_menu_gallery)
1828 .setAlphabeticShortcut('W');
1829 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1830 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001831 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001832 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001833 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1834 .setIcon(android.R.drawable.ic_menu_preferences)
1835 .setIntent(settings)
1836 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001837 if (!helpUrl.isEmpty()) {
1838 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1839 .setIcon(android.R.drawable.ic_menu_help)
1840 .setIntent(help)
1841 .setAlphabeticShortcut('H');
1842 }
Winson Chung70d72102011-08-12 11:24:35 -07001843 return true;
1844 }
1845
1846 @Override
1847 public boolean onPrepareOptionsMenu(Menu menu) {
1848 super.onPrepareOptionsMenu(menu);
1849
1850 if (mAppsCustomizeTabHost.isTransitioning()) {
1851 return false;
1852 }
1853 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1854 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1855
1856 return true;
1857 }
1858
1859 @Override
1860 public boolean onOptionsItemSelected(MenuItem item) {
1861 switch (item.getItemId()) {
1862 case MENU_WALLPAPER_SETTINGS:
1863 startWallpaper();
1864 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001865 }
1866
1867 return super.onOptionsItemSelected(item);
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001870 @Override
1871 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001872 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001873 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001874 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001875 }
1876
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 public boolean isWorkspaceLocked() {
1878 return mWorkspaceLoading || mWaitingForResult;
1879 }
1880
Michael Jurka0280c3b2010-09-17 15:00:07 -07001881 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001882 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001883 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001884 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1885 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001886 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001887 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001888 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001889
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001890 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1891 AppWidgetProviderInfo appWidgetInfo) {
1892 if (appWidgetInfo.configure != null) {
1893 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001894
Bjorn Bringert7984c942009-12-09 15:38:25 +00001895 // Launch over to configure widget, if needed
1896 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001897 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001898 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001899 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001901 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001902 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001903 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001904 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001905 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907 }
Romain Guycbb89e42009-06-08 15:52:54 -07001908
Adam Cohenfbba09b2011-07-18 21:43:05 -07001909 /**
1910 * Process a shortcut drop.
1911 *
1912 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001913 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001914 * @param cell The cell it should be added to, optional
1915 * @param position The location on the screen where it was dropped, optional
1916 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001917 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001918 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001919 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001920 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001921 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001922 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001923
1924 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001925 mPendingAddInfo.cellX = cell[0];
1926 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001927 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001928
1929 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1930 createShortcutIntent.setComponent(componentName);
1931 processShortcut(createShortcutIntent);
1932 }
1933
Adam Cohenfbba09b2011-07-18 21:43:05 -07001934 /**
1935 * Process a widget drop.
1936 *
1937 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001938 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001939 * @param cell The cell it should be added to, optional
1940 * @param position The location on the screen where it was dropped, optional
1941 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001942 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001943 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001944 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001946 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001947 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001948 mPendingAddInfo.minSpanX = info.minSpanX;
1949 mPendingAddInfo.minSpanY = info.minSpanY;
1950
Adam Cohenfbba09b2011-07-18 21:43:05 -07001951 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mPendingAddInfo.cellX = cell[0];
1953 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001954 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001955 if (span != null) {
1956 mPendingAddInfo.spanX = span[0];
1957 mPendingAddInfo.spanY = span[1];
1958 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001959
Adam Cohened66b2b2012-01-23 17:28:51 -08001960 AppWidgetHostView hostView = info.boundWidget;
1961 int appWidgetId;
1962 if (hostView != null) {
1963 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001964 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001965 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001966 // In this case, we either need to start an activity to get permission to bind
1967 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001968 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001969 Bundle options = info.bindOptions;
1970
1971 boolean success = false;
1972 if (options != null) {
1973 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1974 info.componentName, options);
1975 } else {
1976 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1977 info.componentName);
1978 }
1979 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001980 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001981 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001982 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001983 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1984 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1985 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001986 // TODO: we need to make sure that this accounts for the options bundle.
1987 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001988 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1989 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001990 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001991 }
1992
Joe Onoratodeb98af2010-02-19 14:59:39 -08001993 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001994 // Handle case where user selected "Applications"
1995 String applicationName = getResources().getString(R.string.group_applications);
1996 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001998 if (applicationName != null && applicationName.equals(shortcutName)) {
1999 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2000 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002001
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002002 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2003 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002004 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07002005 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002006 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07002007 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Winson Chung24ab2f12010-09-16 14:10:47 -07002011 void processWallpaper(Intent intent) {
2012 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2013 }
2014
Adam Cohendcd297f2013-06-18 13:13:40 -07002015 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002016 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002017 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 folderInfo.title = getText(R.string.folder_name);
2019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002021 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002022 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002023 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024
2025 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002026 FolderIcon newFolder =
2027 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002029 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002030 // Force measure the new folder icon
2031 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2032 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002033 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
Romain Guycbb89e42009-06-08 15:52:54 -07002035
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002037 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002041 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002043 pickWallpaper.setComponent(
2044 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2045 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002048 /**
2049 * Registers various content observers. The current implementation registers
2050 * only a favorites observer to keep track of the favorites applications.
2051 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002052 private void registerContentObservers() {
2053 ContentResolver resolver = getContentResolver();
2054 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2055 true, mWidgetObserver);
2056 }
2057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 @Override
2059 public boolean dispatchKeyEvent(KeyEvent event) {
2060 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2061 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002063 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002064 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002065 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002066 dumpState();
2067 return true;
2068 }
2069 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002070 }
2071 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2072 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002073 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 return true;
2075 }
2076 }
2077
2078 return super.dispatchKeyEvent(event);
2079 }
2080
Joe Onorato88ec0992009-11-19 13:16:06 -08002081 @Override
2082 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002083 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002084 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002085 } else if (mWorkspace.isInOverviewMode()) {
2086 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002087 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002088 Folder openFolder = mWorkspace.getOpenFolder();
2089 if (openFolder.isEditingName()) {
2090 openFolder.dismissEditingName();
2091 } else {
2092 closeFolder();
2093 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002094 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002095 mWorkspace.exitWidgetResizeMode();
2096
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002097 // Back button is a no-op here, but give at least some feedback for the button press
2098 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002099 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002100 }
2101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002103 * Re-listen when widgets are reset.
2104 */
2105 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002106 if (mAppWidgetHost != null) {
2107 mAppWidgetHost.startListening();
2108 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002109 }
2110
2111 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 * Launches the intent referred by the clicked shortcut.
2113 *
2114 * @param v The view representing the clicked shortcut.
2115 */
2116 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002117 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2118 // view has detached (it's possible for this to happen if the view is removed mid touch).
2119 if (v.getWindowToken() == null) {
2120 return;
2121 }
2122
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002123 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002124 return;
2125 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002126
Adam Cohenf358a4b2013-07-23 16:47:31 -07002127 if (v instanceof PageIndicator) {
2128 if (!mWorkspace.isInOverviewMode()) {
2129 mWorkspace.enterOverviewMode();
2130 }
2131 return;
2132 }
2133
2134 if (v instanceof CellLayout) {
2135 if (mWorkspace.isInOverviewMode()) {
2136 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2137 }
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002141 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002143 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2144 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002145
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002146 // Check for special shortcuts
2147 if (intent.getComponent() != null) {
2148 final String shortcutClass = intent.getComponent().getClassName();
2149
2150 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2151 showAllApps(true);
2152 return;
2153 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2154 MemoryDumpActivity.startDump(this);
2155 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002156 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2157 toggleShowWeightWatcher();
2158 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002159 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002160 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002161
2162 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002163 int[] pos = new int[2];
2164 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002165 intent.setSourceBounds(new Rect(pos[0], pos[1],
2166 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002167
2168 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002169
Daniel Sandlerff02d492013-08-05 02:12:05 -04002170 mStats.recordLaunch(intent, shortcut);
2171
Michael Jurkaddd62e92011-02-16 17:49:14 -08002172 if (success && v instanceof BubbleTextView) {
2173 mWaitingForResume = (BubbleTextView) v;
2174 mWaitingForResume.setStayPressed(true);
2175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002177 if (v instanceof FolderIcon) {
2178 FolderIcon fi = (FolderIcon) v;
2179 handleFolderClick(fi);
2180 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002181 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002182 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002183 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002184 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002185 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 }
2188 }
2189
Michael Jurka0e260592010-06-30 17:07:39 -07002190 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002191 return false;
2192 }
2193
Michael Jurkaaf442092010-06-10 17:01:57 -07002194 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002195 * Event handler for the search button
2196 *
2197 * @param v The view that was clicked.
2198 */
2199 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002200 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2201
Amith Yamasania135ba82011-08-09 17:42:01 -07002202 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002203 }
2204
2205 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002206 * Event handler for the voice button
2207 *
2208 * @param v The view that was clicked.
2209 */
2210 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002211 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002212
Bjorn Bringertc459e522013-06-07 19:36:01 +01002213 startVoice();
2214 }
2215
2216 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002217 try {
2218 final SearchManager searchManager =
2219 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2220 ComponentName activityName = searchManager.getGlobalSearchActivity();
2221 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002222 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002223 if (activityName != null) {
2224 intent.setPackage(activityName.getPackageName());
2225 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002226 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002227 } catch (ActivityNotFoundException e) {
2228 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002229 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002230 startActivitySafely(null, intent, "onClickVoiceButton");
2231 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002232 }
2233
2234 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002235 * Event handler for the "grid" button that appears on the home screen, which
2236 * enters all apps mode.
2237 *
2238 * @param v The view that was clicked.
2239 */
2240 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002241 showAllApps(true);
2242 }
2243
2244 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002245 // Provide the same haptic feedback that the system offers for virtual keys.
2246 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002247 }
2248
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002249 public void onClickAppMarketButton(View v) {
2250 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002251 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002252 } else {
2253 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002254 }
2255 }
2256
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002257 void startApplicationDetailsActivity(ComponentName componentName) {
2258 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002259 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2260 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002261 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002262 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002263 }
2264
Michael Jurka1e2f4652013-07-08 18:03:46 -07002265 // returns true if the activity was started
2266 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2267 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002268 // System applications cannot be installed. For now, show a toast explaining that.
2269 // We may give them the option of disabling apps this way.
2270 int messageId = R.string.uninstall_system_app_text;
2271 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002272 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002273 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002274 String packageName = componentName.getPackageName();
2275 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002276 Intent intent = new Intent(
2277 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002278 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2279 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002280 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002281 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002282 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002283 }
2284
Michael Jurka86a720e2012-05-09 11:23:23 -07002285 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002289 // Only launch using the new animation if the shortcut has not opted out (this is a
2290 // private contract between launcher and may be ignored in the future).
2291 boolean useLaunchAnimation = (v != null) &&
2292 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2293 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002294 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2295 v.getMeasuredWidth(), v.getMeasuredHeight());
2296
2297 startActivity(intent, opts.toBundle());
2298 } else {
2299 startActivity(intent);
2300 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002301 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 } catch (SecurityException e) {
2303 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002304 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002306 "or use the exported attribute for this activity. "
2307 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002309 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002311
Michael Jurka86a720e2012-05-09 11:23:23 -07002312 boolean startActivitySafely(View v, Intent intent, Object tag) {
2313 boolean success = false;
2314 try {
2315 success = startActivity(v, intent, tag);
2316 } catch (ActivityNotFoundException e) {
2317 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2318 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2319 }
2320 return success;
2321 }
2322
Romain Guy8e633c52010-03-04 12:51:36 -08002323 void startActivityForResultSafely(Intent intent, int requestCode) {
2324 try {
2325 startActivityForResult(intent, requestCode);
2326 } catch (ActivityNotFoundException e) {
2327 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2328 } catch (SecurityException e) {
2329 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2330 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2331 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2332 "or use the exported attribute for this activity.", e);
2333 }
2334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335
Adam Cohena9cf38f2011-05-02 15:36:58 -07002336 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002337 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002338 Folder openFolder = mWorkspace.getFolderForTag(info);
2339
2340 // If the folder info reports that the associated folder is open, then verify that
2341 // it is actually opened. There have been a few instances where this gets out of sync.
2342 if (info.opened && openFolder == null) {
2343 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002345 info.opened = false;
2346 }
2347
Adam Cohenfb91f302012-06-11 15:45:18 -07002348 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 // Close any open folder
2350 closeFolder();
2351 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002352 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 } else {
2354 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 int folderScreen;
2356 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002357 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 // .. and close it
2359 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002360 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 // Close any folder open on the current screen
2362 closeFolder();
2363 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002364 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002365 }
2366 }
2367 }
2368 }
2369
Adam Cohen268c4752012-06-06 17:47:33 -07002370 /**
2371 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2372 * in the DragLayer in the exact absolute location of the original FolderIcon.
2373 */
2374 private void copyFolderIconToImage(FolderIcon fi) {
2375 final int width = fi.getMeasuredWidth();
2376 final int height = fi.getMeasuredHeight();
2377
2378 // Lazy load ImageView, Bitmap and Canvas
2379 if (mFolderIconImageView == null) {
2380 mFolderIconImageView = new ImageView(this);
2381 }
2382 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2383 mFolderIconBitmap.getHeight() != height) {
2384 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2385 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2386 }
2387
2388 DragLayer.LayoutParams lp;
2389 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2390 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2391 } else {
2392 lp = new DragLayer.LayoutParams(width, height);
2393 }
2394
Adam Cohen307fe232012-08-16 17:55:58 -07002395 // The layout from which the folder is being opened may be scaled, adjust the starting
2396 // view size by this scale factor.
2397 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002398 lp.customPosition = true;
2399 lp.x = mRectForFolderAnimation.left;
2400 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002401 lp.width = (int) (scale * width);
2402 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002403
2404 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2405 fi.draw(mFolderIconCanvas);
2406 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002407 if (fi.getFolder() != null) {
2408 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2409 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002410 }
Adam Cohen268c4752012-06-06 17:47:33 -07002411 // Just in case this image view is still in the drag layer from a previous animation,
2412 // we remove it and re-add it.
2413 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2414 mDragLayer.removeView(mFolderIconImageView);
2415 }
2416 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002417 if (fi.getFolder() != null) {
2418 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002419 }
Adam Cohen268c4752012-06-06 17:47:33 -07002420 }
2421
Adam Cohen2801caf2011-05-13 20:57:39 -07002422 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002423 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002424 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2425 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2426 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2427
Adam Cohenc51934b2011-07-26 21:07:43 -07002428 FolderInfo info = (FolderInfo) fi.getTag();
2429 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2430 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002431 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2432 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002433 }
2434
Adam Cohen268c4752012-06-06 17:47:33 -07002435 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2436 copyFolderIconToImage(fi);
2437 fi.setVisibility(View.INVISIBLE);
2438
Michael Jurka2ecf9952012-06-18 12:52:28 -07002439 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002440 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002441 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2442 oa.start();
2443 }
2444
Adam Cohen268c4752012-06-06 17:47:33 -07002445 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002446 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002447 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2448 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2449 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2450
Adam Cohen268c4752012-06-06 17:47:33 -07002451 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002452
Adam Cohen268c4752012-06-06 17:47:33 -07002453 // We remove and re-draw the FolderIcon in-case it has changed
2454 mDragLayer.removeView(mFolderIconImageView);
2455 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002456 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002457 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002458 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002459 oa.addListener(new AnimatorListenerAdapter() {
2460 @Override
2461 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002462 if (cl != null) {
2463 cl.clearFolderLeaveBehind();
2464 // Remove the ImageView copy of the FolderIcon and make the original visible.
2465 mDragLayer.removeView(mFolderIconImageView);
2466 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002467 }
2468 }
2469 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002470 oa.start();
2471 }
2472
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002473 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002474 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002475 * is animated relative to the specified View. If the View is null, no animation
2476 * is played.
2477 *
2478 * @param folderInfo The FolderInfo describing the folder to open.
2479 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002480 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002481 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002482 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483
Adam Cohena9cf38f2011-05-02 15:36:58 -07002484 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002485
Adam Cohen4554ee12011-08-03 16:13:21 -07002486 // Just verify that the folder hasn't already been added to the DragLayer.
2487 // There was a one-off crash where the folder had a parent already.
2488 if (folder.getParent() == null) {
2489 mDragLayer.addView(folder);
2490 mDragController.addDropTarget((DropTarget) folder);
2491 } else {
2492 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2493 folder.getParent() + ").");
2494 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002495 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002496 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002497
2498 // Notify the accessibility manager that this folder "window" has appeared and occluded
2499 // the workspace items
2500 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2501 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002502 }
2503
2504 public void closeFolder() {
2505 Folder folder = mWorkspace.getOpenFolder();
2506 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002507 if (folder.isEditingName()) {
2508 folder.dismissEditingName();
2509 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002510 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002511
2512 // Dismiss the folder cling
2513 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002514 }
2515 }
2516
2517 void closeFolder(Folder folder) {
2518 folder.getInfo().opened = false;
2519
2520 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2521 if (parent != null) {
2522 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2523 shrinkAndFadeInFolderIcon(fi);
2524 }
2525 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002526
2527 // Notify the accessibility manager that this folder "window" has disappeard and no
2528 // longer occludeds the workspace items
2529 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 }
2531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002532 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002533 if (!isDraggingEnabled()) return false;
2534 if (isWorkspaceLocked()) return false;
2535 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536
2537 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002538 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 }
2540
Michael Jurka0280c3b2010-09-17 15:00:07 -07002541 resetAddInfo();
2542 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002544 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 return true;
2546 }
2547
Winson Chung3d503fb2011-07-13 17:25:49 -07002548 // The hotseat touch handling does not go through Workspace, and we always allow long press
2549 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002550 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002551 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2552 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002553 if (itemUnderLongClick == null) {
2554 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002555 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2556 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002557 // Disabling reordering until we sort out some issues.
Adam Cohenf358a4b2013-07-23 16:47:31 -07002558 if (mWorkspace.isInOverviewMode()) {
2559 mWorkspace.startReordering(v);
Adam Cohendedbd962013-07-11 14:21:49 -07002560 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002561 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002562 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002564 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002566 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567 }
2568 }
2569 }
2570 return true;
2571 }
2572
Winson Chung3d503fb2011-07-13 17:25:49 -07002573 boolean isHotseatLayout(View layout) {
2574 return mHotseat != null && layout != null &&
2575 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2576 }
2577 Hotseat getHotseat() {
2578 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002579 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002580 View getOverviewPanel() {
2581 return mOverviewPanel;
2582 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002583 SearchDropTargetBar getSearchBar() {
2584 return mSearchDropTargetBar;
2585 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002586
Winson Chung3d503fb2011-07-13 17:25:49 -07002587 /**
2588 * Returns the CellLayout of the specified container at the specified screen.
2589 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002590 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002591 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2592 if (mHotseat != null) {
2593 return mHotseat.getLayout();
2594 } else {
2595 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002596 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002597 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002598 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002599 }
2600 }
2601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002602 Workspace getWorkspace() {
2603 return mWorkspace;
2604 }
2605
Daniel Sandler843e8602010-06-07 14:59:01 -04002606 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002607 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002608 }
2609
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 /**
2611 * Helper method for the cameraZoomIn/cameraZoomOut animations
2612 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 * @param scaleFactor The scale factor used for the zoom
2614 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002615 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002617 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002619
Winson Chung18f41f82012-05-09 13:28:10 -07002620 void disableWallpaperIfInAllApps() {
2621 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002622 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002623 if (mAppsCustomizeTabHost != null &&
2624 !mAppsCustomizeTabHost.isTransitioning()) {
2625 updateWallpaperVisibility(false);
2626 }
2627 }
2628 }
2629
Craig Mautner360310b2012-10-26 15:13:08 -07002630 private void setWorkspaceBackground(boolean workspace) {
2631 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002632 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002633 }
2634
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002635 void updateWallpaperVisibility(boolean visible) {
2636 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2637 int curflags = getWindow().getAttributes().flags
2638 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2639 if (wpflags != curflags) {
2640 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2641 }
Craig Mautner360310b2012-10-26 15:13:08 -07002642 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002643 }
2644
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002645 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2646 if (v instanceof LauncherTransitionable) {
2647 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2648 }
2649 }
2650
Michael Jurkabed61d22012-02-14 22:51:29 -08002651 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2652 if (v instanceof LauncherTransitionable) {
2653 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2654 }
Winson Chung70442722012-02-10 15:43:22 -08002655
2656 // Update the workspace transition step as well
2657 dispatchOnLauncherTransitionStep(v, 0f);
2658 }
2659
2660 private void dispatchOnLauncherTransitionStep(View v, float t) {
2661 if (v instanceof LauncherTransitionable) {
2662 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2663 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002664 }
2665
2666 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2667 if (v instanceof LauncherTransitionable) {
2668 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2669 }
Winson Chung70442722012-02-10 15:43:22 -08002670
2671 // Update the workspace transition step as well
2672 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002673 }
2674
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002675 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002676 * Things to test when changing the following seven functions.
2677 * - Home from workspace
2678 * - from center screen
2679 * - from other screens
2680 * - Home from all apps
2681 * - from center screen
2682 * - from other screens
2683 * - Back from all apps
2684 * - from center screen
2685 * - from other screens
2686 * - Launch app from workspace and quit
2687 * - with back
2688 * - with home
2689 * - Launch app from all apps and quit
2690 * - with back
2691 * - with home
2692 * - Go to a screen that's not the default, then all
2693 * apps, and launch and app, and go back
2694 * - with back
2695 * -with home
2696 * - On workspace, long press power and go back
2697 * - with back
2698 * - with home
2699 * - On all apps, long press power and go back
2700 * - with back
2701 * - with home
2702 * - On workspace, power off
2703 * - On all apps, power off
2704 * - Launch an app and turn off the screen while in that app
2705 * - Go back with home key
2706 * - Go back with back key TODO: make this not go to workspace
2707 * - From all apps
2708 * - From workspace
2709 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2710 * - From all apps
2711 * - From the center workspace
2712 * - From another workspace
2713 */
2714
2715 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002716 * Zoom the camera out from the workspace to reveal 'toView'.
2717 * Assumes that the view to show is anchored at either the very top or very bottom
2718 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002719 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002720 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002721 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002722 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002723 mStateAnimation.cancel();
2724 mStateAnimation = null;
2725 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002726 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002727
Winson Chungf0ea4d32011-06-06 14:27:16 -07002728 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2729 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2730 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002731 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002732 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002733 final int startDelay =
2734 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002735
Michael Jurkab3e22d92011-10-31 15:58:33 -07002736 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002737
Michael Jurkad74c9842011-07-10 12:44:21 -07002738 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002739 Animator workspaceAnim =
2740 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741
2742 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002743 toView.setScaleX(scale);
2744 toView.setScaleY(scale);
2745 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2746 scaleAnim.
2747 scaleX(1f).scaleY(1f).
2748 setDuration(duration).
2749 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002750
Winson Chungf0ea4d32011-06-06 14:27:16 -07002751 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002752 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002753 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002754 .ofFloat(toView, "alpha", 0f, 1f)
2755 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002756 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002757 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2758 @Override
2759 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002760 if (animation == null) {
2761 throw new RuntimeException("animation is null");
2762 }
Winson Chung70442722012-02-10 15:43:22 -08002763 float t = (Float) animation.getAnimatedValue();
2764 dispatchOnLauncherTransitionStep(fromView, t);
2765 dispatchOnLauncherTransitionStep(toView, t);
2766 }
2767 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002768
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002769 // toView should appear right at the end of the workspace shrink
2770 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002771 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002772 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002773 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002774
2775 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002776 boolean animationCancelled = false;
2777
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002778 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002779 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002780 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781 // Prepare the position
2782 toView.setTranslationX(0.0f);
2783 toView.setTranslationY(0.0f);
2784 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002785 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002786 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002787 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002788 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002789 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2790 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002791
Adam Cohenf4ddea32011-09-12 13:46:42 -07002792 if (!animationCancelled) {
2793 updateWallpaperVisibility(false);
2794 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002795
2796 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002797 if (mSearchDropTargetBar != null) {
2798 mSearchDropTargetBar.hideSearchBar(false);
2799 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002800 }
2801
Adam Cohencff6af82011-09-13 14:51:53 -07002802 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002803 public void onAnimationCancel(Animator animation) {
2804 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002805 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002806 });
2807
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002808 if (workspaceAnim != null) {
2809 mStateAnimation.play(workspaceAnim);
2810 }
Michael Jurka1899a362011-11-03 13:50:45 -07002811
2812 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002813
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002814 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2815 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002816
2817 // If any of the objects being animated haven't been measured/laid out
2818 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002819 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002820 (mWorkspace.getMeasuredWidth() == 0) ||
2821 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002822 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002823 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002824
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002825 final AnimatorSet stateAnimation = mStateAnimation;
2826 final Runnable startAnimRunnable = new Runnable() {
2827 public void run() {
2828 // Check that mStateAnimation hasn't changed while
2829 // we waited for a layout/draw pass
2830 if (mStateAnimation != stateAnimation)
2831 return;
2832 setPivotsForZoom(toView, scale);
2833 dispatchOnLauncherTransitionStart(fromView, animated, false);
2834 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002835 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002836 }
2837 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002838 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002839 final ViewTreeObserver observer = toView.getViewTreeObserver();
2840 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2841 public void onGlobalLayout() {
2842 startAnimRunnable.run();
2843 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2844 }
2845 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002846 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002847 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002848 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002849 } else {
2850 toView.setTranslationX(0.0f);
2851 toView.setTranslationY(0.0f);
2852 toView.setScaleX(1.0f);
2853 toView.setScaleY(1.0f);
2854 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002855 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002856
Daniel Sandlere4f98912013-06-25 15:13:26 -04002857 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002858 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002859 if (mSearchDropTargetBar != null) {
2860 mSearchDropTargetBar.hideSearchBar(false);
2861 }
Michael Jurkaabded662011-03-04 12:06:57 -08002862 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002863 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002864 dispatchOnLauncherTransitionStart(fromView, animated, false);
2865 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002866 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002867 dispatchOnLauncherTransitionStart(toView, animated, false);
2868 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002869 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002871 }
2872
2873 /**
2874 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002875 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002876 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002877 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002878 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2879 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002880
Michael Jurkab3e22d92011-10-31 15:58:33 -07002881 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002882 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002883 mStateAnimation.cancel();
2884 mStateAnimation = null;
2885 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002886 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002887
Winson Chungf0ea4d32011-06-06 14:27:16 -07002888 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002889 final int fadeOutDuration =
2890 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 final float scaleFactor = (float)
2892 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2893 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002894 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002895 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896 if (toState == State.WORKSPACE) {
2897 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2898 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002899 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002900 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2901 workspaceAnim = mWorkspace.getChangeStateAnimation(
2902 Workspace.State.SPRING_LOADED, animated);
2903 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002904
Michael Jurkab3e22d92011-10-31 15:58:33 -07002905 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002906 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002907 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002908 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002909 final LauncherViewPropertyAnimator scaleAnim =
2910 new LauncherViewPropertyAnimator(fromView);
2911 scaleAnim.
2912 scaleX(scaleFactor).scaleY(scaleFactor).
2913 setDuration(duration).
2914 setInterpolator(new Workspace.ZoomInInterpolator());
2915
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002916 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002917 .ofFloat(fromView, "alpha", 1f, 0f)
2918 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002919 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002920 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2921 @Override
2922 public void onAnimationUpdate(ValueAnimator animation) {
2923 float t = 1f - (Float) animation.getAnimatedValue();
2924 dispatchOnLauncherTransitionStep(fromView, t);
2925 dispatchOnLauncherTransitionStep(toView, t);
2926 }
2927 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002928
Michael Jurka2ecf9952012-06-18 12:52:28 -07002929 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002930
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002931 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2932 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002933 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002934
2935 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002936 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002937 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002938 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002939 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002940 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2941 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002942 if (onCompleteRunnable != null) {
2943 onCompleteRunnable.run();
2944 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002945 mAppsCustomizeContent.updateCurrentPageScroll();
2946 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002947 }
2948 });
2949
Winson Chungf0ea4d32011-06-06 14:27:16 -07002950 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002951 if (workspaceAnim != null) {
2952 mStateAnimation.play(workspaceAnim);
2953 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002954 dispatchOnLauncherTransitionStart(fromView, animated, true);
2955 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002956 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002957 } else {
2958 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002959 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002960 dispatchOnLauncherTransitionStart(fromView, animated, true);
2961 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002962 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002963 dispatchOnLauncherTransitionStart(toView, animated, true);
2964 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002965 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002966 }
2967
Michael Jurkae326f182011-11-21 14:05:46 -08002968 @Override
2969 public void onTrimMemory(int level) {
2970 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002971 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002972 mAppsCustomizeTabHost.onTrimMemory();
2973 }
2974 }
2975
Winson Chung18f41f82012-05-09 13:28:10 -07002976 @Override
2977 public void onWindowFocusChanged(boolean hasFocus) {
2978 if (!hasFocus) {
2979 // When another window occludes launcher (like the notification shade, or recents),
2980 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2981 updateWallpaperVisibility(true);
2982 } else {
2983 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002984 mWorkspace.postDelayed(new Runnable() {
2985 @Override
2986 public void run() {
2987 disableWallpaperIfInAllApps();
2988 }
2989 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002990 }
2991 }
2992
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002993 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002994 showWorkspace(animated, null);
2995 }
2996
2997 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002998 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002999 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003000 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003001 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003002
Winson Chungc7d2b602012-05-16 17:02:20 -07003003 // Show the search bar (only animate if we were showing the drop target bar in spring
3004 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003005 if (mSearchDropTargetBar != null) {
3006 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3007 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003008
Michael Jurkab3e22d92011-10-31 15:58:33 -07003009 // Set focus to the AppsCustomize button
3010 if (mAllAppsButton != null) {
3011 mAllAppsButton.requestFocus();
3012 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003013 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003014
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003015 // Change the state *after* we've called all the transition code
3016 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003017
Winson Chung5fb63472011-02-02 17:03:37 -08003018 // Resume the auto-advance of widgets
3019 mUserPresent = true;
3020 updateRunning();
3021
alanv1d4fde62012-10-17 13:15:47 -07003022 // Send an accessibility event to announce the context change
3023 getWindow().getDecorView()
3024 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003025
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003026 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003027 }
3028
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003029 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003030 }
3031
Michael Jurkab3e22d92011-10-31 15:58:33 -07003032 void showAllApps(boolean animated) {
3033 if (mState != State.WORKSPACE) return;
3034
3035 showAppsCustomizeHelper(animated, false);
3036 mAppsCustomizeTabHost.requestFocus();
3037
Michael Jurkab3e22d92011-10-31 15:58:33 -07003038 // Change the state *after* we've called all the transition code
3039 mState = State.APPS_CUSTOMIZE;
3040
3041 // Pause the auto-advance of widgets until we are out of AllApps
3042 mUserPresent = false;
3043 updateRunning();
3044 closeFolder();
3045
3046 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003047 getWindow().getDecorView()
3048 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003049 }
3050
Adam Cohen7777d962011-08-18 18:58:38 -07003051 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003052 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003053 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003054 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003055 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003056 }
Adam Cohen7777d962011-08-18 18:58:38 -07003057
Adam Cohened66b2b2012-01-23 17:28:51 -08003058 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3059 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003060 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3061
Winson Chunge7a03942011-08-05 15:05:12 -07003062 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003063 @Override
3064 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003065 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003066 // Before we show workspace, hide all apps again because
3067 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3068 // clean up our state transition functions
3069 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003070 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003071 } else {
3072 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003073 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003074 }
3075 }, (extendedDelay ?
3076 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3077 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3078 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003079
Michael Jurkad3ef3062010-11-23 16:23:58 -08003080 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003081 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003082 final boolean animated = true;
3083 final boolean springLoaded = true;
3084 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003085 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003086 }
3087 // Otherwise, we are not in spring loaded mode, so don't do anything.
3088 }
3089
Michael Jurkab3e22d92011-10-31 15:58:33 -07003090 void lockAllApps() {
3091 // TODO
3092 }
3093
3094 void unlockAllApps() {
3095 // TODO
3096 }
3097
Winson Chungf0ea4d32011-06-06 14:27:16 -07003098 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003099 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003100 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003101 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003102 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003103 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003104 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003105 int duration = 0;
3106 if (mSearchDropTargetBar != null) {
3107 duration = mSearchDropTargetBar.getTransitionInDuration();
3108 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003109 mHotseat.animate().alpha(1f).setDuration(duration);
3110 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003112 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003113 }
3114 }
3115 }
3116
3117 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003118 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003119 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003120 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003121 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003122 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003123 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003124 int duration = 0;
3125 if (mSearchDropTargetBar != null) {
3126 duration = mSearchDropTargetBar.getTransitionOutDuration();
3127 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003128 mHotseat.animate().alpha(0f).setDuration(duration);
3129 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003130 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003132 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003133 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003134 }
3135
Patrick Dubroy5f445422011-02-18 14:35:21 -08003136 /**
3137 * Add an item from all apps or customize onto the given workspace screen.
3138 * If layout is null, add to the current screen.
3139 */
3140 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003141 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003142 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003143 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003144 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003145
Winson Chungdff8ebb2011-09-08 17:25:31 -07003146 /** Maps the current orientation to an index for referencing orientation correct global icons */
3147 private int getCurrentOrientationIndexForGlobalIcons() {
3148 // default - 0, landscape - 1
3149 switch (getResources().getConfiguration().orientation) {
3150 case Configuration.ORIENTATION_LANDSCAPE:
3151 return 1;
3152 default:
3153 return 0;
3154 }
3155 }
3156
Michael Jurkaae65ee32012-04-30 11:45:54 -07003157 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003158 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003159 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003160 // Look for the toolbar icon specified in the activity meta-data
3161 Bundle metaData = packageManager.getActivityInfo(
3162 activityName, PackageManager.GET_META_DATA).metaData;
3163 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003164 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003165 if (iconResId != 0) {
3166 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003167 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003168 }
3169 }
3170 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003171 // This can happen if the activity defines an invalid drawable
3172 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3173 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003174 } catch (Resources.NotFoundException nfe) {
3175 // This can happen if the activity defines an invalid drawable
3176 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3177 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003178 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003179 return null;
3180 }
3181
3182 // if successful in getting icon, return it; otherwise, set button to use default drawable
3183 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003184 int buttonId, ComponentName activityName, int fallbackDrawableId,
3185 String toolbarResourceName) {
3186 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003187 Resources r = getResources();
3188 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3189 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003191 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003192 // If we were unable to find the icon via the meta-data, use a generic one
3193 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003194 toolbarIcon = r.getDrawable(fallbackDrawableId);
3195 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003196 if (button != null) {
3197 button.setCompoundDrawables(toolbarIcon, null, null, null);
3198 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003199 return null;
3200 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003201 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003202 if (button != null) {
3203 button.setCompoundDrawables(toolbarIcon, null, null, null);
3204 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003205 return toolbarIcon.getConstantState();
3206 }
3207 }
3208
3209 // if successful in getting icon, return it; otherwise, set button to use default drawable
3210 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003211 int buttonId, ComponentName activityName, int fallbackDrawableId,
3212 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003213 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003214 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003215
Michael Jurka19e0fc52011-07-22 18:00:21 -07003216 if (button != null) {
3217 // If we were unable to find the icon via the meta-data, use a
3218 // generic one
3219 if (toolbarIcon == null) {
3220 button.setImageResource(fallbackDrawableId);
3221 } else {
3222 button.setImageDrawable(toolbarIcon);
3223 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003224 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003225
3226 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3227
Michael Jurka0423dcf2010-10-05 14:56:18 -07003228 }
3229
Winson Chung1b884092012-06-08 17:13:02 -07003230 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003231 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003232 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003233 }
3234
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003235 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003236 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003237 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003238 }
3239
Winson Chungbb185bd2011-11-21 12:31:42 -08003240 private void invalidatePressedFocusedStates(View container, View button) {
3241 if (container instanceof HolographicLinearLayout) {
3242 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3243 layout.invalidatePressedFocusedStates();
3244 } else if (button instanceof HolographicImageView) {
3245 HolographicImageView view = (HolographicImageView) button;
3246 view.invalidatePressedFocusedStates();
3247 }
3248 }
3249
Winson Chungc51db6a2011-10-05 11:44:49 -07003250 private boolean updateGlobalSearchIcon() {
3251 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003252 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003253 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003254 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003255
Winson Chung1cad91e2011-05-25 17:41:01 -07003256 final SearchManager searchManager =
3257 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3258 ComponentName activityName = searchManager.getGlobalSearchActivity();
3259 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003260 int coi = getCurrentOrientationIndexForGlobalIcons();
3261 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003262 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3263 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3264 if (sGlobalSearchIcon[coi] == null) {
3265 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3266 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3267 TOOLBAR_ICON_METADATA_NAME);
3268 }
3269
Winson Chungc51db6a2011-10-05 11:44:49 -07003270 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3271 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003272 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003273 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003274 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003275 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003276 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3277 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3278 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003279 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003280 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003282 }
3283 }
3284
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003285 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003286 final View searchButtonContainer = findViewById(R.id.search_button_container);
3287 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003288 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003289 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003290 }
3291
Winson Chungc51db6a2011-10-05 11:44:49 -07003292 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003293 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003294 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003295
Winson Chungc51db6a2011-10-05 11:44:49 -07003296 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003297 final SearchManager searchManager =
3298 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3299 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3300
3301 ComponentName activityName = null;
3302 if (globalSearchActivity != null) {
3303 // Check if the global search activity handles voice search
3304 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3305 intent.setPackage(globalSearchActivity.getPackageName());
3306 activityName = intent.resolveActivity(getPackageManager());
3307 }
3308
3309 if (activityName == null) {
3310 // Fallback: check if an activity other than the global search activity
3311 // resolves this
3312 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3313 activityName = intent.resolveActivity(getPackageManager());
3314 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003315 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003316 int coi = getCurrentOrientationIndexForGlobalIcons();
3317 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003318 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3319 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3320 if (sVoiceSearchIcon[coi] == null) {
3321 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3322 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3323 TOOLBAR_ICON_METADATA_NAME);
3324 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003325 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003326 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003327 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003328 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003329 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003330 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003331 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003332 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003333 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003334 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003335 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003336 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003337
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003338 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003339 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3340 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003341 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003342 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003343 }
3344
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003345 public void setVoiceButtonProxyVisible(boolean visible) {
3346 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3347 if (voiceButtonProxy != null) {
3348 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3349 }
3350 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003351 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003352 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003353 */
3354 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003355 final View marketButton = findViewById(R.id.market_button);
3356 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3357 // Find the app market activity by resolving an intent.
3358 // (If multiple app markets are installed, it will return the ResolverActivity.)
3359 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003360 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003361 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003362 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003363 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003364 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3365 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003366 marketButton.setVisibility(View.VISIBLE);
3367 } else {
3368 // We should hide and disable the view so that we don't try and restore the visibility
3369 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3370 marketButton.setVisibility(View.GONE);
3371 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003372 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003373 }
3374
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003375 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003376 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3377 Resources r = getResources();
3378 Drawable marketIconDrawable = d.newDrawable(r);
3379 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3380 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3381 marketIconDrawable.setBounds(0, 0, w, h);
3382
3383 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003384 }
3385
Michael Jurkad7c28052012-04-27 15:43:36 -07003386 @Override
3387 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003388 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003389 final List<CharSequence> text = event.getText();
3390 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003391 // Populate event with a fake title based on the current state.
3392 if (mState == State.APPS_CUSTOMIZE) {
3393 text.add(getString(R.string.all_apps_button_label));
3394 } else {
3395 text.add(getString(R.string.all_apps_home_button_label));
3396 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003397 return result;
3398 }
3399
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003400 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003401 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003402 */
3403 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3404 @Override
3405 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003406 closeSystemDialogs();
3407 }
3408 }
3409
3410 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003411 * Receives notifications whenever the appwidgets are reset.
3412 */
3413 private class AppWidgetResetObserver extends ContentObserver {
3414 public AppWidgetResetObserver() {
3415 super(new Handler());
3416 }
3417
3418 @Override
3419 public void onChange(boolean selfChange) {
3420 onAppWidgetReset();
3421 }
3422 }
3423
3424 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003425 * If the activity is currently paused, signal that we need to run the passed Runnable
3426 * in onResume.
3427 *
3428 * This needs to be called from incoming places where resources might have been loaded
3429 * while we are paused. That is becaues the Configuration might be wrong
3430 * when we're not running, and if it comes back to what it was when we
3431 * were paused, we are not restarted.
3432 *
3433 * Implementation of the method from LauncherModel.Callbacks.
3434 *
3435 * @return true if we are currently paused. The caller might be able to
3436 * skip some work in that case since we will come back again.
3437 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003438 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003439 if (mPaused) {
3440 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003441 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003442 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003443 }
3444 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003445 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003446 return true;
3447 } else {
3448 return false;
3449 }
3450 }
3451
Michael Jurkac402cd92013-05-20 15:49:32 +02003452 private boolean waitUntilResume(Runnable run) {
3453 return waitUntilResume(run, false);
3454 }
3455
Michael Jurka1e2f4652013-07-08 18:03:46 -07003456 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003457 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003458 }
3459
3460 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003461 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003462 }
3463
Michael Jurka7607c2f2013-04-03 14:33:19 -07003464 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003465 * If the activity is currently paused, signal that we need to re-run the loader
3466 * in onResume.
3467 *
3468 * This needs to be called from incoming places where resources might have been loaded
3469 * while we are paused. That is becaues the Configuration might be wrong
3470 * when we're not running, and if it comes back to what it was when we
3471 * were paused, we are not restarted.
3472 *
3473 * Implementation of the method from LauncherModel.Callbacks.
3474 *
3475 * @return true if we are currently paused. The caller might be able to
3476 * skip some work in that case since we will come back again.
3477 */
3478 public boolean setLoadOnResume() {
3479 if (mPaused) {
3480 Log.i(TAG, "setLoadOnResume");
3481 mOnResumeNeedsLoad = true;
3482 return true;
3483 } else {
3484 return false;
3485 }
3486 }
3487
3488 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003490 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003492 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003493 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003494 } else {
3495 return SCREEN_COUNT / 2;
3496 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003497 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003498
Joe Onorato9c1289c2009-08-17 11:03:03 -04003499 /**
3500 * Refreshes the shortcuts shown on the workspace.
3501 *
3502 * Implementation of the method from LauncherModel.Callbacks.
3503 */
3504 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003505 // If we're starting binding all over again, clear any bind calls we'd postponed in
3506 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3507 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003508 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003509
Winson Chungd64d1762013-08-20 14:37:16 -07003510 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003511 mWorkspace.clearDropTargets();
Winson Chungd64d1762013-08-20 14:37:16 -07003512 mWorkspace.removeAllViews();
3513
Michael Jurka05bf644e2011-11-30 20:28:53 -08003514 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003515 if (mHotseat != null) {
3516 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003517 }
3518 }
3519
Adam Cohendcd297f2013-06-18 13:13:40 -07003520 @Override
3521 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003522 bindAddScreens(orderedScreenIds);
3523 mWorkspace.addExtraEmptyScreen();
3524 }
3525
3526 @Override
3527 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003528 int count = orderedScreenIds.size();
3529 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003530 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Adam Cohen89bddfa2013-08-20 11:57:13 -07003531 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003532 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003533 }
3534
Adam Cohen39a06042013-07-19 14:30:12 -07003535 private boolean shouldShowWeightWatcher() {
3536 String spKey = LauncherAppState.getSharedPreferencesKey();
3537 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003538 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003539
3540 return show;
3541 }
3542
3543 private void toggleShowWeightWatcher() {
3544 String spKey = LauncherAppState.getSharedPreferencesKey();
3545 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3546 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3547
3548 show = !show;
3549
3550 SharedPreferences.Editor editor = sp.edit();
3551 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3552 editor.commit();
3553
3554 if (mWeightWatcher != null) {
3555 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3556 }
3557 }
3558
Winson Chungd64d1762013-08-20 14:37:16 -07003559 public void bindAppsAdded(final ArrayList<Long> newScreens,
3560 final ArrayList<ItemInfo> addNotAnimated,
3561 final ArrayList<ItemInfo> addAnimated) {
3562 Runnable r = new Runnable() {
3563 public void run() {
3564 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3565 }
3566 };
3567 if (waitUntilResume(r)) {
3568 return;
3569 }
3570
3571 Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
3572
3573 // Add the new screens
3574 bindAddScreens(newScreens);
3575
3576 // We add the items without animation on non-visible pages, and with
3577 // animations on the new page (which we will try and snap to).
3578 if (!addNotAnimated.isEmpty()) {
3579 bindItems(addNotAnimated, 0,
3580 addNotAnimated.size(), false);
3581 }
3582 if (!addAnimated.isEmpty()) {
3583 bindItems(addAnimated, 0,
3584 addAnimated.size(), true);
3585 }
3586 }
3587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003588 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 * Bind the items start-end from the list.
3590 *
3591 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592 */
Winson Chung64359a52013-07-08 17:17:08 -07003593 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3594 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003595 Runnable r = new Runnable() {
3596 public void run() {
3597 bindItems(shortcuts, start, end, forceAnimateIcons);
3598 }
3599 };
3600 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003601 return;
3602 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003603
Winson Chungf0c6ae02012-03-21 16:10:31 -07003604 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003605 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3606 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003607 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003608 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003609 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003610 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003611 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003612
3613 // Short circuit if we are loading dock items for a configuration which has no dock
3614 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3615 mHotseat == null) {
3616 continue;
3617 }
3618
Joe Onorato9c1289c2009-08-17 11:03:03 -04003619 switch (item.itemType) {
3620 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3621 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003622 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003623 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003624
Winson Chung64359a52013-07-08 17:17:08 -07003625 /*
3626 * TODO: FIX collision case
3627 */
Winson Chungce376632013-07-11 16:12:41 -07003628 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3629 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3630 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3631 throw new RuntimeException("OCCUPIED");
3632 }
Winson Chung64359a52013-07-08 17:17:08 -07003633 }
3634
Adam Cohendcd297f2013-06-18 13:13:40 -07003635 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3636 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003637 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003638 // Animate all the applications up now
3639 shortcut.setAlpha(0f);
3640 shortcut.setScaleX(0f);
3641 shortcut.setScaleY(0f);
3642 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003643 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003645 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003646 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003647 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003648 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003649 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003650 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3651 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003652 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003653 default:
3654 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003656 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003657
Winson Chung997a9232013-07-24 15:33:46 -07003658 if (animateIcons) {
3659 // Animate to the correct page
3660 if (newShortcutsScreenId > -1) {
3661 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3662 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3663 if (newShortcutsScreenId != currentScreenId) {
3664 mWorkspace.snapToPage(newScreenIndex);
3665 }
3666 }
3667
Winson Chung64359a52013-07-08 17:17:08 -07003668 // We post the animation slightly delayed to prevent slowdowns when we are loading
3669 // right after we return to launcher.
3670 mWorkspace.postDelayed(new Runnable() {
3671 public void run() {
3672 anim.playTogether(bounceAnims);
3673 anim.start();
3674 }
3675 }, NEW_APPS_ANIMATION_DELAY);
3676 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003677 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003678 }
3679
Joe Onorato9c1289c2009-08-17 11:03:03 -04003680 /**
3681 * Implementation of the method from LauncherModel.Callbacks.
3682 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003683 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003684 Runnable r = new Runnable() {
3685 public void run() {
3686 bindFolders(folders);
3687 }
3688 };
3689 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003690 return;
3691 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003692 sFolders.clear();
3693 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695
3696 /**
3697 * Add the views for a widget to the workspace.
3698 *
3699 * Implementation of the method from LauncherModel.Callbacks.
3700 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003702 Runnable r = new Runnable() {
3703 public void run() {
3704 bindAppWidget(item);
3705 }
3706 };
3707 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003708 return;
3709 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003710
Daniel Sandler843e8602010-06-07 14:59:01 -04003711 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3712 if (DEBUG_WIDGETS) {
3713 Log.d(TAG, "bindAppWidget: " + item);
3714 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 final Workspace workspace = mWorkspace;
3716
3717 final int appWidgetId = item.appWidgetId;
3718 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003719 if (DEBUG_WIDGETS) {
3720 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3721 }
3722
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3724
Joe Onorato9c1289c2009-08-17 11:03:03 -04003725 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003726 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003730 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3731
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 workspace.requestLayout();
3733
Daniel Sandler843e8602010-06-07 14:59:01 -04003734 if (DEBUG_WIDGETS) {
3735 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3736 + (SystemClock.uptimeMillis()-start) + "ms");
3737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738 }
3739
Adam Cohen1462de32012-07-24 22:34:36 -07003740 public void onPageBoundSynchronously(int page) {
3741 mSynchronouslyBoundPages.add(page);
3742 }
3743
Joe Onorato9c1289c2009-08-17 11:03:03 -04003744 /**
3745 * Callback saying that there aren't any more items to bind.
3746 *
3747 * Implementation of the method from LauncherModel.Callbacks.
3748 */
Adam Cohene25af792013-06-06 23:08:25 -07003749 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003750 Runnable r = new Runnable() {
3751 public void run() {
3752 finishBindingItems(upgradePath);
3753 }
3754 };
3755 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003756 return;
3757 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003758 if (mSavedState != null) {
3759 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003760 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 mSavedState = null;
3763 }
3764
Winson Chung98ca0f72013-07-29 12:58:51 -07003765 // Create the custom content page here before onLayout to prevent flashing
3766 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3767 mWorkspace.createCustomContentPage();
3768 }
3769
Adam Cohen1462de32012-07-24 22:34:36 -07003770 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003771
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003772 // If we received the result of any pending adds while the loader was running (e.g. the
3773 // widget configuration forced an orientation change), process them now.
3774 for (int i = 0; i < sPendingAddList.size(); i++) {
3775 completeAdd(sPendingAddList.get(i));
3776 }
3777 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778
Winson Chungc51db6a2011-10-05 11:44:49 -07003779 // Update the market app icon as necessary (the other icons will be managed in response to
3780 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003781 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003782
Winson Chungf0c6ae02012-03-21 16:10:31 -07003783 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003784 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003785 mWorkspace.stripDuplicateApps();
3786 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003787 }
Adam Cohen99894d92013-06-14 11:22:59 -07003788
Adam Cohen66a01fd2013-06-11 12:48:00 -07003789 mWorkspace.post(new Runnable() {
3790 @Override
3791 public void run() {
3792 onFinishBindingItems();
3793 }
3794 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003795 }
3796
Winson Chunga2413752012-04-03 14:22:34 -07003797 private boolean canRunNewAppsAnimation() {
3798 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3799 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3800 }
3801
Winson Chungc9168342013-06-26 14:54:55 -07003802 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3803 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3804 PropertyValuesHolder.ofFloat("alpha", 1f),
3805 PropertyValuesHolder.ofFloat("scaleX", 1f),
3806 PropertyValuesHolder.ofFloat("scaleY", 1f));
3807 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3808 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3809 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3810 return bounceAnim;
3811 }
3812
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003813 @Override
3814 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003815 boolean searchVisible = updateGlobalSearchIcon();
3816 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003817 if (mSearchDropTargetBar != null) {
3818 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3819 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003820 }
3821
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003822 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 * Add the icons for all apps.
3824 *
3825 * Implementation of the method from LauncherModel.Callbacks.
3826 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003827 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003828 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3829 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003830 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003831 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003832 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003833 }
3834
3835 /**
3836 * A package was updated.
3837 *
3838 * Implementation of the method from LauncherModel.Callbacks.
3839 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003840 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003841 Runnable r = new Runnable() {
3842 public void run() {
3843 bindAppsUpdated(apps);
3844 }
3845 };
3846 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003847 return;
3848 }
3849
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003850 if (mWorkspace != null) {
3851 mWorkspace.updateShortcuts(apps);
3852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003853 }
3854
3855 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003856 * A package was uninstalled. We take both the super set of packageNames
3857 * in addition to specific applications to remove, the reason being that
3858 * this can be called when a package is updated as well. In that scenario,
3859 * we only remove specific components from the workspace, where as
3860 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003861 *
3862 * Implementation of the method from LauncherModel.Callbacks.
3863 */
Winson Chung83892cc2013-05-01 16:53:33 -07003864 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3865 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003866 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003867 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003868 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003869 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003870 }
Winson Chungd64d1762013-08-20 14:37:16 -07003871 };
3872 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003873 return;
3874 }
3875
Winson Chung64359a52013-07-08 17:17:08 -07003876 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003877 mWorkspace.removeItemsByPackageName(packageNames);
3878 } else {
3879 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003880 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003881
Winson Chunga1820962011-10-03 16:31:06 -07003882 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003883 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003884 }
Joe Onoratobe386092009-11-17 17:32:16 -08003885
3886 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003887 * A number of packages were updated.
3888 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003889 private ArrayList<Object> mWidgetsAndShortcuts;
3890 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003891 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003892 bindPackagesUpdated(mWidgetsAndShortcuts);
3893 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003894 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003895 };
3896
3897 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3898 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3899 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003900 return;
3901 }
3902
Winson Chung64359a52013-07-08 17:17:08 -07003903 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003904 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003905 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003906 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003907 }
3908
Winson Chung400438b2011-01-16 17:53:48 -08003909 private int mapConfigurationOriActivityInfoOri(int configOri) {
3910 final Display d = getWindowManager().getDefaultDisplay();
3911 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3912 switch (d.getRotation()) {
3913 case Surface.ROTATION_0:
3914 case Surface.ROTATION_180:
3915 // We are currently in the same basic orientation as the natural orientation
3916 naturalOri = configOri;
3917 break;
3918 case Surface.ROTATION_90:
3919 case Surface.ROTATION_270:
3920 // We are currently in the other basic orientation to the natural orientation
3921 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3922 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3923 break;
3924 }
3925
3926 int[] oriMap = {
3927 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3928 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3929 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3930 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3931 };
3932 // Since the map starts at portrait, we need to offset if this device's natural orientation
3933 // is landscape.
3934 int indexOffset = 0;
3935 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3936 indexOffset = 1;
3937 }
3938 return oriMap[(d.getRotation() + indexOffset) % 4];
3939 }
Adam Cohen446e9402011-09-15 18:21:21 -07003940
Winson Chung4b919f82012-05-01 10:44:08 -07003941 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003942 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003943 getResources().getBoolean(R.bool.allow_rotation);
3944 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003945 }
Winson Chung4b919f82012-05-01 10:44:08 -07003946 public void lockScreenOrientation() {
3947 if (isRotationEnabled()) {
3948 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3949 .getConfiguration().orientation));
3950 }
3951 }
3952 public void unlockScreenOrientation(boolean immediate) {
3953 if (isRotationEnabled()) {
3954 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003955 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003956 } else {
3957 mHandler.postDelayed(new Runnable() {
3958 public void run() {
3959 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3960 }
3961 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003962 }
Winson Chung4b919f82012-05-01 10:44:08 -07003963 }
Winson Chung400438b2011-01-16 17:53:48 -08003964 }
3965
Winson Chung82f55532011-08-09 14:14:23 -07003966 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003967 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003968 if (DISABLE_CLINGS) {
3969 return false;
3970 }
3971
Brett Chabot2a9e2282011-08-23 15:03:13 -07003972 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003973 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003974
Michael Jurkae233a8b2013-03-19 13:49:20 +01003975 // Restricted secondary users (child mode) will potentially have very few apps
3976 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003977// boolean supportsLimitedUsers =
3978// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3979// Account[] accounts = AccountManager.get(this).getAccounts();
3980// if (supportsLimitedUsers && accounts.length == 0) {
3981// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3982// Bundle restrictions = um.getUserRestrictions();
3983// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3984// return false;
3985// }
3986// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003987 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003988 }
Michael Jurka22143132012-10-04 17:42:38 +02003989
Winson Chung7d7541e2011-09-16 20:14:36 -07003990 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003991 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003992 if (cling != null) {
3993 cling.init(this, positionData);
3994 cling.setVisibility(View.VISIBLE);
3995 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3996 if (animate) {
3997 cling.buildLayer();
3998 cling.setAlpha(0f);
3999 cling.animate()
4000 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004001 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004002 .setDuration(SHOW_CLING_DURATION)
4003 .setStartDelay(delay)
4004 .start();
4005 } else {
4006 cling.setAlpha(1f);
4007 }
Michael Jurka22143132012-10-04 17:42:38 +02004008 cling.setFocusableInTouchMode(true);
4009 cling.post(new Runnable() {
4010 public void run() {
4011 cling.setFocusable(true);
4012 cling.requestFocus();
4013 }
4014 });
4015 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4016 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004017 }
4018 return cling;
4019 }
Michael Jurka22143132012-10-04 17:42:38 +02004020
Winson Chung7d7541e2011-09-16 20:14:36 -07004021 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004022 // To catch cases where siblings of top-level views are made invisible, just check whether
4023 // the cling is directly set to GONE before dismissing it.
4024 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004025 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004026 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004027 anim.addListener(new AnimatorListenerAdapter() {
4028 public void onAnimationEnd(Animator animation) {
4029 cling.setVisibility(View.GONE);
4030 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004031 // We should update the shared preferences on a background thread
4032 new Thread("dismissClingThread") {
4033 public void run() {
4034 SharedPreferences.Editor editor = mSharedPrefs.edit();
4035 editor.putBoolean(flag, true);
4036 editor.commit();
4037 }
4038 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004039 };
4040 });
4041 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004042 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004043 }
4044 }
Michael Jurka22143132012-10-04 17:42:38 +02004045
Winson Chung9d9d74f2011-09-19 11:49:12 -07004046 private void removeCling(int id) {
4047 final View cling = findViewById(id);
4048 if (cling != null) {
4049 final ViewGroup parent = (ViewGroup) cling.getParent();
4050 parent.post(new Runnable() {
4051 @Override
4052 public void run() {
4053 parent.removeView(cling);
4054 }
4055 });
Michael Jurka22143132012-10-04 17:42:38 +02004056 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004057 }
4058 }
Michael Jurka974c3862012-05-22 22:00:31 -07004059
4060 private boolean skipCustomClingIfNoAccounts() {
4061 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4062 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4063 if (customCling) {
4064 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004065 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004066 Account[] accounts = am.getAccountsByType("com.google");
4067 return accounts.length == 0;
4068 }
4069 return false;
4070 }
4071
Winson Chung7d7541e2011-09-16 20:14:36 -07004072 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004073 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004074 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004075 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4076 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004077 // If we're not using the default workspace layout, replace workspace cling
4078 // with a custom workspace cling (usually specified in an overlay)
4079 // For now, only do this on tablets
4080 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004081 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004082 // Use a custom cling
4083 View cling = findViewById(R.id.workspace_cling);
4084 ViewGroup clingParent = (ViewGroup) cling.getParent();
4085 int clingIndex = clingParent.indexOfChild(cling);
4086 clingParent.removeViewAt(clingIndex);
4087 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4088 clingParent.addView(customCling, clingIndex);
4089 customCling.setId(R.id.workspace_cling);
4090 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004091 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004092 } else {
4093 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004094 }
4095 }
4096 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004097 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004098 if (isClingsEnabled() &&
4099 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004101 } else {
4102 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 }
4104 }
4105 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004106 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004107 if (isClingsEnabled() &&
4108 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4109 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004110 } else {
4111 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004112 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004113 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 }
Michael Jurka104c4562013-07-08 18:03:46 -07004115 protected SharedPreferences getSharedPrefs() {
4116 return mSharedPrefs;
4117 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004118 public boolean isFolderClingVisible() {
4119 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004120 if (cling != null) {
4121 return cling.getVisibility() == View.VISIBLE;
4122 }
4123 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 }
Winson Chung82f55532011-08-09 14:14:23 -07004125 public void dismissWorkspaceCling(View v) {
4126 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004127 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004128 }
4129 public void dismissAllAppsCling(View v) {
4130 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004131 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4132 }
4133 public void dismissFolderCling(View v) {
4134 Cling cling = (Cling) findViewById(R.id.folder_cling);
4135 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004136 }
4137
Winson Chung80baf5a2010-08-09 16:03:15 -07004138 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004139 * Prints out out state for debugging.
4140 */
4141 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004142 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004143 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004144 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4145 Log.d(TAG, "mRestoring=" + mRestoring);
4146 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4147 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004148 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004149 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004150
Winson Chung785d2eb2011-04-14 16:08:02 -07004151 if (mAppsCustomizeContent != null) {
4152 mAppsCustomizeContent.dumpState();
4153 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004154 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004155 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004156
4157 @Override
4158 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4159 super.dump(prefix, fd, writer, args);
4160 writer.println(" ");
4161 writer.println("Debug logs: ");
4162 for (int i = 0; i < sDumpLogs.size(); i++) {
4163 writer.println(" " + sDumpLogs.get(i));
4164 }
4165 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004166
4167 public static void dumpDebugLogsToConsole() {
4168 Log.d(TAG, "");
4169 Log.d(TAG, "*********************");
4170 Log.d(TAG, "Launcher debug logs: ");
4171 for (int i = 0; i < sDumpLogs.size(); i++) {
4172 Log.d(TAG, " " + sDumpLogs.get(i));
4173 }
4174 Log.d(TAG, "*********************");
4175 Log.d(TAG, "");
4176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004177}
Michael Jurka2763be32011-02-24 11:19:57 -08004178
Michael Jurkaabded662011-03-04 12:06:57 -08004179interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004180 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004181 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004182 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004183 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004184 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004185}