blob: 9ffc572eba35ff2bda30d16d1d7890d8f3f43e95 [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);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07002043 Intent chooser = Intent.createChooser(pickWallpaper,
2044 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002045 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2046 // Removed in Eclair MR1
2047// WallpaperManager wm = (WallpaperManager)
2048// getSystemService(Context.WALLPAPER_SERVICE);
2049// WallpaperInfo wi = wm.getWallpaperInfo();
2050// if (wi != null && wi.getSettingsActivity() != null) {
2051// LabeledIntent li = new LabeledIntent(getPackageName(),
2052// R.string.configure_wallpaper, 0);
2053// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2054// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2055// }
Mike Clerona0618e42009-10-22 13:55:21 -07002056 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 }
2058
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002059 /**
2060 * Registers various content observers. The current implementation registers
2061 * only a favorites observer to keep track of the favorites applications.
2062 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002063 private void registerContentObservers() {
2064 ContentResolver resolver = getContentResolver();
2065 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2066 true, mWidgetObserver);
2067 }
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 @Override
2070 public boolean dispatchKeyEvent(KeyEvent event) {
2071 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2072 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002074 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002075 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002076 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002077 dumpState();
2078 return true;
2079 }
2080 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002081 }
2082 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2083 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002084 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 return true;
2086 }
2087 }
2088
2089 return super.dispatchKeyEvent(event);
2090 }
2091
Joe Onorato88ec0992009-11-19 13:16:06 -08002092 @Override
2093 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002094 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002095 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002096 } else if (mWorkspace.isInOverviewMode()) {
2097 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002098 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002099 Folder openFolder = mWorkspace.getOpenFolder();
2100 if (openFolder.isEditingName()) {
2101 openFolder.dismissEditingName();
2102 } else {
2103 closeFolder();
2104 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002105 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002106 mWorkspace.exitWidgetResizeMode();
2107
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002108 // Back button is a no-op here, but give at least some feedback for the button press
2109 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002110 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002111 }
2112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002114 * Re-listen when widgets are reset.
2115 */
2116 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002117 if (mAppWidgetHost != null) {
2118 mAppWidgetHost.startListening();
2119 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002120 }
2121
2122 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 * Launches the intent referred by the clicked shortcut.
2124 *
2125 * @param v The view representing the clicked shortcut.
2126 */
2127 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002128 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2129 // view has detached (it's possible for this to happen if the view is removed mid touch).
2130 if (v.getWindowToken() == null) {
2131 return;
2132 }
2133
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002134 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002135 return;
2136 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002137
Adam Cohenf358a4b2013-07-23 16:47:31 -07002138 if (v instanceof PageIndicator) {
2139 if (!mWorkspace.isInOverviewMode()) {
2140 mWorkspace.enterOverviewMode();
2141 }
2142 return;
2143 }
2144
2145 if (v instanceof CellLayout) {
2146 if (mWorkspace.isInOverviewMode()) {
2147 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2148 }
2149 }
2150
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002151 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002152 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002154 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2155 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002156
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002157 // Check for special shortcuts
2158 if (intent.getComponent() != null) {
2159 final String shortcutClass = intent.getComponent().getClassName();
2160
2161 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2162 showAllApps(true);
2163 return;
2164 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2165 MemoryDumpActivity.startDump(this);
2166 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002167 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2168 toggleShowWeightWatcher();
2169 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002170 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002171 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002172
2173 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002174 int[] pos = new int[2];
2175 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002176 intent.setSourceBounds(new Rect(pos[0], pos[1],
2177 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002178
2179 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002180
Daniel Sandlerff02d492013-08-05 02:12:05 -04002181 mStats.recordLaunch(intent, shortcut);
2182
Michael Jurkaddd62e92011-02-16 17:49:14 -08002183 if (success && v instanceof BubbleTextView) {
2184 mWaitingForResume = (BubbleTextView) v;
2185 mWaitingForResume.setStayPressed(true);
2186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002187 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002188 if (v instanceof FolderIcon) {
2189 FolderIcon fi = (FolderIcon) v;
2190 handleFolderClick(fi);
2191 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002192 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002193 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002194 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002195 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002196 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 }
2199 }
2200
Michael Jurka0e260592010-06-30 17:07:39 -07002201 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002202 return false;
2203 }
2204
Michael Jurkaaf442092010-06-10 17:01:57 -07002205 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002206 * Event handler for the search button
2207 *
2208 * @param v The view that was clicked.
2209 */
2210 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002211 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2212
Amith Yamasania135ba82011-08-09 17:42:01 -07002213 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002214 }
2215
2216 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002217 * Event handler for the voice button
2218 *
2219 * @param v The view that was clicked.
2220 */
2221 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002222 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002223
Bjorn Bringertc459e522013-06-07 19:36:01 +01002224 startVoice();
2225 }
2226
2227 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002228 try {
2229 final SearchManager searchManager =
2230 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2231 ComponentName activityName = searchManager.getGlobalSearchActivity();
2232 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002233 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002234 if (activityName != null) {
2235 intent.setPackage(activityName.getPackageName());
2236 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002237 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002238 } catch (ActivityNotFoundException e) {
2239 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002240 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002241 startActivitySafely(null, intent, "onClickVoiceButton");
2242 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002243 }
2244
2245 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002246 * Event handler for the "grid" button that appears on the home screen, which
2247 * enters all apps mode.
2248 *
2249 * @param v The view that was clicked.
2250 */
2251 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002252 showAllApps(true);
2253 }
2254
2255 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002256 // Provide the same haptic feedback that the system offers for virtual keys.
2257 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002258 }
2259
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002260 public void onClickAppMarketButton(View v) {
2261 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002262 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002263 } else {
2264 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002265 }
2266 }
2267
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002268 void startApplicationDetailsActivity(ComponentName componentName) {
2269 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002270 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2271 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002272 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002273 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002274 }
2275
Michael Jurka1e2f4652013-07-08 18:03:46 -07002276 // returns true if the activity was started
2277 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2278 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002279 // System applications cannot be installed. For now, show a toast explaining that.
2280 // We may give them the option of disabling apps this way.
2281 int messageId = R.string.uninstall_system_app_text;
2282 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002283 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002284 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002285 String packageName = componentName.getPackageName();
2286 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002287 Intent intent = new Intent(
2288 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002289 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2290 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002291 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002292 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002293 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002294 }
2295
Michael Jurka86a720e2012-05-09 11:23:23 -07002296 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002298
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002300 // Only launch using the new animation if the shortcut has not opted out (this is a
2301 // private contract between launcher and may be ignored in the future).
2302 boolean useLaunchAnimation = (v != null) &&
2303 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2304 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002305 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2306 v.getMeasuredWidth(), v.getMeasuredHeight());
2307
2308 startActivity(intent, opts.toBundle());
2309 } else {
2310 startActivity(intent);
2311 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002312 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 } catch (SecurityException e) {
2314 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002315 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002317 "or use the exported attribute for this activity. "
2318 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002320 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002322
Michael Jurka86a720e2012-05-09 11:23:23 -07002323 boolean startActivitySafely(View v, Intent intent, Object tag) {
2324 boolean success = false;
2325 try {
2326 success = startActivity(v, intent, tag);
2327 } catch (ActivityNotFoundException e) {
2328 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2329 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2330 }
2331 return success;
2332 }
2333
Romain Guy8e633c52010-03-04 12:51:36 -08002334 void startActivityForResultSafely(Intent intent, int requestCode) {
2335 try {
2336 startActivityForResult(intent, requestCode);
2337 } catch (ActivityNotFoundException e) {
2338 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2339 } catch (SecurityException e) {
2340 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2341 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2342 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2343 "or use the exported attribute for this activity.", e);
2344 }
2345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346
Adam Cohena9cf38f2011-05-02 15:36:58 -07002347 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002348 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002349 Folder openFolder = mWorkspace.getFolderForTag(info);
2350
2351 // If the folder info reports that the associated folder is open, then verify that
2352 // it is actually opened. There have been a few instances where this gets out of sync.
2353 if (info.opened && openFolder == null) {
2354 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002355 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002356 info.opened = false;
2357 }
2358
Adam Cohenfb91f302012-06-11 15:45:18 -07002359 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002360 // Close any open folder
2361 closeFolder();
2362 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002363 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 } else {
2365 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 int folderScreen;
2367 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002368 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 // .. and close it
2370 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002371 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 // Close any folder open on the current screen
2373 closeFolder();
2374 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002375 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 }
2377 }
2378 }
2379 }
2380
Adam Cohen268c4752012-06-06 17:47:33 -07002381 /**
2382 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2383 * in the DragLayer in the exact absolute location of the original FolderIcon.
2384 */
2385 private void copyFolderIconToImage(FolderIcon fi) {
2386 final int width = fi.getMeasuredWidth();
2387 final int height = fi.getMeasuredHeight();
2388
2389 // Lazy load ImageView, Bitmap and Canvas
2390 if (mFolderIconImageView == null) {
2391 mFolderIconImageView = new ImageView(this);
2392 }
2393 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2394 mFolderIconBitmap.getHeight() != height) {
2395 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2396 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2397 }
2398
2399 DragLayer.LayoutParams lp;
2400 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2401 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2402 } else {
2403 lp = new DragLayer.LayoutParams(width, height);
2404 }
2405
Adam Cohen307fe232012-08-16 17:55:58 -07002406 // The layout from which the folder is being opened may be scaled, adjust the starting
2407 // view size by this scale factor.
2408 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002409 lp.customPosition = true;
2410 lp.x = mRectForFolderAnimation.left;
2411 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002412 lp.width = (int) (scale * width);
2413 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002414
2415 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2416 fi.draw(mFolderIconCanvas);
2417 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002418 if (fi.getFolder() != null) {
2419 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2420 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002421 }
Adam Cohen268c4752012-06-06 17:47:33 -07002422 // Just in case this image view is still in the drag layer from a previous animation,
2423 // we remove it and re-add it.
2424 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2425 mDragLayer.removeView(mFolderIconImageView);
2426 }
2427 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002428 if (fi.getFolder() != null) {
2429 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002430 }
Adam Cohen268c4752012-06-06 17:47:33 -07002431 }
2432
Adam Cohen2801caf2011-05-13 20:57:39 -07002433 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002434 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002435 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2436 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2437 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2438
Adam Cohenc51934b2011-07-26 21:07:43 -07002439 FolderInfo info = (FolderInfo) fi.getTag();
2440 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2441 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002442 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2443 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002444 }
2445
Adam Cohen268c4752012-06-06 17:47:33 -07002446 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2447 copyFolderIconToImage(fi);
2448 fi.setVisibility(View.INVISIBLE);
2449
Michael Jurka2ecf9952012-06-18 12:52:28 -07002450 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002451 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002452 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2453 oa.start();
2454 }
2455
Adam Cohen268c4752012-06-06 17:47:33 -07002456 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002457 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002458 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2459 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2460 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2461
Adam Cohen268c4752012-06-06 17:47:33 -07002462 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002463
Adam Cohen268c4752012-06-06 17:47:33 -07002464 // We remove and re-draw the FolderIcon in-case it has changed
2465 mDragLayer.removeView(mFolderIconImageView);
2466 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002467 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002468 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002469 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002470 oa.addListener(new AnimatorListenerAdapter() {
2471 @Override
2472 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002473 if (cl != null) {
2474 cl.clearFolderLeaveBehind();
2475 // Remove the ImageView copy of the FolderIcon and make the original visible.
2476 mDragLayer.removeView(mFolderIconImageView);
2477 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002478 }
2479 }
2480 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002481 oa.start();
2482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002485 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 * is animated relative to the specified View. If the View is null, no animation
2487 * is played.
2488 *
2489 * @param folderInfo The FolderInfo describing the folder to open.
2490 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002491 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002492 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002493 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494
Adam Cohena9cf38f2011-05-02 15:36:58 -07002495 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496
Adam Cohen4554ee12011-08-03 16:13:21 -07002497 // Just verify that the folder hasn't already been added to the DragLayer.
2498 // There was a one-off crash where the folder had a parent already.
2499 if (folder.getParent() == null) {
2500 mDragLayer.addView(folder);
2501 mDragController.addDropTarget((DropTarget) folder);
2502 } else {
2503 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2504 folder.getParent() + ").");
2505 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002506 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002507 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002508
2509 // Notify the accessibility manager that this folder "window" has appeared and occluded
2510 // the workspace items
2511 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2512 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002513 }
2514
2515 public void closeFolder() {
2516 Folder folder = mWorkspace.getOpenFolder();
2517 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002518 if (folder.isEditingName()) {
2519 folder.dismissEditingName();
2520 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002521 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002522
2523 // Dismiss the folder cling
2524 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002525 }
2526 }
2527
2528 void closeFolder(Folder folder) {
2529 folder.getInfo().opened = false;
2530
2531 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2532 if (parent != null) {
2533 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2534 shrinkAndFadeInFolderIcon(fi);
2535 }
2536 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002537
2538 // Notify the accessibility manager that this folder "window" has disappeard and no
2539 // longer occludeds the workspace items
2540 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002544 if (!isDraggingEnabled()) return false;
2545 if (isWorkspaceLocked()) return false;
2546 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547
2548 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002549 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 }
2551
Michael Jurka0280c3b2010-09-17 15:00:07 -07002552 resetAddInfo();
2553 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002555 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 return true;
2557 }
2558
Winson Chung3d503fb2011-07-13 17:25:49 -07002559 // The hotseat touch handling does not go through Workspace, and we always allow long press
2560 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002561 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002562 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2563 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002564 if (itemUnderLongClick == null) {
2565 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002566 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2567 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002568 // Disabling reordering until we sort out some issues.
Adam Cohenf358a4b2013-07-23 16:47:31 -07002569 if (mWorkspace.isInOverviewMode()) {
2570 mWorkspace.startReordering(v);
Adam Cohendedbd962013-07-11 14:21:49 -07002571 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002572 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002575 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002576 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002577 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002578 }
2579 }
2580 }
2581 return true;
2582 }
2583
Winson Chung3d503fb2011-07-13 17:25:49 -07002584 boolean isHotseatLayout(View layout) {
2585 return mHotseat != null && layout != null &&
2586 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2587 }
2588 Hotseat getHotseat() {
2589 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002590 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002591 View getOverviewPanel() {
2592 return mOverviewPanel;
2593 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002594 SearchDropTargetBar getSearchBar() {
2595 return mSearchDropTargetBar;
2596 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002597
Winson Chung3d503fb2011-07-13 17:25:49 -07002598 /**
2599 * Returns the CellLayout of the specified container at the specified screen.
2600 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002601 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002602 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2603 if (mHotseat != null) {
2604 return mHotseat.getLayout();
2605 } else {
2606 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002607 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002608 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002609 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002610 }
2611 }
2612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 Workspace getWorkspace() {
2614 return mWorkspace;
2615 }
2616
Daniel Sandler843e8602010-06-07 14:59:01 -04002617 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002618 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002619 }
2620
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 /**
2622 * Helper method for the cameraZoomIn/cameraZoomOut animations
2623 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 * @param scaleFactor The scale factor used for the zoom
2625 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002626 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002628 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002630
Winson Chung18f41f82012-05-09 13:28:10 -07002631 void disableWallpaperIfInAllApps() {
2632 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002633 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002634 if (mAppsCustomizeTabHost != null &&
2635 !mAppsCustomizeTabHost.isTransitioning()) {
2636 updateWallpaperVisibility(false);
2637 }
2638 }
2639 }
2640
Craig Mautner360310b2012-10-26 15:13:08 -07002641 private void setWorkspaceBackground(boolean workspace) {
2642 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002643 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002644 }
2645
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002646 void updateWallpaperVisibility(boolean visible) {
2647 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2648 int curflags = getWindow().getAttributes().flags
2649 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2650 if (wpflags != curflags) {
2651 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2652 }
Craig Mautner360310b2012-10-26 15:13:08 -07002653 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002654 }
2655
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002656 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2657 if (v instanceof LauncherTransitionable) {
2658 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2659 }
2660 }
2661
Michael Jurkabed61d22012-02-14 22:51:29 -08002662 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2663 if (v instanceof LauncherTransitionable) {
2664 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2665 }
Winson Chung70442722012-02-10 15:43:22 -08002666
2667 // Update the workspace transition step as well
2668 dispatchOnLauncherTransitionStep(v, 0f);
2669 }
2670
2671 private void dispatchOnLauncherTransitionStep(View v, float t) {
2672 if (v instanceof LauncherTransitionable) {
2673 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2674 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002675 }
2676
2677 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2678 if (v instanceof LauncherTransitionable) {
2679 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2680 }
Winson Chung70442722012-02-10 15:43:22 -08002681
2682 // Update the workspace transition step as well
2683 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002684 }
2685
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002687 * Things to test when changing the following seven functions.
2688 * - Home from workspace
2689 * - from center screen
2690 * - from other screens
2691 * - Home from all apps
2692 * - from center screen
2693 * - from other screens
2694 * - Back from all apps
2695 * - from center screen
2696 * - from other screens
2697 * - Launch app from workspace and quit
2698 * - with back
2699 * - with home
2700 * - Launch app from all apps and quit
2701 * - with back
2702 * - with home
2703 * - Go to a screen that's not the default, then all
2704 * apps, and launch and app, and go back
2705 * - with back
2706 * -with home
2707 * - On workspace, long press power and go back
2708 * - with back
2709 * - with home
2710 * - On all apps, long press power and go back
2711 * - with back
2712 * - with home
2713 * - On workspace, power off
2714 * - On all apps, power off
2715 * - Launch an app and turn off the screen while in that app
2716 * - Go back with home key
2717 * - Go back with back key TODO: make this not go to workspace
2718 * - From all apps
2719 * - From workspace
2720 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2721 * - From all apps
2722 * - From the center workspace
2723 * - From another workspace
2724 */
2725
2726 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 * Zoom the camera out from the workspace to reveal 'toView'.
2728 * Assumes that the view to show is anchored at either the very top or very bottom
2729 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002731 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002732 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002733 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002734 mStateAnimation.cancel();
2735 mStateAnimation = null;
2736 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002737 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002738
Winson Chungf0ea4d32011-06-06 14:27:16 -07002739 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2740 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2741 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002742 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002743 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002744 final int startDelay =
2745 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002746
Michael Jurkab3e22d92011-10-31 15:58:33 -07002747 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002748
Michael Jurkad74c9842011-07-10 12:44:21 -07002749 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002750 Animator workspaceAnim =
2751 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752
2753 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002754 toView.setScaleX(scale);
2755 toView.setScaleY(scale);
2756 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2757 scaleAnim.
2758 scaleX(1f).scaleY(1f).
2759 setDuration(duration).
2760 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002761
Winson Chungf0ea4d32011-06-06 14:27:16 -07002762 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002763 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002764 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002765 .ofFloat(toView, "alpha", 0f, 1f)
2766 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002767 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002768 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2769 @Override
2770 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002771 if (animation == null) {
2772 throw new RuntimeException("animation is null");
2773 }
Winson Chung70442722012-02-10 15:43:22 -08002774 float t = (Float) animation.getAnimatedValue();
2775 dispatchOnLauncherTransitionStep(fromView, t);
2776 dispatchOnLauncherTransitionStep(toView, t);
2777 }
2778 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002779
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002780 // toView should appear right at the end of the workspace shrink
2781 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002782 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002783 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002784 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002785
2786 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002787 boolean animationCancelled = false;
2788
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002789 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002790 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002791 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792 // Prepare the position
2793 toView.setTranslationX(0.0f);
2794 toView.setTranslationY(0.0f);
2795 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002796 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002798 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002799 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002800 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2801 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002802
Adam Cohenf4ddea32011-09-12 13:46:42 -07002803 if (!animationCancelled) {
2804 updateWallpaperVisibility(false);
2805 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002806
2807 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002808 if (mSearchDropTargetBar != null) {
2809 mSearchDropTargetBar.hideSearchBar(false);
2810 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002811 }
2812
Adam Cohencff6af82011-09-13 14:51:53 -07002813 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002814 public void onAnimationCancel(Animator animation) {
2815 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002816 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002817 });
2818
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002819 if (workspaceAnim != null) {
2820 mStateAnimation.play(workspaceAnim);
2821 }
Michael Jurka1899a362011-11-03 13:50:45 -07002822
2823 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002824
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002825 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2826 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002827
2828 // If any of the objects being animated haven't been measured/laid out
2829 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002830 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002831 (mWorkspace.getMeasuredWidth() == 0) ||
2832 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002833 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002834 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002835
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002836 final AnimatorSet stateAnimation = mStateAnimation;
2837 final Runnable startAnimRunnable = new Runnable() {
2838 public void run() {
2839 // Check that mStateAnimation hasn't changed while
2840 // we waited for a layout/draw pass
2841 if (mStateAnimation != stateAnimation)
2842 return;
2843 setPivotsForZoom(toView, scale);
2844 dispatchOnLauncherTransitionStart(fromView, animated, false);
2845 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002846 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002847 }
2848 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002849 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002850 final ViewTreeObserver observer = toView.getViewTreeObserver();
2851 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2852 public void onGlobalLayout() {
2853 startAnimRunnable.run();
2854 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2855 }
2856 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002857 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002858 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002859 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 } else {
2861 toView.setTranslationX(0.0f);
2862 toView.setTranslationY(0.0f);
2863 toView.setScaleX(1.0f);
2864 toView.setScaleY(1.0f);
2865 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002866 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002867
Daniel Sandlere4f98912013-06-25 15:13:26 -04002868 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002869 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002870 if (mSearchDropTargetBar != null) {
2871 mSearchDropTargetBar.hideSearchBar(false);
2872 }
Michael Jurkaabded662011-03-04 12:06:57 -08002873 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002874 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002875 dispatchOnLauncherTransitionStart(fromView, animated, false);
2876 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002877 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002878 dispatchOnLauncherTransitionStart(toView, animated, false);
2879 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002880 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002881 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002882 }
2883
2884 /**
2885 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002886 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002887 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002888 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002889 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2890 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002891
Michael Jurkab3e22d92011-10-31 15:58:33 -07002892 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002893 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002894 mStateAnimation.cancel();
2895 mStateAnimation = null;
2896 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002897 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002898
Winson Chungf0ea4d32011-06-06 14:27:16 -07002899 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002900 final int fadeOutDuration =
2901 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 final float scaleFactor = (float)
2903 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2904 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002905 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002906 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002907 if (toState == State.WORKSPACE) {
2908 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2909 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002910 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002911 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2912 workspaceAnim = mWorkspace.getChangeStateAnimation(
2913 Workspace.State.SPRING_LOADED, animated);
2914 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002915
Michael Jurkab3e22d92011-10-31 15:58:33 -07002916 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002917 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002918 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002919 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002920 final LauncherViewPropertyAnimator scaleAnim =
2921 new LauncherViewPropertyAnimator(fromView);
2922 scaleAnim.
2923 scaleX(scaleFactor).scaleY(scaleFactor).
2924 setDuration(duration).
2925 setInterpolator(new Workspace.ZoomInInterpolator());
2926
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002927 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002928 .ofFloat(fromView, "alpha", 1f, 0f)
2929 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002930 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002931 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2932 @Override
2933 public void onAnimationUpdate(ValueAnimator animation) {
2934 float t = 1f - (Float) animation.getAnimatedValue();
2935 dispatchOnLauncherTransitionStep(fromView, t);
2936 dispatchOnLauncherTransitionStep(toView, t);
2937 }
2938 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002939
Michael Jurka2ecf9952012-06-18 12:52:28 -07002940 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002941
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002942 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2943 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002944 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002945
2946 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002947 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002948 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002949 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002950 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002951 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2952 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002953 if (onCompleteRunnable != null) {
2954 onCompleteRunnable.run();
2955 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002956 mAppsCustomizeContent.updateCurrentPageScroll();
2957 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002958 }
2959 });
2960
Winson Chungf0ea4d32011-06-06 14:27:16 -07002961 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002962 if (workspaceAnim != null) {
2963 mStateAnimation.play(workspaceAnim);
2964 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002965 dispatchOnLauncherTransitionStart(fromView, animated, true);
2966 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002967 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002968 } else {
2969 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002970 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002971 dispatchOnLauncherTransitionStart(fromView, animated, true);
2972 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002973 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002974 dispatchOnLauncherTransitionStart(toView, animated, true);
2975 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002976 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002977 }
2978
Michael Jurkae326f182011-11-21 14:05:46 -08002979 @Override
2980 public void onTrimMemory(int level) {
2981 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002982 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002983 mAppsCustomizeTabHost.onTrimMemory();
2984 }
2985 }
2986
Winson Chung18f41f82012-05-09 13:28:10 -07002987 @Override
2988 public void onWindowFocusChanged(boolean hasFocus) {
2989 if (!hasFocus) {
2990 // When another window occludes launcher (like the notification shade, or recents),
2991 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2992 updateWallpaperVisibility(true);
2993 } else {
2994 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002995 mWorkspace.postDelayed(new Runnable() {
2996 @Override
2997 public void run() {
2998 disableWallpaperIfInAllApps();
2999 }
3000 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07003001 }
3002 }
3003
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003004 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003005 showWorkspace(animated, null);
3006 }
3007
3008 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003009 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003010 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003011 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003012 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003013
Winson Chungc7d2b602012-05-16 17:02:20 -07003014 // Show the search bar (only animate if we were showing the drop target bar in spring
3015 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003016 if (mSearchDropTargetBar != null) {
3017 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3018 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003019
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 // Set focus to the AppsCustomize button
3021 if (mAllAppsButton != null) {
3022 mAllAppsButton.requestFocus();
3023 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003024 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003025
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003026 // Change the state *after* we've called all the transition code
3027 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003028
Winson Chung5fb63472011-02-02 17:03:37 -08003029 // Resume the auto-advance of widgets
3030 mUserPresent = true;
3031 updateRunning();
3032
alanv1d4fde62012-10-17 13:15:47 -07003033 // Send an accessibility event to announce the context change
3034 getWindow().getDecorView()
3035 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003036
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003037 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003038 }
3039
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003040 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003041 }
3042
Michael Jurkab3e22d92011-10-31 15:58:33 -07003043 void showAllApps(boolean animated) {
3044 if (mState != State.WORKSPACE) return;
3045
3046 showAppsCustomizeHelper(animated, false);
3047 mAppsCustomizeTabHost.requestFocus();
3048
Michael Jurkab3e22d92011-10-31 15:58:33 -07003049 // Change the state *after* we've called all the transition code
3050 mState = State.APPS_CUSTOMIZE;
3051
3052 // Pause the auto-advance of widgets until we are out of AllApps
3053 mUserPresent = false;
3054 updateRunning();
3055 closeFolder();
3056
3057 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003058 getWindow().getDecorView()
3059 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003060 }
3061
Adam Cohen7777d962011-08-18 18:58:38 -07003062 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003063 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003064 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003065 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003066 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003067 }
Adam Cohen7777d962011-08-18 18:58:38 -07003068
Adam Cohened66b2b2012-01-23 17:28:51 -08003069 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3070 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003071 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3072
Winson Chunge7a03942011-08-05 15:05:12 -07003073 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003074 @Override
3075 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003076 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003077 // Before we show workspace, hide all apps again because
3078 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3079 // clean up our state transition functions
3080 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003081 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003082 } else {
3083 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003084 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003085 }
3086 }, (extendedDelay ?
3087 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3088 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3089 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003090
Michael Jurkad3ef3062010-11-23 16:23:58 -08003091 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003092 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003093 final boolean animated = true;
3094 final boolean springLoaded = true;
3095 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003096 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003097 }
3098 // Otherwise, we are not in spring loaded mode, so don't do anything.
3099 }
3100
Michael Jurkab3e22d92011-10-31 15:58:33 -07003101 void lockAllApps() {
3102 // TODO
3103 }
3104
3105 void unlockAllApps() {
3106 // TODO
3107 }
3108
Winson Chungf0ea4d32011-06-06 14:27:16 -07003109 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003110 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003111 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003112 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003113 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003115 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003116 int duration = 0;
3117 if (mSearchDropTargetBar != null) {
3118 duration = mSearchDropTargetBar.getTransitionInDuration();
3119 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003120 mHotseat.animate().alpha(1f).setDuration(duration);
3121 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003122 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003123 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003124 }
3125 }
3126 }
3127
3128 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003129 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003130 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003131 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003132 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003134 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003135 int duration = 0;
3136 if (mSearchDropTargetBar != null) {
3137 duration = mSearchDropTargetBar.getTransitionOutDuration();
3138 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003139 mHotseat.animate().alpha(0f).setDuration(duration);
3140 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003141 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003142 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003143 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003144 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003145 }
3146
Patrick Dubroy5f445422011-02-18 14:35:21 -08003147 /**
3148 * Add an item from all apps or customize onto the given workspace screen.
3149 * If layout is null, add to the current screen.
3150 */
3151 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003152 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003153 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003154 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003155 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003156
Winson Chungdff8ebb2011-09-08 17:25:31 -07003157 /** Maps the current orientation to an index for referencing orientation correct global icons */
3158 private int getCurrentOrientationIndexForGlobalIcons() {
3159 // default - 0, landscape - 1
3160 switch (getResources().getConfiguration().orientation) {
3161 case Configuration.ORIENTATION_LANDSCAPE:
3162 return 1;
3163 default:
3164 return 0;
3165 }
3166 }
3167
Michael Jurkaae65ee32012-04-30 11:45:54 -07003168 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003170 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003171 // Look for the toolbar icon specified in the activity meta-data
3172 Bundle metaData = packageManager.getActivityInfo(
3173 activityName, PackageManager.GET_META_DATA).metaData;
3174 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003175 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003176 if (iconResId != 0) {
3177 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179 }
3180 }
3181 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003182 // This can happen if the activity defines an invalid drawable
3183 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3184 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003185 } catch (Resources.NotFoundException nfe) {
3186 // This can happen if the activity defines an invalid drawable
3187 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3188 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003189 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003190 return null;
3191 }
3192
3193 // if successful in getting icon, return it; otherwise, set button to use default drawable
3194 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003195 int buttonId, ComponentName activityName, int fallbackDrawableId,
3196 String toolbarResourceName) {
3197 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003198 Resources r = getResources();
3199 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3200 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003201
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003202 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003203 // If we were unable to find the icon via the meta-data, use a generic one
3204 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003205 toolbarIcon = r.getDrawable(fallbackDrawableId);
3206 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003207 if (button != null) {
3208 button.setCompoundDrawables(toolbarIcon, null, null, null);
3209 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003210 return null;
3211 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003212 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003213 if (button != null) {
3214 button.setCompoundDrawables(toolbarIcon, null, null, null);
3215 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216 return toolbarIcon.getConstantState();
3217 }
3218 }
3219
3220 // if successful in getting icon, return it; otherwise, set button to use default drawable
3221 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003222 int buttonId, ComponentName activityName, int fallbackDrawableId,
3223 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003224 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003225 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003226
Michael Jurka19e0fc52011-07-22 18:00:21 -07003227 if (button != null) {
3228 // If we were unable to find the icon via the meta-data, use a
3229 // generic one
3230 if (toolbarIcon == null) {
3231 button.setImageResource(fallbackDrawableId);
3232 } else {
3233 button.setImageDrawable(toolbarIcon);
3234 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003235 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003236
3237 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3238
Michael Jurka0423dcf2010-10-05 14:56:18 -07003239 }
3240
Winson Chung1b884092012-06-08 17:13:02 -07003241 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003242 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003243 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003244 }
3245
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003246 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003247 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003248 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003249 }
3250
Winson Chungbb185bd2011-11-21 12:31:42 -08003251 private void invalidatePressedFocusedStates(View container, View button) {
3252 if (container instanceof HolographicLinearLayout) {
3253 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3254 layout.invalidatePressedFocusedStates();
3255 } else if (button instanceof HolographicImageView) {
3256 HolographicImageView view = (HolographicImageView) button;
3257 view.invalidatePressedFocusedStates();
3258 }
3259 }
3260
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 private boolean updateGlobalSearchIcon() {
3262 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003263 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003264 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003265 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003266
Winson Chung1cad91e2011-05-25 17:41:01 -07003267 final SearchManager searchManager =
3268 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3269 ComponentName activityName = searchManager.getGlobalSearchActivity();
3270 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003271 int coi = getCurrentOrientationIndexForGlobalIcons();
3272 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003273 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3274 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3275 if (sGlobalSearchIcon[coi] == null) {
3276 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3277 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3278 TOOLBAR_ICON_METADATA_NAME);
3279 }
3280
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3282 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003283 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003285 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003286 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003287 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3288 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3289 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003290 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003291 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003292 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003293 }
3294 }
3295
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003296 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003297 final View searchButtonContainer = findViewById(R.id.search_button_container);
3298 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003299 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003300 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003301 }
3302
Winson Chungc51db6a2011-10-05 11:44:49 -07003303 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003305 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003306
Winson Chungc51db6a2011-10-05 11:44:49 -07003307 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003308 final SearchManager searchManager =
3309 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3310 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3311
3312 ComponentName activityName = null;
3313 if (globalSearchActivity != null) {
3314 // Check if the global search activity handles voice search
3315 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3316 intent.setPackage(globalSearchActivity.getPackageName());
3317 activityName = intent.resolveActivity(getPackageManager());
3318 }
3319
3320 if (activityName == null) {
3321 // Fallback: check if an activity other than the global search activity
3322 // resolves this
3323 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3324 activityName = intent.resolveActivity(getPackageManager());
3325 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003326 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003327 int coi = getCurrentOrientationIndexForGlobalIcons();
3328 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003329 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3330 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3331 if (sVoiceSearchIcon[coi] == null) {
3332 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3333 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3334 TOOLBAR_ICON_METADATA_NAME);
3335 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003336 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003337 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003338 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003339 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003340 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003341 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003342 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003343 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003344 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003345 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003346 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003347 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003348
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003349 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003350 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3351 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003352 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003353 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003354 }
3355
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003356 public void setVoiceButtonProxyVisible(boolean visible) {
3357 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3358 if (voiceButtonProxy != null) {
3359 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3360 }
3361 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003362 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003363 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003364 */
3365 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003366 final View marketButton = findViewById(R.id.market_button);
3367 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3368 // Find the app market activity by resolving an intent.
3369 // (If multiple app markets are installed, it will return the ResolverActivity.)
3370 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003371 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003372 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003373 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003374 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003375 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3376 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003377 marketButton.setVisibility(View.VISIBLE);
3378 } else {
3379 // We should hide and disable the view so that we don't try and restore the visibility
3380 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3381 marketButton.setVisibility(View.GONE);
3382 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003383 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003384 }
3385
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003386 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003387 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3388 Resources r = getResources();
3389 Drawable marketIconDrawable = d.newDrawable(r);
3390 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3391 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3392 marketIconDrawable.setBounds(0, 0, w, h);
3393
3394 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003395 }
3396
Michael Jurkad7c28052012-04-27 15:43:36 -07003397 @Override
3398 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003399 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003400 final List<CharSequence> text = event.getText();
3401 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003402 // Populate event with a fake title based on the current state.
3403 if (mState == State.APPS_CUSTOMIZE) {
3404 text.add(getString(R.string.all_apps_button_label));
3405 } else {
3406 text.add(getString(R.string.all_apps_home_button_label));
3407 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003408 return result;
3409 }
3410
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003411 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003412 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003413 */
3414 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3415 @Override
3416 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003417 closeSystemDialogs();
3418 }
3419 }
3420
3421 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003422 * Receives notifications whenever the appwidgets are reset.
3423 */
3424 private class AppWidgetResetObserver extends ContentObserver {
3425 public AppWidgetResetObserver() {
3426 super(new Handler());
3427 }
3428
3429 @Override
3430 public void onChange(boolean selfChange) {
3431 onAppWidgetReset();
3432 }
3433 }
3434
3435 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003436 * If the activity is currently paused, signal that we need to run the passed Runnable
3437 * in onResume.
3438 *
3439 * This needs to be called from incoming places where resources might have been loaded
3440 * while we are paused. That is becaues the Configuration might be wrong
3441 * when we're not running, and if it comes back to what it was when we
3442 * were paused, we are not restarted.
3443 *
3444 * Implementation of the method from LauncherModel.Callbacks.
3445 *
3446 * @return true if we are currently paused. The caller might be able to
3447 * skip some work in that case since we will come back again.
3448 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003449 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003450 if (mPaused) {
3451 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003452 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003453 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003454 }
3455 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003456 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003457 return true;
3458 } else {
3459 return false;
3460 }
3461 }
3462
Michael Jurkac402cd92013-05-20 15:49:32 +02003463 private boolean waitUntilResume(Runnable run) {
3464 return waitUntilResume(run, false);
3465 }
3466
Michael Jurka1e2f4652013-07-08 18:03:46 -07003467 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003468 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003469 }
3470
3471 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003472 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003473 }
3474
Michael Jurka7607c2f2013-04-03 14:33:19 -07003475 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003476 * If the activity is currently paused, signal that we need to re-run the loader
3477 * in onResume.
3478 *
3479 * This needs to be called from incoming places where resources might have been loaded
3480 * while we are paused. That is becaues the Configuration might be wrong
3481 * when we're not running, and if it comes back to what it was when we
3482 * were paused, we are not restarted.
3483 *
3484 * Implementation of the method from LauncherModel.Callbacks.
3485 *
3486 * @return true if we are currently paused. The caller might be able to
3487 * skip some work in that case since we will come back again.
3488 */
3489 public boolean setLoadOnResume() {
3490 if (mPaused) {
3491 Log.i(TAG, "setLoadOnResume");
3492 mOnResumeNeedsLoad = true;
3493 return true;
3494 } else {
3495 return false;
3496 }
3497 }
3498
3499 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003501 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003503 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003504 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003505 } else {
3506 return SCREEN_COUNT / 2;
3507 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003508 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003509
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 /**
3511 * Refreshes the shortcuts shown on the workspace.
3512 *
3513 * Implementation of the method from LauncherModel.Callbacks.
3514 */
3515 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003516 // If we're starting binding all over again, clear any bind calls we'd postponed in
3517 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3518 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003519 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003520
Winson Chungd64d1762013-08-20 14:37:16 -07003521 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003522 mWorkspace.clearDropTargets();
Winson Chungd64d1762013-08-20 14:37:16 -07003523 mWorkspace.removeAllViews();
3524
Michael Jurka05bf644e2011-11-30 20:28:53 -08003525 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003526 if (mHotseat != null) {
3527 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003528 }
3529 }
3530
Adam Cohendcd297f2013-06-18 13:13:40 -07003531 @Override
3532 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003533 bindAddScreens(orderedScreenIds);
3534 mWorkspace.addExtraEmptyScreen();
3535 }
3536
3537 @Override
3538 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003539 int count = orderedScreenIds.size();
3540 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003541 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Adam Cohen89bddfa2013-08-20 11:57:13 -07003542 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003543 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003544 }
3545
Adam Cohen39a06042013-07-19 14:30:12 -07003546 private boolean shouldShowWeightWatcher() {
3547 String spKey = LauncherAppState.getSharedPreferencesKey();
3548 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003549 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003550
3551 return show;
3552 }
3553
3554 private void toggleShowWeightWatcher() {
3555 String spKey = LauncherAppState.getSharedPreferencesKey();
3556 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3557 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3558
3559 show = !show;
3560
3561 SharedPreferences.Editor editor = sp.edit();
3562 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3563 editor.commit();
3564
3565 if (mWeightWatcher != null) {
3566 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3567 }
3568 }
3569
Winson Chungd64d1762013-08-20 14:37:16 -07003570 public void bindAppsAdded(final ArrayList<Long> newScreens,
3571 final ArrayList<ItemInfo> addNotAnimated,
3572 final ArrayList<ItemInfo> addAnimated) {
3573 Runnable r = new Runnable() {
3574 public void run() {
3575 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3576 }
3577 };
3578 if (waitUntilResume(r)) {
3579 return;
3580 }
3581
3582 Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
3583
3584 // Add the new screens
3585 bindAddScreens(newScreens);
3586
3587 // We add the items without animation on non-visible pages, and with
3588 // animations on the new page (which we will try and snap to).
3589 if (!addNotAnimated.isEmpty()) {
3590 bindItems(addNotAnimated, 0,
3591 addNotAnimated.size(), false);
3592 }
3593 if (!addAnimated.isEmpty()) {
3594 bindItems(addAnimated, 0,
3595 addAnimated.size(), true);
3596 }
3597 }
3598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003599 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003600 * Bind the items start-end from the list.
3601 *
3602 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003603 */
Winson Chung64359a52013-07-08 17:17:08 -07003604 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3605 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003606 Runnable r = new Runnable() {
3607 public void run() {
3608 bindItems(shortcuts, start, end, forceAnimateIcons);
3609 }
3610 };
3611 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003612 return;
3613 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003614
Winson Chungf0c6ae02012-03-21 16:10:31 -07003615 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003616 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3617 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003618 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003619 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003620 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003621 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003623
3624 // Short circuit if we are loading dock items for a configuration which has no dock
3625 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3626 mHotseat == null) {
3627 continue;
3628 }
3629
Joe Onorato9c1289c2009-08-17 11:03:03 -04003630 switch (item.itemType) {
3631 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3632 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003633 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003634 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003635
Winson Chung64359a52013-07-08 17:17:08 -07003636 /*
3637 * TODO: FIX collision case
3638 */
Winson Chungce376632013-07-11 16:12:41 -07003639 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3640 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3641 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3642 throw new RuntimeException("OCCUPIED");
3643 }
Winson Chung64359a52013-07-08 17:17:08 -07003644 }
3645
Adam Cohendcd297f2013-06-18 13:13:40 -07003646 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3647 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003648 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003649 // Animate all the applications up now
3650 shortcut.setAlpha(0f);
3651 shortcut.setScaleX(0f);
3652 shortcut.setScaleY(0f);
3653 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003654 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003655 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003657 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003658 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003659 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003660 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003661 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3662 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003663 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003664 default:
3665 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003666 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003667 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003668
Winson Chung997a9232013-07-24 15:33:46 -07003669 if (animateIcons) {
3670 // Animate to the correct page
3671 if (newShortcutsScreenId > -1) {
3672 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3673 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3674 if (newShortcutsScreenId != currentScreenId) {
3675 mWorkspace.snapToPage(newScreenIndex);
3676 }
3677 }
3678
Winson Chung64359a52013-07-08 17:17:08 -07003679 // We post the animation slightly delayed to prevent slowdowns when we are loading
3680 // right after we return to launcher.
3681 mWorkspace.postDelayed(new Runnable() {
3682 public void run() {
3683 anim.playTogether(bounceAnims);
3684 anim.start();
3685 }
3686 }, NEW_APPS_ANIMATION_DELAY);
3687 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003688 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003689 }
3690
Joe Onorato9c1289c2009-08-17 11:03:03 -04003691 /**
3692 * Implementation of the method from LauncherModel.Callbacks.
3693 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003694 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003695 Runnable r = new Runnable() {
3696 public void run() {
3697 bindFolders(folders);
3698 }
3699 };
3700 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003701 return;
3702 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003703 sFolders.clear();
3704 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003705 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003706
3707 /**
3708 * Add the views for a widget to the workspace.
3709 *
3710 * Implementation of the method from LauncherModel.Callbacks.
3711 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003712 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003713 Runnable r = new Runnable() {
3714 public void run() {
3715 bindAppWidget(item);
3716 }
3717 };
3718 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003719 return;
3720 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003721
Daniel Sandler843e8602010-06-07 14:59:01 -04003722 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3723 if (DEBUG_WIDGETS) {
3724 Log.d(TAG, "bindAppWidget: " + item);
3725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003726 final Workspace workspace = mWorkspace;
3727
3728 final int appWidgetId = item.appWidgetId;
3729 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003730 if (DEBUG_WIDGETS) {
3731 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3732 }
3733
Joe Onorato9c1289c2009-08-17 11:03:03 -04003734 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3735
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003737 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003738
Adam Cohendcd297f2013-06-18 13:13:40 -07003739 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003740 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003741 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3742
Joe Onorato9c1289c2009-08-17 11:03:03 -04003743 workspace.requestLayout();
3744
Daniel Sandler843e8602010-06-07 14:59:01 -04003745 if (DEBUG_WIDGETS) {
3746 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3747 + (SystemClock.uptimeMillis()-start) + "ms");
3748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003749 }
3750
Adam Cohen1462de32012-07-24 22:34:36 -07003751 public void onPageBoundSynchronously(int page) {
3752 mSynchronouslyBoundPages.add(page);
3753 }
3754
Joe Onorato9c1289c2009-08-17 11:03:03 -04003755 /**
3756 * Callback saying that there aren't any more items to bind.
3757 *
3758 * Implementation of the method from LauncherModel.Callbacks.
3759 */
Adam Cohene25af792013-06-06 23:08:25 -07003760 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003761 Runnable r = new Runnable() {
3762 public void run() {
3763 finishBindingItems(upgradePath);
3764 }
3765 };
3766 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003767 return;
3768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003769 if (mSavedState != null) {
3770 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003771 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003772 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003773 mSavedState = null;
3774 }
3775
Winson Chung98ca0f72013-07-29 12:58:51 -07003776 // Create the custom content page here before onLayout to prevent flashing
3777 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3778 mWorkspace.createCustomContentPage();
3779 }
3780
Adam Cohen1462de32012-07-24 22:34:36 -07003781 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003783 // If we received the result of any pending adds while the loader was running (e.g. the
3784 // widget configuration forced an orientation change), process them now.
3785 for (int i = 0; i < sPendingAddList.size(); i++) {
3786 completeAdd(sPendingAddList.get(i));
3787 }
3788 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003789
Winson Chungc51db6a2011-10-05 11:44:49 -07003790 // Update the market app icon as necessary (the other icons will be managed in response to
3791 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003792 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003793
Winson Chungf0c6ae02012-03-21 16:10:31 -07003794 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003795 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003796 mWorkspace.stripDuplicateApps();
3797 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003798 }
Adam Cohen99894d92013-06-14 11:22:59 -07003799
Adam Cohen66a01fd2013-06-11 12:48:00 -07003800 mWorkspace.post(new Runnable() {
3801 @Override
3802 public void run() {
3803 onFinishBindingItems();
3804 }
3805 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003806 }
3807
Winson Chunga2413752012-04-03 14:22:34 -07003808 private boolean canRunNewAppsAnimation() {
3809 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3810 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3811 }
3812
Winson Chungc9168342013-06-26 14:54:55 -07003813 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3814 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3815 PropertyValuesHolder.ofFloat("alpha", 1f),
3816 PropertyValuesHolder.ofFloat("scaleX", 1f),
3817 PropertyValuesHolder.ofFloat("scaleY", 1f));
3818 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3819 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3820 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3821 return bounceAnim;
3822 }
3823
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003824 @Override
3825 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003826 boolean searchVisible = updateGlobalSearchIcon();
3827 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003828 if (mSearchDropTargetBar != null) {
3829 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3830 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003831 }
3832
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003833 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003834 * Add the icons for all apps.
3835 *
3836 * Implementation of the method from LauncherModel.Callbacks.
3837 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003838 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003839 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3840 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003841 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003842 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003844 }
3845
3846 /**
3847 * A package was updated.
3848 *
3849 * Implementation of the method from LauncherModel.Callbacks.
3850 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003851 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003852 Runnable r = new Runnable() {
3853 public void run() {
3854 bindAppsUpdated(apps);
3855 }
3856 };
3857 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003858 return;
3859 }
3860
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003861 if (mWorkspace != null) {
3862 mWorkspace.updateShortcuts(apps);
3863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003864 }
3865
3866 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003867 * A package was uninstalled. We take both the super set of packageNames
3868 * in addition to specific applications to remove, the reason being that
3869 * this can be called when a package is updated as well. In that scenario,
3870 * we only remove specific components from the workspace, where as
3871 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003872 *
3873 * Implementation of the method from LauncherModel.Callbacks.
3874 */
Winson Chung83892cc2013-05-01 16:53:33 -07003875 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3876 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003877 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003878 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003879 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003880 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003881 }
Winson Chungd64d1762013-08-20 14:37:16 -07003882 };
3883 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003884 return;
3885 }
3886
Winson Chung64359a52013-07-08 17:17:08 -07003887 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003888 mWorkspace.removeItemsByPackageName(packageNames);
3889 } else {
3890 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003891 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003892
Winson Chunga1820962011-10-03 16:31:06 -07003893 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003894 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003895 }
Joe Onoratobe386092009-11-17 17:32:16 -08003896
3897 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003898 * A number of packages were updated.
3899 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003900 private ArrayList<Object> mWidgetsAndShortcuts;
3901 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003902 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003903 bindPackagesUpdated(mWidgetsAndShortcuts);
3904 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003905 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003906 };
3907
3908 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3909 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3910 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003911 return;
3912 }
3913
Winson Chung64359a52013-07-08 17:17:08 -07003914 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003915 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003916 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003917 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003918 }
3919
Winson Chung400438b2011-01-16 17:53:48 -08003920 private int mapConfigurationOriActivityInfoOri(int configOri) {
3921 final Display d = getWindowManager().getDefaultDisplay();
3922 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3923 switch (d.getRotation()) {
3924 case Surface.ROTATION_0:
3925 case Surface.ROTATION_180:
3926 // We are currently in the same basic orientation as the natural orientation
3927 naturalOri = configOri;
3928 break;
3929 case Surface.ROTATION_90:
3930 case Surface.ROTATION_270:
3931 // We are currently in the other basic orientation to the natural orientation
3932 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3933 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3934 break;
3935 }
3936
3937 int[] oriMap = {
3938 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3939 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3940 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3941 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3942 };
3943 // Since the map starts at portrait, we need to offset if this device's natural orientation
3944 // is landscape.
3945 int indexOffset = 0;
3946 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3947 indexOffset = 1;
3948 }
3949 return oriMap[(d.getRotation() + indexOffset) % 4];
3950 }
Adam Cohen446e9402011-09-15 18:21:21 -07003951
Winson Chung4b919f82012-05-01 10:44:08 -07003952 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003953 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003954 getResources().getBoolean(R.bool.allow_rotation);
3955 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003956 }
Winson Chung4b919f82012-05-01 10:44:08 -07003957 public void lockScreenOrientation() {
3958 if (isRotationEnabled()) {
3959 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3960 .getConfiguration().orientation));
3961 }
3962 }
3963 public void unlockScreenOrientation(boolean immediate) {
3964 if (isRotationEnabled()) {
3965 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003966 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003967 } else {
3968 mHandler.postDelayed(new Runnable() {
3969 public void run() {
3970 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3971 }
3972 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003973 }
Winson Chung4b919f82012-05-01 10:44:08 -07003974 }
Winson Chung400438b2011-01-16 17:53:48 -08003975 }
3976
Winson Chung82f55532011-08-09 14:14:23 -07003977 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003978 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003979 if (DISABLE_CLINGS) {
3980 return false;
3981 }
3982
Brett Chabot2a9e2282011-08-23 15:03:13 -07003983 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003984 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003985
Michael Jurkae233a8b2013-03-19 13:49:20 +01003986 // Restricted secondary users (child mode) will potentially have very few apps
3987 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003988// boolean supportsLimitedUsers =
3989// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3990// Account[] accounts = AccountManager.get(this).getAccounts();
3991// if (supportsLimitedUsers && accounts.length == 0) {
3992// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3993// Bundle restrictions = um.getUserRestrictions();
3994// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3995// return false;
3996// }
3997// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003998 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003999 }
Michael Jurka22143132012-10-04 17:42:38 +02004000
Winson Chung7d7541e2011-09-16 20:14:36 -07004001 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02004002 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07004003 if (cling != null) {
4004 cling.init(this, positionData);
4005 cling.setVisibility(View.VISIBLE);
4006 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4007 if (animate) {
4008 cling.buildLayer();
4009 cling.setAlpha(0f);
4010 cling.animate()
4011 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004012 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004013 .setDuration(SHOW_CLING_DURATION)
4014 .setStartDelay(delay)
4015 .start();
4016 } else {
4017 cling.setAlpha(1f);
4018 }
Michael Jurka22143132012-10-04 17:42:38 +02004019 cling.setFocusableInTouchMode(true);
4020 cling.post(new Runnable() {
4021 public void run() {
4022 cling.setFocusable(true);
4023 cling.requestFocus();
4024 }
4025 });
4026 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4027 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004028 }
4029 return cling;
4030 }
Michael Jurka22143132012-10-04 17:42:38 +02004031
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004033 // To catch cases where siblings of top-level views are made invisible, just check whether
4034 // the cling is directly set to GONE before dismissing it.
4035 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004036 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004037 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004038 anim.addListener(new AnimatorListenerAdapter() {
4039 public void onAnimationEnd(Animator animation) {
4040 cling.setVisibility(View.GONE);
4041 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004042 // We should update the shared preferences on a background thread
4043 new Thread("dismissClingThread") {
4044 public void run() {
4045 SharedPreferences.Editor editor = mSharedPrefs.edit();
4046 editor.putBoolean(flag, true);
4047 editor.commit();
4048 }
4049 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004050 };
4051 });
4052 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004053 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004054 }
4055 }
Michael Jurka22143132012-10-04 17:42:38 +02004056
Winson Chung9d9d74f2011-09-19 11:49:12 -07004057 private void removeCling(int id) {
4058 final View cling = findViewById(id);
4059 if (cling != null) {
4060 final ViewGroup parent = (ViewGroup) cling.getParent();
4061 parent.post(new Runnable() {
4062 @Override
4063 public void run() {
4064 parent.removeView(cling);
4065 }
4066 });
Michael Jurka22143132012-10-04 17:42:38 +02004067 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004068 }
4069 }
Michael Jurka974c3862012-05-22 22:00:31 -07004070
4071 private boolean skipCustomClingIfNoAccounts() {
4072 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4073 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4074 if (customCling) {
4075 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004076 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004077 Account[] accounts = am.getAccountsByType("com.google");
4078 return accounts.length == 0;
4079 }
4080 return false;
4081 }
4082
Winson Chung7d7541e2011-09-16 20:14:36 -07004083 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004084 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004085 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004086 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4087 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004088 // If we're not using the default workspace layout, replace workspace cling
4089 // with a custom workspace cling (usually specified in an overlay)
4090 // For now, only do this on tablets
4091 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004092 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004093 // Use a custom cling
4094 View cling = findViewById(R.id.workspace_cling);
4095 ViewGroup clingParent = (ViewGroup) cling.getParent();
4096 int clingIndex = clingParent.indexOfChild(cling);
4097 clingParent.removeViewAt(clingIndex);
4098 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4099 clingParent.addView(customCling, clingIndex);
4100 customCling.setId(R.id.workspace_cling);
4101 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004102 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004103 } else {
4104 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004105 }
4106 }
4107 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004108 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004109 if (isClingsEnabled() &&
4110 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004111 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004112 } else {
4113 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004114 }
4115 }
4116 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004117 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004118 if (isClingsEnabled() &&
4119 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4120 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004121 } else {
4122 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004123 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004125 }
4126 public boolean isFolderClingVisible() {
4127 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004128 if (cling != null) {
4129 return cling.getVisibility() == View.VISIBLE;
4130 }
4131 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004132 }
Winson Chung82f55532011-08-09 14:14:23 -07004133 public void dismissWorkspaceCling(View v) {
4134 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004135 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004136 }
4137 public void dismissAllAppsCling(View v) {
4138 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004139 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4140 }
4141 public void dismissFolderCling(View v) {
4142 Cling cling = (Cling) findViewById(R.id.folder_cling);
4143 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004144 }
4145
Winson Chung80baf5a2010-08-09 16:03:15 -07004146 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004147 * Prints out out state for debugging.
4148 */
4149 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004150 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004151 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004152 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4153 Log.d(TAG, "mRestoring=" + mRestoring);
4154 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4155 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004156 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004157 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004158
Winson Chung785d2eb2011-04-14 16:08:02 -07004159 if (mAppsCustomizeContent != null) {
4160 mAppsCustomizeContent.dumpState();
4161 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004162 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004163 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004164
4165 @Override
4166 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4167 super.dump(prefix, fd, writer, args);
4168 writer.println(" ");
4169 writer.println("Debug logs: ");
4170 for (int i = 0; i < sDumpLogs.size(); i++) {
4171 writer.println(" " + sDumpLogs.get(i));
4172 }
4173 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004174
4175 public static void dumpDebugLogsToConsole() {
4176 Log.d(TAG, "");
4177 Log.d(TAG, "*********************");
4178 Log.d(TAG, "Launcher debug logs: ");
4179 for (int i = 0; i < sDumpLogs.size(); i++) {
4180 Log.d(TAG, " " + sDumpLogs.get(i));
4181 }
4182 Log.d(TAG, "*********************");
4183 Log.d(TAG, "");
4184 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004185}
Michael Jurka2763be32011-02-24 11:19:57 -08004186
Michael Jurkaabded662011-03-04 12:06:57 -08004187interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004188 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004189 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004190 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004191 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004192 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004193}