blob: dc298bcfc6c4c4815a299b708038862a44990cb2 [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;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020054import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070056import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040057import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080058import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070059import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080060import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.Selection;
62import android.text.SpannableStringBuilder;
63import android.text.TextUtils;
64import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070075import android.view.ViewGroup;
Adam Cohen860f4c52011-01-27 20:16:13 -080076import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080077import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080078import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070080import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070082import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070083import android.widget.TextView;
84import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070085
Adam Cohendf2cc412011-04-27 16:56:57 -070086import com.android.common.Search;
87import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070088import com.android.launcher2.DropTarget.DragObject;
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
Winson Chung70d72102011-08-12 11:24:35 -0700109 private static final int MENU_GROUP_WALLPAPER = 1;
110 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
111 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700112 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
113 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
115 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700116 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 private static final int REQUEST_PICK_APPLICATION = 6;
118 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700120 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
122 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
123
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800124 static final int SCREEN_COUNT = 5;
125 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
Joe Onorato7c312c12009-08-13 21:36:53 -0700127 static final int DIALOG_CREATE_SHORTCUT = 1;
128 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Romain Guy98d01652009-06-30 16:21:04 -0700130 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 // Type: int
133 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700134 // Type: int
135 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700137 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
138 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
140 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700141 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 // Type: boolean
145 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
146 // Type: long
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
148
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700149 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
150
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700151 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700152 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700153 private State mState = State.WORKSPACE;
154 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700155
Joe Onorato9c1289c2009-08-17 11:03:03 -0400156 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700157 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
158 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800161 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800163 private final BroadcastReceiver mCloseSystemDialogsReceiver
164 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800165 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 private LayoutInflater mInflater;
168
Joe Onorato00acb122009-08-04 16:04:30 -0400169 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700171
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700172 private AppWidgetManager mAppWidgetManager;
173 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
Winson Chung3d503fb2011-07-13 17:25:49 -0700175 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700176 private int[] mTmpAddItemCellCoordinates = new int[2];
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private FolderInfo mFolderInfo;
179
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800181 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700182
Winson Chungf0ea4d32011-06-06 14:27:16 -0700183 private SearchDropTargetBar mSearchDeleteBar;
184 private AppsCustomizeTabHost mAppsCustomizeTabHost;
185 private AppsCustomizePagedView mAppsCustomizeContent;
186 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private Bundle mSavedState;
189
190 private SpannableStringBuilder mDefaultKeySsb = null;
191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 private boolean mWorkspaceLoading = true;
193
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800194 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private boolean mRestoring;
196 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700197 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
199 private Bundle mSavedInstanceState;
200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800202 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800203 private boolean mUserPresent = true;
204 private boolean mVisible = false;
205 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700207 private static LocaleConfiguration sLocaleConfiguration = null;
208
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700209 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700210
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700211 private Intent mAppMarketIntent = null;
212
Adam Cohended9f8d2010-11-03 13:25:16 -0700213 // Related to the auto-advancing of widgets
214 private final int ADVANCE_MSG = 1;
215 private final int mAdvanceInterval = 20000;
216 private final int mAdvanceStagger = 250;
217 private long mAutoAdvanceSentTime;
218 private long mAutoAdvanceTimeLeft = -1;
219 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
220 new HashMap<View, AppWidgetProviderInfo>();
221
Winson Chung400438b2011-01-16 17:53:48 -0800222 // Determines how long to wait after a rotation before restoring the screen orientation to
223 // match the sensor state.
224 private final int mRestoreScreenOrientationDelay = 500;
225
Michael Jurka4ef207b2010-11-29 17:05:45 -0800226 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800227 private static Drawable.ConstantState sGlobalSearchIcon;
228 private static Drawable.ConstantState sVoiceSearchIcon;
229 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800230
Adam Cohen2801caf2011-05-13 20:57:39 -0700231 private DragLayer mDragLayer;
232
Michael Jurkaddd62e92011-02-16 17:49:14 -0800233 private BubbleTextView mWaitingForResume;
234
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800235 private static ArrayList<PendingAddArguments> sPendingAddList
236 = new ArrayList<PendingAddArguments>();
237
238 private static class PendingAddArguments {
239 int requestCode;
240 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700241 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800242 int screen;
243 int cellX;
244 int cellY;
245 }
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 @Override
248 protected void onCreate(Bundle savedInstanceState) {
249 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800250 LauncherApplication app = ((LauncherApplication)getApplication());
251 mModel = app.setLauncher(this);
252 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400253 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800254 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700255
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
258 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200261 android.os.Debug.startMethodTracing(
262 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 }
264
265 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 setContentView(R.layout.launcher);
267 setupViews();
268
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800269 registerContentObservers();
270
Joe Onorato7c312c12009-08-13 21:36:53 -0700271 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 mSavedState = savedInstanceState;
274 restoreState(mSavedState);
275
Winson Chunga12a2502010-12-20 14:41:35 -0800276 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700277 if (mAppsCustomizeContent != null) {
278 mAppsCustomizeContent.onPackagesUpdated();
279 }
Winson Chunga12a2502010-12-20 14:41:35 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 if (PROFILE_STARTUP) {
282 android.os.Debug.stopMethodTracing();
283 }
284
285 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
288
Michael Jurkac57b7a82011-08-09 22:02:20 -0700289 if (!mModel.isAllAppsLoaded()) {
290 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
291 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
292 }
293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 // For handling default keys
295 mDefaultKeySsb = new SpannableStringBuilder();
296 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800297
298 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
299 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800300
301 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700302 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
303 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100304 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700305 }
306 if (sGlobalSearchIcon != null) {
307 updateGlobalSearchIcon(sGlobalSearchIcon);
308 }
309 if (sVoiceSearchIcon != null) {
310 updateVoiceSearchIcon(sVoiceSearchIcon);
311 }
312 if (sAppMarketIcon != null) {
313 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
Romain Guycbb89e42009-06-08 15:52:54 -0700316
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700318 if (sLocaleConfiguration == null) {
319 new AsyncTask<Void, Void, LocaleConfiguration>() {
320 @Override
321 protected LocaleConfiguration doInBackground(Void... unused) {
322 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
323 readConfiguration(Launcher.this, localeConfiguration);
324 return localeConfiguration;
325 }
326
327 @Override
328 protected void onPostExecute(LocaleConfiguration result) {
329 sLocaleConfiguration = result;
330 checkForLocaleChange(); // recursive, but now with a locale configuration
331 }
332 }.execute();
333 return;
334 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 final Configuration configuration = getResources().getConfiguration();
337
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700338 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 final String locale = configuration.locale.toString();
340
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700341 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 final int mcc = configuration.mcc;
343
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700344 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 final int mnc = configuration.mnc;
346
Romain Guy84f296c2009-11-04 15:00:44 -0800347 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348
Romain Guy84f296c2009-11-04 15:00:44 -0800349 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700350 sLocaleConfiguration.locale = locale;
351 sLocaleConfiguration.mcc = mcc;
352 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700353
Joe Onorato0589f0f2010-02-08 13:44:00 -0800354 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355
356 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
357 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700358 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 public void run() {
360 writeConfiguration(Launcher.this, localeConfiguration);
361 }
362 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700363 }
364 }
365
366 private static class LocaleConfiguration {
367 public String locale;
368 public int mcc = -1;
369 public int mnc = -1;
370 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700371
Romain Guy98d01652009-06-30 16:21:04 -0700372 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
373 DataInputStream in = null;
374 try {
375 in = new DataInputStream(context.openFileInput(PREFERENCES));
376 configuration.locale = in.readUTF();
377 configuration.mcc = in.readInt();
378 configuration.mnc = in.readInt();
379 } catch (FileNotFoundException e) {
380 // Ignore
381 } catch (IOException e) {
382 // Ignore
383 } finally {
384 if (in != null) {
385 try {
386 in.close();
387 } catch (IOException e) {
388 // Ignore
389 }
390 }
391 }
392 }
393
394 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
395 DataOutputStream out = null;
396 try {
397 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
398 out.writeUTF(configuration.locale);
399 out.writeInt(configuration.mcc);
400 out.writeInt(configuration.mnc);
401 out.flush();
402 } catch (FileNotFoundException e) {
403 // Ignore
404 } catch (IOException e) {
405 //noinspection ResultOfMethodCallIgnored
406 context.getFileStreamPath(PREFERENCES).delete();
407 } finally {
408 if (out != null) {
409 try {
410 out.close();
411 } catch (IOException e) {
412 // Ignore
413 }
414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416 }
417
Adam Cohen716b51e2011-06-30 12:09:54 -0700418 public DragLayer getDragLayer() {
419 return mDragLayer;
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 static int getScreen() {
423 synchronized (sLock) {
424 return sScreen;
425 }
426 }
427
428 static void setScreen(int screen) {
429 synchronized (sLock) {
430 sScreen = screen;
431 }
432 }
433
Winson Chung557d6ed2011-07-08 15:34:52 -0700434 /**
435 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
436 * a configuration step, this allows the proper animations to run after other transitions.
437 */
438 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700439 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800440 switch (args.requestCode) {
441 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700442 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
443 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800444 break;
445 case REQUEST_PICK_SHORTCUT:
446 processShortcut(args.intent);
447 break;
448 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700449 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
450 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700451 result = true;
452 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800453 case REQUEST_PICK_APPWIDGET:
454 addAppWidgetFromPick(args.intent);
455 break;
456 case REQUEST_CREATE_APPWIDGET:
457 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700458 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700459 result = true;
460 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800461 case REQUEST_PICK_WALLPAPER:
462 // We just wanted the activity result here so we can clear mWaitingForResult
463 break;
464 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 // In any situation where we have a multi-step drop, we should reset the add info only after
466 // we complete the drop
467 resetAddInfo();
468 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800469 }
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800472 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700473 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700474 mWaitingForResult = false;
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 // The pattern used here is that a user PICKs a specific application,
477 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
480 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700481 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800482 final PendingAddArguments args = new PendingAddArguments();
483 args.requestCode = requestCode;
484 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700485 args.container = mPendingAddInfo.container;
486 args.screen = mPendingAddInfo.screen;
487 args.cellX = mPendingAddInfo.cellX;
488 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800489
490 // If the loader is still running, defer the add until it is done.
491 if (isWorkspaceLocked()) {
492 sPendingAddList.add(args);
493 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700494 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 }
Romain Guy18042c82009-11-06 11:44:55 -0800496 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700497 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
498 if (data != null) {
499 // Clean up the appWidgetId if we canceled
500 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
501 if (appWidgetId != -1) {
502 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
503 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
505 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700506
507 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700508 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 }
510
511 @Override
512 protected void onResume() {
513 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800514 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700515 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400516 mWorkspaceLoading = true;
517 mModel.startLoader(this, true);
518 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700519 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800521 if (mWaitingForResume != null) {
522 mWaitingForResume.setStayPressed(false);
523 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700524 // When we resume Launcher, a different Activity might be responsible for the app
525 // market intent, so refresh the icon
526 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 }
528
529 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700530 protected void onPause() {
531 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700532 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800533 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700534 }
Romain Guycbb89e42009-06-08 15:52:54 -0700535
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700536 @Override
537 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 // Flag the loader to stop early before switching
539 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700540 if (mAppsCustomizeContent != null) {
541 mAppsCustomizeContent.surrender();
542 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800543 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700544 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700545
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800546 // We can't hide the IME if it was forced open. So don't bother
547 /*
548 @Override
549 public void onWindowFocusChanged(boolean hasFocus) {
550 super.onWindowFocusChanged(hasFocus);
551
552 if (hasFocus) {
553 final InputMethodManager inputManager = (InputMethodManager)
554 getSystemService(Context.INPUT_METHOD_SERVICE);
555 WindowManager.LayoutParams lp = getWindow().getAttributes();
556 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
557 android.os.Handler()) {
558 protected void onReceiveResult(int resultCode, Bundle resultData) {
559 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
560 }
561 });
562 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
563 }
564 }
565 */
566
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 private boolean acceptFilter() {
568 final InputMethodManager inputManager = (InputMethodManager)
569 getSystemService(Context.INPUT_METHOD_SERVICE);
570 return !inputManager.isFullscreenMode();
571 }
572
573 @Override
574 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700575 final int uniChar = event.getUnicodeChar();
576 final boolean handled = super.onKeyDown(keyCode, event);
577 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
578 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
580 keyCode, event);
581 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700582 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700583 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700584 // showSearchDialog()
585 // If there are multiple keystrokes before the search dialog takes focus,
586 // onSearchRequested() will be called for every keystroke,
587 // but it is idempotent, so it's fine.
588 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 }
590 }
591
Joe Onorato8a9625e2010-01-28 15:55:35 -0800592 // Eat the long press event so the keyboard doesn't come up.
593 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
594 return true;
595 }
596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 return handled;
598 }
599
Karl Rosaen138a0412009-04-23 19:00:21 -0700600 private String getTypedText() {
601 return mDefaultKeySsb.toString();
602 }
603
604 private void clearTypedText() {
605 mDefaultKeySsb.clear();
606 mDefaultKeySsb.clearSpans();
607 Selection.setSelection(mDefaultKeySsb, 0);
608 }
609
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700611 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
612 * State
613 */
614 private static State intToState(int stateOrdinal) {
615 State state = State.WORKSPACE;
616 final State[] stateValues = State.values();
617 for (int i = 0; i < stateValues.length; i++) {
618 if (stateValues[i].ordinal() == stateOrdinal) {
619 state = stateValues[i];
620 break;
621 }
622 }
623 return state;
624 }
625
626 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 * Restores the previous state, if it exists.
628 *
629 * @param savedState The previous state.
630 */
631 private void restoreState(Bundle savedState) {
632 if (savedState == null) {
633 return;
634 }
635
Michael Jurka883f55b2010-10-21 15:47:14 -0700636 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700637 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800638 showAllApps(false);
639 }
640
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
642 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700643 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
645
Winson Chung3d503fb2011-07-13 17:25:49 -0700646 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
647 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700648
Winson Chung3d503fb2011-07-13 17:25:49 -0700649 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
650 mPendingAddInfo.container = pendingAddContainer;
651 mPendingAddInfo.screen = pendingAddScreen;
652 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
653 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 mRestoring = true;
655 }
656
657 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
658 if (renameFolder) {
659 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700660 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 mRestoring = true;
662 }
Winson Chunga12a2502010-12-20 14:41:35 -0800663
Winson Chung785d2eb2011-04-14 16:08:02 -0700664
665 // Restore the AppsCustomize tab
666 if (mAppsCustomizeTabHost != null) {
667 String curTab = savedState.getString("apps_customize_currentTab");
668 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700669 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700670 mAppsCustomizeContent.setContentType(
671 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
672 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700673 mAppsCustomizeContent.loadAssociatedPages(
674 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700675 }
676
Winson Chung5afbf7b2011-07-25 11:53:08 -0700677 int currentIndex = savedState.getInt("apps_customize_currentIndex");
678 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681
682 /**
683 * Finds all the views we need and configure them properly.
684 */
685 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700686 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400687
Winson Chung4c98d922011-05-31 16:50:48 -0700688 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
689 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690
Winson Chung4c98d922011-05-31 16:50:48 -0700691 // Setup the drag layer
692 mDragLayer.setup(this, dragController);
693
Winson Chung3d503fb2011-07-13 17:25:49 -0700694 // Setup the hotseat
695 mHotseat = (Hotseat) findViewById(R.id.hotseat);
696 if (mHotseat != null) {
697 mHotseat.setup(this);
698 }
699
Winson Chung4c98d922011-05-31 16:50:48 -0700700 // Setup the workspace
701 mWorkspace.setHapticFeedbackEnabled(false);
702 mWorkspace.setOnLongClickListener(this);
703 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700704 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700705
Winson Chungf0ea4d32011-06-06 14:27:16 -0700706 // Get the search/delete bar
707 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700708
Winson Chungf0ea4d32011-06-06 14:27:16 -0700709 // Setup AppsCustomize
710 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
711 findViewById(R.id.apps_customize_pane);
712 mAppsCustomizeContent = (AppsCustomizePagedView)
713 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
714 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400715
Romain Guy1fbc1c82009-11-09 20:43:08 -0800716
Winson Chung4c98d922011-05-31 16:50:48 -0700717
Winson Chung3d503fb2011-07-13 17:25:49 -0700718 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700719 dragController.setDragScoller(mWorkspace);
720 dragController.setScrollView(mDragLayer);
721 dragController.setMoveTarget(mWorkspace);
722 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700723 if (mSearchDeleteBar != null) {
724 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
727
Romain Guy8a73c512009-11-09 19:19:59 -0800728
Winson Chungaafa03c2010-06-11 17:34:16 -0700729
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 /**
731 * Creates a view representing a shortcut.
732 *
733 * @param info The data structure describing the shortcut.
734 *
735 * @return A View inflated from R.layout.application.
736 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800737 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700739 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 /**
743 * Creates a view representing a shortcut inflated from the specified resource.
744 *
745 * @param layoutResId The id of the XML layout used to create the shortcut.
746 * @param parent The group the shortcut belongs to.
747 * @param info The data structure describing the shortcut.
748 *
749 * @return A View inflated from layoutResId.
750 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800751 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800752 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
753 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 return favorite;
756 }
757
758 /**
759 * Add an application shortcut to the workspace.
760 *
761 * @param data The intent describing the application.
762 * @param cellInfo The position on screen where to create the shortcut.
763 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700764 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700765 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700766 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700767
Adam Cohenfbba09b2011-07-18 21:43:05 -0700768 // First we check if we already know the exact location where we want to add this item.
769 if (cellX >= 0 && cellY >= 0) {
770 cellXY[0] = cellX;
771 cellXY[1] = cellY;
772 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700773 showOutOfSpaceMessage();
774 return;
775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800777 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800778
Romain Guy73b979d2009-06-09 12:57:21 -0700779 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800780 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800782 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700783 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700784 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800785 } else {
786 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788 }
Romain Guycbb89e42009-06-08 15:52:54 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 /**
791 * Add a shortcut to the workspace.
792 *
793 * @param data The intent describing the shortcut.
794 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700796 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
797 int cellY) {
798 int[] cellXY = mTmpAddItemCellCoordinates;
799 int[] touchXY = mPendingAddInfo.dropPos;
800 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700801
Michael Jurkad3ef3062010-11-23 16:23:58 -0800802 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700803
Adam Cohen558baaf2011-08-15 15:22:57 -0700804 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
805 final View view = createShortcut(info);
806
Adam Cohenfbba09b2011-07-18 21:43:05 -0700807 // First we check if we already know the exact location where we want to add this item.
808 if (cellX >= 0 && cellY >= 0) {
809 cellXY[0] = cellX;
810 cellXY[1] = cellY;
811 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700812
813 // If appropriate, either create a folder or add to an existing folder
814 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
815 true, null,null)) {
816 return;
817 }
818 DragObject dragObject = new DragObject();
819 dragObject.dragInfo = info;
820 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
821 return;
822 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700823 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800824 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700825 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800826 foundCellSpan = (result != null);
827 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700828 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800829 }
830
831 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700832 showOutOfSpaceMessage();
833 return;
834 }
835
Adam Cohen558baaf2011-08-15 15:22:57 -0700836 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837
838 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
840 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
842 }
843
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700847 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700848 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700851 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700855
856 // We want to account for the extra amount of padding that we are adding to the widget
857 // to ensure that it gets the full amount of space that it has requested
858 Resources r = getResources();
859 int requiredWidth = appWidgetInfo.minWidth +
860 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
861 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
862 int requiredHeight = appWidgetInfo.minHeight +
863 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
864 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
865 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700868 // We have saved the position to which the widget was dragged-- this really only matters
869 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 int[] cellXY = mTmpAddItemCellCoordinates;
871 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700872 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
874 cellXY[0] = mPendingAddInfo.cellX;
875 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700876 foundCellSpan = true;
877 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700879 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800881 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700882 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700883 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700884 }
885
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800887 if (appWidgetId != -1) {
888 // Deleting an app widget ID is a void call but writes to disk before returning
889 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800890 new Thread("deleteAppWidgetId") {
891 public void run() {
892 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
893 }
894 }.start();
895 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700896 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800897 return;
898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700900 // Build Launcher-specific widget info and save to database
901 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700902 launcherInfo.spanX = spanXY[0];
903 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700906 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907
908 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700911
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700912 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
Winson Chung3d503fb2011-07-13 17:25:49 -0700915 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400916 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700917
918 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920 }
Romain Guycbb89e42009-06-08 15:52:54 -0700921
Adam Cohended9f8d2010-11-03 13:25:16 -0700922 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
923 @Override
924 public void onReceive(Context context, Intent intent) {
925 final String action = intent.getAction();
926 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
927 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700928 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700929 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700930
Winson Chungc100e8e2011-08-09 16:02:43 -0700931 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700932 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700933 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
934 mAppsCustomizeTabHost.reset();
935 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700936 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700937 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
938 mUserPresent = true;
939 updateRunning();
940 }
941 }
942 };
943
944 @Override
945 public void onAttachedToWindow() {
946 super.onAttachedToWindow();
947
948 // Listen for broadcasts related to user-presence
949 final IntentFilter filter = new IntentFilter();
950 filter.addAction(Intent.ACTION_SCREEN_OFF);
951 filter.addAction(Intent.ACTION_USER_PRESENT);
952 registerReceiver(mReceiver, filter);
953
Adam Cohend113e0c2010-11-11 10:48:05 -0800954 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700955 mVisible = true;
956 }
957
958 @Override
959 public void onDetachedFromWindow() {
960 super.onDetachedFromWindow();
961 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700962 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700963
Adam Cohend113e0c2010-11-11 10:48:05 -0800964 if (mAttached) {
965 unregisterReceiver(mReceiver);
966 mAttached = false;
967 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700968 updateRunning();
969 }
970
971 public void onWindowVisibilityChanged(int visibility) {
972 mVisible = visibility == View.VISIBLE;
973 updateRunning();
974 }
975
976 private void sendAdvanceMessage(long delay) {
977 mHandler.removeMessages(ADVANCE_MSG);
978 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
979 mHandler.sendMessageDelayed(msg, delay);
980 mAutoAdvanceSentTime = System.currentTimeMillis();
981 }
982
983 private void updateRunning() {
984 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
985 if (autoAdvanceRunning != mAutoAdvanceRunning) {
986 mAutoAdvanceRunning = autoAdvanceRunning;
987 if (autoAdvanceRunning) {
988 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
989 sendAdvanceMessage(delay);
990 } else {
991 if (!mWidgetsToAdvance.isEmpty()) {
992 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
993 (System.currentTimeMillis() - mAutoAdvanceSentTime));
994 }
995 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800996 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700997 }
998 }
999 }
1000
1001 private final Handler mHandler = new Handler() {
1002 @Override
1003 public void handleMessage(Message msg) {
1004 if (msg.what == ADVANCE_MSG) {
1005 int i = 0;
1006 for (View key: mWidgetsToAdvance.keySet()) {
1007 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1008 final int delay = mAdvanceStagger * i;
1009 if (v instanceof Advanceable) {
1010 postDelayed(new Runnable() {
1011 public void run() {
1012 ((Advanceable) v).advance();
1013 }
1014 }, delay);
1015 }
1016 i++;
1017 }
1018 sendAdvanceMessage(mAdvanceInterval);
1019 }
1020 }
1021 };
1022
1023 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001024 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001025 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1026 if (v instanceof Advanceable) {
1027 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001028 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001029 updateRunning();
1030 }
1031 }
1032
1033 void removeWidgetToAutoAdvance(View hostView) {
1034 if (mWidgetsToAdvance.containsKey(hostView)) {
1035 mWidgetsToAdvance.remove(hostView);
1036 updateRunning();
1037 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001038 }
1039
Joe Onorato9c1289c2009-08-17 11:03:03 -04001040 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001041 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001042 launcherInfo.hostView = null;
1043 }
1044
Adam Cohended9f8d2010-11-03 13:25:16 -07001045 void showOutOfSpaceMessage() {
1046 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1047 }
1048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 public LauncherAppWidgetHost getAppWidgetHost() {
1050 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Winson Chunga9abd0e2010-10-27 17:18:37 -07001053 public LauncherModel getModel() {
1054 return mModel;
1055 }
1056
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001057 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001058 getWindow().closeAllPanels();
1059
1060 try {
1061 dismissDialog(DIALOG_CREATE_SHORTCUT);
1062 // Unlock the workspace if the dialog was showing
1063 } catch (Exception e) {
1064 // An exception is thrown if the dialog is not visible, which is fine
1065 }
1066
1067 try {
1068 dismissDialog(DIALOG_RENAME_FOLDER);
1069 // Unlock the workspace if the dialog was showing
1070 } catch (Exception e) {
1071 // An exception is thrown if the dialog is not visible, which is fine
1072 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001073
1074 // Whatever we were doing is hereby canceled.
1075 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001076 }
1077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 @Override
1079 protected void onNewIntent(Intent intent) {
1080 super.onNewIntent(intent);
1081
1082 // Close the menu
1083 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001084 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001085 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001086
Adam Cohen95bb8002011-07-03 23:40:28 -07001087 closeFolder();
1088
Joe Onorato14f122b2009-11-19 14:06:36 -08001089 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1090 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001091
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001092 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001093 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001094 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001095 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001096 }
Winson Chungde1af762011-07-21 16:44:07 -07001097 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001098 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001099
Joe Onorato3a8820b2009-11-10 15:06:42 -08001100 final View v = getWindow().peekDecorView();
1101 if (v != null && v.getWindowToken() != null) {
1102 InputMethodManager imm = (InputMethodManager)getSystemService(
1103 INPUT_METHOD_SERVICE);
1104 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001106
Michael Jurka35aa14d2011-07-07 17:01:08 -07001107 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001108 if (mAppsCustomizeTabHost != null) {
1109 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112 }
1113
1114 @Override
1115 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1116 // Do not call super here
1117 mSavedInstanceState = savedInstanceState;
1118 }
1119
1120 @Override
1121 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001122 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001123 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124
Michael Jurka883f55b2010-10-21 15:47:14 -07001125 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001126 // We close any open folder since it will not be re-opened, and we need to make sure
1127 // this state is reflected.
1128 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1131 mWaitingForResult) {
1132 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1133 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1134 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1135 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137
1138 if (mFolderInfo != null && mWaitingForResult) {
1139 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1140 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1141 }
Michael Jurka946ad472010-07-09 18:05:18 -07001142
Winson Chung785d2eb2011-04-14 16:08:02 -07001143 // Save the current AppsCustomize tab
1144 if (mAppsCustomizeTabHost != null) {
1145 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1146 if (currentTabTag != null) {
1147 outState.putString("apps_customize_currentTab", currentTabTag);
1148 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001149 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1150 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 }
1153
1154 @Override
1155 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001157
Winson Chunge7a03942011-08-05 15:05:12 -07001158 // Remove all pending runnables
1159 mHandler.removeMessages(ADVANCE_MSG);
1160 mHandler.removeMessages(0);
1161
Winson Chungcd2b0142011-06-08 16:02:26 -07001162 // Stop callbacks from LauncherModel
1163 LauncherApplication app = ((LauncherApplication) getApplication());
1164 mModel.stopLoader();
1165 app.setLauncher(null);
1166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001168 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001170 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001172 mAppWidgetHost = null;
1173
1174 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
1176 TextKeyListener.getInstance().release();
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Winson Chung3d503fb2011-07-13 17:25:49 -07001179 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001180
1181 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001182 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001183
1184 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1185 mWorkspace.removeAllViews();
1186 mWorkspace = null;
1187 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001188
1189 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
Adam Cohena9cf38f2011-05-02 15:36:58 -07001192 public DragController getDragController() {
1193 return mDragController;
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 @Override
1197 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001198 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 super.startActivityForResult(intent, requestCode);
1200 }
1201
Winson Chung70d72102011-08-12 11:24:35 -07001202 /**
1203 * Indicates that we want global search for this activity by setting the globalSearch
1204 * argument for {@link #startSearch} to true.
1205 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001207 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001209
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001210 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Karl Rosaen138a0412009-04-23 19:00:21 -07001212 if (initialQuery == null) {
1213 // Use any text typed in the launcher as the initial query
1214 initialQuery = getTypedText();
1215 clearTypedText();
1216 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 if (appSearchData == null) {
1218 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001219 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
Romain Guycbb89e42009-06-08 15:52:54 -07001221
Karl Rosaen138a0412009-04-23 19:00:21 -07001222 final SearchManager searchManager =
1223 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001224 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001225 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 }
1227
Winson Chung70d72102011-08-12 11:24:35 -07001228 @Override
1229 public boolean onCreateOptionsMenu(Menu menu) {
1230 if (isWorkspaceLocked()) {
1231 return false;
1232 }
1233
1234 super.onCreateOptionsMenu(menu);
Winson Chung236d4312011-08-22 15:12:27 -07001235 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1236 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1237 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chung70d72102011-08-12 11:24:35 -07001238 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1239 .setIcon(android.R.drawable.ic_menu_gallery)
1240 .setAlphabeticShortcut('W');
1241 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1242 .setIcon(android.R.drawable.ic_menu_manage)
1243 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001244 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1245 .setIcon(android.R.drawable.ic_menu_preferences)
1246 .setIntent(settings)
1247 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001248 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1249 .setIcon(android.R.drawable.ic_menu_help)
1250 .setAlphabeticShortcut('H');
1251 return true;
1252 }
1253
1254 @Override
1255 public boolean onPrepareOptionsMenu(Menu menu) {
1256 super.onPrepareOptionsMenu(menu);
1257
1258 if (mAppsCustomizeTabHost.isTransitioning()) {
1259 return false;
1260 }
1261 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1262 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1263
1264 return true;
1265 }
1266
1267 @Override
1268 public boolean onOptionsItemSelected(MenuItem item) {
1269 switch (item.getItemId()) {
1270 case MENU_WALLPAPER_SETTINGS:
1271 startWallpaper();
1272 return true;
1273 case MENU_MANAGE_APPS:
1274 manageApps();
1275 return true;
1276 case MENU_HELP:
1277 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url))));
1278 return true;
1279 }
1280
1281 return super.onOptionsItemSelected(item);
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001284 @Override
1285 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001286 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001287 // Use a custom animation for launching search
1288 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001290 }
1291
Joe Onorato9c1289c2009-08-17 11:03:03 -04001292 public boolean isWorkspaceLocked() {
1293 return mWorkspaceLoading || mWaitingForResult;
1294 }
1295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001297 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001298 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 }
1300
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001302 mPendingAddInfo.container = ItemInfo.NO_ID;
1303 mPendingAddInfo.screen = -1;
1304 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1305 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1306 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001307 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001308
Winson Chung7ad01412010-09-27 11:33:03 -07001309 private void manageApps() {
1310 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1311 }
1312
Michael Jurkaaf442092010-06-10 17:01:57 -07001313 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001314 // TODO: catch bad widget exception when sent
1315 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001316 // TODO: Is this log message meaningful?
1317 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001318 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001319 }
1320
Winson Chung55cef262010-10-28 14:14:18 -07001321 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001322 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323
Bjorn Bringert7984c942009-12-09 15:38:25 +00001324 if (appWidget.configure != null) {
1325 // Launch over to configure widget, if needed
1326 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1327 intent.setComponent(appWidget.configure);
1328 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001329 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001330 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1331 intent.putExtra(
1332 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1333 info.mimeType);
1334
1335 final String mimeType = info.mimeType;
1336 final ClipData clipData = (ClipData) info.configurationData;
1337 final ClipDescription clipDesc = clipData.getDescription();
1338 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1339 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001340 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001341 final CharSequence stringData = item.getText();
1342 final Uri uriData = item.getUri();
1343 final Intent intentData = item.getIntent();
1344 final String key =
1345 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1346 if (uriData != null) {
1347 intent.putExtra(key, uriData);
1348 } else if (intentData != null) {
1349 intent.putExtra(key, intentData);
1350 } else if (stringData != null) {
1351 intent.putExtra(key, stringData);
1352 }
1353 break;
1354 }
1355 }
1356 }
Winson Chung55cef262010-10-28 14:14:18 -07001357 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001358
Romain Guy8e633c52010-03-04 12:51:36 -08001359 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001361 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001363
1364 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001365 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
Adam Cohenfbba09b2011-07-18 21:43:05 -07001369 /**
1370 * Process a shortcut drop.
1371 *
1372 * @param componentName The name of the component
1373 * @param screen The screen where it should be added
1374 * @param cell The cell it should be added to, optional
1375 * @param position The location on the screen where it was dropped, optional
1376 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1378 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 mPendingAddInfo.container = container;
1381 mPendingAddInfo.screen = screen;
1382 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001383
1384 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001385 mPendingAddInfo.cellX = cell[0];
1386 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001387 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388
1389 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1390 createShortcutIntent.setComponent(componentName);
1391 processShortcut(createShortcutIntent);
1392 }
1393
Adam Cohenfbba09b2011-07-18 21:43:05 -07001394 /**
1395 * Process a widget drop.
1396 *
1397 * @param info The PendingAppWidgetInfo of the widget being added.
1398 * @param screen The screen where it should be added
1399 * @param cell The cell it should be added to, optional
1400 * @param position The location on the screen where it was dropped, optional
1401 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1403 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001404 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 mPendingAddInfo.container = info.container = container;
1406 mPendingAddInfo.screen = info.screen = screen;
1407 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001408 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001409 mPendingAddInfo.cellX = cell[0];
1410 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001411 }
1412
1413 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1414 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1415 addAppWidgetImpl(appWidgetId, info);
1416 }
1417
Joe Onoratodeb98af2010-02-19 14:59:39 -08001418 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001419 // Handle case where user selected "Applications"
1420 String applicationName = getResources().getString(R.string.group_applications);
1421 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001423 if (applicationName != null && applicationName.equals(shortcutName)) {
1424 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1425 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001427 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1428 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001429 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001430 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001431 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001432 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 }
1435
Winson Chung24ab2f12010-09-16 14:10:47 -07001436 void processWallpaper(Intent intent) {
1437 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1438 }
1439
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1441 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001442 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 folderInfo.title = getText(R.string.folder_name);
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1447 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001448 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449
1450 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001451 FolderIcon newFolder =
1452 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1453 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1454 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001455 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
Romain Guycbb89e42009-06-08 15:52:54 -07001457
Joe Onorato9c1289c2009-08-17 11:03:03 -04001458 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001459 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001460 }
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 private void showNotifications() {
1463 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1464 if (statusBar != null) {
1465 statusBar.expand();
1466 }
1467 }
1468
1469 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001470 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001472 Intent chooser = Intent.createChooser(pickWallpaper,
1473 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001474 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1475 // Removed in Eclair MR1
1476// WallpaperManager wm = (WallpaperManager)
1477// getSystemService(Context.WALLPAPER_SERVICE);
1478// WallpaperInfo wi = wm.getWallpaperInfo();
1479// if (wi != null && wi.getSettingsActivity() != null) {
1480// LabeledIntent li = new LabeledIntent(getPackageName(),
1481// R.string.configure_wallpaper, 0);
1482// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1483// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1484// }
Mike Clerona0618e42009-10-22 13:55:21 -07001485 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 }
1487
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001488 /**
1489 * Registers various content observers. The current implementation registers
1490 * only a favorites observer to keep track of the favorites applications.
1491 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001492 private void registerContentObservers() {
1493 ContentResolver resolver = getContentResolver();
1494 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1495 true, mWidgetObserver);
1496 }
1497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 @Override
1499 public boolean dispatchKeyEvent(KeyEvent event) {
1500 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1501 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001503 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001504 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001505 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001506 dumpState();
1507 return true;
1508 }
1509 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001510 }
1511 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1512 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001513 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 return true;
1515 }
1516 }
1517
1518 return super.dispatchKeyEvent(event);
1519 }
1520
Joe Onorato88ec0992009-11-19 13:16:06 -08001521 @Override
1522 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001523 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001524 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001525 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001526 Folder openFolder = mWorkspace.getOpenFolder();
1527 if (openFolder.isEditingName()) {
1528 openFolder.dismissEditingName();
1529 } else {
1530 closeFolder();
1531 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001532 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001533 mWorkspace.exitWidgetResizeMode();
1534
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001535 // Back button is a no-op here, but give at least some feedback for the button press
1536 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001537 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001541 * Re-listen when widgets are reset.
1542 */
1543 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001544 if (mAppWidgetHost != null) {
1545 mAppWidgetHost.startListening();
1546 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001547 }
1548
1549 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1551 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001554 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 /**
1558 * Launches the intent referred by the clicked shortcut.
1559 *
1560 * @param v The view representing the clicked shortcut.
1561 */
1562 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001563 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1564 // view has detached (it's possible for this to happen if the view is removed mid touch).
1565 if (v.getWindowToken() == null) {
1566 return;
1567 }
1568
1569 if (mWorkspace.isSwitchingState()) {
1570 return;
1571 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001572
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001574 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001576 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001577 int[] pos = new int[2];
1578 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001579 intent.setSourceBounds(new Rect(pos[0], pos[1],
1580 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001581 boolean success = startActivitySafely(intent, tag);
1582
1583 if (success && v instanceof BubbleTextView) {
1584 mWaitingForResume = (BubbleTextView) v;
1585 mWaitingForResume.setStayPressed(true);
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001588 if (v instanceof FolderIcon) {
1589 FolderIcon fi = (FolderIcon) v;
1590 handleFolderClick(fi);
1591 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001592 } else if (v == mAllAppsButton) {
1593 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001594 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001595 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001596 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 }
1599 }
1600
Michael Jurka0e260592010-06-30 17:07:39 -07001601 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001602 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001603 // clicking anywhere on the workspace causes the customization drawer to slide down
1604 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001605 return false;
1606 }
1607
Michael Jurkaaf442092010-06-10 17:01:57 -07001608 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001609 * Event handler for the search button
1610 *
1611 * @param v The view that was clicked.
1612 */
1613 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001614 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001615 }
1616
1617 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001618 * Event handler for the voice button
1619 *
1620 * @param v The view that was clicked.
1621 */
1622 public void onClickVoiceButton(View v) {
1623 startVoiceSearch();
1624 }
1625
1626 private void startVoiceSearch() {
1627 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1628 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1629 startActivity(intent);
1630 }
1631
1632 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001633 * Event handler for the "grid" button that appears on the home screen, which
1634 * enters all apps mode.
1635 *
1636 * @param v The view that was clicked.
1637 */
1638 public void onClickAllAppsButton(View v) {
1639 showAllApps(true);
1640 }
1641
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001642 public void onClickAppMarketButton(View v) {
1643 if (mAppMarketIntent != null) {
1644 startActivitySafely(mAppMarketIntent, "app market");
1645 }
1646 }
1647
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001648 void startApplicationDetailsActivity(ComponentName componentName) {
1649 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001650 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1651 Uri.fromParts("package", packageName, null));
1652 startActivity(intent);
1653 }
1654
Patrick Dubroy5539af72010-09-07 15:22:01 -07001655 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1656 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1657 // System applications cannot be installed. For now, show a toast explaining that.
1658 // We may give them the option of disabling apps this way.
1659 int messageId = R.string.uninstall_system_app_text;
1660 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1661 } else {
1662 String packageName = appInfo.componentName.getPackageName();
1663 String className = appInfo.componentName.getClassName();
1664 Intent intent = new Intent(
1665 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1666 startActivity(intent);
1667 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001668 }
1669
Michael Jurkaddd62e92011-02-16 17:49:14 -08001670 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1672 try {
1673 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001674 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 } catch (ActivityNotFoundException e) {
1676 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001677 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 } catch (SecurityException e) {
1679 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001680 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001682 "or use the exported attribute for this activity. "
1683 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001685 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001687
Romain Guy8e633c52010-03-04 12:51:36 -08001688 void startActivityForResultSafely(Intent intent, int requestCode) {
1689 try {
1690 startActivityForResult(intent, requestCode);
1691 } catch (ActivityNotFoundException e) {
1692 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1693 } catch (SecurityException e) {
1694 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1695 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1696 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1697 "or use the exported attribute for this activity.", e);
1698 }
1699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700
Adam Cohena9cf38f2011-05-02 15:36:58 -07001701 private void handleFolderClick(FolderIcon folderIcon) {
1702 final FolderInfo info = folderIcon.mInfo;
1703 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 // Close any open folder
1705 closeFolder();
1706 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001707 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 } else {
1709 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001710 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 int folderScreen;
1712 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001713 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 // .. and close it
1715 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001716 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 // Close any folder open on the current screen
1718 closeFolder();
1719 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001720 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722 }
1723 }
1724 }
1725
Adam Cohen2801caf2011-05-13 20:57:39 -07001726 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001727 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001728 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1729 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1730 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1731
Adam Cohenc51934b2011-07-26 21:07:43 -07001732 FolderInfo info = (FolderInfo) fi.getTag();
1733 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1734 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001735 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1736 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001737 }
1738
Adam Cohen2801caf2011-05-13 20:57:39 -07001739 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1740 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1741 oa.start();
1742 }
1743
1744 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001745 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001746 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1747 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1748 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1749
Adam Cohenc51934b2011-07-26 21:07:43 -07001750 FolderInfo info = (FolderInfo) fi.getTag();
1751 CellLayout cl = null;
1752 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1753 cl = (CellLayout) fi.getParent().getParent();
1754 }
1755
1756 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001757 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1758 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001759 oa.addListener(new AnimatorListenerAdapter() {
1760 @Override
1761 public void onAnimationEnd(Animator animation) {
1762 if (layout != null) {
1763 layout.clearFolderLeaveBehind();
1764 }
1765 }
1766 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001767 oa.start();
1768 }
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001771 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 * is animated relative to the specified View. If the View is null, no animation
1773 * is played.
1774 *
1775 * @param folderInfo The FolderInfo describing the folder to open.
1776 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001777 public void openFolder(FolderIcon folderIcon) {
1778 Folder folder = folderIcon.mFolder;
1779 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780
Adam Cohen2801caf2011-05-13 20:57:39 -07001781 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001782 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783
Adam Cohen4554ee12011-08-03 16:13:21 -07001784 // Just verify that the folder hasn't already been added to the DragLayer.
1785 // There was a one-off crash where the folder had a parent already.
1786 if (folder.getParent() == null) {
1787 mDragLayer.addView(folder);
1788 mDragController.addDropTarget((DropTarget) folder);
1789 } else {
1790 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1791 folder.getParent() + ").");
1792 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001793 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001794 }
1795
1796 public void closeFolder() {
1797 Folder folder = mWorkspace.getOpenFolder();
1798 if (folder != null) {
1799 closeFolder(folder);
1800 }
1801 }
1802
1803 void closeFolder(Folder folder) {
1804 folder.getInfo().opened = false;
1805
1806 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1807 if (parent != null) {
1808 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1809 shrinkAndFadeInFolderIcon(fi);
1810 }
1811 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001815 if (mState != State.WORKSPACE) {
1816 return false;
1817 }
1818
Joe Onorato9c1289c2009-08-17 11:03:03 -04001819 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 return false;
1821 }
1822
1823 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001824 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 resetAddInfo();
1828 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001830 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 return true;
1832 }
1833
Winson Chung3d503fb2011-07-13 17:25:49 -07001834 // The hotseat touch handling does not go through Workspace, and we always allow long press
1835 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001837 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1838 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 if (itemUnderLongClick == null) {
1840 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001841 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1842 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001843 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001847 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
1849 }
1850 }
1851 return true;
1852 }
1853
Winson Chung3d503fb2011-07-13 17:25:49 -07001854 boolean isHotseatLayout(View layout) {
1855 return mHotseat != null && layout != null &&
1856 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1857 }
1858 Hotseat getHotseat() {
1859 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001860 }
1861
Winson Chung3d503fb2011-07-13 17:25:49 -07001862 /**
1863 * Returns the CellLayout of the specified container at the specified screen.
1864 */
1865 CellLayout getCellLayout(long container, int screen) {
1866 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1867 if (mHotseat != null) {
1868 return mHotseat.getLayout();
1869 } else {
1870 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001871 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 } else {
1873 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001874 }
1875 }
1876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 Workspace getWorkspace() {
1878 return mWorkspace;
1879 }
1880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 @Override
1882 protected Dialog onCreateDialog(int id) {
1883 switch (id) {
1884 case DIALOG_CREATE_SHORTCUT:
1885 return new CreateShortcut().createDialog();
1886 case DIALOG_RENAME_FOLDER:
1887 return new RenameFolder().createDialog();
1888 }
1889
1890 return super.onCreateDialog(id);
1891 }
1892
1893 @Override
1894 protected void onPrepareDialog(int id, Dialog dialog) {
1895 switch (id) {
1896 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 break;
1898 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001899 if (mFolderInfo != null) {
1900 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1901 final CharSequence text = mFolderInfo.title;
1902 input.setText(text);
1903 input.setSelection(0, text.length());
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 break;
1906 }
1907 }
1908
1909 void showRenameDialog(FolderInfo info) {
1910 mFolderInfo = info;
1911 mWaitingForResult = true;
1912 showDialog(DIALOG_RENAME_FOLDER);
1913 }
1914
Adam Cohenfbba09b2011-07-18 21:43:05 -07001915 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001916 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1918 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 mWaitingForResult = true;
1920 showDialog(DIALOG_CREATE_SHORTCUT);
1921 }
1922
1923 private class RenameFolder {
1924 private EditText mInput;
1925
1926 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1928 mInput = (EditText) layout.findViewById(R.id.folder_name);
1929
1930 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1931 builder.setIcon(0);
1932 builder.setTitle(getString(R.string.rename_folder_title));
1933 builder.setCancelable(true);
1934 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1935 public void onCancel(DialogInterface dialog) {
1936 cleanup();
1937 }
1938 });
1939 builder.setNegativeButton(getString(R.string.cancel_action),
1940 new Dialog.OnClickListener() {
1941 public void onClick(DialogInterface dialog, int which) {
1942 cleanup();
1943 }
1944 }
1945 );
1946 builder.setPositiveButton(getString(R.string.rename_action),
1947 new Dialog.OnClickListener() {
1948 public void onClick(DialogInterface dialog, int which) {
1949 changeFolderName();
1950 }
1951 }
1952 );
1953 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001954
1955 final AlertDialog dialog = builder.create();
1956 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1957 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001958 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001959 mInput.requestFocus();
1960 InputMethodManager inputManager = (InputMethodManager)
1961 getSystemService(Context.INPUT_METHOD_SERVICE);
1962 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001963 }
1964 });
1965
1966 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 }
1968
1969 private void changeFolderName() {
1970 final String name = mInput.getText().toString();
1971 if (!TextUtils.isEmpty(name)) {
1972 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001973 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 mFolderInfo.title = name;
1975 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1976
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001978 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001979 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 } else {
1981 final FolderIcon folderIcon = (FolderIcon)
1982 mWorkspace.getViewForTag(mFolderInfo);
1983 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 // TODO: At some point we'll probably want some version of setting
1985 // the text for a folder icon.
1986 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 getWorkspace().requestLayout();
1988 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001989 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001990 mWorkspaceLoading = true;
1991 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 }
1993 }
1994 }
1995 cleanup();
1996 }
1997
1998 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 dismissDialog(DIALOG_RENAME_FOLDER);
2000 mWaitingForResult = false;
2001 mFolderInfo = null;
2002 }
2003 }
2004
Daniel Sandler843e8602010-06-07 14:59:01 -04002005 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2006 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002007 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002008 }
2009
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002010 // AllAppsView.Watcher
2011 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002012 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002013 mWorkspace.setVisibility(View.GONE);
2014 }
2015 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002016
2017 /**
2018 * Helper method for the cameraZoomIn/cameraZoomOut animations
2019 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002020 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002021 * @param scaleFactor The scale factor used for the zoom
2022 */
2023 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2024 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002025
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002026 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002027 // Set pivotY so that at the starting zoom factor, the view is partially
2028 // visible. Modifying initialHeightFactor changes how much of the view is
2029 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002030 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002031 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002032 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002033 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002034 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002035 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002036 }
2037 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002038
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002039 /**
2040 * Zoom the camera out from the workspace to reveal 'toView'.
2041 * Assumes that the view to show is anchored at either the very top or very bottom
2042 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002043 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002044 */
Winson Chungc07918d2011-07-01 15:35:26 -07002045 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002046 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002047
Winson Chungf0ea4d32011-06-06 14:27:16 -07002048 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2049 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2050 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2051 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002052
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002053 setPivotsForZoom(toView, toState, scale);
2054
Michael Jurkad74c9842011-07-10 12:44:21 -07002055 // Shrink workspaces away if going to AppsCustomize from workspace
2056 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002057 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002058
2059 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002060 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002061 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002062 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2063 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002064 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002065 toView.setFastScaleX(a * scale + b * 1f);
2066 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002067 }
2068 });
Adam Cohen61033d32010-11-15 18:29:44 -08002069
Winson Chungf0ea4d32011-06-06 14:27:16 -07002070 toView.setVisibility(View.VISIBLE);
2071 toView.setFastAlpha(0f);
2072 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2073 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2074 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2075 public void onAnimationUpdate(float a, float b) {
2076 // don't need to invalidate because we do so above
2077 toView.setFastAlpha(a * 0f + b * 1f);
2078 }
2079 });
2080 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002081
Michael Jurkaabded662011-03-04 12:06:57 -08002082 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002083 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002084 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002085 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002086 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002087 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002088 // Prepare the position
2089 toView.setTranslationX(0.0f);
2090 toView.setTranslationY(0.0f);
2091 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002092 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002093 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002094 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002095 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002096 // If we don't set the final scale values here, if this animation is cancelled
2097 // it will have the wrong scale value and subsequent cameraPan animations will
2098 // not fix that
2099 toView.setScaleX(1.0f);
2100 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002101 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002102 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002103 }
Winson Chung32174c82011-07-19 15:47:55 -07002104
2105 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2106 // Hide the workspace scrollbar
2107 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002108 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002109 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002110 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002111 });
2112
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002113 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002114 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002115
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002116 if (mStateAnimation != null) mStateAnimation.cancel();
2117 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002118 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002119 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002120 } else {
2121 toView.setTranslationX(0.0f);
2122 toView.setTranslationY(0.0f);
2123 toView.setScaleX(1.0f);
2124 toView.setScaleY(1.0f);
2125 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002126 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002127 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002128 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2129 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002130
Winson Chungc07918d2011-07-01 15:35:26 -07002131 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2132 // Hide the workspace scrollbar
2133 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002134 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002135 }
Michael Jurkaabded662011-03-04 12:06:57 -08002136 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002137 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002138 }
2139
2140 /**
2141 * Zoom the camera back into the workspace, hiding 'fromView'.
2142 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002143 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002144 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002145 */
Winson Chungc07918d2011-07-01 15:35:26 -07002146 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002147 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002148
Winson Chungf0ea4d32011-06-06 14:27:16 -07002149 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2150 final float scaleFactor = (float)
2151 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2152 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002153
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002154 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002155
Michael Jurkad3ef3062010-11-23 16:23:58 -08002156 if (!springLoaded) {
2157 mWorkspace.unshrink(animated);
2158 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002159 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002160 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002161 if (mStateAnimation != null) mStateAnimation.cancel();
2162 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002163
Michael Jurka742574b2011-02-02 23:51:01 -08002164 final float oldScaleX = fromView.getScaleX();
2165 final float oldScaleY = fromView.getScaleY();
2166
2167 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2168 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002169 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2170 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002171 ((View)fromView.getParent()).fastInvalidate();
2172 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2173 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2174 }
2175 });
Michael Jurkaabded662011-03-04 12:06:57 -08002176 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002177 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002178 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002179 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2180 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002181 // don't need to invalidate because we do so above
2182 fromView.setFastAlpha(a * 1f + b * 0f);
2183 }
2184 });
Michael Jurkaabded662011-03-04 12:06:57 -08002185 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002186 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002187 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002188 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002189 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002190 public void onAnimationStart(android.animation.Animator animation) {
2191 if (!springLoaded) {
2192 mWorkspace.showDockDivider(false);
2193 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002194 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002195 }
2196 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002197 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002198 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002199 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002200 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002201 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002202 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002203 }
2204 });
2205
Winson Chungf0ea4d32011-06-06 14:27:16 -07002206 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002207 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002208 } else {
2209 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002210 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002211 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2212 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002213
Winson Chungc07918d2011-07-01 15:35:26 -07002214 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002215 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002216 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002217 mWorkspace.flashScrollingIndicator();
2218 }
Michael Jurkaabded662011-03-04 12:06:57 -08002219 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002220 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002221 }
2222
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002223 void showWorkspace(boolean animated) {
2224 showWorkspace(animated, null);
2225 }
2226
2227 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002228 if (layout != null) {
2229 // always animated, but that's ok since we never specify a layout and
2230 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002231 mWorkspace.unshrink(layout);
2232 } else {
2233 mWorkspace.unshrink(animated);
2234 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002235 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002236 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002237 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002238
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002239 // Change the state *after* we've called all the transition code
2240 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002241
Winson Chung5fb63472011-02-02 17:03:37 -08002242 // Resume the auto-advance of widgets
2243 mUserPresent = true;
2244 updateRunning();
2245
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002246 // send an accessibility event to announce the context change
2247 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002248 }
2249
Michael Jurkad3ef3062010-11-23 16:23:58 -08002250 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002251 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002252 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002253 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002254 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002255 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002256 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002257 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002258 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002259 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002260 @Override
2261 public void run() {
2262 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002263
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002264 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002265 // Before we show workspace, hide all apps again because
2266 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2267 // clean up our state transition functions
2268 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002269 showWorkspace(true);
2270 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002271 }
2272 }, (extendedDelay ?
2273 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2274 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2275 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002276 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002277 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002278 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002279 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2280 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002281 }
2282 // Otherwise, we are not in spring loaded mode, so don't do anything.
2283 }
2284
Adam Cohenfc53cd22011-07-20 15:45:11 -07002285 public boolean isAllAppsCustomizeOpen() {
2286 return mState == State.APPS_CUSTOMIZE;
2287 }
2288
Winson Chungf0ea4d32011-06-06 14:27:16 -07002289 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002291 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002293 if (!LauncherApplication.isScreenLarge()) {
2294 if (animated) {
2295 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 }
2300 }
2301 }
2302
2303 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002306 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002307 if (!LauncherApplication.isScreenLarge()) {
2308 if (animated) {
2309 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002311 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002313 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002314 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002315 }
2316
Winson Chung785d2eb2011-04-14 16:08:02 -07002317 void showAllApps(boolean animated) {
2318 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002319
Winson Chungf0ea4d32011-06-06 14:27:16 -07002320 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2321 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002322
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002325
Winson Chungf0ea4d32011-06-06 14:27:16 -07002326 // Change the state *after* we've called all the transition code
2327 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002328
2329 // Pause the auto-advance of widgets until we are out of AllApps
2330 mUserPresent = false;
2331 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002332 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002333
2334 // Send an accessibility event to announce the context change
2335 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2336 }
2337
Joe Onoratob2061212009-11-24 16:13:54 -05002338 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002339 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002340 * - Home from workspace
2341 * - from center screen
2342 * - from other screens
2343 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002344 * - from center screen
2345 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002346 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002347 * - from center screen
2348 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002349 * - Launch app from workspace and quit
2350 * - with back
2351 * - with home
2352 * - Launch app from all apps and quit
2353 * - with back
2354 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002355 * - Go to a screen that's not the default, then all
2356 * apps, and launch and app, and go back
2357 * - with back
2358 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002359 * - On workspace, long press power and go back
2360 * - with back
2361 * - with home
2362 * - On all apps, long press power and go back
2363 * - with back
2364 * - with home
2365 * - On workspace, power off
2366 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002367 * - Launch an app and turn off the screen while in that app
2368 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002369 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002370 * - From all apps
2371 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002372 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2373 * - From all apps
2374 * - From the center workspace
2375 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002376 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002377 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002378 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2379 mWorkspace.setVisibility(View.VISIBLE);
2380 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002381
Winson Chung3d503fb2011-07-13 17:25:49 -07002382 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002383 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002384
Winson Chungf0ea4d32011-06-06 14:27:16 -07002385 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 if (mAllAppsButton != null) {
2387 mAllAppsButton.requestFocus();
2388 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002389 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002390 }
2391
Joe Onorato7c312c12009-08-13 21:36:53 -07002392 void lockAllApps() {
2393 // TODO
2394 }
2395
2396 void unlockAllApps() {
2397 // TODO
2398 }
2399
Patrick Dubroy5f445422011-02-18 14:35:21 -08002400 /**
2401 * Add an item from all apps or customize onto the given workspace screen.
2402 * If layout is null, add to the current screen.
2403 */
2404 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002405 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2406 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002407 } else {
2408 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002409 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002410 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002411
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002412 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002413 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002414 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002415 // Look for the toolbar icon specified in the activity meta-data
2416 Bundle metaData = packageManager.getActivityInfo(
2417 activityName, PackageManager.GET_META_DATA).metaData;
2418 if (metaData != null) {
2419 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2420 if (iconResId != 0) {
2421 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002422 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002423 }
2424 }
2425 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002426 // This can happen if the activity defines an invalid drawable
2427 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2428 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002429 } catch (Resources.NotFoundException nfe) {
2430 // This can happen if the activity defines an invalid drawable
2431 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2432 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002433 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002434 return null;
2435 }
2436
2437 // if successful in getting icon, return it; otherwise, set button to use default drawable
2438 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2439 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2440 TextView button = (TextView) findViewById(buttonId);
2441 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2442
2443 // If we were unable to find the icon via the meta-data, use a generic one
2444 if (toolbarIcon == null) {
2445 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2446 return null;
2447 } else {
2448 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2449 return toolbarIcon.getConstantState();
2450 }
2451 }
2452
2453 // if successful in getting icon, return it; otherwise, set button to use default drawable
2454 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2455 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2456 ImageView button = (ImageView) findViewById(buttonId);
2457 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2458
Michael Jurka19e0fc52011-07-22 18:00:21 -07002459 if (button != null) {
2460 // If we were unable to find the icon via the meta-data, use a
2461 // generic one
2462 if (toolbarIcon == null) {
2463 button.setImageResource(fallbackDrawableId);
2464 } else {
2465 button.setImageDrawable(toolbarIcon);
2466 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002467 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002468
2469 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2470
Michael Jurka0423dcf2010-10-05 14:56:18 -07002471 }
2472
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002473 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2474 TextView button = (TextView) findViewById(buttonId);
2475 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2476 }
2477
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002478 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002479 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002480 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002481 }
2482
Michael Jurka0423dcf2010-10-05 14:56:18 -07002483 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002484 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2485 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002486
Winson Chung1cad91e2011-05-25 17:41:01 -07002487 final SearchManager searchManager =
2488 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2489 ComponentName activityName = searchManager.getGlobalSearchActivity();
2490 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002491 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002492 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002493 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002494 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002495 } else {
2496 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002497 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002498 }
2499 }
2500
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002501 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002502 updateButtonWithDrawable(R.id.search_button, d);
2503 }
2504
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002505 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002506 final View searchDivider = findViewById(R.id.search_divider);
2507 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002508
Winson Chung1cad91e2011-05-25 17:41:01 -07002509 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2510 ComponentName activityName = intent.resolveActivity(getPackageManager());
2511 if (activityName != null) {
2512 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002513 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2514 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002515 voiceButton.setVisibility(View.VISIBLE);
2516 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002517 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002518 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002519 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002520 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002521
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002522 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002523 updateButtonWithDrawable(R.id.voice_button, d);
2524 }
2525
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002526 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002527 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002528 */
2529 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002530 final View marketButton = findViewById(R.id.market_button);
2531 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2532 // Find the app market activity by resolving an intent.
2533 // (If multiple app markets are installed, it will return the ResolverActivity.)
2534 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002535 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002536 mAppMarketIntent = intent;
2537 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002538 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002539 marketButton.setVisibility(View.VISIBLE);
2540 } else {
2541 // We should hide and disable the view so that we don't try and restore the visibility
2542 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2543 marketButton.setVisibility(View.GONE);
2544 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002545 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002546 }
2547
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002548 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002549 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002550 }
2551
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002552 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 * Displays the shortcut creation dialog and launches, if necessary, the
2554 * appropriate activity.
2555 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002556 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002557 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2558 DialogInterface.OnShowListener {
2559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002564
Winson Chung55b65502011-05-26 12:03:43 -07002565 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2566 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002567 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 AlertDialog dialog = builder.create();
2570 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002571 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002572 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 return dialog;
2575 }
2576
2577 public void onCancel(DialogInterface dialog) {
2578 mWaitingForResult = false;
2579 cleanup();
2580 }
2581
Romain Guycbb89e42009-06-08 15:52:54 -07002582 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002583 mWaitingForResult = false;
2584 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002585 }
2586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002587 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002588 try {
2589 dismissDialog(DIALOG_CREATE_SHORTCUT);
2590 } catch (Exception e) {
2591 // An exception is thrown if the dialog is not visible, which is fine
2592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 }
2594
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002595 /**
2596 * Handle the action clicked in the "Add to home" dialog.
2597 */
2598 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002599 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002600
Winson Chung55b65502011-05-26 12:03:43 -07002601 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2602 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002603 case AddAdapter.ITEM_APPLICATION: {
2604 if (mAppsCustomizeTabHost != null) {
2605 mAppsCustomizeTabHost.selectAppsTab();
2606 }
2607 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002608 break;
2609 }
Winson Chung55b65502011-05-26 12:03:43 -07002610 case AddAdapter.ITEM_APPWIDGET: {
2611 if (mAppsCustomizeTabHost != null) {
2612 mAppsCustomizeTabHost.selectWidgetsTab();
2613 }
2614 showAllApps(true);
2615 break;
2616 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002617 case AddAdapter.ITEM_WALLPAPER: {
2618 startWallpaper();
2619 break;
2620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 }
2622 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002623
2624 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002625 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002626 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002627 }
2628
2629 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002630 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002631 */
2632 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2633 @Override
2634 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002635 closeSystemDialogs();
2636 }
2637 }
2638
2639 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002640 * Receives notifications whenever the appwidgets are reset.
2641 */
2642 private class AppWidgetResetObserver extends ContentObserver {
2643 public AppWidgetResetObserver() {
2644 super(new Handler());
2645 }
2646
2647 @Override
2648 public void onChange(boolean selfChange) {
2649 onAppWidgetReset();
2650 }
2651 }
2652
2653 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002654 * If the activity is currently paused, signal that we need to re-run the loader
2655 * in onResume.
2656 *
2657 * This needs to be called from incoming places where resources might have been loaded
2658 * while we are paused. That is becaues the Configuration might be wrong
2659 * when we're not running, and if it comes back to what it was when we
2660 * were paused, we are not restarted.
2661 *
2662 * Implementation of the method from LauncherModel.Callbacks.
2663 *
2664 * @return true if we are currently paused. The caller might be able to
2665 * skip some work in that case since we will come back again.
2666 */
2667 public boolean setLoadOnResume() {
2668 if (mPaused) {
2669 Log.i(TAG, "setLoadOnResume");
2670 mOnResumeNeedsLoad = true;
2671 return true;
2672 } else {
2673 return false;
2674 }
2675 }
2676
2677 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002680 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002681 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002682 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002683 } else {
2684 return SCREEN_COUNT / 2;
2685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002686 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002687
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002688
Joe Onorato9c1289c2009-08-17 11:03:03 -04002689 /**
2690 * Refreshes the shortcuts shown on the workspace.
2691 *
2692 * Implementation of the method from LauncherModel.Callbacks.
2693 */
2694 public void startBinding() {
2695 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002696
2697 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002698 int count = workspace.getChildCount();
2699 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002700 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002701 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2702 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002703 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002704 if (mHotseat != null) {
2705 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002707
Adam Cohen4eac29a2011-07-11 17:53:37 -07002708 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002709 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002710 }
2711
2712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 * Bind the items start-end from the list.
2714 *
2715 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002717 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002718 setLoadOnResume();
2719
Joe Onorato9c1289c2009-08-17 11:03:03 -04002720 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002721 for (int i=start; i<end; i++) {
2722 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002723
2724 // Short circuit if we are loading dock items for a configuration which has no dock
2725 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2726 mHotseat == null) {
2727 continue;
2728 }
2729
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 switch (item.itemType) {
2731 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2732 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002733 View shortcut = createShortcut((ShortcutInfo)item);
2734 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2735 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002736 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002737 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002738 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002739 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002740 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002741 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2742 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002743 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002745 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 }
2748
Joe Onorato9c1289c2009-08-17 11:03:03 -04002749 /**
2750 * Implementation of the method from LauncherModel.Callbacks.
2751 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002752 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002753 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002754 sFolders.clear();
2755 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002757
2758 /**
2759 * Add the views for a widget to the workspace.
2760 *
2761 * Implementation of the method from LauncherModel.Callbacks.
2762 */
2763 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002764 setLoadOnResume();
2765
Daniel Sandler843e8602010-06-07 14:59:01 -04002766 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2767 if (DEBUG_WIDGETS) {
2768 Log.d(TAG, "bindAppWidget: " + item);
2769 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 final Workspace workspace = mWorkspace;
2771
2772 final int appWidgetId = item.appWidgetId;
2773 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002774 if (DEBUG_WIDGETS) {
2775 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2776 }
2777
Joe Onorato9c1289c2009-08-17 11:03:03 -04002778 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2779
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2781 item.hostView.setTag(item);
2782
Winson Chung3d503fb2011-07-13 17:25:49 -07002783 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002784 item.cellY, item.spanX, item.spanY, false);
2785
Adam Cohended9f8d2010-11-03 13:25:16 -07002786 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2787
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 workspace.requestLayout();
2789
Daniel Sandler843e8602010-06-07 14:59:01 -04002790 if (DEBUG_WIDGETS) {
2791 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2792 + (SystemClock.uptimeMillis()-start) + "ms");
2793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 }
2795
2796 /**
2797 * Callback saying that there aren't any more items to bind.
2798 *
2799 * Implementation of the method from LauncherModel.Callbacks.
2800 */
2801 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002802 setLoadOnResume();
2803
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 if (mSavedState != null) {
2805 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002806 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 mSavedState = null;
2809 }
2810
2811 if (mSavedInstanceState != null) {
2812 super.onRestoreInstanceState(mSavedInstanceState);
2813 mSavedInstanceState = null;
2814 }
2815
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002817
2818 // If we received the result of any pending adds while the loader was running (e.g. the
2819 // widget configuration forced an orientation change), process them now.
2820 for (int i = 0; i < sPendingAddList.size(); i++) {
2821 completeAdd(sPendingAddList.get(i));
2822 }
2823 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 }
2825
2826 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002827 * Updates the icons on the launcher that are affected by changes to the package list
2828 * on the device.
2829 */
2830 private void updateIconsAffectedByPackageManagerChanges() {
2831 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002832 updateVoiceSearchIcon();
2833 }
2834
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002835 @Override
2836 public void bindSearchablesChanged() {
2837 updateGlobalSearchIcon();
2838 }
2839
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002840 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841 * Add the icons for all apps.
2842 *
2843 * Implementation of the method from LauncherModel.Callbacks.
2844 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002845 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2846 // Remove the progress bar entirely; we could also make it GONE
2847 // but better to remove it since we know it's not going to be used
2848 View progressBar = mAppsCustomizeTabHost.
2849 findViewById(R.id.apps_customize_progress_bar);
2850 if (progressBar != null) {
2851 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002852 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002853 // We just post the call to setApps so the user sees the progress bar
2854 // disappear-- otherwise, it just looks like the progress bar froze
2855 // which doesn't look great
2856 mAppsCustomizeTabHost.post(new Runnable() {
2857 public void run() {
2858 if (mAppsCustomizeContent != null) {
2859 mAppsCustomizeContent.setApps(apps);
2860 }
2861 }
2862 });
2863
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002864 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002865 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 }
2867
2868 /**
2869 * A package was installed.
2870 *
2871 * Implementation of the method from LauncherModel.Callbacks.
2872 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002873 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002874 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876
Winson Chung785d2eb2011-04-14 16:08:02 -07002877 if (mAppsCustomizeContent != null) {
2878 mAppsCustomizeContent.addApps(apps);
2879 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002880 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 }
2882
2883 /**
2884 * A package was updated.
2885 *
2886 * Implementation of the method from LauncherModel.Callbacks.
2887 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002888 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002889 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002891 if (mWorkspace != null) {
2892 mWorkspace.updateShortcuts(apps);
2893 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002894
Winson Chung785d2eb2011-04-14 16:08:02 -07002895 if (mAppsCustomizeContent != null) {
2896 mAppsCustomizeContent.updateApps(apps);
2897 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002898 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 }
2900
2901 /**
2902 * A package was uninstalled.
2903 *
2904 * Implementation of the method from LauncherModel.Callbacks.
2905 */
Joe Onorato36115782010-06-17 13:28:48 -04002906 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002908 if (permanent) {
2909 mWorkspace.removeItems(apps);
2910 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002911
Winson Chung785d2eb2011-04-14 16:08:02 -07002912 if (mAppsCustomizeContent != null) {
2913 mAppsCustomizeContent.removeApps(apps);
2914 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002915 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 }
Joe Onoratobe386092009-11-17 17:32:16 -08002917
2918 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002919 * A number of packages were updated.
2920 */
2921 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002922
Winson Chung785d2eb2011-04-14 16:08:02 -07002923 if (mAppsCustomizeContent != null) {
2924 mAppsCustomizeContent.onPackagesUpdated();
2925 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002926 }
2927
Winson Chung400438b2011-01-16 17:53:48 -08002928 private int mapConfigurationOriActivityInfoOri(int configOri) {
2929 final Display d = getWindowManager().getDefaultDisplay();
2930 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2931 switch (d.getRotation()) {
2932 case Surface.ROTATION_0:
2933 case Surface.ROTATION_180:
2934 // We are currently in the same basic orientation as the natural orientation
2935 naturalOri = configOri;
2936 break;
2937 case Surface.ROTATION_90:
2938 case Surface.ROTATION_270:
2939 // We are currently in the other basic orientation to the natural orientation
2940 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2941 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2942 break;
2943 }
2944
2945 int[] oriMap = {
2946 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2947 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2948 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2949 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2950 };
2951 // Since the map starts at portrait, we need to offset if this device's natural orientation
2952 // is landscape.
2953 int indexOffset = 0;
2954 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2955 indexOffset = 1;
2956 }
2957 return oriMap[(d.getRotation() + indexOffset) % 4];
2958 }
2959 public void lockScreenOrientation() {
2960 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2961 .getConfiguration().orientation));
2962 }
2963 public void unlockScreenOrientation() {
2964 mHandler.postDelayed(new Runnable() {
2965 public void run() {
2966 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2967 }
2968 }, mRestoreScreenOrientationDelay);
2969 }
2970
Winson Chung80baf5a2010-08-09 16:03:15 -07002971 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002972 * Prints out out state for debugging.
2973 */
2974 public void dumpState() {
2975 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002976 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002977 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2978 Log.d(TAG, "mRestoring=" + mRestoring);
2979 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2980 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002981 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002982 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002983
Winson Chung785d2eb2011-04-14 16:08:02 -07002984 if (mAppsCustomizeContent != null) {
2985 mAppsCustomizeContent.dumpState();
2986 }
Joe Onoratobe386092009-11-17 17:32:16 -08002987 Log.d(TAG, "END launcher2 dump state");
2988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989}
Michael Jurka2763be32011-02-24 11:19:57 -08002990
Michael Jurkaabded662011-03-04 12:06:57 -08002991interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002992 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2993 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002994}