blob: 400c8813a22fbb2a4afbd9d497fb33b96295cd20 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040052import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
Adam Cohen228da5a2011-07-27 22:23:47 -070054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040059import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080060import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070061import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080062import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070077import android.view.ViewGroup;
Adam Cohen860f4c52011-01-27 20:16:13 -080078import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080079import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080080import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070082import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070084import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070085import android.widget.TextView;
86import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070087
Adam Cohendf2cc412011-04-27 16:56:57 -070088import com.android.common.Search;
89import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -080090
Adam Cohenc0dcf592011-06-01 15:30:43 -070091import java.io.DataInputStream;
92import java.io.DataOutputStream;
93import java.io.FileNotFoundException;
94import java.io.IOException;
95import java.util.ArrayList;
96import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098/**
99 * Default launcher application.
100 */
Michael Jurka946ad472010-07-09 18:05:18 -0700101public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700102 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
103 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700105 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400108 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800111 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700114 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
115 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
117 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700118 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato7c312c12009-08-13 21:36:53 -0700132 static final int DIALOG_CREATE_SHORTCUT = 1;
133 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Romain Guy98d01652009-06-30 16:21:04 -0700135 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 // Type: int
138 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700139 // Type: int
140 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700142 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
143 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
145 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700154 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
155
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700156 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700158 private State mState = State.WORKSPACE;
159 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700162 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
163 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800168 private final BroadcastReceiver mCloseSystemDialogsReceiver
169 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800170 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private LayoutInflater mInflater;
173
Joe Onorato00acb122009-08-04 16:04:30 -0400174 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private AppWidgetManager mAppWidgetManager;
178 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700181 private int[] mTmpAddItemCellCoordinates = new int[2];
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private FolderInfo mFolderInfo;
184
Winson Chung3d503fb2011-07-13 17:25:49 -0700185 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800186 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187
Winson Chungf0ea4d32011-06-06 14:27:16 -0700188 private SearchDropTargetBar mSearchDeleteBar;
189 private AppsCustomizeTabHost mAppsCustomizeTabHost;
190 private AppsCustomizePagedView mAppsCustomizeContent;
191 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
204 private Bundle mSavedInstanceState;
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800208 private boolean mUserPresent = true;
209 private boolean mVisible = false;
210 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700212 private static LocaleConfiguration sLocaleConfiguration = null;
213
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700214 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700216 private Intent mAppMarketIntent = null;
217
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 // Related to the auto-advancing of widgets
219 private final int ADVANCE_MSG = 1;
220 private final int mAdvanceInterval = 20000;
221 private final int mAdvanceStagger = 250;
222 private long mAutoAdvanceSentTime;
223 private long mAutoAdvanceTimeLeft = -1;
224 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
225 new HashMap<View, AppWidgetProviderInfo>();
226
Winson Chung400438b2011-01-16 17:53:48 -0800227 // Determines how long to wait after a rotation before restoring the screen orientation to
228 // match the sensor state.
229 private final int mRestoreScreenOrientationDelay = 500;
230
Michael Jurka4ef207b2010-11-29 17:05:45 -0800231 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800232 private static Drawable.ConstantState sGlobalSearchIcon;
233 private static Drawable.ConstantState sVoiceSearchIcon;
234 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800235
Adam Cohen2801caf2011-05-13 20:57:39 -0700236 private DragLayer mDragLayer;
237
Michael Jurkaddd62e92011-02-16 17:49:14 -0800238 private BubbleTextView mWaitingForResume;
239
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800240 private static ArrayList<PendingAddArguments> sPendingAddList
241 = new ArrayList<PendingAddArguments>();
242
243 private static class PendingAddArguments {
244 int requestCode;
245 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800247 int screen;
248 int cellX;
249 int cellY;
250 }
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 @Override
253 protected void onCreate(Bundle savedInstanceState) {
254 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 LauncherApplication app = ((LauncherApplication)getApplication());
256 mModel = app.setLauncher(this);
257 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400258 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
263 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200266 android.os.Debug.startMethodTracing(
267 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
270 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
272 setupViews();
273
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800274 registerContentObservers();
275
Joe Onorato7c312c12009-08-13 21:36:53 -0700276 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 mSavedState = savedInstanceState;
279 restoreState(mSavedState);
280
Winson Chunga12a2502010-12-20 14:41:35 -0800281 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700282 if (mAppsCustomizeContent != null) {
283 mAppsCustomizeContent.onPackagesUpdated();
284 }
Winson Chunga12a2502010-12-20 14:41:35 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 if (PROFILE_STARTUP) {
287 android.os.Debug.stopMethodTracing();
288 }
289
290 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
294 // 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) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800439 switch (args.requestCode) {
440 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700441 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
442 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800443 break;
444 case REQUEST_PICK_SHORTCUT:
445 processShortcut(args.intent);
446 break;
447 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700448 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
449 args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700450 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800451 case REQUEST_PICK_APPWIDGET:
452 addAppWidgetFromPick(args.intent);
453 break;
454 case REQUEST_CREATE_APPWIDGET:
455 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700456 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700457 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800458 case REQUEST_PICK_WALLPAPER:
459 // We just wanted the activity result here so we can clear mWaitingForResult
460 break;
461 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700462 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800463 }
464
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800466 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700467 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700468 mWaitingForResult = false;
469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 // The pattern used here is that a user PICKs a specific application,
471 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
474 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700475 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
476 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800477 final PendingAddArguments args = new PendingAddArguments();
478 args.requestCode = requestCode;
479 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700480 args.container = mPendingAddInfo.container;
481 args.screen = mPendingAddInfo.screen;
482 args.cellX = mPendingAddInfo.cellX;
483 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800484
485 // If the loader is still running, defer the add until it is done.
486 if (isWorkspaceLocked()) {
487 sPendingAddList.add(args);
488 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700489 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
Romain Guy18042c82009-11-06 11:44:55 -0800491 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700492 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
493 if (data != null) {
494 // Clean up the appWidgetId if we canceled
495 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
496 if (appWidgetId != -1) {
497 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
500 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700501
502 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
503 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
505
506 @Override
507 protected void onResume() {
508 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800509 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700510 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400511 mWorkspaceLoading = true;
512 mModel.startLoader(this, true);
513 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700514 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800516 if (mWaitingForResume != null) {
517 mWaitingForResume.setStayPressed(false);
518 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700519 // When we resume Launcher, a different Activity might be responsible for the app
520 // market intent, so refresh the icon
521 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 }
523
524 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700525 protected void onPause() {
526 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700527 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800528 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700529 }
Romain Guycbb89e42009-06-08 15:52:54 -0700530
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700531 @Override
532 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400533 // Flag the loader to stop early before switching
534 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700535 if (mAppsCustomizeContent != null) {
536 mAppsCustomizeContent.surrender();
537 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800538 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700539 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800541 // We can't hide the IME if it was forced open. So don't bother
542 /*
543 @Override
544 public void onWindowFocusChanged(boolean hasFocus) {
545 super.onWindowFocusChanged(hasFocus);
546
547 if (hasFocus) {
548 final InputMethodManager inputManager = (InputMethodManager)
549 getSystemService(Context.INPUT_METHOD_SERVICE);
550 WindowManager.LayoutParams lp = getWindow().getAttributes();
551 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
552 android.os.Handler()) {
553 protected void onReceiveResult(int resultCode, Bundle resultData) {
554 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
555 }
556 });
557 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
558 }
559 }
560 */
561
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 private boolean acceptFilter() {
563 final InputMethodManager inputManager = (InputMethodManager)
564 getSystemService(Context.INPUT_METHOD_SERVICE);
565 return !inputManager.isFullscreenMode();
566 }
567
568 @Override
569 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700570 final int uniChar = event.getUnicodeChar();
571 final boolean handled = super.onKeyDown(keyCode, event);
572 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
573 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
575 keyCode, event);
576 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700577 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700578 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700579 // showSearchDialog()
580 // If there are multiple keystrokes before the search dialog takes focus,
581 // onSearchRequested() will be called for every keystroke,
582 // but it is idempotent, so it's fine.
583 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 }
585 }
586
Joe Onorato8a9625e2010-01-28 15:55:35 -0800587 // Eat the long press event so the keyboard doesn't come up.
588 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
589 return true;
590 }
591
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 return handled;
593 }
594
Karl Rosaen138a0412009-04-23 19:00:21 -0700595 private String getTypedText() {
596 return mDefaultKeySsb.toString();
597 }
598
599 private void clearTypedText() {
600 mDefaultKeySsb.clear();
601 mDefaultKeySsb.clearSpans();
602 Selection.setSelection(mDefaultKeySsb, 0);
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700606 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
607 * State
608 */
609 private static State intToState(int stateOrdinal) {
610 State state = State.WORKSPACE;
611 final State[] stateValues = State.values();
612 for (int i = 0; i < stateValues.length; i++) {
613 if (stateValues[i].ordinal() == stateOrdinal) {
614 state = stateValues[i];
615 break;
616 }
617 }
618 return state;
619 }
620
621 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 * Restores the previous state, if it exists.
623 *
624 * @param savedState The previous state.
625 */
626 private void restoreState(Bundle savedState) {
627 if (savedState == null) {
628 return;
629 }
630
Michael Jurka883f55b2010-10-21 15:47:14 -0700631 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
632
Winson Chungf0ea4d32011-06-06 14:27:16 -0700633 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800634 showAllApps(false);
635 }
636
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
638 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700639 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
641
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
643 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700644
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
646 mPendingAddInfo.container = pendingAddContainer;
647 mPendingAddInfo.screen = pendingAddScreen;
648 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
649 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 mRestoring = true;
651 }
652
653 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
654 if (renameFolder) {
655 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700656 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 mRestoring = true;
658 }
Winson Chunga12a2502010-12-20 14:41:35 -0800659
Winson Chung785d2eb2011-04-14 16:08:02 -0700660
661 // Restore the AppsCustomize tab
662 if (mAppsCustomizeTabHost != null) {
663 String curTab = savedState.getString("apps_customize_currentTab");
664 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700665 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700666 mAppsCustomizeContent.setContentType(
667 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
668 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
669 }
670
671 // Note: currently we do not restore the page for the AppsCustomize pane because the
672 // change in layout can drastically affect the saved page index
673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
675
676 /**
677 * Finds all the views we need and configure them properly.
678 */
679 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700680 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400681
Winson Chung4c98d922011-05-31 16:50:48 -0700682 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
683 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684
Winson Chung4c98d922011-05-31 16:50:48 -0700685 // Setup the drag layer
686 mDragLayer.setup(this, dragController);
687
Winson Chung3d503fb2011-07-13 17:25:49 -0700688 // Setup the hotseat
689 mHotseat = (Hotseat) findViewById(R.id.hotseat);
690 if (mHotseat != null) {
691 mHotseat.setup(this);
692 }
693
Winson Chung4c98d922011-05-31 16:50:48 -0700694 // Setup the workspace
695 mWorkspace.setHapticFeedbackEnabled(false);
696 mWorkspace.setOnLongClickListener(this);
697 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700698 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700699
Winson Chungf0ea4d32011-06-06 14:27:16 -0700700 // Get the search/delete bar
701 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700702
Winson Chungf0ea4d32011-06-06 14:27:16 -0700703 // Setup AppsCustomize
704 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
705 findViewById(R.id.apps_customize_pane);
706 mAppsCustomizeContent = (AppsCustomizePagedView)
707 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
708 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400709
Romain Guy1fbc1c82009-11-09 20:43:08 -0800710
Winson Chung4c98d922011-05-31 16:50:48 -0700711
Winson Chung3d503fb2011-07-13 17:25:49 -0700712 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700713 dragController.setDragScoller(mWorkspace);
714 dragController.setScrollView(mDragLayer);
715 dragController.setMoveTarget(mWorkspace);
716 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700717 if (mSearchDeleteBar != null) {
718 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 }
721
Romain Guy8a73c512009-11-09 19:19:59 -0800722
Winson Chungaafa03c2010-06-11 17:34:16 -0700723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 /**
725 * Creates a view representing a shortcut.
726 *
727 * @param info The data structure describing the shortcut.
728 *
729 * @return A View inflated from R.layout.application.
730 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800731 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700733 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735
736 /**
737 * Creates a view representing a shortcut inflated from the specified resource.
738 *
739 * @param layoutResId The id of the XML layout used to create the shortcut.
740 * @param parent The group the shortcut belongs to.
741 * @param info The data structure describing the shortcut.
742 *
743 * @return A View inflated from layoutResId.
744 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800745 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800746 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
747 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 return favorite;
750 }
751
752 /**
753 * Add an application shortcut to the workspace.
754 *
755 * @param data The intent describing the application.
756 * @param cellInfo The position on screen where to create the shortcut.
757 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700758 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700759 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700760 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700761
Adam Cohenfbba09b2011-07-18 21:43:05 -0700762 // First we check if we already know the exact location where we want to add this item.
763 if (cellX >= 0 && cellY >= 0) {
764 cellXY[0] = cellX;
765 cellXY[1] = cellY;
766 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700767 showOutOfSpaceMessage();
768 return;
769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800771 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800772
Romain Guy73b979d2009-06-09 12:57:21 -0700773 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800774 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800776 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700777 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700778 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800779 } else {
780 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
782 }
Romain Guycbb89e42009-06-08 15:52:54 -0700783
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 /**
785 * Add a shortcut to the workspace.
786 *
787 * @param data The intent describing the shortcut.
788 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
791 int cellY) {
792 int[] cellXY = mTmpAddItemCellCoordinates;
793 int[] touchXY = mPendingAddInfo.dropPos;
794 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700795
Michael Jurkad3ef3062010-11-23 16:23:58 -0800796 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700797
798 // First we check if we already know the exact location where we want to add this item.
799 if (cellX >= 0 && cellY >= 0) {
800 cellXY[0] = cellX;
801 cellXY[1] = cellY;
802 foundCellSpan = true;
803 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800804 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700805 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800806 foundCellSpan = (result != null);
807 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700808 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800809 }
810
811 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700812 showOutOfSpaceMessage();
813 return;
814 }
815
816 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700817 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818
819 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700821 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
822 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823 }
824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700827 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700829 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700830 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700832 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700833 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700834
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700835 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700836 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700837
838 // We want to account for the extra amount of padding that we are adding to the widget
839 // to ensure that it gets the full amount of space that it has requested
840 Resources r = getResources();
841 int requiredWidth = appWidgetInfo.minWidth +
842 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
843 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
844 int requiredHeight = appWidgetInfo.minHeight +
845 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
846 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
847 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700848
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700850 // We have saved the position to which the widget was dragged-- this really only matters
851 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700852 int[] cellXY = mTmpAddItemCellCoordinates;
853 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700854 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700855 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
856 cellXY[0] = mPendingAddInfo.cellX;
857 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700858 foundCellSpan = true;
859 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700860 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700861 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700862 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800863 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700864 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700865 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700866 }
867
Michael Jurka0280c3b2010-09-17 15:00:07 -0700868 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800869 if (appWidgetId != -1) {
870 // Deleting an app widget ID is a void call but writes to disk before returning
871 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800872 new Thread("deleteAppWidgetId") {
873 public void run() {
874 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
875 }
876 }.start();
877 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800879 return;
880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700882 // Build Launcher-specific widget info and save to database
883 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700884 launcherInfo.spanX = spanXY[0];
885 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700888 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889
890 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700894 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700896
Winson Chung3d503fb2011-07-13 17:25:49 -0700897 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400898 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700899
900 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
902 }
Romain Guycbb89e42009-06-08 15:52:54 -0700903
Adam Cohended9f8d2010-11-03 13:25:16 -0700904 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
905 @Override
906 public void onReceive(Context context, Intent intent) {
907 final String action = intent.getAction();
908 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
909 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700910 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700911 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700912
913 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -0700914 if (mAppsCustomizeContent != null) {
915 mAppsCustomizeContent.reset();
916 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700917 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
918 mUserPresent = true;
919 updateRunning();
920 }
921 }
922 };
923
924 @Override
925 public void onAttachedToWindow() {
926 super.onAttachedToWindow();
927
928 // Listen for broadcasts related to user-presence
929 final IntentFilter filter = new IntentFilter();
930 filter.addAction(Intent.ACTION_SCREEN_OFF);
931 filter.addAction(Intent.ACTION_USER_PRESENT);
932 registerReceiver(mReceiver, filter);
933
Adam Cohend113e0c2010-11-11 10:48:05 -0800934 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700935 mVisible = true;
936 }
937
938 @Override
939 public void onDetachedFromWindow() {
940 super.onDetachedFromWindow();
941 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700942 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700943
Adam Cohend113e0c2010-11-11 10:48:05 -0800944 if (mAttached) {
945 unregisterReceiver(mReceiver);
946 mAttached = false;
947 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700948 updateRunning();
949 }
950
951 public void onWindowVisibilityChanged(int visibility) {
952 mVisible = visibility == View.VISIBLE;
953 updateRunning();
954 }
955
956 private void sendAdvanceMessage(long delay) {
957 mHandler.removeMessages(ADVANCE_MSG);
958 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
959 mHandler.sendMessageDelayed(msg, delay);
960 mAutoAdvanceSentTime = System.currentTimeMillis();
961 }
962
963 private void updateRunning() {
964 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
965 if (autoAdvanceRunning != mAutoAdvanceRunning) {
966 mAutoAdvanceRunning = autoAdvanceRunning;
967 if (autoAdvanceRunning) {
968 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
969 sendAdvanceMessage(delay);
970 } else {
971 if (!mWidgetsToAdvance.isEmpty()) {
972 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
973 (System.currentTimeMillis() - mAutoAdvanceSentTime));
974 }
975 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800976 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700977 }
978 }
979 }
980
981 private final Handler mHandler = new Handler() {
982 @Override
983 public void handleMessage(Message msg) {
984 if (msg.what == ADVANCE_MSG) {
985 int i = 0;
986 for (View key: mWidgetsToAdvance.keySet()) {
987 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
988 final int delay = mAdvanceStagger * i;
989 if (v instanceof Advanceable) {
990 postDelayed(new Runnable() {
991 public void run() {
992 ((Advanceable) v).advance();
993 }
994 }, delay);
995 }
996 i++;
997 }
998 sendAdvanceMessage(mAdvanceInterval);
999 }
1000 }
1001 };
1002
1003 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001004 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001005 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1006 if (v instanceof Advanceable) {
1007 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001008 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001009 updateRunning();
1010 }
1011 }
1012
1013 void removeWidgetToAutoAdvance(View hostView) {
1014 if (mWidgetsToAdvance.containsKey(hostView)) {
1015 mWidgetsToAdvance.remove(hostView);
1016 updateRunning();
1017 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001018 }
1019
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001021 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001022 launcherInfo.hostView = null;
1023 }
1024
Adam Cohended9f8d2010-11-03 13:25:16 -07001025 void showOutOfSpaceMessage() {
1026 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1027 }
1028
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 public LauncherAppWidgetHost getAppWidgetHost() {
1030 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
Romain Guycbb89e42009-06-08 15:52:54 -07001032
Winson Chunga9abd0e2010-10-27 17:18:37 -07001033 public LauncherModel getModel() {
1034 return mModel;
1035 }
1036
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001037 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001038 getWindow().closeAllPanels();
1039
1040 try {
1041 dismissDialog(DIALOG_CREATE_SHORTCUT);
1042 // Unlock the workspace if the dialog was showing
1043 } catch (Exception e) {
1044 // An exception is thrown if the dialog is not visible, which is fine
1045 }
1046
1047 try {
1048 dismissDialog(DIALOG_RENAME_FOLDER);
1049 // Unlock the workspace if the dialog was showing
1050 } catch (Exception e) {
1051 // An exception is thrown if the dialog is not visible, which is fine
1052 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001053
1054 // Whatever we were doing is hereby canceled.
1055 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001056 }
1057
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 @Override
1059 protected void onNewIntent(Intent intent) {
1060 super.onNewIntent(intent);
1061
1062 // Close the menu
1063 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001064 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001065 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001066
Adam Cohen95bb8002011-07-03 23:40:28 -07001067 closeFolder();
1068
Joe Onorato14f122b2009-11-19 14:06:36 -08001069 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1070 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001071
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001072 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001073 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001074 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001075 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001076 }
Winson Chungde1af762011-07-21 16:44:07 -07001077 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001078 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001079
Joe Onorato3a8820b2009-11-10 15:06:42 -08001080 final View v = getWindow().peekDecorView();
1081 if (v != null && v.getWindowToken() != null) {
1082 InputMethodManager imm = (InputMethodManager)getSystemService(
1083 INPUT_METHOD_SERVICE);
1084 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001086
Michael Jurka35aa14d2011-07-07 17:01:08 -07001087 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001088 if (mAppsCustomizeContent != null) {
1089 mAppsCustomizeContent.reset();
1090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 }
1092 }
1093
1094 @Override
1095 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1096 // Do not call super here
1097 mSavedInstanceState = savedInstanceState;
1098 }
1099
1100 @Override
1101 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001102 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001103 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
Michael Jurka883f55b2010-10-21 15:47:14 -07001105 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001106 // We close any open folder since it will not be re-opened, and we need to make sure
1107 // this state is reflected.
1108 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109
Winson Chung3d503fb2011-07-13 17:25:49 -07001110 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1111 mWaitingForResult) {
1112 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1113 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1114 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1115 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 }
1117
1118 if (mFolderInfo != null && mWaitingForResult) {
1119 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1120 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1121 }
Michael Jurka946ad472010-07-09 18:05:18 -07001122
Winson Chung785d2eb2011-04-14 16:08:02 -07001123 // Save the current AppsCustomize tab
1124 if (mAppsCustomizeTabHost != null) {
1125 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1126 if (currentTabTag != null) {
1127 outState.putString("apps_customize_currentTab", currentTabTag);
1128 }
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 }
1131
1132 @Override
1133 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001135
Winson Chungcd2b0142011-06-08 16:02:26 -07001136 // Stop callbacks from LauncherModel
1137 LauncherApplication app = ((LauncherApplication) getApplication());
1138 mModel.stopLoader();
1139 app.setLauncher(null);
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001142 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001144 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001146 mAppWidgetHost = null;
1147
1148 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149
1150 TextKeyListener.getInstance().release();
1151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152
Winson Chung3d503fb2011-07-13 17:25:49 -07001153 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001154
1155 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001156 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001157
1158 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1159 mWorkspace.removeAllViews();
1160 mWorkspace = null;
1161 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001162
1163 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
1165
Adam Cohena9cf38f2011-05-02 15:36:58 -07001166 public DragController getDragController() {
1167 return mDragController;
1168 }
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 @Override
1171 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001172 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 super.startActivityForResult(intent, requestCode);
1174 }
1175
1176 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001177 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001179
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001180 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001181
Karl Rosaen138a0412009-04-23 19:00:21 -07001182 if (initialQuery == null) {
1183 // Use any text typed in the launcher as the initial query
1184 initialQuery = getTypedText();
1185 clearTypedText();
1186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 if (appSearchData == null) {
1188 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001189 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
Karl Rosaen138a0412009-04-23 19:00:21 -07001192 final SearchManager searchManager =
1193 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001194 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001195 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
1197
1198 @Override
1199 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001200 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 return false;
1202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203
1204 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1207 .setIcon(android.R.drawable.ic_menu_add)
1208 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001209 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1210 .setIcon(android.R.drawable.ic_menu_manage)
1211 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001212 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 .setIcon(android.R.drawable.ic_menu_gallery)
1214 .setAlphabeticShortcut('W');
1215 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1216 .setIcon(android.R.drawable.ic_search_category_default)
1217 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1218 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1219 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1220 .setAlphabeticShortcut('N');
1221
1222 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001223 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1224 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
1226 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1227 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1228 .setIntent(settings);
1229
1230 return true;
1231 }
1232
1233 @Override
1234 public boolean onPrepareOptionsMenu(Menu menu) {
1235 super.onPrepareOptionsMenu(menu);
1236
Winson Chung785d2eb2011-04-14 16:08:02 -07001237 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1238 // related code?
1239 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
1241 return true;
1242 }
1243
1244 @Override
1245 public boolean onOptionsItemSelected(MenuItem item) {
1246 switch (item.getItemId()) {
1247 case MENU_ADD:
1248 addItems();
1249 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001250 case MENU_MANAGE_APPS:
1251 manageApps();
1252 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 case MENU_WALLPAPER_SETTINGS:
1254 startWallpaper();
1255 return true;
1256 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001257 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 return true;
1259 case MENU_NOTIFICATIONS:
1260 showNotifications();
1261 return true;
1262 }
1263
1264 return super.onOptionsItemSelected(item);
1265 }
Romain Guycbb89e42009-06-08 15:52:54 -07001266
Karl Rosaen138a0412009-04-23 19:00:21 -07001267 /**
1268 * Indicates that we want global search for this activity by setting the globalSearch
1269 * argument for {@link #startSearch} to true.
1270 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001272 @Override
1273 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001274 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001276 }
1277
Joe Onorato9c1289c2009-08-17 11:03:03 -04001278 public boolean isWorkspaceLocked() {
1279 return mWorkspaceLoading || mWaitingForResult;
1280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001283 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001284 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
Michael Jurka0280c3b2010-09-17 15:00:07 -07001287 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001288 mPendingAddInfo.container = ItemInfo.NO_ID;
1289 mPendingAddInfo.screen = -1;
1290 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1291 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1292 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001294
Winson Chung7ad01412010-09-27 11:33:03 -07001295 private void manageApps() {
1296 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1297 }
1298
Michael Jurkaaf442092010-06-10 17:01:57 -07001299 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001300 // TODO: catch bad widget exception when sent
1301 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001302 // TODO: Is this log message meaningful?
1303 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001304 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001305 }
1306
Winson Chung55cef262010-10-28 14:14:18 -07001307 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001308 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309
Bjorn Bringert7984c942009-12-09 15:38:25 +00001310 if (appWidget.configure != null) {
1311 // Launch over to configure widget, if needed
1312 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1313 intent.setComponent(appWidget.configure);
1314 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001315 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001316 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1317 intent.putExtra(
1318 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1319 info.mimeType);
1320
1321 final String mimeType = info.mimeType;
1322 final ClipData clipData = (ClipData) info.configurationData;
1323 final ClipDescription clipDesc = clipData.getDescription();
1324 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1325 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001326 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001327 final CharSequence stringData = item.getText();
1328 final Uri uriData = item.getUri();
1329 final Intent intentData = item.getIntent();
1330 final String key =
1331 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1332 if (uriData != null) {
1333 intent.putExtra(key, uriData);
1334 } else if (intentData != null) {
1335 intent.putExtra(key, intentData);
1336 } else if (stringData != null) {
1337 intent.putExtra(key, stringData);
1338 }
1339 break;
1340 }
1341 }
1342 }
Winson Chung55cef262010-10-28 14:14:18 -07001343 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001344
Romain Guy8e633c52010-03-04 12:51:36 -08001345 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001347 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001348 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001349
1350 // Exit spring loaded mode if necessary after adding the widget
1351 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353 }
Romain Guycbb89e42009-06-08 15:52:54 -07001354
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 /**
1356 * Process a shortcut drop.
1357 *
1358 * @param componentName The name of the component
1359 * @param screen The screen where it should be added
1360 * @param cell The cell it should be added to, optional
1361 * @param position The location on the screen where it was dropped, optional
1362 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001363 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1364 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001366 mPendingAddInfo.container = container;
1367 mPendingAddInfo.screen = screen;
1368 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001369
1370 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001371 mPendingAddInfo.cellX = cell[0];
1372 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001373 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001374
1375 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1376 createShortcutIntent.setComponent(componentName);
1377 processShortcut(createShortcutIntent);
1378 }
1379
Adam Cohenfbba09b2011-07-18 21:43:05 -07001380 /**
1381 * Process a widget drop.
1382 *
1383 * @param info The PendingAppWidgetInfo of the widget being added.
1384 * @param screen The screen where it should be added
1385 * @param cell The cell it should be added to, optional
1386 * @param position The location on the screen where it was dropped, optional
1387 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001388 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1389 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001390 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001391 mPendingAddInfo.container = info.container = container;
1392 mPendingAddInfo.screen = info.screen = screen;
1393 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001394 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 mPendingAddInfo.cellX = cell[0];
1396 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001397 }
1398
1399 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1400 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1401 addAppWidgetImpl(appWidgetId, info);
1402 }
1403
Joe Onoratodeb98af2010-02-19 14:59:39 -08001404 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001405 // Handle case where user selected "Applications"
1406 String applicationName = getResources().getString(R.string.group_applications);
1407 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001408
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001409 if (applicationName != null && applicationName.equals(shortcutName)) {
1410 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1411 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001412
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001413 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1414 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001415 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001416 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001417 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001418 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421
Winson Chung24ab2f12010-09-16 14:10:47 -07001422 void processWallpaper(Intent intent) {
1423 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1424 }
1425
Winson Chung3d503fb2011-07-13 17:25:49 -07001426 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1427 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001428 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 folderInfo.title = getText(R.string.folder_name);
1430
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1433 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001434 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
1436 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001437 FolderIcon newFolder =
1438 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1439 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1440 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001441 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
Romain Guycbb89e42009-06-08 15:52:54 -07001443
Joe Onorato9c1289c2009-08-17 11:03:03 -04001444 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001445 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 }
1447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 private void showNotifications() {
1449 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1450 if (statusBar != null) {
1451 statusBar.expand();
1452 }
1453 }
1454
1455 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001456 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001458 Intent chooser = Intent.createChooser(pickWallpaper,
1459 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001460 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1461 // Removed in Eclair MR1
1462// WallpaperManager wm = (WallpaperManager)
1463// getSystemService(Context.WALLPAPER_SERVICE);
1464// WallpaperInfo wi = wm.getWallpaperInfo();
1465// if (wi != null && wi.getSettingsActivity() != null) {
1466// LabeledIntent li = new LabeledIntent(getPackageName(),
1467// R.string.configure_wallpaper, 0);
1468// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1469// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1470// }
Mike Clerona0618e42009-10-22 13:55:21 -07001471 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 }
1473
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001474 /**
1475 * Registers various content observers. The current implementation registers
1476 * only a favorites observer to keep track of the favorites applications.
1477 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001478 private void registerContentObservers() {
1479 ContentResolver resolver = getContentResolver();
1480 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1481 true, mWidgetObserver);
1482 }
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 @Override
1485 public boolean dispatchKeyEvent(KeyEvent event) {
1486 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1487 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001489 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001490 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001491 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001492 dumpState();
1493 return true;
1494 }
1495 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001496 }
1497 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1498 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001499 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 return true;
1501 }
1502 }
1503
1504 return super.dispatchKeyEvent(event);
1505 }
1506
Joe Onorato88ec0992009-11-19 13:16:06 -08001507 @Override
1508 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001509 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001510 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001511 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001512 Folder openFolder = mWorkspace.getOpenFolder();
1513 if (openFolder.isEditingName()) {
1514 openFolder.dismissEditingName();
1515 } else {
1516 closeFolder();
1517 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001518 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001519 mWorkspace.exitWidgetResizeMode();
1520
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001521 // Back button is a no-op here, but give at least some feedback for the button press
1522 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001523 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001524 }
1525
Adam Cohen2801caf2011-05-13 20:57:39 -07001526 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 Folder folder = mWorkspace.getOpenFolder();
1528 if (folder != null) {
1529 closeFolder(folder);
1530 }
1531 }
1532
1533 void closeFolder(Folder folder) {
1534 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001535
Michael Jurka8c920dd2011-01-20 14:16:56 -08001536 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 if (parent != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001538 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Adam Cohen2801caf2011-05-13 20:57:39 -07001539 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001540 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001542 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 }
1544
1545 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001546 * Re-listen when widgets are reset.
1547 */
1548 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001549 if (mAppWidgetHost != null) {
1550 mAppWidgetHost.startListening();
1551 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001552 }
1553
1554 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1556 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001559 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 /**
1563 * Launches the intent referred by the clicked shortcut.
1564 *
1565 * @param v The view representing the clicked shortcut.
1566 */
1567 public void onClick(View v) {
Adam Cohen2f84ef22011-07-26 17:16:44 -07001568 // Make sure that rogue clicks don't get through while allapps is launching
1569 if (mWorkspace.isSwitchingState()) return;
1570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001572 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001574 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001575 int[] pos = new int[2];
1576 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001577 intent.setSourceBounds(new Rect(pos[0], pos[1],
1578 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001579 boolean success = startActivitySafely(intent, tag);
1580
1581 if (success && v instanceof BubbleTextView) {
1582 mWaitingForResume = (BubbleTextView) v;
1583 mWaitingForResume.setStayPressed(true);
1584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001586 if (v instanceof FolderIcon) {
1587 FolderIcon fi = (FolderIcon) v;
1588 handleFolderClick(fi);
1589 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001590 } else if (v == mAllAppsButton) {
1591 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001592 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001593 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001594 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
1597 }
1598
Michael Jurka0e260592010-06-30 17:07:39 -07001599 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001600 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001601 // clicking anywhere on the workspace causes the customization drawer to slide down
1602 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001603 return false;
1604 }
1605
Michael Jurkaaf442092010-06-10 17:01:57 -07001606 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001607 * Event handler for the search button
1608 *
1609 * @param v The view that was clicked.
1610 */
1611 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001612 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001613 // Use a custom animation for launching search
1614 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
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 "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001634 * enters home screen customization mode.
1635 *
1636 * @param v The view that was clicked.
1637 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001638 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001639 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001640 }
1641
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001642 /**
1643 * Event handler for the "grid" button that appears on the home screen, which
1644 * enters all apps mode.
1645 *
1646 * @param v The view that was clicked.
1647 */
1648 public void onClickAllAppsButton(View v) {
1649 showAllApps(true);
1650 }
1651
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001652 public void onClickAppMarketButton(View v) {
1653 if (mAppMarketIntent != null) {
1654 startActivitySafely(mAppMarketIntent, "app market");
1655 }
1656 }
1657
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001658 void startApplicationDetailsActivity(ComponentName componentName) {
1659 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001660 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1661 Uri.fromParts("package", packageName, null));
1662 startActivity(intent);
1663 }
1664
Patrick Dubroy5539af72010-09-07 15:22:01 -07001665 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1666 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1667 // System applications cannot be installed. For now, show a toast explaining that.
1668 // We may give them the option of disabling apps this way.
1669 int messageId = R.string.uninstall_system_app_text;
1670 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1671 } else {
1672 String packageName = appInfo.componentName.getPackageName();
1673 String className = appInfo.componentName.getClassName();
1674 Intent intent = new Intent(
1675 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1676 startActivity(intent);
1677 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001678 }
1679
Michael Jurkaddd62e92011-02-16 17:49:14 -08001680 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1682 try {
1683 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001684 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 } catch (ActivityNotFoundException e) {
1686 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001687 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 } catch (SecurityException e) {
1689 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001690 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001692 "or use the exported attribute for this activity. "
1693 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001695 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001697
Romain Guy8e633c52010-03-04 12:51:36 -08001698 void startActivityForResultSafely(Intent intent, int requestCode) {
1699 try {
1700 startActivityForResult(intent, requestCode);
1701 } catch (ActivityNotFoundException e) {
1702 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1703 } catch (SecurityException e) {
1704 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1705 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1706 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1707 "or use the exported attribute for this activity.", e);
1708 }
1709 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710
Adam Cohena9cf38f2011-05-02 15:36:58 -07001711 private void handleFolderClick(FolderIcon folderIcon) {
1712 final FolderInfo info = folderIcon.mInfo;
1713 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 // Close any open folder
1715 closeFolder();
1716 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001717 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 } else {
1719 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001720 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 int folderScreen;
1722 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001723 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 // .. and close it
1725 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001726 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 // Close any folder open on the current screen
1728 closeFolder();
1729 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001730 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 }
1732 }
1733 }
1734 }
1735
Adam Cohen2801caf2011-05-13 20:57:39 -07001736 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1737 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1738 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1739 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1740
Adam Cohenc51934b2011-07-26 21:07:43 -07001741 FolderInfo info = (FolderInfo) fi.getTag();
1742 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1743 CellLayout cl = (CellLayout) fi.getParent().getParent();
1744 cl.setFolderLeaveBehindCell(info.cellX, info.cellY);
1745 }
1746
Adam Cohen2801caf2011-05-13 20:57:39 -07001747 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1748 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1749 oa.start();
1750 }
1751
1752 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1753 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1754 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1755 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1756
Adam Cohenc51934b2011-07-26 21:07:43 -07001757 FolderInfo info = (FolderInfo) fi.getTag();
1758 CellLayout cl = null;
1759 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1760 cl = (CellLayout) fi.getParent().getParent();
1761 }
1762
1763 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001764 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1765 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001766 oa.addListener(new AnimatorListenerAdapter() {
1767 @Override
1768 public void onAnimationEnd(Animator animation) {
1769 if (layout != null) {
1770 layout.clearFolderLeaveBehind();
1771 }
1772 }
1773 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001774 oa.start();
1775 }
1776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001778 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 * is animated relative to the specified View. If the View is null, no animation
1780 * is played.
1781 *
1782 * @param folderInfo The FolderInfo describing the folder to open.
1783 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001784 public void openFolder(FolderIcon folderIcon) {
1785 Folder folder = folderIcon.mFolder;
1786 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787
Adam Cohen2801caf2011-05-13 20:57:39 -07001788 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001789 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790
Adam Cohen8e776a62011-06-28 18:10:06 -07001791 mDragLayer.addView(folder);
1792 mDragController.addDropTarget((DropTarget) folder);
1793
Adam Cohena9cf38f2011-05-02 15:36:58 -07001794 folder.animateOpen();
1795 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001799 if (mState != State.WORKSPACE) {
1800 return false;
1801 }
1802
Joe Onorato9c1289c2009-08-17 11:03:03 -04001803 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 return false;
1805 }
1806
1807 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001808 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 resetAddInfo();
1812 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001814 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 return true;
1816 }
1817
Winson Chung3d503fb2011-07-13 17:25:49 -07001818 // The hotseat touch handling does not go through Workspace, and we always allow long press
1819 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001820 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001821 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1822 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 if (itemUnderLongClick == null) {
1824 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1826 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001827 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001831 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1832 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
1835 }
1836 }
1837 return true;
1838 }
1839
Winson Chung3d503fb2011-07-13 17:25:49 -07001840 boolean isHotseatLayout(View layout) {
1841 return mHotseat != null && layout != null &&
1842 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1843 }
1844 Hotseat getHotseat() {
1845 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001846 }
1847
Winson Chung3d503fb2011-07-13 17:25:49 -07001848 /**
1849 * Returns the CellLayout of the specified container at the specified screen.
1850 */
1851 CellLayout getCellLayout(long container, int screen) {
1852 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1853 if (mHotseat != null) {
1854 return mHotseat.getLayout();
1855 } else {
1856 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001857 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001858 } else {
1859 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001860 }
1861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 Workspace getWorkspace() {
1864 return mWorkspace;
1865 }
1866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 @Override
1868 protected Dialog onCreateDialog(int id) {
1869 switch (id) {
1870 case DIALOG_CREATE_SHORTCUT:
1871 return new CreateShortcut().createDialog();
1872 case DIALOG_RENAME_FOLDER:
1873 return new RenameFolder().createDialog();
1874 }
1875
1876 return super.onCreateDialog(id);
1877 }
1878
1879 @Override
1880 protected void onPrepareDialog(int id, Dialog dialog) {
1881 switch (id) {
1882 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 break;
1884 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001885 if (mFolderInfo != null) {
1886 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1887 final CharSequence text = mFolderInfo.title;
1888 input.setText(text);
1889 input.setSelection(0, text.length());
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 break;
1892 }
1893 }
1894
1895 void showRenameDialog(FolderInfo info) {
1896 mFolderInfo = info;
1897 mWaitingForResult = true;
1898 showDialog(DIALOG_RENAME_FOLDER);
1899 }
1900
Adam Cohenfbba09b2011-07-18 21:43:05 -07001901 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001902 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001903 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1904 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 mWaitingForResult = true;
1906 showDialog(DIALOG_CREATE_SHORTCUT);
1907 }
1908
1909 private class RenameFolder {
1910 private EditText mInput;
1911
1912 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1914 mInput = (EditText) layout.findViewById(R.id.folder_name);
1915
1916 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1917 builder.setIcon(0);
1918 builder.setTitle(getString(R.string.rename_folder_title));
1919 builder.setCancelable(true);
1920 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1921 public void onCancel(DialogInterface dialog) {
1922 cleanup();
1923 }
1924 });
1925 builder.setNegativeButton(getString(R.string.cancel_action),
1926 new Dialog.OnClickListener() {
1927 public void onClick(DialogInterface dialog, int which) {
1928 cleanup();
1929 }
1930 }
1931 );
1932 builder.setPositiveButton(getString(R.string.rename_action),
1933 new Dialog.OnClickListener() {
1934 public void onClick(DialogInterface dialog, int which) {
1935 changeFolderName();
1936 }
1937 }
1938 );
1939 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001940
1941 final AlertDialog dialog = builder.create();
1942 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1943 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001944 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001945 mInput.requestFocus();
1946 InputMethodManager inputManager = (InputMethodManager)
1947 getSystemService(Context.INPUT_METHOD_SERVICE);
1948 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001949 }
1950 });
1951
1952 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
1955 private void changeFolderName() {
1956 final String name = mInput.getText().toString();
1957 if (!TextUtils.isEmpty(name)) {
1958 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001959 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 mFolderInfo.title = name;
1961 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1962
Joe Onorato9c1289c2009-08-17 11:03:03 -04001963 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001964 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001965 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 } else {
1967 final FolderIcon folderIcon = (FolderIcon)
1968 mWorkspace.getViewForTag(mFolderInfo);
1969 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001970 // TODO: At some point we'll probably want some version of setting
1971 // the text for a folder icon.
1972 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 getWorkspace().requestLayout();
1974 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001975 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976 mWorkspaceLoading = true;
1977 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 }
1979 }
1980 }
1981 cleanup();
1982 }
1983
1984 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 dismissDialog(DIALOG_RENAME_FOLDER);
1986 mWaitingForResult = false;
1987 mFolderInfo = null;
1988 }
1989 }
1990
Daniel Sandler843e8602010-06-07 14:59:01 -04001991 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1992 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001993 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001994 }
1995
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001996 // AllAppsView.Watcher
1997 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001998 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001999 mWorkspace.setVisibility(View.GONE);
2000 }
2001 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002002
2003 /**
2004 * Helper method for the cameraZoomIn/cameraZoomOut animations
2005 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002006 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002007 * @param scaleFactor The scale factor used for the zoom
2008 */
2009 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2010 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002011
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002012 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002013 // Set pivotY so that at the starting zoom factor, the view is partially
2014 // visible. Modifying initialHeightFactor changes how much of the view is
2015 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002016 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002017 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002018 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002019 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002020 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002021 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002022 }
2023 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002024
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002025 /**
2026 * Zoom the camera out from the workspace to reveal 'toView'.
2027 * Assumes that the view to show is anchored at either the very top or very bottom
2028 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002029 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002030 */
Winson Chungc07918d2011-07-01 15:35:26 -07002031 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002032 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002033
Winson Chungf0ea4d32011-06-06 14:27:16 -07002034 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2035 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2036 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2037 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002038
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002039 setPivotsForZoom(toView, toState, scale);
2040
Michael Jurkad74c9842011-07-10 12:44:21 -07002041 // Shrink workspaces away if going to AppsCustomize from workspace
2042 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002043 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002044
2045 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002046 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002047 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002048 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2049 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002050 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002051 toView.setFastScaleX(a * scale + b * 1f);
2052 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002053 }
2054 });
Adam Cohen61033d32010-11-15 18:29:44 -08002055
Winson Chungf0ea4d32011-06-06 14:27:16 -07002056 toView.setVisibility(View.VISIBLE);
2057 toView.setFastAlpha(0f);
2058 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2059 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2060 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2061 public void onAnimationUpdate(float a, float b) {
2062 // don't need to invalidate because we do so above
2063 toView.setFastAlpha(a * 0f + b * 1f);
2064 }
2065 });
2066 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002067
Michael Jurkaabded662011-03-04 12:06:57 -08002068 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002069 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002070 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002071 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002072 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002073 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002074 // Prepare the position
2075 toView.setTranslationX(0.0f);
2076 toView.setTranslationY(0.0f);
2077 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002078 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002079 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002080 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002081 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002082 // If we don't set the final scale values here, if this animation is cancelled
2083 // it will have the wrong scale value and subsequent cameraPan animations will
2084 // not fix that
2085 toView.setScaleX(1.0f);
2086 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002087 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002088 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002089 }
Winson Chung32174c82011-07-19 15:47:55 -07002090
2091 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2092 // Hide the workspace scrollbar
2093 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002094 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002095 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002096 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002097 });
2098
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002099 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002100 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002101
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002102 if (mStateAnimation != null) mStateAnimation.cancel();
2103 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002104 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002105 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002106 } else {
2107 toView.setTranslationX(0.0f);
2108 toView.setTranslationY(0.0f);
2109 toView.setScaleX(1.0f);
2110 toView.setScaleY(1.0f);
2111 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002112 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002113 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002114 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2115 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002116
Winson Chungc07918d2011-07-01 15:35:26 -07002117 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2118 // Hide the workspace scrollbar
2119 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002120 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002121 }
Michael Jurkaabded662011-03-04 12:06:57 -08002122 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002123 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002124 }
2125
2126 /**
2127 * Zoom the camera back into the workspace, hiding 'fromView'.
2128 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002129 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002130 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002131 */
Winson Chungc07918d2011-07-01 15:35:26 -07002132 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002133 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002134
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2136 final float scaleFactor = (float)
2137 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2138 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002139
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002140 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002141
Michael Jurkad3ef3062010-11-23 16:23:58 -08002142 if (!springLoaded) {
2143 mWorkspace.unshrink(animated);
2144 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002145 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002146 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002147 if (mStateAnimation != null) mStateAnimation.cancel();
2148 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002149
Michael Jurka742574b2011-02-02 23:51:01 -08002150 final float oldScaleX = fromView.getScaleX();
2151 final float oldScaleY = fromView.getScaleY();
2152
2153 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2154 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002155 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2156 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002157 ((View)fromView.getParent()).fastInvalidate();
2158 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2159 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2160 }
2161 });
Michael Jurkaabded662011-03-04 12:06:57 -08002162 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002163 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002164 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002165 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2166 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002167 // don't need to invalidate because we do so above
2168 fromView.setFastAlpha(a * 1f + b * 0f);
2169 }
2170 });
Michael Jurkaabded662011-03-04 12:06:57 -08002171 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002172 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002173 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002174 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002175 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002176 public void onAnimationStart(android.animation.Animator animation) {
2177 if (!springLoaded) {
2178 mWorkspace.showDockDivider(false);
2179 }
2180 }
2181 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002182 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002183 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002184 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002185 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002186 }
Winson Chung5a808352011-06-27 19:08:49 -07002187 mWorkspace.flashScrollingIndicator();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002188 }
2189 });
2190
Winson Chungf0ea4d32011-06-06 14:27:16 -07002191 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002192 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002193 } else {
2194 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002195 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002196 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2197 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002198
Winson Chungc07918d2011-07-01 15:35:26 -07002199 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002200 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002201 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002202 mWorkspace.flashScrollingIndicator();
2203 }
Michael Jurkaabded662011-03-04 12:06:57 -08002204 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002205 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002206 }
2207
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002208 void showWorkspace(boolean animated) {
2209 showWorkspace(animated, null);
2210 }
2211
2212 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002213 if (layout != null) {
2214 // always animated, but that's ok since we never specify a layout and
2215 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002216 mWorkspace.unshrink(layout);
2217 } else {
2218 mWorkspace.unshrink(animated);
2219 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002220 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002221 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002222 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002223
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002224 // Change the state *after* we've called all the transition code
2225 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002226
Winson Chung5fb63472011-02-02 17:03:37 -08002227 // Resume the auto-advance of widgets
2228 mUserPresent = true;
2229 updateRunning();
2230
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002231 // send an accessibility event to announce the context change
2232 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002233 }
2234
Michael Jurkad3ef3062010-11-23 16:23:58 -08002235 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002236 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002237 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002238 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002239 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002240 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002241 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002242 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002243 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2244 mWorkspace.postDelayed(new Runnable() {
2245 @Override
2246 public void run() {
2247 exitSpringLoadedDragMode();
2248 }
2249 }, (extendedDelay ?
2250 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2251 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2252 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002253 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002254 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002255 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002256 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2257 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002258 }
2259 // Otherwise, we are not in spring loaded mode, so don't do anything.
2260 }
2261
Adam Cohenfc53cd22011-07-20 15:45:11 -07002262 public boolean isAllAppsCustomizeOpen() {
2263 return mState == State.APPS_CUSTOMIZE;
2264 }
2265
Winson Chungf0ea4d32011-06-06 14:27:16 -07002266 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002268 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002270 if (!LauncherApplication.isScreenLarge()) {
2271 if (animated) {
2272 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002273 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002274 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002276 }
2277 }
2278 }
2279
2280 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002282 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002283 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002284 if (!LauncherApplication.isScreenLarge()) {
2285 if (animated) {
2286 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002290 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002291 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002292 }
2293
Winson Chung785d2eb2011-04-14 16:08:02 -07002294 void showAllApps(boolean animated) {
2295 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002296
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2298 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002299
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002302
Winson Chungf0ea4d32011-06-06 14:27:16 -07002303 // Change the state *after* we've called all the transition code
2304 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002305
2306 // Pause the auto-advance of widgets until we are out of AllApps
2307 mUserPresent = false;
2308 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002309 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002310
2311 // Send an accessibility event to announce the context change
2312 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2313 }
2314
Joe Onoratob2061212009-11-24 16:13:54 -05002315 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002316 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002317 * - Home from workspace
2318 * - from center screen
2319 * - from other screens
2320 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002321 * - from center screen
2322 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002323 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002324 * - from center screen
2325 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002326 * - Launch app from workspace and quit
2327 * - with back
2328 * - with home
2329 * - Launch app from all apps and quit
2330 * - with back
2331 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002332 * - Go to a screen that's not the default, then all
2333 * apps, and launch and app, and go back
2334 * - with back
2335 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002336 * - On workspace, long press power and go back
2337 * - with back
2338 * - with home
2339 * - On all apps, long press power and go back
2340 * - with back
2341 * - with home
2342 * - On workspace, power off
2343 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002344 * - Launch an app and turn off the screen while in that app
2345 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002346 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002347 * - From all apps
2348 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002349 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2350 * - From all apps
2351 * - From the center workspace
2352 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002353 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002354 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2356 mWorkspace.setVisibility(View.VISIBLE);
2357 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002358
Winson Chung3d503fb2011-07-13 17:25:49 -07002359 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002360 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002361
Winson Chungf0ea4d32011-06-06 14:27:16 -07002362 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 if (mAllAppsButton != null) {
2364 mAllAppsButton.requestFocus();
2365 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002366 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002367 }
2368
Joe Onorato7c312c12009-08-13 21:36:53 -07002369 void lockAllApps() {
2370 // TODO
2371 }
2372
2373 void unlockAllApps() {
2374 // TODO
2375 }
2376
Patrick Dubroy5f445422011-02-18 14:35:21 -08002377 /**
2378 * Add an item from all apps or customize onto the given workspace screen.
2379 * If layout is null, add to the current screen.
2380 */
2381 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002382 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2383 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002384 } else {
2385 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002386 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002387 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002388
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002389 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002390 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002391 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002392 // Look for the toolbar icon specified in the activity meta-data
2393 Bundle metaData = packageManager.getActivityInfo(
2394 activityName, PackageManager.GET_META_DATA).metaData;
2395 if (metaData != null) {
2396 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2397 if (iconResId != 0) {
2398 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002399 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002400 }
2401 }
2402 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002403 // This can happen if the activity defines an invalid drawable
2404 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2405 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002406 } catch (Resources.NotFoundException nfe) {
2407 // This can happen if the activity defines an invalid drawable
2408 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2409 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002410 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002411 return null;
2412 }
2413
2414 // if successful in getting icon, return it; otherwise, set button to use default drawable
2415 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2416 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2417 TextView button = (TextView) findViewById(buttonId);
2418 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2419
2420 // If we were unable to find the icon via the meta-data, use a generic one
2421 if (toolbarIcon == null) {
2422 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2423 return null;
2424 } else {
2425 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2426 return toolbarIcon.getConstantState();
2427 }
2428 }
2429
2430 // if successful in getting icon, return it; otherwise, set button to use default drawable
2431 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2432 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2433 ImageView button = (ImageView) findViewById(buttonId);
2434 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2435
Michael Jurka19e0fc52011-07-22 18:00:21 -07002436 if (button != null) {
2437 // If we were unable to find the icon via the meta-data, use a
2438 // generic one
2439 if (toolbarIcon == null) {
2440 button.setImageResource(fallbackDrawableId);
2441 } else {
2442 button.setImageDrawable(toolbarIcon);
2443 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002444 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002445
2446 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2447
Michael Jurka0423dcf2010-10-05 14:56:18 -07002448 }
2449
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002450 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2451 TextView button = (TextView) findViewById(buttonId);
2452 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2453 }
2454
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002455 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002456 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002457 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002458 }
2459
Michael Jurka0423dcf2010-10-05 14:56:18 -07002460 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002461 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2462 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002463
Winson Chung1cad91e2011-05-25 17:41:01 -07002464 final SearchManager searchManager =
2465 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2466 ComponentName activityName = searchManager.getGlobalSearchActivity();
2467 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002468 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002469 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002470 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002471 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002472 } else {
2473 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002474 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002475 }
2476 }
2477
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002478 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002479 updateButtonWithDrawable(R.id.search_button, d);
2480 }
2481
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002482 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002483 final View searchDivider = findViewById(R.id.search_divider);
2484 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002485
Winson Chung1cad91e2011-05-25 17:41:01 -07002486 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2487 ComponentName activityName = intent.resolveActivity(getPackageManager());
2488 if (activityName != null) {
2489 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002490 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2491 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002492 voiceButton.setVisibility(View.VISIBLE);
2493 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002494 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002495 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002496 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002497 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002498
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002499 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002500 updateButtonWithDrawable(R.id.voice_button, d);
2501 }
2502
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002503 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002504 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002505 */
2506 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002507 final View marketButton = findViewById(R.id.market_button);
2508 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2509 // Find the app market activity by resolving an intent.
2510 // (If multiple app markets are installed, it will return the ResolverActivity.)
2511 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002512 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002513 mAppMarketIntent = intent;
2514 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002515 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002516 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002517
2518 // Remove the shop icon text in the Phone UI
2519 if (!LauncherApplication.isScreenLarge()) {
2520 ((TextView) marketButton).setText("");
2521 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002522 } else {
2523 // We should hide and disable the view so that we don't try and restore the visibility
2524 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2525 marketButton.setVisibility(View.GONE);
2526 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002527 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002528 }
2529
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002530 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002531 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002532 }
2533
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002534 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002535 * Displays the shortcut creation dialog and launches, if necessary, the
2536 * appropriate activity.
2537 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002538 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002539 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2540 DialogInterface.OnShowListener {
2541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002542 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002546
Winson Chung55b65502011-05-26 12:03:43 -07002547 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2548 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002549 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 AlertDialog dialog = builder.create();
2552 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002553 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002554 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 return dialog;
2557 }
2558
2559 public void onCancel(DialogInterface dialog) {
2560 mWaitingForResult = false;
2561 cleanup();
2562 }
2563
Romain Guycbb89e42009-06-08 15:52:54 -07002564 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002565 mWaitingForResult = false;
2566 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002567 }
2568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002570 try {
2571 dismissDialog(DIALOG_CREATE_SHORTCUT);
2572 } catch (Exception e) {
2573 // An exception is thrown if the dialog is not visible, which is fine
2574 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002575 }
2576
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002577 /**
2578 * Handle the action clicked in the "Add to home" dialog.
2579 */
2580 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002581 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002582
Winson Chung55b65502011-05-26 12:03:43 -07002583 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2584 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002585 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002586 if (mAppsCustomizeTabHost != null) {
Winson Chung5a808352011-06-27 19:08:49 -07002587 mAppsCustomizeTabHost.selectShortcutsTab();
Winson Chungd2945262011-06-24 15:22:14 -07002588 }
2589 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002590 break;
2591 }
Winson Chung55b65502011-05-26 12:03:43 -07002592 case AddAdapter.ITEM_APPLICATION: {
2593 if (mAppsCustomizeTabHost != null) {
2594 mAppsCustomizeTabHost.selectAppsTab();
2595 }
2596 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002597 break;
2598 }
Winson Chung55b65502011-05-26 12:03:43 -07002599 case AddAdapter.ITEM_APPWIDGET: {
2600 if (mAppsCustomizeTabHost != null) {
2601 mAppsCustomizeTabHost.selectWidgetsTab();
2602 }
2603 showAllApps(true);
2604 break;
2605 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002606 case AddAdapter.ITEM_WALLPAPER: {
2607 startWallpaper();
2608 break;
2609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 }
2611 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002612
2613 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002614 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002615 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002616 }
2617
2618 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002619 * Receives notifications when applications are added/removed.
2620 */
2621 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2622 @Override
2623 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002624 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002625 String reason = intent.getStringExtra("reason");
2626 if (!"homekey".equals(reason)) {
2627 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002628 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002629 animate = false;
2630 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002631 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002632 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002633 }
2634 }
2635
2636 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002637 * Receives notifications whenever the appwidgets are reset.
2638 */
2639 private class AppWidgetResetObserver extends ContentObserver {
2640 public AppWidgetResetObserver() {
2641 super(new Handler());
2642 }
2643
2644 @Override
2645 public void onChange(boolean selfChange) {
2646 onAppWidgetReset();
2647 }
2648 }
2649
2650 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002651 * If the activity is currently paused, signal that we need to re-run the loader
2652 * in onResume.
2653 *
2654 * This needs to be called from incoming places where resources might have been loaded
2655 * while we are paused. That is becaues the Configuration might be wrong
2656 * when we're not running, and if it comes back to what it was when we
2657 * were paused, we are not restarted.
2658 *
2659 * Implementation of the method from LauncherModel.Callbacks.
2660 *
2661 * @return true if we are currently paused. The caller might be able to
2662 * skip some work in that case since we will come back again.
2663 */
2664 public boolean setLoadOnResume() {
2665 if (mPaused) {
2666 Log.i(TAG, "setLoadOnResume");
2667 mOnResumeNeedsLoad = true;
2668 return true;
2669 } else {
2670 return false;
2671 }
2672 }
2673
2674 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002675 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002677 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002678 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002679 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002680 } else {
2681 return SCREEN_COUNT / 2;
2682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002684
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002685
Joe Onorato9c1289c2009-08-17 11:03:03 -04002686 /**
2687 * Refreshes the shortcuts shown on the workspace.
2688 *
2689 * Implementation of the method from LauncherModel.Callbacks.
2690 */
2691 public void startBinding() {
2692 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002693
2694 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002695 int count = workspace.getChildCount();
2696 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002697 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002698 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2699 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002700 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002701 if (mHotseat != null) {
2702 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002703 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002704
Adam Cohen4eac29a2011-07-11 17:53:37 -07002705 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002706 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 }
2708
2709 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002710 * Bind the items start-end from the list.
2711 *
2712 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002714 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002715 setLoadOnResume();
2716
Joe Onorato9c1289c2009-08-17 11:03:03 -04002717 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002718 for (int i=start; i<end; i++) {
2719 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002720
2721 // Short circuit if we are loading dock items for a configuration which has no dock
2722 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2723 mHotseat == null) {
2724 continue;
2725 }
2726
Joe Onorato9c1289c2009-08-17 11:03:03 -04002727 switch (item.itemType) {
2728 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2729 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002730 View shortcut = createShortcut((ShortcutInfo)item);
2731 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2732 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002734 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002735 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002736 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002737 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002738 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2739 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002743 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 }
2745
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 /**
2747 * Implementation of the method from LauncherModel.Callbacks.
2748 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002749 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002750 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002751 sFolders.clear();
2752 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754
2755 /**
2756 * Add the views for a widget to the workspace.
2757 *
2758 * Implementation of the method from LauncherModel.Callbacks.
2759 */
2760 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002761 setLoadOnResume();
2762
Daniel Sandler843e8602010-06-07 14:59:01 -04002763 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2764 if (DEBUG_WIDGETS) {
2765 Log.d(TAG, "bindAppWidget: " + item);
2766 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002767 final Workspace workspace = mWorkspace;
2768
2769 final int appWidgetId = item.appWidgetId;
2770 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002771 if (DEBUG_WIDGETS) {
2772 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2773 }
2774
Joe Onorato9c1289c2009-08-17 11:03:03 -04002775 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2776
Joe Onorato9c1289c2009-08-17 11:03:03 -04002777 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2778 item.hostView.setTag(item);
2779
Winson Chung3d503fb2011-07-13 17:25:49 -07002780 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002781 item.cellY, item.spanX, item.spanY, false);
2782
Adam Cohended9f8d2010-11-03 13:25:16 -07002783 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2784
Joe Onorato9c1289c2009-08-17 11:03:03 -04002785 workspace.requestLayout();
2786
Daniel Sandler843e8602010-06-07 14:59:01 -04002787 if (DEBUG_WIDGETS) {
2788 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2789 + (SystemClock.uptimeMillis()-start) + "ms");
2790 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002791 }
2792
2793 /**
2794 * Callback saying that there aren't any more items to bind.
2795 *
2796 * Implementation of the method from LauncherModel.Callbacks.
2797 */
2798 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002799 setLoadOnResume();
2800
Joe Onorato9c1289c2009-08-17 11:03:03 -04002801 if (mSavedState != null) {
2802 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002803 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002805 mSavedState = null;
2806 }
2807
2808 if (mSavedInstanceState != null) {
2809 super.onRestoreInstanceState(mSavedInstanceState);
2810 mSavedInstanceState = null;
2811 }
2812
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002814
2815 // If we received the result of any pending adds while the loader was running (e.g. the
2816 // widget configuration forced an orientation change), process them now.
2817 for (int i = 0; i < sPendingAddList.size(); i++) {
2818 completeAdd(sPendingAddList.get(i));
2819 }
2820 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002821 }
2822
2823 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002824 * Updates the icons on the launcher that are affected by changes to the package list
2825 * on the device.
2826 */
2827 private void updateIconsAffectedByPackageManagerChanges() {
2828 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002829 updateVoiceSearchIcon();
2830 }
2831
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002832 @Override
2833 public void bindSearchablesChanged() {
2834 updateGlobalSearchIcon();
2835 }
2836
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002837 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 * Add the icons for all apps.
2839 *
2840 * Implementation of the method from LauncherModel.Callbacks.
2841 */
2842 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002843 if (mAppsCustomizeContent != null) {
2844 mAppsCustomizeContent.setApps(apps);
2845 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002846 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002847 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 }
2849
2850 /**
2851 * A package was installed.
2852 *
2853 * Implementation of the method from LauncherModel.Callbacks.
2854 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002855 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002856 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002858
Winson Chung785d2eb2011-04-14 16:08:02 -07002859 if (mAppsCustomizeContent != null) {
2860 mAppsCustomizeContent.addApps(apps);
2861 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002862 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 }
2864
2865 /**
2866 * A package was updated.
2867 *
2868 * Implementation of the method from LauncherModel.Callbacks.
2869 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002870 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002871 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002873 if (mWorkspace != null) {
2874 mWorkspace.updateShortcuts(apps);
2875 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002876
Winson Chung785d2eb2011-04-14 16:08:02 -07002877 if (mAppsCustomizeContent != null) {
2878 mAppsCustomizeContent.updateApps(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 uninstalled.
2885 *
2886 * Implementation of the method from LauncherModel.Callbacks.
2887 */
Joe Onorato36115782010-06-17 13:28:48 -04002888 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002890 if (permanent) {
2891 mWorkspace.removeItems(apps);
2892 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002893
Winson Chung785d2eb2011-04-14 16:08:02 -07002894 if (mAppsCustomizeContent != null) {
2895 mAppsCustomizeContent.removeApps(apps);
2896 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002897 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898 }
Joe Onoratobe386092009-11-17 17:32:16 -08002899
2900 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002901 * A number of packages were updated.
2902 */
2903 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002904
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 if (mAppsCustomizeContent != null) {
2906 mAppsCustomizeContent.onPackagesUpdated();
2907 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002908 }
2909
Winson Chung400438b2011-01-16 17:53:48 -08002910 private int mapConfigurationOriActivityInfoOri(int configOri) {
2911 final Display d = getWindowManager().getDefaultDisplay();
2912 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2913 switch (d.getRotation()) {
2914 case Surface.ROTATION_0:
2915 case Surface.ROTATION_180:
2916 // We are currently in the same basic orientation as the natural orientation
2917 naturalOri = configOri;
2918 break;
2919 case Surface.ROTATION_90:
2920 case Surface.ROTATION_270:
2921 // We are currently in the other basic orientation to the natural orientation
2922 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2923 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2924 break;
2925 }
2926
2927 int[] oriMap = {
2928 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2929 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2930 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2931 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2932 };
2933 // Since the map starts at portrait, we need to offset if this device's natural orientation
2934 // is landscape.
2935 int indexOffset = 0;
2936 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2937 indexOffset = 1;
2938 }
2939 return oriMap[(d.getRotation() + indexOffset) % 4];
2940 }
2941 public void lockScreenOrientation() {
2942 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2943 .getConfiguration().orientation));
2944 }
2945 public void unlockScreenOrientation() {
2946 mHandler.postDelayed(new Runnable() {
2947 public void run() {
2948 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2949 }
2950 }, mRestoreScreenOrientationDelay);
2951 }
2952
Winson Chung80baf5a2010-08-09 16:03:15 -07002953 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002954 * Prints out out state for debugging.
2955 */
2956 public void dumpState() {
2957 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002958 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002959 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2960 Log.d(TAG, "mRestoring=" + mRestoring);
2961 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2962 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002963 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002964 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002965
Winson Chung785d2eb2011-04-14 16:08:02 -07002966 if (mAppsCustomizeContent != null) {
2967 mAppsCustomizeContent.dumpState();
2968 }
Joe Onoratobe386092009-11-17 17:32:16 -08002969 Log.d(TAG, "END launcher2 dump state");
2970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971}
Michael Jurka2763be32011-02-24 11:19:57 -08002972
Michael Jurkaabded662011-03-04 12:06:57 -08002973interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002974 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2975 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002976}