blob: a0601e0043b798f37d5c14d657599b75ed9aa8f0 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070043import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080044import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070045import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070047import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080048import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070049import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040051import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070052import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020054import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070056import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040057import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080058import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070059import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080060import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080061import android.text.Selection;
62import android.text.SpannableStringBuilder;
63import android.text.TextUtils;
64import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070065import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080066import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080067import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.KeyEvent;
69import android.view.LayoutInflater;
70import android.view.Menu;
71import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070072import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080073import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070075import android.view.ViewGroup;
Adam Cohen860f4c52011-01-27 20:16:13 -080076import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080077import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080078import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070080import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070082import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070083import android.widget.TextView;
84import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070085
Adam Cohendf2cc412011-04-27 16:56:57 -070086import com.android.common.Search;
87import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070088import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080089
Adam Cohenc0dcf592011-06-01 15:30:43 -070090import java.io.DataInputStream;
91import java.io.DataOutputStream;
92import java.io.FileNotFoundException;
93import java.io.IOException;
94import java.util.ArrayList;
95import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070096
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097/**
98 * Default launcher application.
99 */
Michael Jurka946ad472010-07-09 18:05:18 -0700100public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700101 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
102 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800103 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700104 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105
Joe Onorato9c1289c2009-08-17 11:03:03 -0400106 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400107 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700108
Winson Chung70d72102011-08-12 11:24:35 -0700109 private static final int MENU_GROUP_WALLPAPER = 1;
110 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
111 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
112 private static final int MENU_HELP = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
114 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700115 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int REQUEST_PICK_APPLICATION = 6;
117 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700118 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700119 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
122
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800123 static final int SCREEN_COUNT = 5;
124 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato7c312c12009-08-13 21:36:53 -0700126 static final int DIALOG_CREATE_SHORTCUT = 1;
127 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Romain Guy98d01652009-06-30 16:21:04 -0700129 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 // Type: int
132 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700133 // Type: int
134 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700136 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
137 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
139 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700140 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700142 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 // Type: boolean
144 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
145 // Type: long
146 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
147
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700148 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
149
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700150 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700151 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700152 private State mState = State.WORKSPACE;
153 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700154
Joe Onorato9c1289c2009-08-17 11:03:03 -0400155 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700156 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
157 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800160 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800162 private final BroadcastReceiver mCloseSystemDialogsReceiver
163 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800164 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private LayoutInflater mInflater;
167
Joe Onorato00acb122009-08-04 16:04:30 -0400168 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700170
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700171 private AppWidgetManager mAppWidgetManager;
172 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700173
Winson Chung3d503fb2011-07-13 17:25:49 -0700174 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700175 private int[] mTmpAddItemCellCoordinates = new int[2];
176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private FolderInfo mFolderInfo;
178
Winson Chung3d503fb2011-07-13 17:25:49 -0700179 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800180 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700181
Winson Chungf0ea4d32011-06-06 14:27:16 -0700182 private SearchDropTargetBar mSearchDeleteBar;
183 private AppsCustomizeTabHost mAppsCustomizeTabHost;
184 private AppsCustomizePagedView mAppsCustomizeContent;
185 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Bundle mSavedState;
188
189 private SpannableStringBuilder mDefaultKeySsb = null;
190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 private boolean mWorkspaceLoading = true;
192
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800193 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private boolean mRestoring;
195 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700196 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
198 private Bundle mSavedInstanceState;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800201 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800202 private boolean mUserPresent = true;
203 private boolean mVisible = false;
204 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700206 private static LocaleConfiguration sLocaleConfiguration = null;
207
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700208 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700209
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700210 private Intent mAppMarketIntent = null;
211
Adam Cohended9f8d2010-11-03 13:25:16 -0700212 // Related to the auto-advancing of widgets
213 private final int ADVANCE_MSG = 1;
214 private final int mAdvanceInterval = 20000;
215 private final int mAdvanceStagger = 250;
216 private long mAutoAdvanceSentTime;
217 private long mAutoAdvanceTimeLeft = -1;
218 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
219 new HashMap<View, AppWidgetProviderInfo>();
220
Winson Chung400438b2011-01-16 17:53:48 -0800221 // Determines how long to wait after a rotation before restoring the screen orientation to
222 // match the sensor state.
223 private final int mRestoreScreenOrientationDelay = 500;
224
Michael Jurka4ef207b2010-11-29 17:05:45 -0800225 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800226 private static Drawable.ConstantState sGlobalSearchIcon;
227 private static Drawable.ConstantState sVoiceSearchIcon;
228 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800229
Adam Cohen2801caf2011-05-13 20:57:39 -0700230 private DragLayer mDragLayer;
231
Michael Jurkaddd62e92011-02-16 17:49:14 -0800232 private BubbleTextView mWaitingForResume;
233
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800234 private static ArrayList<PendingAddArguments> sPendingAddList
235 = new ArrayList<PendingAddArguments>();
236
237 private static class PendingAddArguments {
238 int requestCode;
239 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700240 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800241 int screen;
242 int cellX;
243 int cellY;
244 }
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 @Override
247 protected void onCreate(Bundle savedInstanceState) {
248 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800249 LauncherApplication app = ((LauncherApplication)getApplication());
250 mModel = app.setLauncher(this);
251 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400252 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700254
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700256 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
257 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200260 android.os.Debug.startMethodTracing(
261 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 }
263
264 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 setContentView(R.layout.launcher);
266 setupViews();
267
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800268 registerContentObservers();
269
Joe Onorato7c312c12009-08-13 21:36:53 -0700270 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 mSavedState = savedInstanceState;
273 restoreState(mSavedState);
274
Winson Chunga12a2502010-12-20 14:41:35 -0800275 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700276 if (mAppsCustomizeContent != null) {
277 mAppsCustomizeContent.onPackagesUpdated();
278 }
Winson Chunga12a2502010-12-20 14:41:35 -0800279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 if (PROFILE_STARTUP) {
281 android.os.Debug.stopMethodTracing();
282 }
283
284 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 }
287
Michael Jurkac57b7a82011-08-09 22:02:20 -0700288 if (!mModel.isAllAppsLoaded()) {
289 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
290 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
291 }
292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 // For handling default keys
294 mDefaultKeySsb = new SpannableStringBuilder();
295 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800296
297 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
298 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299
300 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700301 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
302 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100303 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700304 }
305 if (sGlobalSearchIcon != null) {
306 updateGlobalSearchIcon(sGlobalSearchIcon);
307 }
308 if (sVoiceSearchIcon != null) {
309 updateVoiceSearchIcon(sVoiceSearchIcon);
310 }
311 if (sAppMarketIcon != null) {
312 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 }
Romain Guycbb89e42009-06-08 15:52:54 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700317 if (sLocaleConfiguration == null) {
318 new AsyncTask<Void, Void, LocaleConfiguration>() {
319 @Override
320 protected LocaleConfiguration doInBackground(Void... unused) {
321 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
322 readConfiguration(Launcher.this, localeConfiguration);
323 return localeConfiguration;
324 }
325
326 @Override
327 protected void onPostExecute(LocaleConfiguration result) {
328 sLocaleConfiguration = result;
329 checkForLocaleChange(); // recursive, but now with a locale configuration
330 }
331 }.execute();
332 return;
333 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700334
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800335 final Configuration configuration = getResources().getConfiguration();
336
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700337 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 final String locale = configuration.locale.toString();
339
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700340 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 final int mcc = configuration.mcc;
342
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700343 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 final int mnc = configuration.mnc;
345
Romain Guy84f296c2009-11-04 15:00:44 -0800346 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347
Romain Guy84f296c2009-11-04 15:00:44 -0800348 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700349 sLocaleConfiguration.locale = locale;
350 sLocaleConfiguration.mcc = mcc;
351 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700352
Joe Onorato0589f0f2010-02-08 13:44:00 -0800353 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354
355 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
356 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700357 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 public void run() {
359 writeConfiguration(Launcher.this, localeConfiguration);
360 }
361 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700362 }
363 }
364
365 private static class LocaleConfiguration {
366 public String locale;
367 public int mcc = -1;
368 public int mnc = -1;
369 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700370
Romain Guy98d01652009-06-30 16:21:04 -0700371 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
372 DataInputStream in = null;
373 try {
374 in = new DataInputStream(context.openFileInput(PREFERENCES));
375 configuration.locale = in.readUTF();
376 configuration.mcc = in.readInt();
377 configuration.mnc = in.readInt();
378 } catch (FileNotFoundException e) {
379 // Ignore
380 } catch (IOException e) {
381 // Ignore
382 } finally {
383 if (in != null) {
384 try {
385 in.close();
386 } catch (IOException e) {
387 // Ignore
388 }
389 }
390 }
391 }
392
393 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
394 DataOutputStream out = null;
395 try {
396 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
397 out.writeUTF(configuration.locale);
398 out.writeInt(configuration.mcc);
399 out.writeInt(configuration.mnc);
400 out.flush();
401 } catch (FileNotFoundException e) {
402 // Ignore
403 } catch (IOException e) {
404 //noinspection ResultOfMethodCallIgnored
405 context.getFileStreamPath(PREFERENCES).delete();
406 } finally {
407 if (out != null) {
408 try {
409 out.close();
410 } catch (IOException e) {
411 // Ignore
412 }
413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414 }
415 }
416
Adam Cohen716b51e2011-06-30 12:09:54 -0700417 public DragLayer getDragLayer() {
418 return mDragLayer;
419 }
420
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 static int getScreen() {
422 synchronized (sLock) {
423 return sScreen;
424 }
425 }
426
427 static void setScreen(int screen) {
428 synchronized (sLock) {
429 sScreen = screen;
430 }
431 }
432
Winson Chung557d6ed2011-07-08 15:34:52 -0700433 /**
434 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
435 * a configuration step, this allows the proper animations to run after other transitions.
436 */
437 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700438 boolean result = false;
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 Chungb8472bb2011-08-05 13:49:21 -0700450 result = true;
451 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800452 case REQUEST_PICK_APPWIDGET:
453 addAppWidgetFromPick(args.intent);
454 break;
455 case REQUEST_CREATE_APPWIDGET:
456 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700457 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700458 result = true;
459 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800460 case REQUEST_PICK_WALLPAPER:
461 // We just wanted the activity result here so we can clear mWaitingForResult
462 break;
463 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700464 // In any situation where we have a multi-step drop, we should reset the add info only after
465 // we complete the drop
466 resetAddInfo();
467 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800468 }
469
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800471 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700472 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700473 mWaitingForResult = false;
474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 // The pattern used here is that a user PICKs a specific application,
476 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700477
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
479 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700480 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800481 final PendingAddArguments args = new PendingAddArguments();
482 args.requestCode = requestCode;
483 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700484 args.container = mPendingAddInfo.container;
485 args.screen = mPendingAddInfo.screen;
486 args.cellX = mPendingAddInfo.cellX;
487 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488
489 // If the loader is still running, defer the add until it is done.
490 if (isWorkspaceLocked()) {
491 sPendingAddList.add(args);
492 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700493 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 }
Romain Guy18042c82009-11-06 11:44:55 -0800495 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700496 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
497 if (data != null) {
498 // Clean up the appWidgetId if we canceled
499 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
500 if (appWidgetId != -1) {
501 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
502 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 }
504 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700505
506 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700507 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 }
509
510 @Override
511 protected void onResume() {
512 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800513 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700514 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400515 mWorkspaceLoading = true;
516 mModel.startLoader(this, true);
517 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700518 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800520 if (mWaitingForResume != null) {
521 mWaitingForResume.setStayPressed(false);
522 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700523 // When we resume Launcher, a different Activity might be responsible for the app
524 // market intent, so refresh the icon
525 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
527
528 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700529 protected void onPause() {
530 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700531 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800532 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700533 }
Romain Guycbb89e42009-06-08 15:52:54 -0700534
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700535 @Override
536 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400537 // Flag the loader to stop early before switching
538 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700539 if (mAppsCustomizeContent != null) {
540 mAppsCustomizeContent.surrender();
541 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800542 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700543 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800545 // We can't hide the IME if it was forced open. So don't bother
546 /*
547 @Override
548 public void onWindowFocusChanged(boolean hasFocus) {
549 super.onWindowFocusChanged(hasFocus);
550
551 if (hasFocus) {
552 final InputMethodManager inputManager = (InputMethodManager)
553 getSystemService(Context.INPUT_METHOD_SERVICE);
554 WindowManager.LayoutParams lp = getWindow().getAttributes();
555 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
556 android.os.Handler()) {
557 protected void onReceiveResult(int resultCode, Bundle resultData) {
558 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
559 }
560 });
561 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
562 }
563 }
564 */
565
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 private boolean acceptFilter() {
567 final InputMethodManager inputManager = (InputMethodManager)
568 getSystemService(Context.INPUT_METHOD_SERVICE);
569 return !inputManager.isFullscreenMode();
570 }
571
572 @Override
573 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700574 final int uniChar = event.getUnicodeChar();
575 final boolean handled = super.onKeyDown(keyCode, event);
576 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
577 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
579 keyCode, event);
580 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700581 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700582 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700583 // showSearchDialog()
584 // If there are multiple keystrokes before the search dialog takes focus,
585 // onSearchRequested() will be called for every keystroke,
586 // but it is idempotent, so it's fine.
587 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 }
589 }
590
Joe Onorato8a9625e2010-01-28 15:55:35 -0800591 // Eat the long press event so the keyboard doesn't come up.
592 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
593 return true;
594 }
595
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800596 return handled;
597 }
598
Karl Rosaen138a0412009-04-23 19:00:21 -0700599 private String getTypedText() {
600 return mDefaultKeySsb.toString();
601 }
602
603 private void clearTypedText() {
604 mDefaultKeySsb.clear();
605 mDefaultKeySsb.clearSpans();
606 Selection.setSelection(mDefaultKeySsb, 0);
607 }
608
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700610 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
611 * State
612 */
613 private static State intToState(int stateOrdinal) {
614 State state = State.WORKSPACE;
615 final State[] stateValues = State.values();
616 for (int i = 0; i < stateValues.length; i++) {
617 if (stateValues[i].ordinal() == stateOrdinal) {
618 state = stateValues[i];
619 break;
620 }
621 }
622 return state;
623 }
624
625 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 * Restores the previous state, if it exists.
627 *
628 * @param savedState The previous state.
629 */
630 private void restoreState(Bundle savedState) {
631 if (savedState == null) {
632 return;
633 }
634
Michael Jurka883f55b2010-10-21 15:47:14 -0700635 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700636 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800637 showAllApps(false);
638 }
639
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
641 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700642 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644
Winson Chung3d503fb2011-07-13 17:25:49 -0700645 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
646 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700647
Winson Chung3d503fb2011-07-13 17:25:49 -0700648 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
649 mPendingAddInfo.container = pendingAddContainer;
650 mPendingAddInfo.screen = pendingAddScreen;
651 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
652 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 mRestoring = true;
654 }
655
656 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
657 if (renameFolder) {
658 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700659 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 mRestoring = true;
661 }
Winson Chunga12a2502010-12-20 14:41:35 -0800662
Winson Chung785d2eb2011-04-14 16:08:02 -0700663
664 // Restore the AppsCustomize tab
665 if (mAppsCustomizeTabHost != null) {
666 String curTab = savedState.getString("apps_customize_currentTab");
667 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700668 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700669 mAppsCustomizeContent.setContentType(
670 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
671 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700672 mAppsCustomizeContent.loadAssociatedPages(
673 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700674 }
675
Winson Chung5afbf7b2011-07-25 11:53:08 -0700676 int currentIndex = savedState.getInt("apps_customize_currentIndex");
677 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680
681 /**
682 * Finds all the views we need and configure them properly.
683 */
684 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700685 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400686
Winson Chung4c98d922011-05-31 16:50:48 -0700687 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
688 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689
Winson Chung4c98d922011-05-31 16:50:48 -0700690 // Setup the drag layer
691 mDragLayer.setup(this, dragController);
692
Winson Chung3d503fb2011-07-13 17:25:49 -0700693 // Setup the hotseat
694 mHotseat = (Hotseat) findViewById(R.id.hotseat);
695 if (mHotseat != null) {
696 mHotseat.setup(this);
697 }
698
Winson Chung4c98d922011-05-31 16:50:48 -0700699 // Setup the workspace
700 mWorkspace.setHapticFeedbackEnabled(false);
701 mWorkspace.setOnLongClickListener(this);
702 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700703 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700704
Winson Chungf0ea4d32011-06-06 14:27:16 -0700705 // Get the search/delete bar
706 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700707
Winson Chungf0ea4d32011-06-06 14:27:16 -0700708 // Setup AppsCustomize
709 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
710 findViewById(R.id.apps_customize_pane);
711 mAppsCustomizeContent = (AppsCustomizePagedView)
712 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
713 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400714
Romain Guy1fbc1c82009-11-09 20:43:08 -0800715
Winson Chung4c98d922011-05-31 16:50:48 -0700716
Winson Chung3d503fb2011-07-13 17:25:49 -0700717 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700718 dragController.setDragScoller(mWorkspace);
719 dragController.setScrollView(mDragLayer);
720 dragController.setMoveTarget(mWorkspace);
721 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700722 if (mSearchDeleteBar != null) {
723 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726
Romain Guy8a73c512009-11-09 19:19:59 -0800727
Winson Chungaafa03c2010-06-11 17:34:16 -0700728
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729 /**
730 * Creates a view representing a shortcut.
731 *
732 * @param info The data structure describing the shortcut.
733 *
734 * @return A View inflated from R.layout.application.
735 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800736 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700738 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740
741 /**
742 * Creates a view representing a shortcut inflated from the specified resource.
743 *
744 * @param layoutResId The id of the XML layout used to create the shortcut.
745 * @param parent The group the shortcut belongs to.
746 * @param info The data structure describing the shortcut.
747 *
748 * @return A View inflated from layoutResId.
749 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800750 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800751 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
752 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 return favorite;
755 }
756
757 /**
758 * Add an application shortcut to the workspace.
759 *
760 * @param data The intent describing the application.
761 * @param cellInfo The position on screen where to create the shortcut.
762 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700763 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700764 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700765 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700766
Adam Cohenfbba09b2011-07-18 21:43:05 -0700767 // First we check if we already know the exact location where we want to add this item.
768 if (cellX >= 0 && cellY >= 0) {
769 cellXY[0] = cellX;
770 cellXY[1] = cellY;
771 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700772 showOutOfSpaceMessage();
773 return;
774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800776 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800777
Romain Guy73b979d2009-06-09 12:57:21 -0700778 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800779 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800781 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700782 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700783 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800784 } else {
785 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
787 }
Romain Guycbb89e42009-06-08 15:52:54 -0700788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 /**
790 * Add a shortcut to the workspace.
791 *
792 * @param data The intent describing the shortcut.
793 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
796 int cellY) {
797 int[] cellXY = mTmpAddItemCellCoordinates;
798 int[] touchXY = mPendingAddInfo.dropPos;
799 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700800
Michael Jurkad3ef3062010-11-23 16:23:58 -0800801 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700802
Adam Cohen558baaf2011-08-15 15:22:57 -0700803 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
804 final View view = createShortcut(info);
805
Adam Cohenfbba09b2011-07-18 21:43:05 -0700806 // First we check if we already know the exact location where we want to add this item.
807 if (cellX >= 0 && cellY >= 0) {
808 cellXY[0] = cellX;
809 cellXY[1] = cellY;
810 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700811
812 // If appropriate, either create a folder or add to an existing folder
813 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
814 true, null,null)) {
815 return;
816 }
817 DragObject dragObject = new DragObject();
818 dragObject.dragInfo = info;
819 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
820 return;
821 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700822 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800823 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700824 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800825 foundCellSpan = (result != null);
826 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700827 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800828 }
829
830 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700831 showOutOfSpaceMessage();
832 return;
833 }
834
Adam Cohen558baaf2011-08-15 15:22:57 -0700835 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836
837 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700838 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
839 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700844 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700846 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700847 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700850 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700851
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700854
855 // We want to account for the extra amount of padding that we are adding to the widget
856 // to ensure that it gets the full amount of space that it has requested
857 Resources r = getResources();
858 int requiredWidth = appWidgetInfo.minWidth +
859 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
860 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
861 int requiredHeight = appWidgetInfo.minHeight +
862 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
863 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
864 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700867 // We have saved the position to which the widget was dragged-- this really only matters
868 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 int[] cellXY = mTmpAddItemCellCoordinates;
870 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700871 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
873 cellXY[0] = mPendingAddInfo.cellX;
874 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700875 foundCellSpan = true;
876 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700877 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700879 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800880 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700881 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700882 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700883 }
884
Michael Jurka0280c3b2010-09-17 15:00:07 -0700885 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800886 if (appWidgetId != -1) {
887 // Deleting an app widget ID is a void call but writes to disk before returning
888 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800889 new Thread("deleteAppWidgetId") {
890 public void run() {
891 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
892 }
893 }.start();
894 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800896 return;
897 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700899 // Build Launcher-specific widget info and save to database
900 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700901 launcherInfo.spanX = spanXY[0];
902 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700905 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
907 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700909 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700910
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700913
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400915 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700916
917 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 }
919 }
Romain Guycbb89e42009-06-08 15:52:54 -0700920
Adam Cohended9f8d2010-11-03 13:25:16 -0700921 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
922 @Override
923 public void onReceive(Context context, Intent intent) {
924 final String action = intent.getAction();
925 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
926 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700927 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700928 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700929
Winson Chungc100e8e2011-08-09 16:02:43 -0700930 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700931 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700932 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
933 mAppsCustomizeTabHost.reset();
934 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700935 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700936 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
937 mUserPresent = true;
938 updateRunning();
939 }
940 }
941 };
942
943 @Override
944 public void onAttachedToWindow() {
945 super.onAttachedToWindow();
946
947 // Listen for broadcasts related to user-presence
948 final IntentFilter filter = new IntentFilter();
949 filter.addAction(Intent.ACTION_SCREEN_OFF);
950 filter.addAction(Intent.ACTION_USER_PRESENT);
951 registerReceiver(mReceiver, filter);
952
Adam Cohend113e0c2010-11-11 10:48:05 -0800953 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700954 mVisible = true;
955 }
956
957 @Override
958 public void onDetachedFromWindow() {
959 super.onDetachedFromWindow();
960 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700961 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700962
Adam Cohend113e0c2010-11-11 10:48:05 -0800963 if (mAttached) {
964 unregisterReceiver(mReceiver);
965 mAttached = false;
966 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700967 updateRunning();
968 }
969
970 public void onWindowVisibilityChanged(int visibility) {
971 mVisible = visibility == View.VISIBLE;
972 updateRunning();
973 }
974
975 private void sendAdvanceMessage(long delay) {
976 mHandler.removeMessages(ADVANCE_MSG);
977 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
978 mHandler.sendMessageDelayed(msg, delay);
979 mAutoAdvanceSentTime = System.currentTimeMillis();
980 }
981
982 private void updateRunning() {
983 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
984 if (autoAdvanceRunning != mAutoAdvanceRunning) {
985 mAutoAdvanceRunning = autoAdvanceRunning;
986 if (autoAdvanceRunning) {
987 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
988 sendAdvanceMessage(delay);
989 } else {
990 if (!mWidgetsToAdvance.isEmpty()) {
991 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
992 (System.currentTimeMillis() - mAutoAdvanceSentTime));
993 }
994 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800995 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700996 }
997 }
998 }
999
1000 private final Handler mHandler = new Handler() {
1001 @Override
1002 public void handleMessage(Message msg) {
1003 if (msg.what == ADVANCE_MSG) {
1004 int i = 0;
1005 for (View key: mWidgetsToAdvance.keySet()) {
1006 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1007 final int delay = mAdvanceStagger * i;
1008 if (v instanceof Advanceable) {
1009 postDelayed(new Runnable() {
1010 public void run() {
1011 ((Advanceable) v).advance();
1012 }
1013 }, delay);
1014 }
1015 i++;
1016 }
1017 sendAdvanceMessage(mAdvanceInterval);
1018 }
1019 }
1020 };
1021
1022 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001023 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001024 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1025 if (v instanceof Advanceable) {
1026 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001027 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001028 updateRunning();
1029 }
1030 }
1031
1032 void removeWidgetToAutoAdvance(View hostView) {
1033 if (mWidgetsToAdvance.containsKey(hostView)) {
1034 mWidgetsToAdvance.remove(hostView);
1035 updateRunning();
1036 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001037 }
1038
Joe Onorato9c1289c2009-08-17 11:03:03 -04001039 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001040 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001041 launcherInfo.hostView = null;
1042 }
1043
Adam Cohended9f8d2010-11-03 13:25:16 -07001044 void showOutOfSpaceMessage() {
1045 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1046 }
1047
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 public LauncherAppWidgetHost getAppWidgetHost() {
1049 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
Winson Chunga9abd0e2010-10-27 17:18:37 -07001052 public LauncherModel getModel() {
1053 return mModel;
1054 }
1055
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001056 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001057 getWindow().closeAllPanels();
1058
1059 try {
1060 dismissDialog(DIALOG_CREATE_SHORTCUT);
1061 // Unlock the workspace if the dialog was showing
1062 } catch (Exception e) {
1063 // An exception is thrown if the dialog is not visible, which is fine
1064 }
1065
1066 try {
1067 dismissDialog(DIALOG_RENAME_FOLDER);
1068 // Unlock the workspace if the dialog was showing
1069 } catch (Exception e) {
1070 // An exception is thrown if the dialog is not visible, which is fine
1071 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001072
1073 // Whatever we were doing is hereby canceled.
1074 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 @Override
1078 protected void onNewIntent(Intent intent) {
1079 super.onNewIntent(intent);
1080
1081 // Close the menu
1082 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001083 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001084 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001085
Adam Cohen95bb8002011-07-03 23:40:28 -07001086 closeFolder();
1087
Joe Onorato14f122b2009-11-19 14:06:36 -08001088 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1089 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001090
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001091 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001092 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001093 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001094 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001095 }
Winson Chungde1af762011-07-21 16:44:07 -07001096 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001097 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001098
Joe Onorato3a8820b2009-11-10 15:06:42 -08001099 final View v = getWindow().peekDecorView();
1100 if (v != null && v.getWindowToken() != null) {
1101 InputMethodManager imm = (InputMethodManager)getSystemService(
1102 INPUT_METHOD_SERVICE);
1103 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001105
Michael Jurka35aa14d2011-07-07 17:01:08 -07001106 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001107 if (mAppsCustomizeTabHost != null) {
1108 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 }
1111 }
1112
1113 @Override
1114 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1115 // Do not call super here
1116 mSavedInstanceState = savedInstanceState;
1117 }
1118
1119 @Override
1120 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001121 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001122 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123
Michael Jurka883f55b2010-10-21 15:47:14 -07001124 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001125 // We close any open folder since it will not be re-opened, and we need to make sure
1126 // this state is reflected.
1127 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128
Winson Chung3d503fb2011-07-13 17:25:49 -07001129 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1130 mWaitingForResult) {
1131 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1132 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1133 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1134 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136
1137 if (mFolderInfo != null && mWaitingForResult) {
1138 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1139 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1140 }
Michael Jurka946ad472010-07-09 18:05:18 -07001141
Winson Chung785d2eb2011-04-14 16:08:02 -07001142 // Save the current AppsCustomize tab
1143 if (mAppsCustomizeTabHost != null) {
1144 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1145 if (currentTabTag != null) {
1146 outState.putString("apps_customize_currentTab", currentTabTag);
1147 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001148 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1149 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152
1153 @Override
1154 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Winson Chunge7a03942011-08-05 15:05:12 -07001157 // Remove all pending runnables
1158 mHandler.removeMessages(ADVANCE_MSG);
1159 mHandler.removeMessages(0);
1160
Winson Chungcd2b0142011-06-08 16:02:26 -07001161 // Stop callbacks from LauncherModel
1162 LauncherApplication app = ((LauncherApplication) getApplication());
1163 mModel.stopLoader();
1164 app.setLauncher(null);
1165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001167 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001169 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001171 mAppWidgetHost = null;
1172
1173 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174
1175 TextKeyListener.getInstance().release();
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177
Winson Chung3d503fb2011-07-13 17:25:49 -07001178 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001179
1180 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001181 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001182
1183 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1184 mWorkspace.removeAllViews();
1185 mWorkspace = null;
1186 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001187
1188 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190
Adam Cohena9cf38f2011-05-02 15:36:58 -07001191 public DragController getDragController() {
1192 return mDragController;
1193 }
1194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 @Override
1196 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001197 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 super.startActivityForResult(intent, requestCode);
1199 }
1200
Winson Chung70d72102011-08-12 11:24:35 -07001201 /**
1202 * Indicates that we want global search for this activity by setting the globalSearch
1203 * argument for {@link #startSearch} to true.
1204 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001206 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001208
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001209 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Karl Rosaen138a0412009-04-23 19:00:21 -07001211 if (initialQuery == null) {
1212 // Use any text typed in the launcher as the initial query
1213 initialQuery = getTypedText();
1214 clearTypedText();
1215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 if (appSearchData == null) {
1217 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001218 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
Romain Guycbb89e42009-06-08 15:52:54 -07001220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 final SearchManager searchManager =
1222 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001223 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001224 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226
Winson Chung70d72102011-08-12 11:24:35 -07001227 @Override
1228 public boolean onCreateOptionsMenu(Menu menu) {
1229 if (isWorkspaceLocked()) {
1230 return false;
1231 }
1232
1233 super.onCreateOptionsMenu(menu);
1234 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1235 .setIcon(android.R.drawable.ic_menu_gallery)
1236 .setAlphabeticShortcut('W');
1237 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1238 .setIcon(android.R.drawable.ic_menu_manage)
1239 .setAlphabeticShortcut('M');
1240 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1241 .setIcon(android.R.drawable.ic_menu_help)
1242 .setAlphabeticShortcut('H');
1243 return true;
1244 }
1245
1246 @Override
1247 public boolean onPrepareOptionsMenu(Menu menu) {
1248 super.onPrepareOptionsMenu(menu);
1249
1250 if (mAppsCustomizeTabHost.isTransitioning()) {
1251 return false;
1252 }
1253 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1254 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1255
1256 return true;
1257 }
1258
1259 @Override
1260 public boolean onOptionsItemSelected(MenuItem item) {
1261 switch (item.getItemId()) {
1262 case MENU_WALLPAPER_SETTINGS:
1263 startWallpaper();
1264 return true;
1265 case MENU_MANAGE_APPS:
1266 manageApps();
1267 return true;
1268 case MENU_HELP:
1269 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url))));
1270 return true;
1271 }
1272
1273 return super.onOptionsItemSelected(item);
1274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001276 @Override
1277 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001278 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001279 // Use a custom animation for launching search
1280 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001281 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001282 }
1283
Joe Onorato9c1289c2009-08-17 11:03:03 -04001284 public boolean isWorkspaceLocked() {
1285 return mWorkspaceLoading || mWaitingForResult;
1286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001289 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001290 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
Michael Jurka0280c3b2010-09-17 15:00:07 -07001293 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001294 mPendingAddInfo.container = ItemInfo.NO_ID;
1295 mPendingAddInfo.screen = -1;
1296 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1297 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1298 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001299 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001300
Winson Chung7ad01412010-09-27 11:33:03 -07001301 private void manageApps() {
1302 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1303 }
1304
Michael Jurkaaf442092010-06-10 17:01:57 -07001305 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001306 // TODO: catch bad widget exception when sent
1307 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001308 // TODO: Is this log message meaningful?
1309 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001310 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001311 }
1312
Winson Chung55cef262010-10-28 14:14:18 -07001313 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001314 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315
Bjorn Bringert7984c942009-12-09 15:38:25 +00001316 if (appWidget.configure != null) {
1317 // Launch over to configure widget, if needed
1318 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1319 intent.setComponent(appWidget.configure);
1320 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001321 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001322 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1323 intent.putExtra(
1324 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1325 info.mimeType);
1326
1327 final String mimeType = info.mimeType;
1328 final ClipData clipData = (ClipData) info.configurationData;
1329 final ClipDescription clipDesc = clipData.getDescription();
1330 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1331 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001332 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001333 final CharSequence stringData = item.getText();
1334 final Uri uriData = item.getUri();
1335 final Intent intentData = item.getIntent();
1336 final String key =
1337 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1338 if (uriData != null) {
1339 intent.putExtra(key, uriData);
1340 } else if (intentData != null) {
1341 intent.putExtra(key, intentData);
1342 } else if (stringData != null) {
1343 intent.putExtra(key, stringData);
1344 }
1345 break;
1346 }
1347 }
1348 }
Winson Chung55cef262010-10-28 14:14:18 -07001349 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001350
Romain Guy8e633c52010-03-04 12:51:36 -08001351 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001353 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001355
1356 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001357 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 }
1359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Adam Cohenfbba09b2011-07-18 21:43:05 -07001361 /**
1362 * Process a shortcut drop.
1363 *
1364 * @param componentName The name of the component
1365 * @param screen The screen where it should be added
1366 * @param cell The cell it should be added to, optional
1367 * @param position The location on the screen where it was dropped, optional
1368 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1370 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001371 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001372 mPendingAddInfo.container = container;
1373 mPendingAddInfo.screen = screen;
1374 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001375
1376 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001377 mPendingAddInfo.cellX = cell[0];
1378 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001379 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001380
1381 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1382 createShortcutIntent.setComponent(componentName);
1383 processShortcut(createShortcutIntent);
1384 }
1385
Adam Cohenfbba09b2011-07-18 21:43:05 -07001386 /**
1387 * Process a widget drop.
1388 *
1389 * @param info The PendingAppWidgetInfo of the widget being added.
1390 * @param screen The screen where it should be added
1391 * @param cell The cell it should be added to, optional
1392 * @param position The location on the screen where it was dropped, optional
1393 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001394 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1395 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001396 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001397 mPendingAddInfo.container = info.container = container;
1398 mPendingAddInfo.screen = info.screen = screen;
1399 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001400 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001401 mPendingAddInfo.cellX = cell[0];
1402 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001403 }
1404
1405 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1406 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1407 addAppWidgetImpl(appWidgetId, info);
1408 }
1409
Joe Onoratodeb98af2010-02-19 14:59:39 -08001410 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001411 // Handle case where user selected "Applications"
1412 String applicationName = getResources().getString(R.string.group_applications);
1413 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001414
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001415 if (applicationName != null && applicationName.equals(shortcutName)) {
1416 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1417 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001418
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001419 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1420 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001421 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001422 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001423 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001424 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
1427
Winson Chung24ab2f12010-09-16 14:10:47 -07001428 void processWallpaper(Intent intent) {
1429 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1430 }
1431
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1433 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001434 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 folderInfo.title = getText(R.string.folder_name);
1436
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001438 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1439 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001440 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441
1442 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001443 FolderIcon newFolder =
1444 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1445 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1446 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001447 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
Romain Guycbb89e42009-06-08 15:52:54 -07001449
Joe Onorato9c1289c2009-08-17 11:03:03 -04001450 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001451 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001452 }
1453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 private void showNotifications() {
1455 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1456 if (statusBar != null) {
1457 statusBar.expand();
1458 }
1459 }
1460
1461 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001462 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001464 Intent chooser = Intent.createChooser(pickWallpaper,
1465 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001466 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1467 // Removed in Eclair MR1
1468// WallpaperManager wm = (WallpaperManager)
1469// getSystemService(Context.WALLPAPER_SERVICE);
1470// WallpaperInfo wi = wm.getWallpaperInfo();
1471// if (wi != null && wi.getSettingsActivity() != null) {
1472// LabeledIntent li = new LabeledIntent(getPackageName(),
1473// R.string.configure_wallpaper, 0);
1474// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1475// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1476// }
Mike Clerona0618e42009-10-22 13:55:21 -07001477 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001480 /**
1481 * Registers various content observers. The current implementation registers
1482 * only a favorites observer to keep track of the favorites applications.
1483 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001484 private void registerContentObservers() {
1485 ContentResolver resolver = getContentResolver();
1486 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1487 true, mWidgetObserver);
1488 }
1489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 @Override
1491 public boolean dispatchKeyEvent(KeyEvent event) {
1492 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1493 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001495 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001496 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001497 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001498 dumpState();
1499 return true;
1500 }
1501 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001502 }
1503 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1504 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001505 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 return true;
1507 }
1508 }
1509
1510 return super.dispatchKeyEvent(event);
1511 }
1512
Joe Onorato88ec0992009-11-19 13:16:06 -08001513 @Override
1514 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001515 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001516 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001517 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001518 Folder openFolder = mWorkspace.getOpenFolder();
1519 if (openFolder.isEditingName()) {
1520 openFolder.dismissEditingName();
1521 } else {
1522 closeFolder();
1523 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001524 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001525 mWorkspace.exitWidgetResizeMode();
1526
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001527 // Back button is a no-op here, but give at least some feedback for the button press
1528 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001529 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001530 }
1531
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001533 * Re-listen when widgets are reset.
1534 */
1535 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001536 if (mAppWidgetHost != null) {
1537 mAppWidgetHost.startListening();
1538 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001539 }
1540
1541 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001542 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1543 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001546 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 /**
1550 * Launches the intent referred by the clicked shortcut.
1551 *
1552 * @param v The view representing the clicked shortcut.
1553 */
1554 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001555 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1556 // view has detached (it's possible for this to happen if the view is removed mid touch).
1557 if (v.getWindowToken() == null) {
1558 return;
1559 }
1560
1561 if (mWorkspace.isSwitchingState()) {
1562 return;
1563 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001564
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001566 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001568 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001569 int[] pos = new int[2];
1570 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001571 intent.setSourceBounds(new Rect(pos[0], pos[1],
1572 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001573 boolean success = startActivitySafely(intent, tag);
1574
1575 if (success && v instanceof BubbleTextView) {
1576 mWaitingForResume = (BubbleTextView) v;
1577 mWaitingForResume.setStayPressed(true);
1578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001580 if (v instanceof FolderIcon) {
1581 FolderIcon fi = (FolderIcon) v;
1582 handleFolderClick(fi);
1583 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001584 } else if (v == mAllAppsButton) {
1585 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001586 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001587 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001588 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591 }
1592
Michael Jurka0e260592010-06-30 17:07:39 -07001593 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001594 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001595 // clicking anywhere on the workspace causes the customization drawer to slide down
1596 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001597 return false;
1598 }
1599
Michael Jurkaaf442092010-06-10 17:01:57 -07001600 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001601 * Event handler for the search button
1602 *
1603 * @param v The view that was clicked.
1604 */
1605 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001606 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001607 }
1608
1609 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001610 * Event handler for the voice button
1611 *
1612 * @param v The view that was clicked.
1613 */
1614 public void onClickVoiceButton(View v) {
1615 startVoiceSearch();
1616 }
1617
1618 private void startVoiceSearch() {
1619 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1620 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1621 startActivity(intent);
1622 }
1623
1624 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001625 * Event handler for the "grid" button that appears on the home screen, which
1626 * enters all apps mode.
1627 *
1628 * @param v The view that was clicked.
1629 */
1630 public void onClickAllAppsButton(View v) {
1631 showAllApps(true);
1632 }
1633
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001634 public void onClickAppMarketButton(View v) {
1635 if (mAppMarketIntent != null) {
1636 startActivitySafely(mAppMarketIntent, "app market");
1637 }
1638 }
1639
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001640 void startApplicationDetailsActivity(ComponentName componentName) {
1641 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001642 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1643 Uri.fromParts("package", packageName, null));
1644 startActivity(intent);
1645 }
1646
Patrick Dubroy5539af72010-09-07 15:22:01 -07001647 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1648 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1649 // System applications cannot be installed. For now, show a toast explaining that.
1650 // We may give them the option of disabling apps this way.
1651 int messageId = R.string.uninstall_system_app_text;
1652 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1653 } else {
1654 String packageName = appInfo.componentName.getPackageName();
1655 String className = appInfo.componentName.getClassName();
1656 Intent intent = new Intent(
1657 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1658 startActivity(intent);
1659 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001660 }
1661
Michael Jurkaddd62e92011-02-16 17:49:14 -08001662 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1664 try {
1665 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001666 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 } catch (ActivityNotFoundException e) {
1668 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001669 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 } catch (SecurityException e) {
1671 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001672 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001674 "or use the exported attribute for this activity. "
1675 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001677 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001679
Romain Guy8e633c52010-03-04 12:51:36 -08001680 void startActivityForResultSafely(Intent intent, int requestCode) {
1681 try {
1682 startActivityForResult(intent, requestCode);
1683 } catch (ActivityNotFoundException e) {
1684 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1685 } catch (SecurityException e) {
1686 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1687 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1688 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1689 "or use the exported attribute for this activity.", e);
1690 }
1691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692
Adam Cohena9cf38f2011-05-02 15:36:58 -07001693 private void handleFolderClick(FolderIcon folderIcon) {
1694 final FolderInfo info = folderIcon.mInfo;
1695 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 // Close any open folder
1697 closeFolder();
1698 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001699 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 } else {
1701 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001702 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 int folderScreen;
1704 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001705 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 // .. and close it
1707 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001708 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 // Close any folder open on the current screen
1710 closeFolder();
1711 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001712 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
1714 }
1715 }
1716 }
1717
Adam Cohen2801caf2011-05-13 20:57:39 -07001718 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001719 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001720 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1721 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1722 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1723
Adam Cohenc51934b2011-07-26 21:07:43 -07001724 FolderInfo info = (FolderInfo) fi.getTag();
1725 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1726 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001727 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1728 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001729 }
1730
Adam Cohen2801caf2011-05-13 20:57:39 -07001731 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1732 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1733 oa.start();
1734 }
1735
1736 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001737 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001738 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1739 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1740 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1741
Adam Cohenc51934b2011-07-26 21:07:43 -07001742 FolderInfo info = (FolderInfo) fi.getTag();
1743 CellLayout cl = null;
1744 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1745 cl = (CellLayout) fi.getParent().getParent();
1746 }
1747
1748 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001749 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1750 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001751 oa.addListener(new AnimatorListenerAdapter() {
1752 @Override
1753 public void onAnimationEnd(Animator animation) {
1754 if (layout != null) {
1755 layout.clearFolderLeaveBehind();
1756 }
1757 }
1758 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001759 oa.start();
1760 }
1761
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001763 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 * is animated relative to the specified View. If the View is null, no animation
1765 * is played.
1766 *
1767 * @param folderInfo The FolderInfo describing the folder to open.
1768 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001769 public void openFolder(FolderIcon folderIcon) {
1770 Folder folder = folderIcon.mFolder;
1771 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772
Adam Cohen2801caf2011-05-13 20:57:39 -07001773 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001774 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775
Adam Cohen4554ee12011-08-03 16:13:21 -07001776 // Just verify that the folder hasn't already been added to the DragLayer.
1777 // There was a one-off crash where the folder had a parent already.
1778 if (folder.getParent() == null) {
1779 mDragLayer.addView(folder);
1780 mDragController.addDropTarget((DropTarget) folder);
1781 } else {
1782 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1783 folder.getParent() + ").");
1784 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001785 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001786 }
1787
1788 public void closeFolder() {
1789 Folder folder = mWorkspace.getOpenFolder();
1790 if (folder != null) {
1791 closeFolder(folder);
1792 }
1793 }
1794
1795 void closeFolder(Folder folder) {
1796 folder.getInfo().opened = false;
1797
1798 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1799 if (parent != null) {
1800 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1801 shrinkAndFadeInFolderIcon(fi);
1802 }
1803 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001807 if (mState != State.WORKSPACE) {
1808 return false;
1809 }
1810
Joe Onorato9c1289c2009-08-17 11:03:03 -04001811 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 return false;
1813 }
1814
1815 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001816 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818
Michael Jurka0280c3b2010-09-17 15:00:07 -07001819 resetAddInfo();
1820 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001822 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 return true;
1824 }
1825
Winson Chung3d503fb2011-07-13 17:25:49 -07001826 // The hotseat touch handling does not go through Workspace, and we always allow long press
1827 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001828 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001829 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1830 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 if (itemUnderLongClick == null) {
1832 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001833 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1834 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001835 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001837 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841 }
1842 }
1843 return true;
1844 }
1845
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 boolean isHotseatLayout(View layout) {
1847 return mHotseat != null && layout != null &&
1848 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1849 }
1850 Hotseat getHotseat() {
1851 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001852 }
1853
Winson Chung3d503fb2011-07-13 17:25:49 -07001854 /**
1855 * Returns the CellLayout of the specified container at the specified screen.
1856 */
1857 CellLayout getCellLayout(long container, int screen) {
1858 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1859 if (mHotseat != null) {
1860 return mHotseat.getLayout();
1861 } else {
1862 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001863 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001864 } else {
1865 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001866 }
1867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 Workspace getWorkspace() {
1870 return mWorkspace;
1871 }
1872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 @Override
1874 protected Dialog onCreateDialog(int id) {
1875 switch (id) {
1876 case DIALOG_CREATE_SHORTCUT:
1877 return new CreateShortcut().createDialog();
1878 case DIALOG_RENAME_FOLDER:
1879 return new RenameFolder().createDialog();
1880 }
1881
1882 return super.onCreateDialog(id);
1883 }
1884
1885 @Override
1886 protected void onPrepareDialog(int id, Dialog dialog) {
1887 switch (id) {
1888 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 break;
1890 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001891 if (mFolderInfo != null) {
1892 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1893 final CharSequence text = mFolderInfo.title;
1894 input.setText(text);
1895 input.setSelection(0, text.length());
1896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 break;
1898 }
1899 }
1900
1901 void showRenameDialog(FolderInfo info) {
1902 mFolderInfo = info;
1903 mWaitingForResult = true;
1904 showDialog(DIALOG_RENAME_FOLDER);
1905 }
1906
Adam Cohenfbba09b2011-07-18 21:43:05 -07001907 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001908 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001909 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1910 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 mWaitingForResult = true;
1912 showDialog(DIALOG_CREATE_SHORTCUT);
1913 }
1914
1915 private class RenameFolder {
1916 private EditText mInput;
1917
1918 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1920 mInput = (EditText) layout.findViewById(R.id.folder_name);
1921
1922 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1923 builder.setIcon(0);
1924 builder.setTitle(getString(R.string.rename_folder_title));
1925 builder.setCancelable(true);
1926 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1927 public void onCancel(DialogInterface dialog) {
1928 cleanup();
1929 }
1930 });
1931 builder.setNegativeButton(getString(R.string.cancel_action),
1932 new Dialog.OnClickListener() {
1933 public void onClick(DialogInterface dialog, int which) {
1934 cleanup();
1935 }
1936 }
1937 );
1938 builder.setPositiveButton(getString(R.string.rename_action),
1939 new Dialog.OnClickListener() {
1940 public void onClick(DialogInterface dialog, int which) {
1941 changeFolderName();
1942 }
1943 }
1944 );
1945 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001946
1947 final AlertDialog dialog = builder.create();
1948 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1949 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001950 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001951 mInput.requestFocus();
1952 InputMethodManager inputManager = (InputMethodManager)
1953 getSystemService(Context.INPUT_METHOD_SERVICE);
1954 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001955 }
1956 });
1957
1958 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960
1961 private void changeFolderName() {
1962 final String name = mInput.getText().toString();
1963 if (!TextUtils.isEmpty(name)) {
1964 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001965 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 mFolderInfo.title = name;
1967 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1968
Joe Onorato9c1289c2009-08-17 11:03:03 -04001969 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001970 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 } else {
1973 final FolderIcon folderIcon = (FolderIcon)
1974 mWorkspace.getViewForTag(mFolderInfo);
1975 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001976 // TODO: At some point we'll probably want some version of setting
1977 // the text for a folder icon.
1978 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 getWorkspace().requestLayout();
1980 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001981 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 mWorkspaceLoading = true;
1983 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985 }
1986 }
1987 cleanup();
1988 }
1989
1990 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 dismissDialog(DIALOG_RENAME_FOLDER);
1992 mWaitingForResult = false;
1993 mFolderInfo = null;
1994 }
1995 }
1996
Daniel Sandler843e8602010-06-07 14:59:01 -04001997 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1998 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001999 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002000 }
2001
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002002 // AllAppsView.Watcher
2003 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002004 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002005 mWorkspace.setVisibility(View.GONE);
2006 }
2007 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002008
2009 /**
2010 * Helper method for the cameraZoomIn/cameraZoomOut animations
2011 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002012 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002013 * @param scaleFactor The scale factor used for the zoom
2014 */
2015 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2016 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002017
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002018 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002019 // Set pivotY so that at the starting zoom factor, the view is partially
2020 // visible. Modifying initialHeightFactor changes how much of the view is
2021 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002022 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002023 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002024 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002025 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002026 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002027 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002028 }
2029 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002030
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002031 /**
2032 * Zoom the camera out from the workspace to reveal 'toView'.
2033 * Assumes that the view to show is anchored at either the very top or very bottom
2034 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002035 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002036 */
Winson Chungc07918d2011-07-01 15:35:26 -07002037 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002038 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002039
Winson Chungf0ea4d32011-06-06 14:27:16 -07002040 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2041 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2042 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2043 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002044
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002045 setPivotsForZoom(toView, toState, scale);
2046
Michael Jurkad74c9842011-07-10 12:44:21 -07002047 // Shrink workspaces away if going to AppsCustomize from workspace
2048 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002049 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002050
2051 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002052 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002053 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002054 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2055 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002056 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002057 toView.setFastScaleX(a * scale + b * 1f);
2058 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002059 }
2060 });
Adam Cohen61033d32010-11-15 18:29:44 -08002061
Winson Chungf0ea4d32011-06-06 14:27:16 -07002062 toView.setVisibility(View.VISIBLE);
2063 toView.setFastAlpha(0f);
2064 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2065 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2066 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2067 public void onAnimationUpdate(float a, float b) {
2068 // don't need to invalidate because we do so above
2069 toView.setFastAlpha(a * 0f + b * 1f);
2070 }
2071 });
2072 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002073
Michael Jurkaabded662011-03-04 12:06:57 -08002074 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002075 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002076 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002077 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002078 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002079 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002080 // Prepare the position
2081 toView.setTranslationX(0.0f);
2082 toView.setTranslationY(0.0f);
2083 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002084 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002085 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002086 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002087 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002088 // If we don't set the final scale values here, if this animation is cancelled
2089 // it will have the wrong scale value and subsequent cameraPan animations will
2090 // not fix that
2091 toView.setScaleX(1.0f);
2092 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002093 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002094 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002095 }
Winson Chung32174c82011-07-19 15:47:55 -07002096
2097 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2098 // Hide the workspace scrollbar
2099 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002100 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002101 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002102 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002103 });
2104
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002105 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002106 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002107
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002108 if (mStateAnimation != null) mStateAnimation.cancel();
2109 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002110 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002111 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002112 } else {
2113 toView.setTranslationX(0.0f);
2114 toView.setTranslationY(0.0f);
2115 toView.setScaleX(1.0f);
2116 toView.setScaleY(1.0f);
2117 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002118 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002119 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002120 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2121 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002122
Winson Chungc07918d2011-07-01 15:35:26 -07002123 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2124 // Hide the workspace scrollbar
2125 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002126 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002127 }
Michael Jurkaabded662011-03-04 12:06:57 -08002128 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002129 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002130 }
2131
2132 /**
2133 * Zoom the camera back into the workspace, hiding 'fromView'.
2134 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002136 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002137 */
Winson Chungc07918d2011-07-01 15:35:26 -07002138 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002139 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002140
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2142 final float scaleFactor = (float)
2143 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2144 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002145
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002146 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002147
Michael Jurkad3ef3062010-11-23 16:23:58 -08002148 if (!springLoaded) {
2149 mWorkspace.unshrink(animated);
2150 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002151 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002152 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002153 if (mStateAnimation != null) mStateAnimation.cancel();
2154 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002155
Michael Jurka742574b2011-02-02 23:51:01 -08002156 final float oldScaleX = fromView.getScaleX();
2157 final float oldScaleY = fromView.getScaleY();
2158
2159 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2160 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002161 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2162 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002163 ((View)fromView.getParent()).fastInvalidate();
2164 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2165 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2166 }
2167 });
Michael Jurkaabded662011-03-04 12:06:57 -08002168 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002169 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002170 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002171 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2172 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002173 // don't need to invalidate because we do so above
2174 fromView.setFastAlpha(a * 1f + b * 0f);
2175 }
2176 });
Michael Jurkaabded662011-03-04 12:06:57 -08002177 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002178 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002179 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002180 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002181 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002182 public void onAnimationStart(android.animation.Animator animation) {
2183 if (!springLoaded) {
2184 mWorkspace.showDockDivider(false);
2185 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002186 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002187 }
2188 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002189 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002190 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002191 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002192 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002193 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002194 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002195 }
2196 });
2197
Winson Chungf0ea4d32011-06-06 14:27:16 -07002198 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002199 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002200 } else {
2201 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002202 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002203 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2204 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002205
Winson Chungc07918d2011-07-01 15:35:26 -07002206 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002207 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002208 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002209 mWorkspace.flashScrollingIndicator();
2210 }
Michael Jurkaabded662011-03-04 12:06:57 -08002211 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002212 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002213 }
2214
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002215 void showWorkspace(boolean animated) {
2216 showWorkspace(animated, null);
2217 }
2218
2219 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002220 if (layout != null) {
2221 // always animated, but that's ok since we never specify a layout and
2222 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002223 mWorkspace.unshrink(layout);
2224 } else {
2225 mWorkspace.unshrink(animated);
2226 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002227 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002229 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002230
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002231 // Change the state *after* we've called all the transition code
2232 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002233
Winson Chung5fb63472011-02-02 17:03:37 -08002234 // Resume the auto-advance of widgets
2235 mUserPresent = true;
2236 updateRunning();
2237
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002238 // send an accessibility event to announce the context change
2239 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002240 }
2241
Michael Jurkad3ef3062010-11-23 16:23:58 -08002242 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002243 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002244 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002245 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002246 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002247 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002248 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002249 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002250 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002251 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002252 @Override
2253 public void run() {
2254 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002255
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002256 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002257 // Before we show workspace, hide all apps again because
2258 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2259 // clean up our state transition functions
2260 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002261 showWorkspace(true);
2262 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002263 }
2264 }, (extendedDelay ?
2265 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2266 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2267 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002268 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002269 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002270 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002271 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2272 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002273 }
2274 // Otherwise, we are not in spring loaded mode, so don't do anything.
2275 }
2276
Adam Cohenfc53cd22011-07-20 15:45:11 -07002277 public boolean isAllAppsCustomizeOpen() {
2278 return mState == State.APPS_CUSTOMIZE;
2279 }
2280
Winson Chungf0ea4d32011-06-06 14:27:16 -07002281 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002282 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002283 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002284 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002285 if (!LauncherApplication.isScreenLarge()) {
2286 if (animated) {
2287 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002288 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002289 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002291 }
2292 }
2293 }
2294
2295 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 if (!LauncherApplication.isScreenLarge()) {
2300 if (animated) {
2301 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002302 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002303 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002306 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002307 }
2308
Winson Chung785d2eb2011-04-14 16:08:02 -07002309 void showAllApps(boolean animated) {
2310 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002311
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2313 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002314
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002317
Winson Chungf0ea4d32011-06-06 14:27:16 -07002318 // Change the state *after* we've called all the transition code
2319 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002320
2321 // Pause the auto-advance of widgets until we are out of AllApps
2322 mUserPresent = false;
2323 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002324 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002325
2326 // Send an accessibility event to announce the context change
2327 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2328 }
2329
Joe Onoratob2061212009-11-24 16:13:54 -05002330 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002331 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002332 * - Home from workspace
2333 * - from center screen
2334 * - from other screens
2335 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002336 * - from center screen
2337 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002338 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002339 * - from center screen
2340 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002341 * - Launch app from workspace and quit
2342 * - with back
2343 * - with home
2344 * - Launch app from all apps and quit
2345 * - with back
2346 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002347 * - Go to a screen that's not the default, then all
2348 * apps, and launch and app, and go back
2349 * - with back
2350 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002351 * - On workspace, long press power and go back
2352 * - with back
2353 * - with home
2354 * - On all apps, long press power and go back
2355 * - with back
2356 * - with home
2357 * - On workspace, power off
2358 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002359 * - Launch an app and turn off the screen while in that app
2360 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002361 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002362 * - From all apps
2363 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002364 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2365 * - From all apps
2366 * - From the center workspace
2367 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002368 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002369 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002370 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2371 mWorkspace.setVisibility(View.VISIBLE);
2372 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002373
Winson Chung3d503fb2011-07-13 17:25:49 -07002374 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002375 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002376
Winson Chungf0ea4d32011-06-06 14:27:16 -07002377 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002378 if (mAllAppsButton != null) {
2379 mAllAppsButton.requestFocus();
2380 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002381 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002382 }
2383
Joe Onorato7c312c12009-08-13 21:36:53 -07002384 void lockAllApps() {
2385 // TODO
2386 }
2387
2388 void unlockAllApps() {
2389 // TODO
2390 }
2391
Patrick Dubroy5f445422011-02-18 14:35:21 -08002392 /**
2393 * Add an item from all apps or customize onto the given workspace screen.
2394 * If layout is null, add to the current screen.
2395 */
2396 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002397 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2398 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002399 } else {
2400 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002401 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002402 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002403
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002404 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002405 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002406 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002407 // Look for the toolbar icon specified in the activity meta-data
2408 Bundle metaData = packageManager.getActivityInfo(
2409 activityName, PackageManager.GET_META_DATA).metaData;
2410 if (metaData != null) {
2411 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2412 if (iconResId != 0) {
2413 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002414 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002415 }
2416 }
2417 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002418 // This can happen if the activity defines an invalid drawable
2419 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2420 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002421 } catch (Resources.NotFoundException nfe) {
2422 // This can happen if the activity defines an invalid drawable
2423 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2424 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002425 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002426 return null;
2427 }
2428
2429 // if successful in getting icon, return it; otherwise, set button to use default drawable
2430 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2431 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2432 TextView button = (TextView) findViewById(buttonId);
2433 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2434
2435 // If we were unable to find the icon via the meta-data, use a generic one
2436 if (toolbarIcon == null) {
2437 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2438 return null;
2439 } else {
2440 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2441 return toolbarIcon.getConstantState();
2442 }
2443 }
2444
2445 // if successful in getting icon, return it; otherwise, set button to use default drawable
2446 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2447 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2448 ImageView button = (ImageView) findViewById(buttonId);
2449 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2450
Michael Jurka19e0fc52011-07-22 18:00:21 -07002451 if (button != null) {
2452 // If we were unable to find the icon via the meta-data, use a
2453 // generic one
2454 if (toolbarIcon == null) {
2455 button.setImageResource(fallbackDrawableId);
2456 } else {
2457 button.setImageDrawable(toolbarIcon);
2458 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002459 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002460
2461 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2462
Michael Jurka0423dcf2010-10-05 14:56:18 -07002463 }
2464
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002465 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2466 TextView button = (TextView) findViewById(buttonId);
2467 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2468 }
2469
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002470 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002471 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002472 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002473 }
2474
Michael Jurka0423dcf2010-10-05 14:56:18 -07002475 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002476 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2477 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002478
Winson Chung1cad91e2011-05-25 17:41:01 -07002479 final SearchManager searchManager =
2480 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2481 ComponentName activityName = searchManager.getGlobalSearchActivity();
2482 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002483 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002484 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002485 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002486 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002487 } else {
2488 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002489 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002490 }
2491 }
2492
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002493 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002494 updateButtonWithDrawable(R.id.search_button, d);
2495 }
2496
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002497 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002498 final View searchDivider = findViewById(R.id.search_divider);
2499 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002500
Winson Chung1cad91e2011-05-25 17:41:01 -07002501 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2502 ComponentName activityName = intent.resolveActivity(getPackageManager());
2503 if (activityName != null) {
2504 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002505 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2506 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002507 voiceButton.setVisibility(View.VISIBLE);
2508 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002509 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002510 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002511 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002512 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002513
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002514 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002515 updateButtonWithDrawable(R.id.voice_button, d);
2516 }
2517
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002518 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002519 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002520 */
2521 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002522 final View marketButton = findViewById(R.id.market_button);
2523 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2524 // Find the app market activity by resolving an intent.
2525 // (If multiple app markets are installed, it will return the ResolverActivity.)
2526 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002527 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002528 mAppMarketIntent = intent;
2529 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002530 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002531 marketButton.setVisibility(View.VISIBLE);
2532 } else {
2533 // We should hide and disable the view so that we don't try and restore the visibility
2534 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2535 marketButton.setVisibility(View.GONE);
2536 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002537 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002538 }
2539
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002540 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002541 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002542 }
2543
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002544 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 * Displays the shortcut creation dialog and launches, if necessary, the
2546 * appropriate activity.
2547 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002548 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002549 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2550 DialogInterface.OnShowListener {
2551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002553
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002554 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002556
Winson Chung55b65502011-05-26 12:03:43 -07002557 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2558 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002559 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 AlertDialog dialog = builder.create();
2562 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002563 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002564 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002565
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 return dialog;
2567 }
2568
2569 public void onCancel(DialogInterface dialog) {
2570 mWaitingForResult = false;
2571 cleanup();
2572 }
2573
Romain Guycbb89e42009-06-08 15:52:54 -07002574 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002575 mWaitingForResult = false;
2576 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002577 }
2578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002580 try {
2581 dismissDialog(DIALOG_CREATE_SHORTCUT);
2582 } catch (Exception e) {
2583 // An exception is thrown if the dialog is not visible, which is fine
2584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002585 }
2586
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002587 /**
2588 * Handle the action clicked in the "Add to home" dialog.
2589 */
2590 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002591 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002592
Winson Chung55b65502011-05-26 12:03:43 -07002593 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2594 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002595 case AddAdapter.ITEM_APPLICATION: {
2596 if (mAppsCustomizeTabHost != null) {
2597 mAppsCustomizeTabHost.selectAppsTab();
2598 }
2599 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002600 break;
2601 }
Winson Chung55b65502011-05-26 12:03:43 -07002602 case AddAdapter.ITEM_APPWIDGET: {
2603 if (mAppsCustomizeTabHost != null) {
2604 mAppsCustomizeTabHost.selectWidgetsTab();
2605 }
2606 showAllApps(true);
2607 break;
2608 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002609 case AddAdapter.ITEM_WALLPAPER: {
2610 startWallpaper();
2611 break;
2612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 }
2614 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002615
2616 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002617 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619 }
2620
2621 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002622 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002623 */
2624 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2625 @Override
2626 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002627 closeSystemDialogs();
2628 }
2629 }
2630
2631 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002632 * Receives notifications whenever the appwidgets are reset.
2633 */
2634 private class AppWidgetResetObserver extends ContentObserver {
2635 public AppWidgetResetObserver() {
2636 super(new Handler());
2637 }
2638
2639 @Override
2640 public void onChange(boolean selfChange) {
2641 onAppWidgetReset();
2642 }
2643 }
2644
2645 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002646 * If the activity is currently paused, signal that we need to re-run the loader
2647 * in onResume.
2648 *
2649 * This needs to be called from incoming places where resources might have been loaded
2650 * while we are paused. That is becaues the Configuration might be wrong
2651 * when we're not running, and if it comes back to what it was when we
2652 * were paused, we are not restarted.
2653 *
2654 * Implementation of the method from LauncherModel.Callbacks.
2655 *
2656 * @return true if we are currently paused. The caller might be able to
2657 * skip some work in that case since we will come back again.
2658 */
2659 public boolean setLoadOnResume() {
2660 if (mPaused) {
2661 Log.i(TAG, "setLoadOnResume");
2662 mOnResumeNeedsLoad = true;
2663 return true;
2664 } else {
2665 return false;
2666 }
2667 }
2668
2669 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002670 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002673 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002674 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002675 } else {
2676 return SCREEN_COUNT / 2;
2677 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002679
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002680
Joe Onorato9c1289c2009-08-17 11:03:03 -04002681 /**
2682 * Refreshes the shortcuts shown on the workspace.
2683 *
2684 * Implementation of the method from LauncherModel.Callbacks.
2685 */
2686 public void startBinding() {
2687 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002688
2689 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002690 int count = workspace.getChildCount();
2691 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002692 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002693 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2694 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002695 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002696 if (mHotseat != null) {
2697 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002699
Adam Cohen4eac29a2011-07-11 17:53:37 -07002700 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002701 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 }
2703
2704 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002705 * Bind the items start-end from the list.
2706 *
2707 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002709 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002710 setLoadOnResume();
2711
Joe Onorato9c1289c2009-08-17 11:03:03 -04002712 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 for (int i=start; i<end; i++) {
2714 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002715
2716 // Short circuit if we are loading dock items for a configuration which has no dock
2717 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2718 mHotseat == null) {
2719 continue;
2720 }
2721
Joe Onorato9c1289c2009-08-17 11:03:03 -04002722 switch (item.itemType) {
2723 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2724 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002725 View shortcut = createShortcut((ShortcutInfo)item);
2726 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2727 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002728 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002729 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002730 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002731 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002732 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002733 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2734 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002735 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002739 }
2740
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 /**
2742 * Implementation of the method from LauncherModel.Callbacks.
2743 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002744 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002745 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002746 sFolders.clear();
2747 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002749
2750 /**
2751 * Add the views for a widget to the workspace.
2752 *
2753 * Implementation of the method from LauncherModel.Callbacks.
2754 */
2755 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002756 setLoadOnResume();
2757
Daniel Sandler843e8602010-06-07 14:59:01 -04002758 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2759 if (DEBUG_WIDGETS) {
2760 Log.d(TAG, "bindAppWidget: " + item);
2761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002762 final Workspace workspace = mWorkspace;
2763
2764 final int appWidgetId = item.appWidgetId;
2765 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002766 if (DEBUG_WIDGETS) {
2767 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2768 }
2769
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2771
Joe Onorato9c1289c2009-08-17 11:03:03 -04002772 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2773 item.hostView.setTag(item);
2774
Winson Chung3d503fb2011-07-13 17:25:49 -07002775 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 item.cellY, item.spanX, item.spanY, false);
2777
Adam Cohended9f8d2010-11-03 13:25:16 -07002778 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2779
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 workspace.requestLayout();
2781
Daniel Sandler843e8602010-06-07 14:59:01 -04002782 if (DEBUG_WIDGETS) {
2783 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2784 + (SystemClock.uptimeMillis()-start) + "ms");
2785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 }
2787
2788 /**
2789 * Callback saying that there aren't any more items to bind.
2790 *
2791 * Implementation of the method from LauncherModel.Callbacks.
2792 */
2793 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002794 setLoadOnResume();
2795
Joe Onorato9c1289c2009-08-17 11:03:03 -04002796 if (mSavedState != null) {
2797 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002798 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002799 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002800 mSavedState = null;
2801 }
2802
2803 if (mSavedInstanceState != null) {
2804 super.onRestoreInstanceState(mSavedInstanceState);
2805 mSavedInstanceState = null;
2806 }
2807
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002809
2810 // If we received the result of any pending adds while the loader was running (e.g. the
2811 // widget configuration forced an orientation change), process them now.
2812 for (int i = 0; i < sPendingAddList.size(); i++) {
2813 completeAdd(sPendingAddList.get(i));
2814 }
2815 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 }
2817
2818 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002819 * Updates the icons on the launcher that are affected by changes to the package list
2820 * on the device.
2821 */
2822 private void updateIconsAffectedByPackageManagerChanges() {
2823 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002824 updateVoiceSearchIcon();
2825 }
2826
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002827 @Override
2828 public void bindSearchablesChanged() {
2829 updateGlobalSearchIcon();
2830 }
2831
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002832 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 * Add the icons for all apps.
2834 *
2835 * Implementation of the method from LauncherModel.Callbacks.
2836 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002837 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2838 // Remove the progress bar entirely; we could also make it GONE
2839 // but better to remove it since we know it's not going to be used
2840 View progressBar = mAppsCustomizeTabHost.
2841 findViewById(R.id.apps_customize_progress_bar);
2842 if (progressBar != null) {
2843 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002844 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002845 // We just post the call to setApps so the user sees the progress bar
2846 // disappear-- otherwise, it just looks like the progress bar froze
2847 // which doesn't look great
2848 mAppsCustomizeTabHost.post(new Runnable() {
2849 public void run() {
2850 if (mAppsCustomizeContent != null) {
2851 mAppsCustomizeContent.setApps(apps);
2852 }
2853 }
2854 });
2855
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002856 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002857 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 }
2859
2860 /**
2861 * A package was installed.
2862 *
2863 * Implementation of the method from LauncherModel.Callbacks.
2864 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002865 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002866 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002868
Winson Chung785d2eb2011-04-14 16:08:02 -07002869 if (mAppsCustomizeContent != null) {
2870 mAppsCustomizeContent.addApps(apps);
2871 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002872 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 }
2874
2875 /**
2876 * A package was updated.
2877 *
2878 * Implementation of the method from LauncherModel.Callbacks.
2879 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002880 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002881 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002883 if (mWorkspace != null) {
2884 mWorkspace.updateShortcuts(apps);
2885 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002886
Winson Chung785d2eb2011-04-14 16:08:02 -07002887 if (mAppsCustomizeContent != null) {
2888 mAppsCustomizeContent.updateApps(apps);
2889 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002890 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 }
2892
2893 /**
2894 * A package was uninstalled.
2895 *
2896 * Implementation of the method from LauncherModel.Callbacks.
2897 */
Joe Onorato36115782010-06-17 13:28:48 -04002898 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002900 if (permanent) {
2901 mWorkspace.removeItems(apps);
2902 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002903
Winson Chung785d2eb2011-04-14 16:08:02 -07002904 if (mAppsCustomizeContent != null) {
2905 mAppsCustomizeContent.removeApps(apps);
2906 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002907 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 }
Joe Onoratobe386092009-11-17 17:32:16 -08002909
2910 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002911 * A number of packages were updated.
2912 */
2913 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002914
Winson Chung785d2eb2011-04-14 16:08:02 -07002915 if (mAppsCustomizeContent != null) {
2916 mAppsCustomizeContent.onPackagesUpdated();
2917 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002918 }
2919
Winson Chung400438b2011-01-16 17:53:48 -08002920 private int mapConfigurationOriActivityInfoOri(int configOri) {
2921 final Display d = getWindowManager().getDefaultDisplay();
2922 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2923 switch (d.getRotation()) {
2924 case Surface.ROTATION_0:
2925 case Surface.ROTATION_180:
2926 // We are currently in the same basic orientation as the natural orientation
2927 naturalOri = configOri;
2928 break;
2929 case Surface.ROTATION_90:
2930 case Surface.ROTATION_270:
2931 // We are currently in the other basic orientation to the natural orientation
2932 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2933 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2934 break;
2935 }
2936
2937 int[] oriMap = {
2938 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2939 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2940 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2941 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2942 };
2943 // Since the map starts at portrait, we need to offset if this device's natural orientation
2944 // is landscape.
2945 int indexOffset = 0;
2946 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2947 indexOffset = 1;
2948 }
2949 return oriMap[(d.getRotation() + indexOffset) % 4];
2950 }
2951 public void lockScreenOrientation() {
2952 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2953 .getConfiguration().orientation));
2954 }
2955 public void unlockScreenOrientation() {
2956 mHandler.postDelayed(new Runnable() {
2957 public void run() {
2958 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2959 }
2960 }, mRestoreScreenOrientationDelay);
2961 }
2962
Winson Chung80baf5a2010-08-09 16:03:15 -07002963 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002964 * Prints out out state for debugging.
2965 */
2966 public void dumpState() {
2967 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002968 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002969 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2970 Log.d(TAG, "mRestoring=" + mRestoring);
2971 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2972 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002973 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002974 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002975
Winson Chung785d2eb2011-04-14 16:08:02 -07002976 if (mAppsCustomizeContent != null) {
2977 mAppsCustomizeContent.dumpState();
2978 }
Joe Onoratobe386092009-11-17 17:32:16 -08002979 Log.d(TAG, "END launcher2 dump state");
2980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981}
Michael Jurka2763be32011-02-24 11:19:57 -08002982
Michael Jurkaabded662011-03-04 12:06:57 -08002983interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002984 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2985 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002986}