blob: efdc6b01ed0fdc0ee59233e4bceb8cf5ccfcb6eb [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
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040052import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020055import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070057import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040058import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080059import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070060import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080061import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.text.Selection;
63import android.text.SpannableStringBuilder;
64import android.text.TextUtils;
65import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080067import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080068import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.KeyEvent;
70import android.view.LayoutInflater;
71import android.view.Menu;
72import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070073import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080074import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070076import android.view.ViewGroup;
Adam Cohen860f4c52011-01-27 20:16:13 -080077import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080078import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080079import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070081import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070083import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070084import android.widget.TextView;
85import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070086
Adam Cohendf2cc412011-04-27 16:56:57 -070087import com.android.common.Search;
88import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -080089
Adam Cohenc0dcf592011-06-01 15:30:43 -070090import java.io.DataInputStream;
91import java.io.DataOutputStream;
92import java.io.FileNotFoundException;
93import java.io.IOException;
94import java.util.ArrayList;
95import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Default launcher application.
99 */
Michael Jurka946ad472010-07-09 18:05:18 -0700100public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700101 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
102 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800103 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700104 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400107 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800110 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700113 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
114 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
116 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700117 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
119 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700124 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
127
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800128 static final int SCREEN_COUNT = 5;
129 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Joe Onorato7c312c12009-08-13 21:36:53 -0700131 static final int DIALOG_CREATE_SHORTCUT = 1;
132 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Romain Guy98d01652009-06-30 16:21:04 -0700134 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 // Type: int
137 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700138 // Type: int
139 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700141 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
142 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
144 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700153 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
154
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700155 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700156 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700157 private State mState = State.WORKSPACE;
158 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700161 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
162 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800167 private final BroadcastReceiver mCloseSystemDialogsReceiver
168 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800169 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private LayoutInflater mInflater;
172
Joe Onorato00acb122009-08-04 16:04:30 -0400173 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private AppWidgetManager mAppWidgetManager;
177 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700178
Winson Chung3d503fb2011-07-13 17:25:49 -0700179 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700180 private int[] mTmpAddItemCellCoordinates = new int[2];
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private FolderInfo mFolderInfo;
183
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800185 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700186
Winson Chungf0ea4d32011-06-06 14:27:16 -0700187 private SearchDropTargetBar mSearchDeleteBar;
188 private AppsCustomizeTabHost mAppsCustomizeTabHost;
189 private AppsCustomizePagedView mAppsCustomizeContent;
190 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private Bundle mSavedState;
193
194 private SpannableStringBuilder mDefaultKeySsb = null;
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 private boolean mWorkspaceLoading = true;
197
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800198 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private boolean mRestoring;
200 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700201 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
203 private Bundle mSavedInstanceState;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800207 private boolean mUserPresent = true;
208 private boolean mVisible = false;
209 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700211 private static LocaleConfiguration sLocaleConfiguration = null;
212
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700213 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400215 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400216 private String[] mHotseatConfig = null;
217 private Intent[] mHotseats = null;
218 private Drawable[] mHotseatIcons = null;
219 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400220
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700221 private Intent mAppMarketIntent = null;
222
Adam Cohended9f8d2010-11-03 13:25:16 -0700223 // Related to the auto-advancing of widgets
224 private final int ADVANCE_MSG = 1;
225 private final int mAdvanceInterval = 20000;
226 private final int mAdvanceStagger = 250;
227 private long mAutoAdvanceSentTime;
228 private long mAutoAdvanceTimeLeft = -1;
229 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
230 new HashMap<View, AppWidgetProviderInfo>();
231
Winson Chung400438b2011-01-16 17:53:48 -0800232 // Determines how long to wait after a rotation before restoring the screen orientation to
233 // match the sensor state.
234 private final int mRestoreScreenOrientationDelay = 500;
235
Michael Jurka4ef207b2010-11-29 17:05:45 -0800236 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800237 private static Drawable.ConstantState sGlobalSearchIcon;
238 private static Drawable.ConstantState sVoiceSearchIcon;
239 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800240
Adam Cohen2801caf2011-05-13 20:57:39 -0700241 private DragLayer mDragLayer;
242
Michael Jurkaddd62e92011-02-16 17:49:14 -0800243 private BubbleTextView mWaitingForResume;
244
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800245 private static ArrayList<PendingAddArguments> sPendingAddList
246 = new ArrayList<PendingAddArguments>();
247
248 private static class PendingAddArguments {
249 int requestCode;
250 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700251 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800252 int screen;
253 int cellX;
254 int cellY;
255 }
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 @Override
258 protected void onCreate(Bundle savedInstanceState) {
259 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800260 LauncherApplication app = ((LauncherApplication)getApplication());
261 mModel = app.setLauncher(this);
262 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400263 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700266 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700267 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
268 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700269
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200271 android.os.Debug.startMethodTracing(
272 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 }
274
275 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 setContentView(R.layout.launcher);
277 setupViews();
278
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800279 registerContentObservers();
280
Joe Onorato7c312c12009-08-13 21:36:53 -0700281 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 mSavedState = savedInstanceState;
284 restoreState(mSavedState);
285
Winson Chunga12a2502010-12-20 14:41:35 -0800286 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700287 if (mAppsCustomizeContent != null) {
288 mAppsCustomizeContent.onPackagesUpdated();
289 }
Winson Chunga12a2502010-12-20 14:41:35 -0800290
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 if (PROFILE_STARTUP) {
292 android.os.Debug.stopMethodTracing();
293 }
294
295 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 }
298
299 // For handling default keys
300 mDefaultKeySsb = new SpannableStringBuilder();
301 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800302
303 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
304 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800305
306 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700307 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
308 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100309 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700310 }
311 if (sGlobalSearchIcon != null) {
312 updateGlobalSearchIcon(sGlobalSearchIcon);
313 }
314 if (sVoiceSearchIcon != null) {
315 updateVoiceSearchIcon(sVoiceSearchIcon);
316 }
317 if (sAppMarketIcon != null) {
318 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
Romain Guycbb89e42009-06-08 15:52:54 -0700321
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700323 if (sLocaleConfiguration == null) {
324 new AsyncTask<Void, Void, LocaleConfiguration>() {
325 @Override
326 protected LocaleConfiguration doInBackground(Void... unused) {
327 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
328 readConfiguration(Launcher.this, localeConfiguration);
329 return localeConfiguration;
330 }
331
332 @Override
333 protected void onPostExecute(LocaleConfiguration result) {
334 sLocaleConfiguration = result;
335 checkForLocaleChange(); // recursive, but now with a locale configuration
336 }
337 }.execute();
338 return;
339 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 final Configuration configuration = getResources().getConfiguration();
342
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700343 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 final String locale = configuration.locale.toString();
345
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700346 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 final int mcc = configuration.mcc;
348
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700349 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 final int mnc = configuration.mnc;
351
Romain Guy84f296c2009-11-04 15:00:44 -0800352 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353
Romain Guy84f296c2009-11-04 15:00:44 -0800354 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 sLocaleConfiguration.locale = locale;
356 sLocaleConfiguration.mcc = mcc;
357 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700358
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360
361 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
362 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700363 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 public void run() {
365 writeConfiguration(Launcher.this, localeConfiguration);
366 }
367 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700368 }
369 }
370
371 private static class LocaleConfiguration {
372 public String locale;
373 public int mcc = -1;
374 public int mnc = -1;
375 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700376
Romain Guy98d01652009-06-30 16:21:04 -0700377 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
378 DataInputStream in = null;
379 try {
380 in = new DataInputStream(context.openFileInput(PREFERENCES));
381 configuration.locale = in.readUTF();
382 configuration.mcc = in.readInt();
383 configuration.mnc = in.readInt();
384 } catch (FileNotFoundException e) {
385 // Ignore
386 } catch (IOException e) {
387 // Ignore
388 } finally {
389 if (in != null) {
390 try {
391 in.close();
392 } catch (IOException e) {
393 // Ignore
394 }
395 }
396 }
397 }
398
399 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
400 DataOutputStream out = null;
401 try {
402 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
403 out.writeUTF(configuration.locale);
404 out.writeInt(configuration.mcc);
405 out.writeInt(configuration.mnc);
406 out.flush();
407 } catch (FileNotFoundException e) {
408 // Ignore
409 } catch (IOException e) {
410 //noinspection ResultOfMethodCallIgnored
411 context.getFileStreamPath(PREFERENCES).delete();
412 } finally {
413 if (out != null) {
414 try {
415 out.close();
416 } catch (IOException e) {
417 // Ignore
418 }
419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421 }
422
Adam Cohen716b51e2011-06-30 12:09:54 -0700423 public DragLayer getDragLayer() {
424 return mDragLayer;
425 }
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 static int getScreen() {
428 synchronized (sLock) {
429 return sScreen;
430 }
431 }
432
433 static void setScreen(int screen) {
434 synchronized (sLock) {
435 sScreen = screen;
436 }
437 }
438
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400439 // Note: This doesn't do all the client-id magic that BrowserProvider does
440 // in Browser. (http://b/2425179)
441 private Uri getDefaultBrowserUri() {
442 String url = getString(R.string.default_browser_url);
443 if (url.indexOf("{CID}") != -1) {
444 url = url.replace("{CID}", "android-google");
445 }
446 return Uri.parse(url);
447 }
448
Winson Chung557d6ed2011-07-08 15:34:52 -0700449 /**
450 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
451 * a configuration step, this allows the proper animations to run after other transitions.
452 */
453 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800454 switch (args.requestCode) {
455 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700456 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
457 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800458 break;
459 case REQUEST_PICK_SHORTCUT:
460 processShortcut(args.intent);
461 break;
462 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700463 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
464 args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700465 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800466 case REQUEST_PICK_APPWIDGET:
467 addAppWidgetFromPick(args.intent);
468 break;
469 case REQUEST_CREATE_APPWIDGET:
470 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700471 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700472 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800473 case REQUEST_PICK_WALLPAPER:
474 // We just wanted the activity result here so we can clear mWaitingForResult
475 break;
476 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700477 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800481 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700482 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700483 mWaitingForResult = false;
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 // The pattern used here is that a user PICKs a specific application,
486 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
489 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700490 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
491 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800492 final PendingAddArguments args = new PendingAddArguments();
493 args.requestCode = requestCode;
494 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700495 args.container = mPendingAddInfo.container;
496 args.screen = mPendingAddInfo.screen;
497 args.cellX = mPendingAddInfo.cellX;
498 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800499
500 // If the loader is still running, defer the add until it is done.
501 if (isWorkspaceLocked()) {
502 sPendingAddList.add(args);
503 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700504 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
Romain Guy18042c82009-11-06 11:44:55 -0800506 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700507 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
508 if (data != null) {
509 // Clean up the appWidgetId if we canceled
510 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
511 if (appWidgetId != -1) {
512 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 }
515 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700516
517 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
518 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 }
520
521 @Override
522 protected void onResume() {
523 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800524 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700525 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400526 mWorkspaceLoading = true;
527 mModel.startLoader(this, true);
528 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700529 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800531 if (mWaitingForResume != null) {
532 mWaitingForResume.setStayPressed(false);
533 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700534 // When we resume Launcher, a different Activity might be responsible for the app
535 // market intent, so refresh the icon
536 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800537 }
538
539 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 protected void onPause() {
541 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700542 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800543 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 }
Romain Guycbb89e42009-06-08 15:52:54 -0700545
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700546 @Override
547 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 // Flag the loader to stop early before switching
549 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700550 if (mAppsCustomizeContent != null) {
551 mAppsCustomizeContent.surrender();
552 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800553 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700554 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700555
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800556 // We can't hide the IME if it was forced open. So don't bother
557 /*
558 @Override
559 public void onWindowFocusChanged(boolean hasFocus) {
560 super.onWindowFocusChanged(hasFocus);
561
562 if (hasFocus) {
563 final InputMethodManager inputManager = (InputMethodManager)
564 getSystemService(Context.INPUT_METHOD_SERVICE);
565 WindowManager.LayoutParams lp = getWindow().getAttributes();
566 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
567 android.os.Handler()) {
568 protected void onReceiveResult(int resultCode, Bundle resultData) {
569 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
570 }
571 });
572 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
573 }
574 }
575 */
576
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 private boolean acceptFilter() {
578 final InputMethodManager inputManager = (InputMethodManager)
579 getSystemService(Context.INPUT_METHOD_SERVICE);
580 return !inputManager.isFullscreenMode();
581 }
582
583 @Override
584 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700585 final int uniChar = event.getUnicodeChar();
586 final boolean handled = super.onKeyDown(keyCode, event);
587 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
588 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
590 keyCode, event);
591 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700592 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700593 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700594 // showSearchDialog()
595 // If there are multiple keystrokes before the search dialog takes focus,
596 // onSearchRequested() will be called for every keystroke,
597 // but it is idempotent, so it's fine.
598 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 }
600 }
601
Joe Onorato8a9625e2010-01-28 15:55:35 -0800602 // Eat the long press event so the keyboard doesn't come up.
603 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
604 return true;
605 }
606
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 return handled;
608 }
609
Karl Rosaen138a0412009-04-23 19:00:21 -0700610 private String getTypedText() {
611 return mDefaultKeySsb.toString();
612 }
613
614 private void clearTypedText() {
615 mDefaultKeySsb.clear();
616 mDefaultKeySsb.clearSpans();
617 Selection.setSelection(mDefaultKeySsb, 0);
618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700621 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
622 * State
623 */
624 private static State intToState(int stateOrdinal) {
625 State state = State.WORKSPACE;
626 final State[] stateValues = State.values();
627 for (int i = 0; i < stateValues.length; i++) {
628 if (stateValues[i].ordinal() == stateOrdinal) {
629 state = stateValues[i];
630 break;
631 }
632 }
633 return state;
634 }
635
636 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 * Restores the previous state, if it exists.
638 *
639 * @param savedState The previous state.
640 */
641 private void restoreState(Bundle savedState) {
642 if (savedState == null) {
643 return;
644 }
645
Michael Jurka883f55b2010-10-21 15:47:14 -0700646 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
647
Winson Chungf0ea4d32011-06-06 14:27:16 -0700648 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800649 showAllApps(false);
650 }
651
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
653 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700654 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
656
Winson Chung3d503fb2011-07-13 17:25:49 -0700657 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
658 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659
Winson Chung3d503fb2011-07-13 17:25:49 -0700660
661 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
662 mPendingAddInfo.container = pendingAddContainer;
663 mPendingAddInfo.screen = pendingAddScreen;
664 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
665 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 mRestoring = true;
667 }
668
669 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
670 if (renameFolder) {
671 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700672 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 mRestoring = true;
674 }
Winson Chunga12a2502010-12-20 14:41:35 -0800675
Winson Chung785d2eb2011-04-14 16:08:02 -0700676
677 // Restore the AppsCustomize tab
678 if (mAppsCustomizeTabHost != null) {
679 String curTab = savedState.getString("apps_customize_currentTab");
680 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700681 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700682 mAppsCustomizeContent.setContentType(
683 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
684 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
685 }
686
687 // Note: currently we do not restore the page for the AppsCustomize pane because the
688 // change in layout can drastically affect the saved page index
689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 }
691
692 /**
693 * Finds all the views we need and configure them properly.
694 */
695 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700696 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400697
Winson Chung4c98d922011-05-31 16:50:48 -0700698 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
699 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700
Winson Chung4c98d922011-05-31 16:50:48 -0700701 // Setup the drag layer
702 mDragLayer.setup(this, dragController);
703
Winson Chung3d503fb2011-07-13 17:25:49 -0700704 // Setup the hotseat
705 mHotseat = (Hotseat) findViewById(R.id.hotseat);
706 if (mHotseat != null) {
707 mHotseat.setup(this);
708 }
709
Winson Chung4c98d922011-05-31 16:50:48 -0700710 // Setup the workspace
711 mWorkspace.setHapticFeedbackEnabled(false);
712 mWorkspace.setOnLongClickListener(this);
713 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700714 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700715
Winson Chungf0ea4d32011-06-06 14:27:16 -0700716 // Get the search/delete bar
717 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700718
Winson Chungf0ea4d32011-06-06 14:27:16 -0700719 // Setup AppsCustomize
720 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
721 findViewById(R.id.apps_customize_pane);
722 mAppsCustomizeContent = (AppsCustomizePagedView)
723 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
724 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400725
Romain Guy1fbc1c82009-11-09 20:43:08 -0800726
Winson Chung4c98d922011-05-31 16:50:48 -0700727
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700729 dragController.setDragScoller(mWorkspace);
730 dragController.setScrollView(mDragLayer);
731 dragController.setMoveTarget(mWorkspace);
732 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700733 if (mSearchDeleteBar != null) {
734 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737
Romain Guy8a73c512009-11-09 19:19:59 -0800738
Winson Chungaafa03c2010-06-11 17:34:16 -0700739
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 /**
741 * Creates a view representing a shortcut.
742 *
743 * @param info The data structure describing the shortcut.
744 *
745 * @return A View inflated from R.layout.application.
746 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800747 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700749 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751
752 /**
753 * Creates a view representing a shortcut inflated from the specified resource.
754 *
755 * @param layoutResId The id of the XML layout used to create the shortcut.
756 * @param parent The group the shortcut belongs to.
757 * @param info The data structure describing the shortcut.
758 *
759 * @return A View inflated from layoutResId.
760 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800761 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800762 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
763 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 return favorite;
766 }
767
768 /**
769 * Add an application shortcut to the workspace.
770 *
771 * @param data The intent describing the application.
772 * @param cellInfo The position on screen where to create the shortcut.
773 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700774 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700775 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700776 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777
Adam Cohenfbba09b2011-07-18 21:43:05 -0700778 // First we check if we already know the exact location where we want to add this item.
779 if (cellX >= 0 && cellY >= 0) {
780 cellXY[0] = cellX;
781 cellXY[1] = cellY;
782 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700783 showOutOfSpaceMessage();
784 return;
785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800787 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800788
Romain Guy73b979d2009-06-09 12:57:21 -0700789 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800790 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800792 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700793 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700794 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800795 } else {
796 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798 }
Romain Guycbb89e42009-06-08 15:52:54 -0700799
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 /**
801 * Add a shortcut to the workspace.
802 *
803 * @param data The intent describing the shortcut.
804 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700806 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
807 int cellY) {
808 int[] cellXY = mTmpAddItemCellCoordinates;
809 int[] touchXY = mPendingAddInfo.dropPos;
810 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700811
Michael Jurkad3ef3062010-11-23 16:23:58 -0800812 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700813
814 // First we check if we already know the exact location where we want to add this item.
815 if (cellX >= 0 && cellY >= 0) {
816 cellXY[0] = cellX;
817 cellXY[1] = cellY;
818 foundCellSpan = true;
819 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800820 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700821 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800822 foundCellSpan = (result != null);
823 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700824 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800825 }
826
827 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700828 showOutOfSpaceMessage();
829 return;
830 }
831
832 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700833 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834
835 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
838 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 }
840 }
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700845 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700846 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700850
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700851 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700853
854 // We want to account for the extra amount of padding that we are adding to the widget
855 // to ensure that it gets the full amount of space that it has requested
856 Resources r = getResources();
857 int requiredWidth = appWidgetInfo.minWidth +
858 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
859 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
860 int requiredHeight = appWidgetInfo.minHeight +
861 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
862 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
863 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700864
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700866 // We have saved the position to which the widget was dragged-- this really only matters
867 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 int[] cellXY = mTmpAddItemCellCoordinates;
869 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700870 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
872 cellXY[0] = mPendingAddInfo.cellX;
873 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700874 foundCellSpan = true;
875 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800879 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700881 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700882 }
883
Michael Jurka0280c3b2010-09-17 15:00:07 -0700884 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800885 if (appWidgetId != -1) {
886 // Deleting an app widget ID is a void call but writes to disk before returning
887 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800888 new Thread("deleteAppWidgetId") {
889 public void run() {
890 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
891 }
892 }.start();
893 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700894 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800895 return;
896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700898 // Build Launcher-specific widget info and save to database
899 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700900 launcherInfo.spanX = spanXY[0];
901 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700904 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905
906 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700908 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
Winson Chung3d503fb2011-07-13 17:25:49 -0700913 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700915
916 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918 }
Romain Guycbb89e42009-06-08 15:52:54 -0700919
Adam Cohended9f8d2010-11-03 13:25:16 -0700920 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
921 @Override
922 public void onReceive(Context context, Intent intent) {
923 final String action = intent.getAction();
924 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
925 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700926 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700927 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700928
929 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -0700930 if (mAppsCustomizeContent != null) {
931 mAppsCustomizeContent.reset();
932 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700933 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
934 mUserPresent = true;
935 updateRunning();
936 }
937 }
938 };
939
940 @Override
941 public void onAttachedToWindow() {
942 super.onAttachedToWindow();
943
944 // Listen for broadcasts related to user-presence
945 final IntentFilter filter = new IntentFilter();
946 filter.addAction(Intent.ACTION_SCREEN_OFF);
947 filter.addAction(Intent.ACTION_USER_PRESENT);
948 registerReceiver(mReceiver, filter);
949
Adam Cohend113e0c2010-11-11 10:48:05 -0800950 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700951 mVisible = true;
952 }
953
954 @Override
955 public void onDetachedFromWindow() {
956 super.onDetachedFromWindow();
957 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700958 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700959
Adam Cohend113e0c2010-11-11 10:48:05 -0800960 if (mAttached) {
961 unregisterReceiver(mReceiver);
962 mAttached = false;
963 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700964 updateRunning();
965 }
966
967 public void onWindowVisibilityChanged(int visibility) {
968 mVisible = visibility == View.VISIBLE;
969 updateRunning();
970 }
971
972 private void sendAdvanceMessage(long delay) {
973 mHandler.removeMessages(ADVANCE_MSG);
974 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
975 mHandler.sendMessageDelayed(msg, delay);
976 mAutoAdvanceSentTime = System.currentTimeMillis();
977 }
978
979 private void updateRunning() {
980 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
981 if (autoAdvanceRunning != mAutoAdvanceRunning) {
982 mAutoAdvanceRunning = autoAdvanceRunning;
983 if (autoAdvanceRunning) {
984 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
985 sendAdvanceMessage(delay);
986 } else {
987 if (!mWidgetsToAdvance.isEmpty()) {
988 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
989 (System.currentTimeMillis() - mAutoAdvanceSentTime));
990 }
991 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800992 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700993 }
994 }
995 }
996
997 private final Handler mHandler = new Handler() {
998 @Override
999 public void handleMessage(Message msg) {
1000 if (msg.what == ADVANCE_MSG) {
1001 int i = 0;
1002 for (View key: mWidgetsToAdvance.keySet()) {
1003 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1004 final int delay = mAdvanceStagger * i;
1005 if (v instanceof Advanceable) {
1006 postDelayed(new Runnable() {
1007 public void run() {
1008 ((Advanceable) v).advance();
1009 }
1010 }, delay);
1011 }
1012 i++;
1013 }
1014 sendAdvanceMessage(mAdvanceInterval);
1015 }
1016 }
1017 };
1018
1019 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001020 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001021 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1022 if (v instanceof Advanceable) {
1023 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001024 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001025 updateRunning();
1026 }
1027 }
1028
1029 void removeWidgetToAutoAdvance(View hostView) {
1030 if (mWidgetsToAdvance.containsKey(hostView)) {
1031 mWidgetsToAdvance.remove(hostView);
1032 updateRunning();
1033 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 }
1035
Joe Onorato9c1289c2009-08-17 11:03:03 -04001036 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001037 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001038 launcherInfo.hostView = null;
1039 }
1040
Adam Cohended9f8d2010-11-03 13:25:16 -07001041 void showOutOfSpaceMessage() {
1042 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1043 }
1044
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 public LauncherAppWidgetHost getAppWidgetHost() {
1046 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 }
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Winson Chunga9abd0e2010-10-27 17:18:37 -07001049 public LauncherModel getModel() {
1050 return mModel;
1051 }
1052
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001053 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001054 getWindow().closeAllPanels();
1055
1056 try {
1057 dismissDialog(DIALOG_CREATE_SHORTCUT);
1058 // Unlock the workspace if the dialog was showing
1059 } catch (Exception e) {
1060 // An exception is thrown if the dialog is not visible, which is fine
1061 }
1062
1063 try {
1064 dismissDialog(DIALOG_RENAME_FOLDER);
1065 // Unlock the workspace if the dialog was showing
1066 } catch (Exception e) {
1067 // An exception is thrown if the dialog is not visible, which is fine
1068 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001069
1070 // Whatever we were doing is hereby canceled.
1071 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001072 }
1073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 @Override
1075 protected void onNewIntent(Intent intent) {
1076 super.onNewIntent(intent);
1077
1078 // Close the menu
1079 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001080 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001081 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001082
Adam Cohen95bb8002011-07-03 23:40:28 -07001083 closeFolder();
1084
Joe Onorato14f122b2009-11-19 14:06:36 -08001085 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1086 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001087
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001088 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001089 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001090 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001091 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001092 }
Winson Chungde1af762011-07-21 16:44:07 -07001093 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001094 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001095
Joe Onorato3a8820b2009-11-10 15:06:42 -08001096 final View v = getWindow().peekDecorView();
1097 if (v != null && v.getWindowToken() != null) {
1098 InputMethodManager imm = (InputMethodManager)getSystemService(
1099 INPUT_METHOD_SERVICE);
1100 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001102
Michael Jurka35aa14d2011-07-07 17:01:08 -07001103 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001104 if (mAppsCustomizeContent != null) {
1105 mAppsCustomizeContent.reset();
1106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
1108 }
1109
1110 @Override
1111 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1112 // Do not call super here
1113 mSavedInstanceState = savedInstanceState;
1114 }
1115
1116 @Override
1117 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001118 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001119 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120
Michael Jurka883f55b2010-10-21 15:47:14 -07001121 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001122 // We close any open folder since it will not be re-opened, and we need to make sure
1123 // this state is reflected.
1124 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125
Winson Chung3d503fb2011-07-13 17:25:49 -07001126 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1127 mWaitingForResult) {
1128 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1129 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1130 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1131 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 }
1133
1134 if (mFolderInfo != null && mWaitingForResult) {
1135 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1136 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1137 }
Michael Jurka946ad472010-07-09 18:05:18 -07001138
Winson Chung785d2eb2011-04-14 16:08:02 -07001139 // Save the current AppsCustomize tab
1140 if (mAppsCustomizeTabHost != null) {
1141 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1142 if (currentTabTag != null) {
1143 outState.putString("apps_customize_currentTab", currentTabTag);
1144 }
1145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 }
1147
1148 @Override
1149 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Winson Chungcd2b0142011-06-08 16:02:26 -07001152 // Stop callbacks from LauncherModel
1153 LauncherApplication app = ((LauncherApplication) getApplication());
1154 mModel.stopLoader();
1155 app.setLauncher(null);
1156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001158 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001160 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001162 mAppWidgetHost = null;
1163
1164 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
1166 TextKeyListener.getInstance().release();
1167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168
Winson Chung3d503fb2011-07-13 17:25:49 -07001169 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001170
1171 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001172 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001173
1174 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1175 mWorkspace.removeAllViews();
1176 mWorkspace = null;
1177 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001178
1179 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181
Adam Cohena9cf38f2011-05-02 15:36:58 -07001182 public DragController getDragController() {
1183 return mDragController;
1184 }
1185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 @Override
1187 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001188 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 super.startActivityForResult(intent, requestCode);
1190 }
1191
1192 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001193 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001195
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001196 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001197
Karl Rosaen138a0412009-04-23 19:00:21 -07001198 if (initialQuery == null) {
1199 // Use any text typed in the launcher as the initial query
1200 initialQuery = getTypedText();
1201 clearTypedText();
1202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 if (appSearchData == null) {
1204 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001205 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 final SearchManager searchManager =
1209 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001211 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
1214 @Override
1215 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001216 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 return false;
1218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
1220 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1223 .setIcon(android.R.drawable.ic_menu_add)
1224 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001225 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1226 .setIcon(android.R.drawable.ic_menu_manage)
1227 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001228 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 .setIcon(android.R.drawable.ic_menu_gallery)
1230 .setAlphabeticShortcut('W');
1231 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1232 .setIcon(android.R.drawable.ic_search_category_default)
1233 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1234 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1235 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1236 .setAlphabeticShortcut('N');
1237
1238 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001239 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1240 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
1242 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1243 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1244 .setIntent(settings);
1245
1246 return true;
1247 }
1248
1249 @Override
1250 public boolean onPrepareOptionsMenu(Menu menu) {
1251 super.onPrepareOptionsMenu(menu);
1252
Winson Chung785d2eb2011-04-14 16:08:02 -07001253 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1254 // related code?
1255 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
1257 return true;
1258 }
1259
1260 @Override
1261 public boolean onOptionsItemSelected(MenuItem item) {
1262 switch (item.getItemId()) {
1263 case MENU_ADD:
1264 addItems();
1265 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001266 case MENU_MANAGE_APPS:
1267 manageApps();
1268 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 case MENU_WALLPAPER_SETTINGS:
1270 startWallpaper();
1271 return true;
1272 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001273 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 return true;
1275 case MENU_NOTIFICATIONS:
1276 showNotifications();
1277 return true;
1278 }
1279
1280 return super.onOptionsItemSelected(item);
1281 }
Romain Guycbb89e42009-06-08 15:52:54 -07001282
Karl Rosaen138a0412009-04-23 19:00:21 -07001283 /**
1284 * Indicates that we want global search for this activity by setting the globalSearch
1285 * argument for {@link #startSearch} to true.
1286 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001288 @Override
1289 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001290 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001291 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001292 }
1293
Joe Onorato9c1289c2009-08-17 11:03:03 -04001294 public boolean isWorkspaceLocked() {
1295 return mWorkspaceLoading || mWaitingForResult;
1296 }
1297
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001300 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302
Michael Jurka0280c3b2010-09-17 15:00:07 -07001303 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001304 mPendingAddInfo.container = ItemInfo.NO_ID;
1305 mPendingAddInfo.screen = -1;
1306 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1307 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1308 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001309 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001310
Winson Chung7ad01412010-09-27 11:33:03 -07001311 private void manageApps() {
1312 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1313 }
1314
Michael Jurkaaf442092010-06-10 17:01:57 -07001315 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001316 // TODO: catch bad widget exception when sent
1317 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001318 // TODO: Is this log message meaningful?
1319 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001320 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001321 }
1322
Winson Chung55cef262010-10-28 14:14:18 -07001323 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001324 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Bjorn Bringert7984c942009-12-09 15:38:25 +00001326 if (appWidget.configure != null) {
1327 // Launch over to configure widget, if needed
1328 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1329 intent.setComponent(appWidget.configure);
1330 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001331 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001332 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1333 intent.putExtra(
1334 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1335 info.mimeType);
1336
1337 final String mimeType = info.mimeType;
1338 final ClipData clipData = (ClipData) info.configurationData;
1339 final ClipDescription clipDesc = clipData.getDescription();
1340 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1341 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001342 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001343 final CharSequence stringData = item.getText();
1344 final Uri uriData = item.getUri();
1345 final Intent intentData = item.getIntent();
1346 final String key =
1347 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1348 if (uriData != null) {
1349 intent.putExtra(key, uriData);
1350 } else if (intentData != null) {
1351 intent.putExtra(key, intentData);
1352 } else if (stringData != null) {
1353 intent.putExtra(key, stringData);
1354 }
1355 break;
1356 }
1357 }
1358 }
Winson Chung55cef262010-10-28 14:14:18 -07001359 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001360
Romain Guy8e633c52010-03-04 12:51:36 -08001361 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001363 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001364 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001365
1366 // Exit spring loaded mode if necessary after adding the widget
1367 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369 }
Romain Guycbb89e42009-06-08 15:52:54 -07001370
Adam Cohenfbba09b2011-07-18 21:43:05 -07001371 /**
1372 * Process a shortcut drop.
1373 *
1374 * @param componentName The name of the component
1375 * @param screen The screen where it should be added
1376 * @param cell The cell it should be added to, optional
1377 * @param position The location on the screen where it was dropped, optional
1378 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1380 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001381 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001382 mPendingAddInfo.container = container;
1383 mPendingAddInfo.screen = screen;
1384 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001385
1386 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 mPendingAddInfo.cellX = cell[0];
1388 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001389 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001390
1391 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1392 createShortcutIntent.setComponent(componentName);
1393 processShortcut(createShortcutIntent);
1394 }
1395
Adam Cohenfbba09b2011-07-18 21:43:05 -07001396 /**
1397 * Process a widget drop.
1398 *
1399 * @param info The PendingAppWidgetInfo of the widget being added.
1400 * @param screen The screen where it should be added
1401 * @param cell The cell it should be added to, optional
1402 * @param position The location on the screen where it was dropped, optional
1403 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1405 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001407 mPendingAddInfo.container = info.container = container;
1408 mPendingAddInfo.screen = info.screen = screen;
1409 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001410 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001411 mPendingAddInfo.cellX = cell[0];
1412 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001413 }
1414
1415 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1416 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1417 addAppWidgetImpl(appWidgetId, info);
1418 }
1419
Joe Onoratodeb98af2010-02-19 14:59:39 -08001420 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001421 // Handle case where user selected "Applications"
1422 String applicationName = getResources().getString(R.string.group_applications);
1423 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001424
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001425 if (applicationName != null && applicationName.equals(shortcutName)) {
1426 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1427 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001429 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1430 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001431 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001432 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001433 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001434 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001435 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
1437
Winson Chung24ab2f12010-09-16 14:10:47 -07001438 void processWallpaper(Intent intent) {
1439 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1440 }
1441
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1443 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001444 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 folderInfo.title = getText(R.string.folder_name);
1446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1449 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001450 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
1452 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 FolderIcon newFolder =
1454 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1455 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1456 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001457 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001461 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 }
1463
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 private void showNotifications() {
1465 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1466 if (statusBar != null) {
1467 statusBar.expand();
1468 }
1469 }
1470
1471 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001472 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001474 Intent chooser = Intent.createChooser(pickWallpaper,
1475 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001476 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1477 // Removed in Eclair MR1
1478// WallpaperManager wm = (WallpaperManager)
1479// getSystemService(Context.WALLPAPER_SERVICE);
1480// WallpaperInfo wi = wm.getWallpaperInfo();
1481// if (wi != null && wi.getSettingsActivity() != null) {
1482// LabeledIntent li = new LabeledIntent(getPackageName(),
1483// R.string.configure_wallpaper, 0);
1484// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1485// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1486// }
Mike Clerona0618e42009-10-22 13:55:21 -07001487 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001490 /**
1491 * Registers various content observers. The current implementation registers
1492 * only a favorites observer to keep track of the favorites applications.
1493 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001494 private void registerContentObservers() {
1495 ContentResolver resolver = getContentResolver();
1496 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1497 true, mWidgetObserver);
1498 }
1499
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 @Override
1501 public boolean dispatchKeyEvent(KeyEvent event) {
1502 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1503 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001505 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001506 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001507 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001508 dumpState();
1509 return true;
1510 }
1511 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001512 }
1513 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1514 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001515 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 return true;
1517 }
1518 }
1519
1520 return super.dispatchKeyEvent(event);
1521 }
1522
Joe Onorato88ec0992009-11-19 13:16:06 -08001523 @Override
1524 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001525 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001526 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001527 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001528 Folder openFolder = mWorkspace.getOpenFolder();
1529 if (openFolder.isEditingName()) {
1530 openFolder.dismissEditingName();
1531 } else {
1532 closeFolder();
1533 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001534 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001535 mWorkspace.exitWidgetResizeMode();
1536
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001537 // Back button is a no-op here, but give at least some feedback for the button press
1538 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001539 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001540 }
1541
Adam Cohen2801caf2011-05-13 20:57:39 -07001542 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 Folder folder = mWorkspace.getOpenFolder();
1544 if (folder != null) {
1545 closeFolder(folder);
1546 }
1547 }
1548
1549 void closeFolder(Folder folder) {
1550 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001551
Michael Jurka8c920dd2011-01-20 14:16:56 -08001552 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 if (parent != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Adam Cohen2801caf2011-05-13 20:57:39 -07001555 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001556 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001558 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560
1561 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001562 * Re-listen when widgets are reset.
1563 */
1564 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001565 if (mAppWidgetHost != null) {
1566 mAppWidgetHost.startListening();
1567 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001568 }
1569
1570 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001571 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1572 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001574 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001575 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 /**
1579 * Launches the intent referred by the clicked shortcut.
1580 *
1581 * @param v The view representing the clicked shortcut.
1582 */
1583 public void onClick(View v) {
1584 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001585 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001587 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001588 int[] pos = new int[2];
1589 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001590 intent.setSourceBounds(new Rect(pos[0], pos[1],
1591 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001592 boolean success = startActivitySafely(intent, tag);
1593
1594 if (success && v instanceof BubbleTextView) {
1595 mWaitingForResume = (BubbleTextView) v;
1596 mWaitingForResume.setStayPressed(true);
1597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001599 if (v instanceof FolderIcon) {
1600 FolderIcon fi = (FolderIcon) v;
1601 handleFolderClick(fi);
1602 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001603 } else if (v == mAllAppsButton) {
1604 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001605 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001606 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001607 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 }
1610 }
1611
Michael Jurka0e260592010-06-30 17:07:39 -07001612 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001613 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001614 // clicking anywhere on the workspace causes the customization drawer to slide down
1615 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001616 return false;
1617 }
1618
Michael Jurkaaf442092010-06-10 17:01:57 -07001619 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001620 * Event handler for the search button
1621 *
1622 * @param v The view that was clicked.
1623 */
1624 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001625 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001626 // Use a custom animation for launching search
1627 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001628 }
1629
1630 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001631 * Event handler for the voice button
1632 *
1633 * @param v The view that was clicked.
1634 */
1635 public void onClickVoiceButton(View v) {
1636 startVoiceSearch();
1637 }
1638
1639 private void startVoiceSearch() {
1640 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1641 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1642 startActivity(intent);
1643 }
1644
1645 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001646 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001647 * enters home screen customization mode.
1648 *
1649 * @param v The view that was clicked.
1650 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001651 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001652 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001653 }
1654
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001655 /**
1656 * Event handler for the "grid" button that appears on the home screen, which
1657 * enters all apps mode.
1658 *
1659 * @param v The view that was clicked.
1660 */
1661 public void onClickAllAppsButton(View v) {
1662 showAllApps(true);
1663 }
1664
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001665 public void onClickAppMarketButton(View v) {
1666 if (mAppMarketIntent != null) {
1667 startActivitySafely(mAppMarketIntent, "app market");
1668 }
1669 }
1670
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001671 void startApplicationDetailsActivity(ComponentName componentName) {
1672 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001673 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1674 Uri.fromParts("package", packageName, null));
1675 startActivity(intent);
1676 }
1677
Patrick Dubroy5539af72010-09-07 15:22:01 -07001678 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1679 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1680 // System applications cannot be installed. For now, show a toast explaining that.
1681 // We may give them the option of disabling apps this way.
1682 int messageId = R.string.uninstall_system_app_text;
1683 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1684 } else {
1685 String packageName = appInfo.componentName.getPackageName();
1686 String className = appInfo.componentName.getClassName();
1687 Intent intent = new Intent(
1688 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1689 startActivity(intent);
1690 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001691 }
1692
Michael Jurkaddd62e92011-02-16 17:49:14 -08001693 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1695 try {
1696 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001697 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 } catch (ActivityNotFoundException e) {
1699 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001700 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 } catch (SecurityException e) {
1702 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001703 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001705 "or use the exported attribute for this activity. "
1706 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001708 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001710
Romain Guy8e633c52010-03-04 12:51:36 -08001711 void startActivityForResultSafely(Intent intent, int requestCode) {
1712 try {
1713 startActivityForResult(intent, requestCode);
1714 } catch (ActivityNotFoundException e) {
1715 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1716 } catch (SecurityException e) {
1717 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1718 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1719 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1720 "or use the exported attribute for this activity.", e);
1721 }
1722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723
Adam Cohena9cf38f2011-05-02 15:36:58 -07001724 private void handleFolderClick(FolderIcon folderIcon) {
1725 final FolderInfo info = folderIcon.mInfo;
1726 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 // Close any open folder
1728 closeFolder();
1729 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001730 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 } else {
1732 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001733 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 int folderScreen;
1735 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001736 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 // .. and close it
1738 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001739 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 // Close any folder open on the current screen
1741 closeFolder();
1742 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001743 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
1745 }
1746 }
1747 }
1748
Adam Cohen2801caf2011-05-13 20:57:39 -07001749 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1750 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1751 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1752 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1753
1754 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1755 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1756 oa.start();
1757 }
1758
1759 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1760 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1761 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1762 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1763
1764 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1765 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1766 oa.start();
1767 }
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001770 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 * is animated relative to the specified View. If the View is null, no animation
1772 * is played.
1773 *
1774 * @param folderInfo The FolderInfo describing the folder to open.
1775 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001776 public void openFolder(FolderIcon folderIcon) {
1777 Folder folder = folderIcon.mFolder;
1778 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779
Adam Cohen2801caf2011-05-13 20:57:39 -07001780 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001781 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782
Adam Cohen8e776a62011-06-28 18:10:06 -07001783 mDragLayer.addView(folder);
1784 mDragController.addDropTarget((DropTarget) folder);
1785
Adam Cohena9cf38f2011-05-02 15:36:58 -07001786 folder.animateOpen();
1787 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001791 if (mState != State.WORKSPACE) {
1792 return false;
1793 }
1794
Joe Onorato9c1289c2009-08-17 11:03:03 -04001795 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 return false;
1797 }
1798
1799 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001800 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 }
1802
Michael Jurka0280c3b2010-09-17 15:00:07 -07001803 resetAddInfo();
1804 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001806 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 return true;
1808 }
1809
Winson Chung3d503fb2011-07-13 17:25:49 -07001810 // The hotseat touch handling does not go through Workspace, and we always allow long press
1811 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001812 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001813 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1814 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001815 if (itemUnderLongClick == null) {
1816 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1818 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001819 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001821 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001823 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1824 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827 }
1828 }
1829 return true;
1830 }
1831
Winson Chung3d503fb2011-07-13 17:25:49 -07001832 boolean isHotseatLayout(View layout) {
1833 return mHotseat != null && layout != null &&
1834 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1835 }
1836 Hotseat getHotseat() {
1837 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001838 }
1839
Winson Chung3d503fb2011-07-13 17:25:49 -07001840 /**
1841 * Returns the CellLayout of the specified container at the specified screen.
1842 */
1843 CellLayout getCellLayout(long container, int screen) {
1844 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1845 if (mHotseat != null) {
1846 return mHotseat.getLayout();
1847 } else {
1848 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001849 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001850 } else {
1851 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001852 }
1853 }
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 Workspace getWorkspace() {
1856 return mWorkspace;
1857 }
1858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 @Override
1860 protected Dialog onCreateDialog(int id) {
1861 switch (id) {
1862 case DIALOG_CREATE_SHORTCUT:
1863 return new CreateShortcut().createDialog();
1864 case DIALOG_RENAME_FOLDER:
1865 return new RenameFolder().createDialog();
1866 }
1867
1868 return super.onCreateDialog(id);
1869 }
1870
1871 @Override
1872 protected void onPrepareDialog(int id, Dialog dialog) {
1873 switch (id) {
1874 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 break;
1876 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001877 if (mFolderInfo != null) {
1878 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1879 final CharSequence text = mFolderInfo.title;
1880 input.setText(text);
1881 input.setSelection(0, text.length());
1882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 break;
1884 }
1885 }
1886
1887 void showRenameDialog(FolderInfo info) {
1888 mFolderInfo = info;
1889 mWaitingForResult = true;
1890 showDialog(DIALOG_RENAME_FOLDER);
1891 }
1892
Adam Cohenfbba09b2011-07-18 21:43:05 -07001893 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001894 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001895 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1896 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 mWaitingForResult = true;
1898 showDialog(DIALOG_CREATE_SHORTCUT);
1899 }
1900
Joe Onoratodeb98af2010-02-19 14:59:39 -08001901 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001902 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001903 Bundle bundle = new Bundle();
1904
1905 ArrayList<String> shortcutNames = new ArrayList<String>();
1906 shortcutNames.add(getString(R.string.group_applications));
1907 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1908
1909 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1910 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1911 R.drawable.ic_launcher_application));
1912 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1913
1914 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1915 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001916 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001917 pickIntent.putExtras(bundle);
1918
Joe Onoratodeb98af2010-02-19 14:59:39 -08001919 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001920 }
1921
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 private class RenameFolder {
1923 private EditText mInput;
1924
1925 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1927 mInput = (EditText) layout.findViewById(R.id.folder_name);
1928
1929 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1930 builder.setIcon(0);
1931 builder.setTitle(getString(R.string.rename_folder_title));
1932 builder.setCancelable(true);
1933 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1934 public void onCancel(DialogInterface dialog) {
1935 cleanup();
1936 }
1937 });
1938 builder.setNegativeButton(getString(R.string.cancel_action),
1939 new Dialog.OnClickListener() {
1940 public void onClick(DialogInterface dialog, int which) {
1941 cleanup();
1942 }
1943 }
1944 );
1945 builder.setPositiveButton(getString(R.string.rename_action),
1946 new Dialog.OnClickListener() {
1947 public void onClick(DialogInterface dialog, int which) {
1948 changeFolderName();
1949 }
1950 }
1951 );
1952 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001953
1954 final AlertDialog dialog = builder.create();
1955 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1956 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001957 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001958 mInput.requestFocus();
1959 InputMethodManager inputManager = (InputMethodManager)
1960 getSystemService(Context.INPUT_METHOD_SERVICE);
1961 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001962 }
1963 });
1964
1965 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
1967
1968 private void changeFolderName() {
1969 final String name = mInput.getText().toString();
1970 if (!TextUtils.isEmpty(name)) {
1971 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001972 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 mFolderInfo.title = name;
1974 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1975
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001977 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001978 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 } else {
1980 final FolderIcon folderIcon = (FolderIcon)
1981 mWorkspace.getViewForTag(mFolderInfo);
1982 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001983 // TODO: At some point we'll probably want some version of setting
1984 // the text for a folder icon.
1985 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 getWorkspace().requestLayout();
1987 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001988 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001989 mWorkspaceLoading = true;
1990 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992 }
1993 }
1994 cleanup();
1995 }
1996
1997 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 dismissDialog(DIALOG_RENAME_FOLDER);
1999 mWaitingForResult = false;
2000 mFolderInfo = null;
2001 }
2002 }
2003
Daniel Sandler843e8602010-06-07 14:59:01 -04002004 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2005 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002006 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002007 }
2008
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002009 // AllAppsView.Watcher
2010 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002011 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002012 mWorkspace.setVisibility(View.GONE);
2013 }
2014 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002015
2016 /**
2017 * Helper method for the cameraZoomIn/cameraZoomOut animations
2018 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002019 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002020 * @param scaleFactor The scale factor used for the zoom
2021 */
2022 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2023 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002024
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002025 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002026 // Set pivotY so that at the starting zoom factor, the view is partially
2027 // visible. Modifying initialHeightFactor changes how much of the view is
2028 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002029 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002030 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002031 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002032 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002033 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002034 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002035 }
2036 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002037
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002038 /**
2039 * Zoom the camera out from the workspace to reveal 'toView'.
2040 * Assumes that the view to show is anchored at either the very top or very bottom
2041 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002042 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002043 */
Winson Chungc07918d2011-07-01 15:35:26 -07002044 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002045 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002046
Winson Chungf0ea4d32011-06-06 14:27:16 -07002047 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2048 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2049 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2050 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002051
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002052 setPivotsForZoom(toView, toState, scale);
2053
Michael Jurkad74c9842011-07-10 12:44:21 -07002054 // Shrink workspaces away if going to AppsCustomize from workspace
2055 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002056
2057 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002058 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002059 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002060 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2061 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002062 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002063 toView.setFastScaleX(a * scale + b * 1f);
2064 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002065 }
2066 });
Adam Cohen61033d32010-11-15 18:29:44 -08002067
Winson Chungf0ea4d32011-06-06 14:27:16 -07002068 toView.setVisibility(View.VISIBLE);
2069 toView.setFastAlpha(0f);
2070 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2071 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2072 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2073 public void onAnimationUpdate(float a, float b) {
2074 // don't need to invalidate because we do so above
2075 toView.setFastAlpha(a * 0f + b * 1f);
2076 }
2077 });
2078 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002079
Michael Jurkaabded662011-03-04 12:06:57 -08002080 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002081 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002082 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002083 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002084 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002085 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002086 // Prepare the position
2087 toView.setTranslationX(0.0f);
2088 toView.setTranslationY(0.0f);
2089 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002090 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002091 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002092 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002093 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002094 // If we don't set the final scale values here, if this animation is cancelled
2095 // it will have the wrong scale value and subsequent cameraPan animations will
2096 // not fix that
2097 toView.setScaleX(1.0f);
2098 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002099 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002100 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002101 }
Winson Chung32174c82011-07-19 15:47:55 -07002102
2103 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2104 // Hide the workspace scrollbar
2105 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002106 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002107 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002108 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002109 });
2110
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002111 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002112 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002113
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002114 if (mStateAnimation != null) mStateAnimation.cancel();
2115 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002116 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002117 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002118 } else {
2119 toView.setTranslationX(0.0f);
2120 toView.setTranslationY(0.0f);
2121 toView.setScaleX(1.0f);
2122 toView.setScaleY(1.0f);
2123 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002124 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002125 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002126 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2127 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002128
Winson Chungc07918d2011-07-01 15:35:26 -07002129 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2130 // Hide the workspace scrollbar
2131 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002132 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002133 }
Michael Jurkaabded662011-03-04 12:06:57 -08002134 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002135 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002136 }
2137
2138 /**
2139 * Zoom the camera back into the workspace, hiding 'fromView'.
2140 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002142 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002143 */
Winson Chungc07918d2011-07-01 15:35:26 -07002144 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002145 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002146
Winson Chungf0ea4d32011-06-06 14:27:16 -07002147 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2148 final float scaleFactor = (float)
2149 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2150 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002151
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002152 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002153
Michael Jurkad3ef3062010-11-23 16:23:58 -08002154 if (!springLoaded) {
2155 mWorkspace.unshrink(animated);
2156 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002157 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002158 if (mStateAnimation != null) mStateAnimation.cancel();
2159 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002160
Michael Jurka742574b2011-02-02 23:51:01 -08002161 final float oldScaleX = fromView.getScaleX();
2162 final float oldScaleY = fromView.getScaleY();
2163
2164 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2165 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002166 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2167 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002168 ((View)fromView.getParent()).fastInvalidate();
2169 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2170 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2171 }
2172 });
Michael Jurkaabded662011-03-04 12:06:57 -08002173 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002174 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002175 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002176 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2177 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002178 // don't need to invalidate because we do so above
2179 fromView.setFastAlpha(a * 1f + b * 0f);
2180 }
2181 });
Michael Jurkaabded662011-03-04 12:06:57 -08002182 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002183 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002184 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002185 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002186 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002187 public void onAnimationStart(android.animation.Animator animation) {
2188 if (!springLoaded) {
2189 mWorkspace.showDockDivider(false);
2190 }
2191 }
2192 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002193 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002194 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002195 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002196 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002197 }
Winson Chung5a808352011-06-27 19:08:49 -07002198 mWorkspace.flashScrollingIndicator();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002199 }
2200 });
2201
Winson Chungf0ea4d32011-06-06 14:27:16 -07002202 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002203 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002204 } else {
2205 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002206 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002207 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2208 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002209
Winson Chungc07918d2011-07-01 15:35:26 -07002210 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002211 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002212 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002213 mWorkspace.flashScrollingIndicator();
2214 }
Michael Jurkaabded662011-03-04 12:06:57 -08002215 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002216 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002217 }
2218
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002219 void showWorkspace(boolean animated) {
2220 showWorkspace(animated, null);
2221 }
2222
2223 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002224 if (layout != null) {
2225 // always animated, but that's ok since we never specify a layout and
2226 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002227 mWorkspace.unshrink(layout);
2228 } else {
2229 mWorkspace.unshrink(animated);
2230 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002231 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002232 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002233 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002234
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002235 // Change the state *after* we've called all the transition code
2236 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002237
Winson Chung5fb63472011-02-02 17:03:37 -08002238 // Resume the auto-advance of widgets
2239 mUserPresent = true;
2240 updateRunning();
2241
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002242 // send an accessibility event to announce the context change
2243 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002244 }
2245
Michael Jurkad3ef3062010-11-23 16:23:58 -08002246 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002247 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002248 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002249 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002250 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002251 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002252 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002253 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002254 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2255 mWorkspace.postDelayed(new Runnable() {
2256 @Override
2257 public void run() {
2258 exitSpringLoadedDragMode();
2259 }
2260 }, (extendedDelay ?
2261 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2262 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2263 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002264 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002265 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002266 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002267 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2268 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002269 }
2270 // Otherwise, we are not in spring loaded mode, so don't do anything.
2271 }
2272
Adam Cohenfc53cd22011-07-20 15:45:11 -07002273 public boolean isAllAppsCustomizeOpen() {
2274 return mState == State.APPS_CUSTOMIZE;
2275 }
2276
Winson Chungf0ea4d32011-06-06 14:27:16 -07002277 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002278 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002279 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002281 if (!LauncherApplication.isScreenLarge()) {
2282 if (animated) {
2283 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002284 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002285 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002286 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002287 }
2288 }
2289 }
2290
2291 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002293 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002294 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002295 if (!LauncherApplication.isScreenLarge()) {
2296 if (animated) {
2297 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002302 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002303 }
2304
Winson Chung785d2eb2011-04-14 16:08:02 -07002305 void showAllApps(boolean animated) {
2306 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002307
Winson Chungf0ea4d32011-06-06 14:27:16 -07002308 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2309 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002310
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 hideHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002314
Winson Chungf0ea4d32011-06-06 14:27:16 -07002315 // Change the state *after* we've called all the transition code
2316 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002317
2318 // Pause the auto-advance of widgets until we are out of AllApps
2319 mUserPresent = false;
2320 updateRunning();
2321
2322 // Send an accessibility event to announce the context change
2323 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2324 }
2325
Joe Onoratob2061212009-11-24 16:13:54 -05002326 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002327 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002328 * - Home from workspace
2329 * - from center screen
2330 * - from other screens
2331 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002332 * - from center screen
2333 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002334 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002335 * - from center screen
2336 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002337 * - Launch app from workspace and quit
2338 * - with back
2339 * - with home
2340 * - Launch app from all apps and quit
2341 * - with back
2342 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002343 * - Go to a screen that's not the default, then all
2344 * apps, and launch and app, and go back
2345 * - with back
2346 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002347 * - On workspace, long press power and go back
2348 * - with back
2349 * - with home
2350 * - On all apps, long press power and go back
2351 * - with back
2352 * - with home
2353 * - On workspace, power off
2354 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002355 * - Launch an app and turn off the screen while in that app
2356 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002357 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002358 * - From all apps
2359 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002360 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2361 * - From all apps
2362 * - From the center workspace
2363 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002364 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002365 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002366 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2367 mWorkspace.setVisibility(View.VISIBLE);
2368 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002369
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002371 mSearchDeleteBar.showSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002372 showHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002373
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 if (mAllAppsButton != null) {
2376 mAllAppsButton.requestFocus();
2377 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002378 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002379 }
2380
Joe Onorato7c312c12009-08-13 21:36:53 -07002381 void lockAllApps() {
2382 // TODO
2383 }
2384
2385 void unlockAllApps() {
2386 // TODO
2387 }
2388
Patrick Dubroy5f445422011-02-18 14:35:21 -08002389 /**
2390 * Add an item from all apps or customize onto the given workspace screen.
2391 * If layout is null, add to the current screen.
2392 */
2393 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002394 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2395 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002396 } else {
2397 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002398 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002399 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002400
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002401 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002402 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002403 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002404 // Look for the toolbar icon specified in the activity meta-data
2405 Bundle metaData = packageManager.getActivityInfo(
2406 activityName, PackageManager.GET_META_DATA).metaData;
2407 if (metaData != null) {
2408 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2409 if (iconResId != 0) {
2410 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002411 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002412 }
2413 }
2414 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002415 // This can happen if the activity defines an invalid drawable
2416 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2417 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002418 } catch (Resources.NotFoundException nfe) {
2419 // This can happen if the activity defines an invalid drawable
2420 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2421 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002422 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002423 return null;
2424 }
2425
2426 // if successful in getting icon, return it; otherwise, set button to use default drawable
2427 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2428 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2429 TextView button = (TextView) findViewById(buttonId);
2430 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2431
2432 // If we were unable to find the icon via the meta-data, use a generic one
2433 if (toolbarIcon == null) {
2434 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2435 return null;
2436 } else {
2437 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2438 return toolbarIcon.getConstantState();
2439 }
2440 }
2441
2442 // if successful in getting icon, return it; otherwise, set button to use default drawable
2443 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2444 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2445 ImageView button = (ImageView) findViewById(buttonId);
2446 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2447
Michael Jurka19e0fc52011-07-22 18:00:21 -07002448 if (button != null) {
2449 // If we were unable to find the icon via the meta-data, use a
2450 // generic one
2451 if (toolbarIcon == null) {
2452 button.setImageResource(fallbackDrawableId);
2453 } else {
2454 button.setImageDrawable(toolbarIcon);
2455 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002456 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002457
2458 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2459
Michael Jurka0423dcf2010-10-05 14:56:18 -07002460 }
2461
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002462 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2463 TextView button = (TextView) findViewById(buttonId);
2464 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2465 }
2466
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002467 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002468 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002469 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002470 }
2471
Michael Jurka0423dcf2010-10-05 14:56:18 -07002472 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002473 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2474 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002475
Winson Chung1cad91e2011-05-25 17:41:01 -07002476 final SearchManager searchManager =
2477 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2478 ComponentName activityName = searchManager.getGlobalSearchActivity();
2479 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002480 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002481 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002482 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002483 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002484 } else {
2485 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002486 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002487 }
2488 }
2489
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002490 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002491 updateButtonWithDrawable(R.id.search_button, d);
2492 }
2493
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002494 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002495 final View searchDivider = findViewById(R.id.search_divider);
2496 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002497
Winson Chung1cad91e2011-05-25 17:41:01 -07002498 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2499 ComponentName activityName = intent.resolveActivity(getPackageManager());
2500 if (activityName != null) {
2501 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002502 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2503 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002504 voiceButton.setVisibility(View.VISIBLE);
2505 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002506 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002507 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002508 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002509 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002510
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002511 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002512 updateButtonWithDrawable(R.id.voice_button, d);
2513 }
2514
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002515 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002516 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002517 */
2518 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002519 final View marketButton = findViewById(R.id.market_button);
2520 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2521 // Find the app market activity by resolving an intent.
2522 // (If multiple app markets are installed, it will return the ResolverActivity.)
2523 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002524 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002525 mAppMarketIntent = intent;
2526 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002527 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002528 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002529
2530 // Remove the shop icon text in the Phone UI
2531 if (!LauncherApplication.isScreenLarge()) {
2532 ((TextView) marketButton).setText("");
2533 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002534 } else {
2535 // We should hide and disable the view so that we don't try and restore the visibility
2536 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2537 marketButton.setVisibility(View.GONE);
2538 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002539 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002540 }
2541
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002542 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002543 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002544 }
2545
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002546 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002547 * Displays the shortcut creation dialog and launches, if necessary, the
2548 * appropriate activity.
2549 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002550 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002551 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2552 DialogInterface.OnShowListener {
2553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002558
Winson Chung55b65502011-05-26 12:03:43 -07002559 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2560 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002561 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 AlertDialog dialog = builder.create();
2564 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002565 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002566 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 return dialog;
2569 }
2570
2571 public void onCancel(DialogInterface dialog) {
2572 mWaitingForResult = false;
2573 cleanup();
2574 }
2575
Romain Guycbb89e42009-06-08 15:52:54 -07002576 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002577 mWaitingForResult = false;
2578 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002579 }
2580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002582 try {
2583 dismissDialog(DIALOG_CREATE_SHORTCUT);
2584 } catch (Exception e) {
2585 // An exception is thrown if the dialog is not visible, which is fine
2586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 }
2588
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002589 /**
2590 * Handle the action clicked in the "Add to home" dialog.
2591 */
2592 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002593 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002594
Winson Chung55b65502011-05-26 12:03:43 -07002595 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2596 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002597 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002598 if (mAppsCustomizeTabHost != null) {
Winson Chung5a808352011-06-27 19:08:49 -07002599 mAppsCustomizeTabHost.selectShortcutsTab();
Winson Chungd2945262011-06-24 15:22:14 -07002600 }
2601 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002602 break;
2603 }
Winson Chung55b65502011-05-26 12:03:43 -07002604 case AddAdapter.ITEM_APPLICATION: {
2605 if (mAppsCustomizeTabHost != null) {
2606 mAppsCustomizeTabHost.selectAppsTab();
2607 }
2608 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002609 break;
2610 }
Winson Chung55b65502011-05-26 12:03:43 -07002611 case AddAdapter.ITEM_APPWIDGET: {
2612 if (mAppsCustomizeTabHost != null) {
2613 mAppsCustomizeTabHost.selectWidgetsTab();
2614 }
2615 showAllApps(true);
2616 break;
2617 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002618 case AddAdapter.ITEM_WALLPAPER: {
2619 startWallpaper();
2620 break;
2621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002622 }
2623 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002624
2625 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002626 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002627 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002628 }
2629
2630 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002631 * Receives notifications when applications are added/removed.
2632 */
2633 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2634 @Override
2635 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002636 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002637 String reason = intent.getStringExtra("reason");
2638 if (!"homekey".equals(reason)) {
2639 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002640 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002641 animate = false;
2642 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002643 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002644 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002645 }
2646 }
2647
2648 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002649 * Receives notifications whenever the appwidgets are reset.
2650 */
2651 private class AppWidgetResetObserver extends ContentObserver {
2652 public AppWidgetResetObserver() {
2653 super(new Handler());
2654 }
2655
2656 @Override
2657 public void onChange(boolean selfChange) {
2658 onAppWidgetReset();
2659 }
2660 }
2661
2662 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002663 * If the activity is currently paused, signal that we need to re-run the loader
2664 * in onResume.
2665 *
2666 * This needs to be called from incoming places where resources might have been loaded
2667 * while we are paused. That is becaues the Configuration might be wrong
2668 * when we're not running, and if it comes back to what it was when we
2669 * were paused, we are not restarted.
2670 *
2671 * Implementation of the method from LauncherModel.Callbacks.
2672 *
2673 * @return true if we are currently paused. The caller might be able to
2674 * skip some work in that case since we will come back again.
2675 */
2676 public boolean setLoadOnResume() {
2677 if (mPaused) {
2678 Log.i(TAG, "setLoadOnResume");
2679 mOnResumeNeedsLoad = true;
2680 return true;
2681 } else {
2682 return false;
2683 }
2684 }
2685
2686 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002687 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002689 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002690 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002691 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002692 } else {
2693 return SCREEN_COUNT / 2;
2694 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002695 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002696
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002697
Joe Onorato9c1289c2009-08-17 11:03:03 -04002698 /**
2699 * Refreshes the shortcuts shown on the workspace.
2700 *
2701 * Implementation of the method from LauncherModel.Callbacks.
2702 */
2703 public void startBinding() {
2704 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002705
2706 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 int count = workspace.getChildCount();
2708 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002709 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002710 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2711 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002712 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002713 if (mHotseat != null) {
2714 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002716
Adam Cohen4eac29a2011-07-11 17:53:37 -07002717 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002718 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002719 }
2720
2721 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002722 * Bind the items start-end from the list.
2723 *
2724 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002726 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002727 setLoadOnResume();
2728
Joe Onorato9c1289c2009-08-17 11:03:03 -04002729 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 for (int i=start; i<end; i++) {
2731 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002732
2733 // Short circuit if we are loading dock items for a configuration which has no dock
2734 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2735 mHotseat == null) {
2736 continue;
2737 }
2738
Joe Onorato9c1289c2009-08-17 11:03:03 -04002739 switch (item.itemType) {
2740 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2741 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002742 View shortcut = createShortcut((ShortcutInfo)item);
2743 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2744 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002746 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002747 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002748 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002749 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002750 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2751 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002752 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002755 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
2757
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758 /**
2759 * Implementation of the method from LauncherModel.Callbacks.
2760 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002761 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002762 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002763 sFolders.clear();
2764 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002766
2767 /**
2768 * Add the views for a widget to the workspace.
2769 *
2770 * Implementation of the method from LauncherModel.Callbacks.
2771 */
2772 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002773 setLoadOnResume();
2774
Daniel Sandler843e8602010-06-07 14:59:01 -04002775 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2776 if (DEBUG_WIDGETS) {
2777 Log.d(TAG, "bindAppWidget: " + item);
2778 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002779 final Workspace workspace = mWorkspace;
2780
2781 final int appWidgetId = item.appWidgetId;
2782 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002783 if (DEBUG_WIDGETS) {
2784 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2785 }
2786
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2788
Joe Onorato9c1289c2009-08-17 11:03:03 -04002789 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2790 item.hostView.setTag(item);
2791
Winson Chung3d503fb2011-07-13 17:25:49 -07002792 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 item.cellY, item.spanX, item.spanY, false);
2794
Adam Cohended9f8d2010-11-03 13:25:16 -07002795 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2796
Joe Onorato9c1289c2009-08-17 11:03:03 -04002797 workspace.requestLayout();
2798
Daniel Sandler843e8602010-06-07 14:59:01 -04002799 if (DEBUG_WIDGETS) {
2800 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2801 + (SystemClock.uptimeMillis()-start) + "ms");
2802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002803 }
2804
2805 /**
2806 * Callback saying that there aren't any more items to bind.
2807 *
2808 * Implementation of the method from LauncherModel.Callbacks.
2809 */
2810 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002811 setLoadOnResume();
2812
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 if (mSavedState != null) {
2814 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002815 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 mSavedState = null;
2818 }
2819
2820 if (mSavedInstanceState != null) {
2821 super.onRestoreInstanceState(mSavedInstanceState);
2822 mSavedInstanceState = null;
2823 }
2824
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002826
2827 // If we received the result of any pending adds while the loader was running (e.g. the
2828 // widget configuration forced an orientation change), process them now.
2829 for (int i = 0; i < sPendingAddList.size(); i++) {
2830 completeAdd(sPendingAddList.get(i));
2831 }
2832 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 }
2834
2835 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002836 * Updates the icons on the launcher that are affected by changes to the package list
2837 * on the device.
2838 */
2839 private void updateIconsAffectedByPackageManagerChanges() {
2840 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002841 updateVoiceSearchIcon();
2842 }
2843
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002844 @Override
2845 public void bindSearchablesChanged() {
2846 updateGlobalSearchIcon();
2847 }
2848
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002849 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 * Add the icons for all apps.
2851 *
2852 * Implementation of the method from LauncherModel.Callbacks.
2853 */
2854 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002855 if (mAppsCustomizeContent != null) {
2856 mAppsCustomizeContent.setApps(apps);
2857 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002858 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002859 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 }
2861
2862 /**
2863 * A package was installed.
2864 *
2865 * Implementation of the method from LauncherModel.Callbacks.
2866 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002867 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002868 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002870
Winson Chung785d2eb2011-04-14 16:08:02 -07002871 if (mAppsCustomizeContent != null) {
2872 mAppsCustomizeContent.addApps(apps);
2873 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002874 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 }
2876
2877 /**
2878 * A package was updated.
2879 *
2880 * Implementation of the method from LauncherModel.Callbacks.
2881 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002882 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002883 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002885 if (mWorkspace != null) {
2886 mWorkspace.updateShortcuts(apps);
2887 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002888
Winson Chung785d2eb2011-04-14 16:08:02 -07002889 if (mAppsCustomizeContent != null) {
2890 mAppsCustomizeContent.updateApps(apps);
2891 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002892 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 }
2894
2895 /**
2896 * A package was uninstalled.
2897 *
2898 * Implementation of the method from LauncherModel.Callbacks.
2899 */
Joe Onorato36115782010-06-17 13:28:48 -04002900 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002902 if (permanent) {
2903 mWorkspace.removeItems(apps);
2904 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002905
Winson Chung785d2eb2011-04-14 16:08:02 -07002906 if (mAppsCustomizeContent != null) {
2907 mAppsCustomizeContent.removeApps(apps);
2908 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002909 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 }
Joe Onoratobe386092009-11-17 17:32:16 -08002911
2912 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002913 * A number of packages were updated.
2914 */
2915 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002916
Winson Chung785d2eb2011-04-14 16:08:02 -07002917 if (mAppsCustomizeContent != null) {
2918 mAppsCustomizeContent.onPackagesUpdated();
2919 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002920 }
2921
Winson Chung400438b2011-01-16 17:53:48 -08002922 private int mapConfigurationOriActivityInfoOri(int configOri) {
2923 final Display d = getWindowManager().getDefaultDisplay();
2924 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2925 switch (d.getRotation()) {
2926 case Surface.ROTATION_0:
2927 case Surface.ROTATION_180:
2928 // We are currently in the same basic orientation as the natural orientation
2929 naturalOri = configOri;
2930 break;
2931 case Surface.ROTATION_90:
2932 case Surface.ROTATION_270:
2933 // We are currently in the other basic orientation to the natural orientation
2934 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2935 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2936 break;
2937 }
2938
2939 int[] oriMap = {
2940 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2941 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2942 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2943 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2944 };
2945 // Since the map starts at portrait, we need to offset if this device's natural orientation
2946 // is landscape.
2947 int indexOffset = 0;
2948 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2949 indexOffset = 1;
2950 }
2951 return oriMap[(d.getRotation() + indexOffset) % 4];
2952 }
2953 public void lockScreenOrientation() {
2954 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2955 .getConfiguration().orientation));
2956 }
2957 public void unlockScreenOrientation() {
2958 mHandler.postDelayed(new Runnable() {
2959 public void run() {
2960 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2961 }
2962 }, mRestoreScreenOrientationDelay);
2963 }
2964
Winson Chung80baf5a2010-08-09 16:03:15 -07002965 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002966 * Prints out out state for debugging.
2967 */
2968 public void dumpState() {
2969 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002970 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002971 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2972 Log.d(TAG, "mRestoring=" + mRestoring);
2973 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2974 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002975 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002976 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002977
Winson Chung785d2eb2011-04-14 16:08:02 -07002978 if (mAppsCustomizeContent != null) {
2979 mAppsCustomizeContent.dumpState();
2980 }
Joe Onoratobe386092009-11-17 17:32:16 -08002981 Log.d(TAG, "END launcher2 dump state");
2982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002983}
Michael Jurka2763be32011-02-24 11:19:57 -08002984
Michael Jurkaabded662011-03-04 12:06:57 -08002985interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002986 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2987 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002988}