blob: 414cf282b739791efc8c8fa0f85714734f396270 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040052import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
Adam Cohen228da5a2011-07-27 22:23:47 -070054import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040059import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080060import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070061import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080062import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070077import android.view.ViewGroup;
Adam Cohen860f4c52011-01-27 20:16:13 -080078import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080079import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080080import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070082import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070084import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070085import android.widget.TextView;
86import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070087
Adam Cohendf2cc412011-04-27 16:56:57 -070088import com.android.common.Search;
89import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -080090
Adam Cohenc0dcf592011-06-01 15:30:43 -070091import java.io.DataInputStream;
92import java.io.DataOutputStream;
93import java.io.FileNotFoundException;
94import java.io.IOException;
95import java.util.ArrayList;
96import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070097
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098/**
99 * Default launcher application.
100 */
Michael Jurka946ad472010-07-09 18:05:18 -0700101public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700102 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
103 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800104 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700105 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106
Joe Onorato9c1289c2009-08-17 11:03:03 -0400107 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400108 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800111 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700114 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
115 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
117 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700118 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato7c312c12009-08-13 21:36:53 -0700132 static final int DIALOG_CREATE_SHORTCUT = 1;
133 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Romain Guy98d01652009-06-30 16:21:04 -0700135 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 // Type: int
138 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700139 // Type: int
140 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700142 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
143 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
145 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700154 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
155
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700156 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700158 private State mState = State.WORKSPACE;
159 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700162 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
163 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800168 private final BroadcastReceiver mCloseSystemDialogsReceiver
169 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800170 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private LayoutInflater mInflater;
173
Joe Onorato00acb122009-08-04 16:04:30 -0400174 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private AppWidgetManager mAppWidgetManager;
178 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
Winson Chung3d503fb2011-07-13 17:25:49 -0700180 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700181 private int[] mTmpAddItemCellCoordinates = new int[2];
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private FolderInfo mFolderInfo;
184
Winson Chung3d503fb2011-07-13 17:25:49 -0700185 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800186 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187
Winson Chungf0ea4d32011-06-06 14:27:16 -0700188 private SearchDropTargetBar mSearchDeleteBar;
189 private AppsCustomizeTabHost mAppsCustomizeTabHost;
190 private AppsCustomizePagedView mAppsCustomizeContent;
191 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
204 private Bundle mSavedInstanceState;
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800208 private boolean mUserPresent = true;
209 private boolean mVisible = false;
210 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700212 private static LocaleConfiguration sLocaleConfiguration = null;
213
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700214 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700216 private Intent mAppMarketIntent = null;
217
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 // Related to the auto-advancing of widgets
219 private final int ADVANCE_MSG = 1;
220 private final int mAdvanceInterval = 20000;
221 private final int mAdvanceStagger = 250;
222 private long mAutoAdvanceSentTime;
223 private long mAutoAdvanceTimeLeft = -1;
224 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
225 new HashMap<View, AppWidgetProviderInfo>();
226
Winson Chung400438b2011-01-16 17:53:48 -0800227 // Determines how long to wait after a rotation before restoring the screen orientation to
228 // match the sensor state.
229 private final int mRestoreScreenOrientationDelay = 500;
230
Michael Jurka4ef207b2010-11-29 17:05:45 -0800231 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800232 private static Drawable.ConstantState sGlobalSearchIcon;
233 private static Drawable.ConstantState sVoiceSearchIcon;
234 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800235
Adam Cohen2801caf2011-05-13 20:57:39 -0700236 private DragLayer mDragLayer;
237
Michael Jurkaddd62e92011-02-16 17:49:14 -0800238 private BubbleTextView mWaitingForResume;
239
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800240 private static ArrayList<PendingAddArguments> sPendingAddList
241 = new ArrayList<PendingAddArguments>();
242
243 private static class PendingAddArguments {
244 int requestCode;
245 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800247 int screen;
248 int cellX;
249 int cellY;
250 }
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 @Override
253 protected void onCreate(Bundle savedInstanceState) {
254 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 LauncherApplication app = ((LauncherApplication)getApplication());
256 mModel = app.setLauncher(this);
257 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400258 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
263 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200266 android.os.Debug.startMethodTracing(
267 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
270 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
272 setupViews();
273
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800274 registerContentObservers();
275
Joe Onorato7c312c12009-08-13 21:36:53 -0700276 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 mSavedState = savedInstanceState;
279 restoreState(mSavedState);
280
Winson Chunga12a2502010-12-20 14:41:35 -0800281 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700282 if (mAppsCustomizeContent != null) {
283 mAppsCustomizeContent.onPackagesUpdated();
284 }
Winson Chunga12a2502010-12-20 14:41:35 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 if (PROFILE_STARTUP) {
287 android.os.Debug.stopMethodTracing();
288 }
289
290 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400291 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
294 // For handling default keys
295 mDefaultKeySsb = new SpannableStringBuilder();
296 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800297
298 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
299 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800300
301 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700302 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
303 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100304 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700305 }
306 if (sGlobalSearchIcon != null) {
307 updateGlobalSearchIcon(sGlobalSearchIcon);
308 }
309 if (sVoiceSearchIcon != null) {
310 updateVoiceSearchIcon(sVoiceSearchIcon);
311 }
312 if (sAppMarketIcon != null) {
313 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
Romain Guycbb89e42009-06-08 15:52:54 -0700316
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700318 if (sLocaleConfiguration == null) {
319 new AsyncTask<Void, Void, LocaleConfiguration>() {
320 @Override
321 protected LocaleConfiguration doInBackground(Void... unused) {
322 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
323 readConfiguration(Launcher.this, localeConfiguration);
324 return localeConfiguration;
325 }
326
327 @Override
328 protected void onPostExecute(LocaleConfiguration result) {
329 sLocaleConfiguration = result;
330 checkForLocaleChange(); // recursive, but now with a locale configuration
331 }
332 }.execute();
333 return;
334 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700335
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800336 final Configuration configuration = getResources().getConfiguration();
337
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700338 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 final String locale = configuration.locale.toString();
340
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700341 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 final int mcc = configuration.mcc;
343
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700344 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 final int mnc = configuration.mnc;
346
Romain Guy84f296c2009-11-04 15:00:44 -0800347 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348
Romain Guy84f296c2009-11-04 15:00:44 -0800349 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700350 sLocaleConfiguration.locale = locale;
351 sLocaleConfiguration.mcc = mcc;
352 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700353
Joe Onorato0589f0f2010-02-08 13:44:00 -0800354 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355
356 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
357 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700358 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 public void run() {
360 writeConfiguration(Launcher.this, localeConfiguration);
361 }
362 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700363 }
364 }
365
366 private static class LocaleConfiguration {
367 public String locale;
368 public int mcc = -1;
369 public int mnc = -1;
370 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700371
Romain Guy98d01652009-06-30 16:21:04 -0700372 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
373 DataInputStream in = null;
374 try {
375 in = new DataInputStream(context.openFileInput(PREFERENCES));
376 configuration.locale = in.readUTF();
377 configuration.mcc = in.readInt();
378 configuration.mnc = in.readInt();
379 } catch (FileNotFoundException e) {
380 // Ignore
381 } catch (IOException e) {
382 // Ignore
383 } finally {
384 if (in != null) {
385 try {
386 in.close();
387 } catch (IOException e) {
388 // Ignore
389 }
390 }
391 }
392 }
393
394 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
395 DataOutputStream out = null;
396 try {
397 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
398 out.writeUTF(configuration.locale);
399 out.writeInt(configuration.mcc);
400 out.writeInt(configuration.mnc);
401 out.flush();
402 } catch (FileNotFoundException e) {
403 // Ignore
404 } catch (IOException e) {
405 //noinspection ResultOfMethodCallIgnored
406 context.getFileStreamPath(PREFERENCES).delete();
407 } finally {
408 if (out != null) {
409 try {
410 out.close();
411 } catch (IOException e) {
412 // Ignore
413 }
414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
416 }
417
Adam Cohen716b51e2011-06-30 12:09:54 -0700418 public DragLayer getDragLayer() {
419 return mDragLayer;
420 }
421
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422 static int getScreen() {
423 synchronized (sLock) {
424 return sScreen;
425 }
426 }
427
428 static void setScreen(int screen) {
429 synchronized (sLock) {
430 sScreen = screen;
431 }
432 }
433
Winson Chung557d6ed2011-07-08 15:34:52 -0700434 /**
435 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
436 * a configuration step, this allows the proper animations to run after other transitions.
437 */
438 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700439 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800440 switch (args.requestCode) {
441 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700442 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
443 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800444 break;
445 case REQUEST_PICK_SHORTCUT:
446 processShortcut(args.intent);
447 break;
448 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700449 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
450 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700451 result = true;
452 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800453 case REQUEST_PICK_APPWIDGET:
454 addAppWidgetFromPick(args.intent);
455 break;
456 case REQUEST_CREATE_APPWIDGET:
457 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700458 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700459 result = true;
460 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800461 case REQUEST_PICK_WALLPAPER:
462 // We just wanted the activity result here so we can clear mWaitingForResult
463 break;
464 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 // In any situation where we have a multi-step drop, we should reset the add info only after
466 // we complete the drop
467 resetAddInfo();
468 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800469 }
470
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800472 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700473 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700474 mWaitingForResult = false;
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 // The pattern used here is that a user PICKs a specific application,
477 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
480 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700481 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
482 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 final PendingAddArguments args = new PendingAddArguments();
484 args.requestCode = requestCode;
485 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700486 args.container = mPendingAddInfo.container;
487 args.screen = mPendingAddInfo.screen;
488 args.cellX = mPendingAddInfo.cellX;
489 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800490
491 // If the loader is still running, defer the add until it is done.
492 if (isWorkspaceLocked()) {
493 sPendingAddList.add(args);
494 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700495 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 }
Romain Guy18042c82009-11-06 11:44:55 -0800497 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700498 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
499 if (data != null) {
500 // Clean up the appWidgetId if we canceled
501 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
502 if (appWidgetId != -1) {
503 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
506 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700507
508 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700509 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
511
512 @Override
513 protected void onResume() {
514 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800515 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700516 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400517 mWorkspaceLoading = true;
518 mModel.startLoader(this, true);
519 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700520 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800522 if (mWaitingForResume != null) {
523 mWaitingForResume.setStayPressed(false);
524 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700525 // When we resume Launcher, a different Activity might be responsible for the app
526 // market intent, so refresh the icon
527 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 }
529
530 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700531 protected void onPause() {
532 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700533 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800534 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700535 }
Romain Guycbb89e42009-06-08 15:52:54 -0700536
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700537 @Override
538 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400539 // Flag the loader to stop early before switching
540 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700541 if (mAppsCustomizeContent != null) {
542 mAppsCustomizeContent.surrender();
543 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800544 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700545 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800547 // We can't hide the IME if it was forced open. So don't bother
548 /*
549 @Override
550 public void onWindowFocusChanged(boolean hasFocus) {
551 super.onWindowFocusChanged(hasFocus);
552
553 if (hasFocus) {
554 final InputMethodManager inputManager = (InputMethodManager)
555 getSystemService(Context.INPUT_METHOD_SERVICE);
556 WindowManager.LayoutParams lp = getWindow().getAttributes();
557 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
558 android.os.Handler()) {
559 protected void onReceiveResult(int resultCode, Bundle resultData) {
560 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
561 }
562 });
563 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
564 }
565 }
566 */
567
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800568 private boolean acceptFilter() {
569 final InputMethodManager inputManager = (InputMethodManager)
570 getSystemService(Context.INPUT_METHOD_SERVICE);
571 return !inputManager.isFullscreenMode();
572 }
573
574 @Override
575 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700576 final int uniChar = event.getUnicodeChar();
577 final boolean handled = super.onKeyDown(keyCode, event);
578 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
579 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
581 keyCode, event);
582 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700583 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700584 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700585 // showSearchDialog()
586 // If there are multiple keystrokes before the search dialog takes focus,
587 // onSearchRequested() will be called for every keystroke,
588 // but it is idempotent, so it's fine.
589 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
591 }
592
Joe Onorato8a9625e2010-01-28 15:55:35 -0800593 // Eat the long press event so the keyboard doesn't come up.
594 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
595 return true;
596 }
597
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 return handled;
599 }
600
Karl Rosaen138a0412009-04-23 19:00:21 -0700601 private String getTypedText() {
602 return mDefaultKeySsb.toString();
603 }
604
605 private void clearTypedText() {
606 mDefaultKeySsb.clear();
607 mDefaultKeySsb.clearSpans();
608 Selection.setSelection(mDefaultKeySsb, 0);
609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700612 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
613 * State
614 */
615 private static State intToState(int stateOrdinal) {
616 State state = State.WORKSPACE;
617 final State[] stateValues = State.values();
618 for (int i = 0; i < stateValues.length; i++) {
619 if (stateValues[i].ordinal() == stateOrdinal) {
620 state = stateValues[i];
621 break;
622 }
623 }
624 return state;
625 }
626
627 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 * Restores the previous state, if it exists.
629 *
630 * @param savedState The previous state.
631 */
632 private void restoreState(Bundle savedState) {
633 if (savedState == null) {
634 return;
635 }
636
Michael Jurka883f55b2010-10-21 15:47:14 -0700637 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700638 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800639 showAllApps(false);
640 }
641
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
643 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700644 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
646
Winson Chung3d503fb2011-07-13 17:25:49 -0700647 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
648 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700649
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
651 mPendingAddInfo.container = pendingAddContainer;
652 mPendingAddInfo.screen = pendingAddScreen;
653 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
654 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 mRestoring = true;
656 }
657
658 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
659 if (renameFolder) {
660 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700661 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 mRestoring = true;
663 }
Winson Chunga12a2502010-12-20 14:41:35 -0800664
Winson Chung785d2eb2011-04-14 16:08:02 -0700665
666 // Restore the AppsCustomize tab
667 if (mAppsCustomizeTabHost != null) {
668 String curTab = savedState.getString("apps_customize_currentTab");
669 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700670 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700671 mAppsCustomizeContent.setContentType(
672 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
673 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
674 }
675
676 // Note: currently we do not restore the page for the AppsCustomize pane because the
677 // change in layout can drastically affect the saved page index
678 }
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
803 // First we check if we already know the exact location where we want to add this item.
804 if (cellX >= 0 && cellY >= 0) {
805 cellXY[0] = cellX;
806 cellXY[1] = cellY;
807 foundCellSpan = true;
808 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800809 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800811 foundCellSpan = (result != null);
812 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700813 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800814 }
815
816 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700817 showOutOfSpaceMessage();
818 return;
819 }
820
821 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700822 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
824 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700826 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
827 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 }
829 }
830
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700832 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700834 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700835 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700838 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700839
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700840 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700841 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700842
843 // We want to account for the extra amount of padding that we are adding to the widget
844 // to ensure that it gets the full amount of space that it has requested
845 Resources r = getResources();
846 int requiredWidth = appWidgetInfo.minWidth +
847 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
848 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
849 int requiredHeight = appWidgetInfo.minHeight +
850 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
851 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
852 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700853
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700855 // We have saved the position to which the widget was dragged-- this really only matters
856 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700857 int[] cellXY = mTmpAddItemCellCoordinates;
858 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700859 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700860 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
861 cellXY[0] = mPendingAddInfo.cellX;
862 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700863 foundCellSpan = true;
864 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700865 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700866 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700867 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800868 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700869 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700870 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700871 }
872
Michael Jurka0280c3b2010-09-17 15:00:07 -0700873 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800874 if (appWidgetId != -1) {
875 // Deleting an app widget ID is a void call but writes to disk before returning
876 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800877 new Thread("deleteAppWidgetId") {
878 public void run() {
879 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
880 }
881 }.start();
882 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700883 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800884 return;
885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 // Build Launcher-specific widget info and save to database
888 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889 launcherInfo.spanX = spanXY[0];
890 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700891
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700893 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894
895 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700897 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700898
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700899 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700901
Winson Chung3d503fb2011-07-13 17:25:49 -0700902 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400903 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700904
905 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 }
907 }
Romain Guycbb89e42009-06-08 15:52:54 -0700908
Adam Cohended9f8d2010-11-03 13:25:16 -0700909 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
910 @Override
911 public void onReceive(Context context, Intent intent) {
912 final String action = intent.getAction();
913 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
914 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700915 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700916 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700917
Winson Chungc100e8e2011-08-09 16:02:43 -0700918 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700919 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700920 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
921 mAppsCustomizeTabHost.reset();
922 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700923 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700924 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
925 mUserPresent = true;
926 updateRunning();
927 }
928 }
929 };
930
931 @Override
932 public void onAttachedToWindow() {
933 super.onAttachedToWindow();
934
935 // Listen for broadcasts related to user-presence
936 final IntentFilter filter = new IntentFilter();
937 filter.addAction(Intent.ACTION_SCREEN_OFF);
938 filter.addAction(Intent.ACTION_USER_PRESENT);
939 registerReceiver(mReceiver, filter);
940
Adam Cohend113e0c2010-11-11 10:48:05 -0800941 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700942 mVisible = true;
943 }
944
945 @Override
946 public void onDetachedFromWindow() {
947 super.onDetachedFromWindow();
948 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700949 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700950
Adam Cohend113e0c2010-11-11 10:48:05 -0800951 if (mAttached) {
952 unregisterReceiver(mReceiver);
953 mAttached = false;
954 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700955 updateRunning();
956 }
957
958 public void onWindowVisibilityChanged(int visibility) {
959 mVisible = visibility == View.VISIBLE;
960 updateRunning();
961 }
962
963 private void sendAdvanceMessage(long delay) {
964 mHandler.removeMessages(ADVANCE_MSG);
965 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
966 mHandler.sendMessageDelayed(msg, delay);
967 mAutoAdvanceSentTime = System.currentTimeMillis();
968 }
969
970 private void updateRunning() {
971 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
972 if (autoAdvanceRunning != mAutoAdvanceRunning) {
973 mAutoAdvanceRunning = autoAdvanceRunning;
974 if (autoAdvanceRunning) {
975 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
976 sendAdvanceMessage(delay);
977 } else {
978 if (!mWidgetsToAdvance.isEmpty()) {
979 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
980 (System.currentTimeMillis() - mAutoAdvanceSentTime));
981 }
982 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800983 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700984 }
985 }
986 }
987
988 private final Handler mHandler = new Handler() {
989 @Override
990 public void handleMessage(Message msg) {
991 if (msg.what == ADVANCE_MSG) {
992 int i = 0;
993 for (View key: mWidgetsToAdvance.keySet()) {
994 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
995 final int delay = mAdvanceStagger * i;
996 if (v instanceof Advanceable) {
997 postDelayed(new Runnable() {
998 public void run() {
999 ((Advanceable) v).advance();
1000 }
1001 }, delay);
1002 }
1003 i++;
1004 }
1005 sendAdvanceMessage(mAdvanceInterval);
1006 }
1007 }
1008 };
1009
1010 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001011 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001012 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1013 if (v instanceof Advanceable) {
1014 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001015 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001016 updateRunning();
1017 }
1018 }
1019
1020 void removeWidgetToAutoAdvance(View hostView) {
1021 if (mWidgetsToAdvance.containsKey(hostView)) {
1022 mWidgetsToAdvance.remove(hostView);
1023 updateRunning();
1024 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001025 }
1026
Joe Onorato9c1289c2009-08-17 11:03:03 -04001027 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001028 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001029 launcherInfo.hostView = null;
1030 }
1031
Adam Cohended9f8d2010-11-03 13:25:16 -07001032 void showOutOfSpaceMessage() {
1033 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1034 }
1035
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 public LauncherAppWidgetHost getAppWidgetHost() {
1037 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
Romain Guycbb89e42009-06-08 15:52:54 -07001039
Winson Chunga9abd0e2010-10-27 17:18:37 -07001040 public LauncherModel getModel() {
1041 return mModel;
1042 }
1043
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001044 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001045 getWindow().closeAllPanels();
1046
1047 try {
1048 dismissDialog(DIALOG_CREATE_SHORTCUT);
1049 // Unlock the workspace if the dialog was showing
1050 } catch (Exception e) {
1051 // An exception is thrown if the dialog is not visible, which is fine
1052 }
1053
1054 try {
1055 dismissDialog(DIALOG_RENAME_FOLDER);
1056 // Unlock the workspace if the dialog was showing
1057 } catch (Exception e) {
1058 // An exception is thrown if the dialog is not visible, which is fine
1059 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001060
1061 // Whatever we were doing is hereby canceled.
1062 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001063 }
1064
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 @Override
1066 protected void onNewIntent(Intent intent) {
1067 super.onNewIntent(intent);
1068
1069 // Close the menu
1070 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001071 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001072 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001073
Adam Cohen95bb8002011-07-03 23:40:28 -07001074 closeFolder();
1075
Joe Onorato14f122b2009-11-19 14:06:36 -08001076 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1077 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001078
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001079 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001080 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001081 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001082 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001083 }
Winson Chungde1af762011-07-21 16:44:07 -07001084 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001085 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001086
Joe Onorato3a8820b2009-11-10 15:06:42 -08001087 final View v = getWindow().peekDecorView();
1088 if (v != null && v.getWindowToken() != null) {
1089 InputMethodManager imm = (InputMethodManager)getSystemService(
1090 INPUT_METHOD_SERVICE);
1091 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001093
Michael Jurka35aa14d2011-07-07 17:01:08 -07001094 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001095 if (mAppsCustomizeTabHost != null) {
1096 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 }
1099 }
1100
1101 @Override
1102 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1103 // Do not call super here
1104 mSavedInstanceState = savedInstanceState;
1105 }
1106
1107 @Override
1108 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001109 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001110 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111
Michael Jurka883f55b2010-10-21 15:47:14 -07001112 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001113 // We close any open folder since it will not be re-opened, and we need to make sure
1114 // this state is reflected.
1115 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
Winson Chung3d503fb2011-07-13 17:25:49 -07001117 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1118 mWaitingForResult) {
1119 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1120 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1121 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1122 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
1125 if (mFolderInfo != null && mWaitingForResult) {
1126 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1127 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1128 }
Michael Jurka946ad472010-07-09 18:05:18 -07001129
Winson Chung785d2eb2011-04-14 16:08:02 -07001130 // Save the current AppsCustomize tab
1131 if (mAppsCustomizeTabHost != null) {
1132 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1133 if (currentTabTag != null) {
1134 outState.putString("apps_customize_currentTab", currentTabTag);
1135 }
1136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
1138
1139 @Override
1140 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Winson Chunge7a03942011-08-05 15:05:12 -07001143 // Remove all pending runnables
1144 mHandler.removeMessages(ADVANCE_MSG);
1145 mHandler.removeMessages(0);
1146
Winson Chungcd2b0142011-06-08 16:02:26 -07001147 // Stop callbacks from LauncherModel
1148 LauncherApplication app = ((LauncherApplication) getApplication());
1149 mModel.stopLoader();
1150 app.setLauncher(null);
1151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001153 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001155 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001157 mAppWidgetHost = null;
1158
1159 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160
1161 TextKeyListener.getInstance().release();
1162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163
Winson Chung3d503fb2011-07-13 17:25:49 -07001164 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001165
1166 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001167 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001168
1169 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1170 mWorkspace.removeAllViews();
1171 mWorkspace = null;
1172 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001173
1174 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
1176
Adam Cohena9cf38f2011-05-02 15:36:58 -07001177 public DragController getDragController() {
1178 return mDragController;
1179 }
1180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 @Override
1182 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001183 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 super.startActivityForResult(intent, requestCode);
1185 }
1186
1187 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001188 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001190
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001191 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001192
Karl Rosaen138a0412009-04-23 19:00:21 -07001193 if (initialQuery == null) {
1194 // Use any text typed in the launcher as the initial query
1195 initialQuery = getTypedText();
1196 clearTypedText();
1197 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 if (appSearchData == null) {
1199 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001200 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
Romain Guycbb89e42009-06-08 15:52:54 -07001202
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 final SearchManager searchManager =
1204 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001205 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001206 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
Karl Rosaen138a0412009-04-23 19:00:21 -07001209 /**
1210 * Indicates that we want global search for this activity by setting the globalSearch
1211 * argument for {@link #startSearch} to true.
1212 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001214 @Override
1215 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001216 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001217 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001218 }
1219
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 public boolean isWorkspaceLocked() {
1221 return mWorkspaceLoading || mWaitingForResult;
1222 }
1223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001225 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001226 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
Michael Jurka0280c3b2010-09-17 15:00:07 -07001229 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001230 mPendingAddInfo.container = ItemInfo.NO_ID;
1231 mPendingAddInfo.screen = -1;
1232 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1233 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1234 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001236
Winson Chung7ad01412010-09-27 11:33:03 -07001237 private void manageApps() {
1238 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1239 }
1240
Michael Jurkaaf442092010-06-10 17:01:57 -07001241 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001242 // TODO: catch bad widget exception when sent
1243 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001244 // TODO: Is this log message meaningful?
1245 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001246 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001247 }
1248
Winson Chung55cef262010-10-28 14:14:18 -07001249 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001250 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
Bjorn Bringert7984c942009-12-09 15:38:25 +00001252 if (appWidget.configure != null) {
1253 // Launch over to configure widget, if needed
1254 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1255 intent.setComponent(appWidget.configure);
1256 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001257 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001258 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1259 intent.putExtra(
1260 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1261 info.mimeType);
1262
1263 final String mimeType = info.mimeType;
1264 final ClipData clipData = (ClipData) info.configurationData;
1265 final ClipDescription clipDesc = clipData.getDescription();
1266 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1267 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001268 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001269 final CharSequence stringData = item.getText();
1270 final Uri uriData = item.getUri();
1271 final Intent intentData = item.getIntent();
1272 final String key =
1273 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1274 if (uriData != null) {
1275 intent.putExtra(key, uriData);
1276 } else if (intentData != null) {
1277 intent.putExtra(key, intentData);
1278 } else if (stringData != null) {
1279 intent.putExtra(key, stringData);
1280 }
1281 break;
1282 }
1283 }
1284 }
Winson Chung55cef262010-10-28 14:14:18 -07001285 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001286
Romain Guy8e633c52010-03-04 12:51:36 -08001287 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001289 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001290 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001291
1292 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001293 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 }
1295 }
Romain Guycbb89e42009-06-08 15:52:54 -07001296
Adam Cohenfbba09b2011-07-18 21:43:05 -07001297 /**
1298 * Process a shortcut drop.
1299 *
1300 * @param componentName The name of the component
1301 * @param screen The screen where it should be added
1302 * @param cell The cell it should be added to, optional
1303 * @param position The location on the screen where it was dropped, optional
1304 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001305 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1306 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001307 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001308 mPendingAddInfo.container = container;
1309 mPendingAddInfo.screen = screen;
1310 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001311
1312 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001313 mPendingAddInfo.cellX = cell[0];
1314 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001315 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001316
1317 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1318 createShortcutIntent.setComponent(componentName);
1319 processShortcut(createShortcutIntent);
1320 }
1321
Adam Cohenfbba09b2011-07-18 21:43:05 -07001322 /**
1323 * Process a widget drop.
1324 *
1325 * @param info The PendingAppWidgetInfo of the widget being added.
1326 * @param screen The screen where it should be added
1327 * @param cell The cell it should be added to, optional
1328 * @param position The location on the screen where it was dropped, optional
1329 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001330 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1331 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001332 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001333 mPendingAddInfo.container = info.container = container;
1334 mPendingAddInfo.screen = info.screen = screen;
1335 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001336 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 mPendingAddInfo.cellX = cell[0];
1338 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001339 }
1340
1341 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1342 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1343 addAppWidgetImpl(appWidgetId, info);
1344 }
1345
Joe Onoratodeb98af2010-02-19 14:59:39 -08001346 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001347 // Handle case where user selected "Applications"
1348 String applicationName = getResources().getString(R.string.group_applications);
1349 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001350
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001351 if (applicationName != null && applicationName.equals(shortcutName)) {
1352 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1353 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001354
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001355 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1356 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001357 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001358 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001359 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001360 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363
Winson Chung24ab2f12010-09-16 14:10:47 -07001364 void processWallpaper(Intent intent) {
1365 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1366 }
1367
Winson Chung3d503fb2011-07-13 17:25:49 -07001368 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1369 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001370 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 folderInfo.title = getText(R.string.folder_name);
1372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1375 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001376 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
1378 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 FolderIcon newFolder =
1380 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1381 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1382 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001383 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
Romain Guycbb89e42009-06-08 15:52:54 -07001385
Joe Onorato9c1289c2009-08-17 11:03:03 -04001386 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001387 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 }
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 private void showNotifications() {
1391 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1392 if (statusBar != null) {
1393 statusBar.expand();
1394 }
1395 }
1396
1397 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001398 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001400 Intent chooser = Intent.createChooser(pickWallpaper,
1401 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001402 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1403 // Removed in Eclair MR1
1404// WallpaperManager wm = (WallpaperManager)
1405// getSystemService(Context.WALLPAPER_SERVICE);
1406// WallpaperInfo wi = wm.getWallpaperInfo();
1407// if (wi != null && wi.getSettingsActivity() != null) {
1408// LabeledIntent li = new LabeledIntent(getPackageName(),
1409// R.string.configure_wallpaper, 0);
1410// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1411// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1412// }
Mike Clerona0618e42009-10-22 13:55:21 -07001413 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001416 /**
1417 * Registers various content observers. The current implementation registers
1418 * only a favorites observer to keep track of the favorites applications.
1419 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001420 private void registerContentObservers() {
1421 ContentResolver resolver = getContentResolver();
1422 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1423 true, mWidgetObserver);
1424 }
1425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 @Override
1427 public boolean dispatchKeyEvent(KeyEvent event) {
1428 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1429 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001431 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001432 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001433 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001434 dumpState();
1435 return true;
1436 }
1437 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001438 }
1439 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1440 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001441 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 return true;
1443 }
1444 }
1445
1446 return super.dispatchKeyEvent(event);
1447 }
1448
Joe Onorato88ec0992009-11-19 13:16:06 -08001449 @Override
1450 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001451 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001452 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001453 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001454 Folder openFolder = mWorkspace.getOpenFolder();
1455 if (openFolder.isEditingName()) {
1456 openFolder.dismissEditingName();
1457 } else {
1458 closeFolder();
1459 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001460 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001461 mWorkspace.exitWidgetResizeMode();
1462
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001463 // Back button is a no-op here, but give at least some feedback for the button press
1464 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001465 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001466 }
1467
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001469 * Re-listen when widgets are reset.
1470 */
1471 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001472 if (mAppWidgetHost != null) {
1473 mAppWidgetHost.startListening();
1474 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001475 }
1476
1477 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1479 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001481 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001482 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 /**
1486 * Launches the intent referred by the clicked shortcut.
1487 *
1488 * @param v The view representing the clicked shortcut.
1489 */
1490 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001491 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1492 // view has detached (it's possible for this to happen if the view is removed mid touch).
1493 if (v.getWindowToken() == null) {
1494 return;
1495 }
1496
1497 if (mWorkspace.isSwitchingState()) {
1498 return;
1499 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001502 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001504 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001505 int[] pos = new int[2];
1506 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001507 intent.setSourceBounds(new Rect(pos[0], pos[1],
1508 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001509 boolean success = startActivitySafely(intent, tag);
1510
1511 if (success && v instanceof BubbleTextView) {
1512 mWaitingForResume = (BubbleTextView) v;
1513 mWaitingForResume.setStayPressed(true);
1514 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001516 if (v instanceof FolderIcon) {
1517 FolderIcon fi = (FolderIcon) v;
1518 handleFolderClick(fi);
1519 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001520 } else if (v == mAllAppsButton) {
1521 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001522 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001523 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001524 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
1527 }
1528
Michael Jurka0e260592010-06-30 17:07:39 -07001529 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001530 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001531 // clicking anywhere on the workspace causes the customization drawer to slide down
1532 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001533 return false;
1534 }
1535
Michael Jurkaaf442092010-06-10 17:01:57 -07001536 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001537 * Event handler for the search button
1538 *
1539 * @param v The view that was clicked.
1540 */
1541 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001542 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001543 // Use a custom animation for launching search
1544 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001545 }
1546
1547 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001548 * Event handler for the voice button
1549 *
1550 * @param v The view that was clicked.
1551 */
1552 public void onClickVoiceButton(View v) {
1553 startVoiceSearch();
1554 }
1555
1556 private void startVoiceSearch() {
1557 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1558 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1559 startActivity(intent);
1560 }
1561
1562 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001563 * Event handler for the "grid" button that appears on the home screen, which
1564 * enters all apps mode.
1565 *
1566 * @param v The view that was clicked.
1567 */
1568 public void onClickAllAppsButton(View v) {
1569 showAllApps(true);
1570 }
1571
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001572 public void onClickAppMarketButton(View v) {
1573 if (mAppMarketIntent != null) {
1574 startActivitySafely(mAppMarketIntent, "app market");
1575 }
1576 }
1577
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001578 void startApplicationDetailsActivity(ComponentName componentName) {
1579 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001580 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1581 Uri.fromParts("package", packageName, null));
1582 startActivity(intent);
1583 }
1584
Patrick Dubroy5539af72010-09-07 15:22:01 -07001585 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1586 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1587 // System applications cannot be installed. For now, show a toast explaining that.
1588 // We may give them the option of disabling apps this way.
1589 int messageId = R.string.uninstall_system_app_text;
1590 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1591 } else {
1592 String packageName = appInfo.componentName.getPackageName();
1593 String className = appInfo.componentName.getClassName();
1594 Intent intent = new Intent(
1595 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1596 startActivity(intent);
1597 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001598 }
1599
Michael Jurkaddd62e92011-02-16 17:49:14 -08001600 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1602 try {
1603 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001604 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 } catch (ActivityNotFoundException e) {
1606 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001607 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 } catch (SecurityException e) {
1609 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001610 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001612 "or use the exported attribute for this activity. "
1613 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001615 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001617
Romain Guy8e633c52010-03-04 12:51:36 -08001618 void startActivityForResultSafely(Intent intent, int requestCode) {
1619 try {
1620 startActivityForResult(intent, requestCode);
1621 } catch (ActivityNotFoundException e) {
1622 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1623 } catch (SecurityException e) {
1624 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1625 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1626 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1627 "or use the exported attribute for this activity.", e);
1628 }
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
Adam Cohena9cf38f2011-05-02 15:36:58 -07001631 private void handleFolderClick(FolderIcon folderIcon) {
1632 final FolderInfo info = folderIcon.mInfo;
1633 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 // Close any open folder
1635 closeFolder();
1636 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001637 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 } else {
1639 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001640 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 int folderScreen;
1642 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001643 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 // .. and close it
1645 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001646 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 // Close any folder open on the current screen
1648 closeFolder();
1649 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001650 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652 }
1653 }
1654 }
1655
Adam Cohen2801caf2011-05-13 20:57:39 -07001656 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001657 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001658 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1659 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1660 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1661
Adam Cohenc51934b2011-07-26 21:07:43 -07001662 FolderInfo info = (FolderInfo) fi.getTag();
1663 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1664 CellLayout cl = (CellLayout) fi.getParent().getParent();
1665 cl.setFolderLeaveBehindCell(info.cellX, info.cellY);
1666 }
1667
Adam Cohen2801caf2011-05-13 20:57:39 -07001668 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1669 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1670 oa.start();
1671 }
1672
1673 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001674 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001675 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1676 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1677 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1678
Adam Cohenc51934b2011-07-26 21:07:43 -07001679 FolderInfo info = (FolderInfo) fi.getTag();
1680 CellLayout cl = null;
1681 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1682 cl = (CellLayout) fi.getParent().getParent();
1683 }
1684
1685 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001686 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1687 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001688 oa.addListener(new AnimatorListenerAdapter() {
1689 @Override
1690 public void onAnimationEnd(Animator animation) {
1691 if (layout != null) {
1692 layout.clearFolderLeaveBehind();
1693 }
1694 }
1695 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001696 oa.start();
1697 }
1698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001700 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 * is animated relative to the specified View. If the View is null, no animation
1702 * is played.
1703 *
1704 * @param folderInfo The FolderInfo describing the folder to open.
1705 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001706 public void openFolder(FolderIcon folderIcon) {
1707 Folder folder = folderIcon.mFolder;
1708 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709
Adam Cohen2801caf2011-05-13 20:57:39 -07001710 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001711 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712
Adam Cohen4554ee12011-08-03 16:13:21 -07001713 // Just verify that the folder hasn't already been added to the DragLayer.
1714 // There was a one-off crash where the folder had a parent already.
1715 if (folder.getParent() == null) {
1716 mDragLayer.addView(folder);
1717 mDragController.addDropTarget((DropTarget) folder);
1718 } else {
1719 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1720 folder.getParent() + ").");
1721 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001722 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001723 }
1724
1725 public void closeFolder() {
1726 Folder folder = mWorkspace.getOpenFolder();
1727 if (folder != null) {
1728 closeFolder(folder);
1729 }
1730 }
1731
1732 void closeFolder(Folder folder) {
1733 folder.getInfo().opened = false;
1734
1735 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1736 if (parent != null) {
1737 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1738 shrinkAndFadeInFolderIcon(fi);
1739 }
1740 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
1742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001744 if (mState != State.WORKSPACE) {
1745 return false;
1746 }
1747
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 return false;
1750 }
1751
1752 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001753 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755
Michael Jurka0280c3b2010-09-17 15:00:07 -07001756 resetAddInfo();
1757 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001759 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 return true;
1761 }
1762
Winson Chung3d503fb2011-07-13 17:25:49 -07001763 // The hotseat touch handling does not go through Workspace, and we always allow long press
1764 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001766 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1767 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 if (itemUnderLongClick == null) {
1769 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1771 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001772 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
1778 }
1779 }
1780 return true;
1781 }
1782
Winson Chung3d503fb2011-07-13 17:25:49 -07001783 boolean isHotseatLayout(View layout) {
1784 return mHotseat != null && layout != null &&
1785 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1786 }
1787 Hotseat getHotseat() {
1788 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001789 }
1790
Winson Chung3d503fb2011-07-13 17:25:49 -07001791 /**
1792 * Returns the CellLayout of the specified container at the specified screen.
1793 */
1794 CellLayout getCellLayout(long container, int screen) {
1795 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1796 if (mHotseat != null) {
1797 return mHotseat.getLayout();
1798 } else {
1799 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001800 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001801 } else {
1802 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001803 }
1804 }
1805
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 Workspace getWorkspace() {
1807 return mWorkspace;
1808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 protected Dialog onCreateDialog(int id) {
1812 switch (id) {
1813 case DIALOG_CREATE_SHORTCUT:
1814 return new CreateShortcut().createDialog();
1815 case DIALOG_RENAME_FOLDER:
1816 return new RenameFolder().createDialog();
1817 }
1818
1819 return super.onCreateDialog(id);
1820 }
1821
1822 @Override
1823 protected void onPrepareDialog(int id, Dialog dialog) {
1824 switch (id) {
1825 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 break;
1827 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001828 if (mFolderInfo != null) {
1829 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1830 final CharSequence text = mFolderInfo.title;
1831 input.setText(text);
1832 input.setSelection(0, text.length());
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 break;
1835 }
1836 }
1837
1838 void showRenameDialog(FolderInfo info) {
1839 mFolderInfo = info;
1840 mWaitingForResult = true;
1841 showDialog(DIALOG_RENAME_FOLDER);
1842 }
1843
Adam Cohenfbba09b2011-07-18 21:43:05 -07001844 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001845 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001846 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1847 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 mWaitingForResult = true;
1849 showDialog(DIALOG_CREATE_SHORTCUT);
1850 }
1851
1852 private class RenameFolder {
1853 private EditText mInput;
1854
1855 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1857 mInput = (EditText) layout.findViewById(R.id.folder_name);
1858
1859 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1860 builder.setIcon(0);
1861 builder.setTitle(getString(R.string.rename_folder_title));
1862 builder.setCancelable(true);
1863 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1864 public void onCancel(DialogInterface dialog) {
1865 cleanup();
1866 }
1867 });
1868 builder.setNegativeButton(getString(R.string.cancel_action),
1869 new Dialog.OnClickListener() {
1870 public void onClick(DialogInterface dialog, int which) {
1871 cleanup();
1872 }
1873 }
1874 );
1875 builder.setPositiveButton(getString(R.string.rename_action),
1876 new Dialog.OnClickListener() {
1877 public void onClick(DialogInterface dialog, int which) {
1878 changeFolderName();
1879 }
1880 }
1881 );
1882 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001883
1884 final AlertDialog dialog = builder.create();
1885 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1886 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001887 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001888 mInput.requestFocus();
1889 InputMethodManager inputManager = (InputMethodManager)
1890 getSystemService(Context.INPUT_METHOD_SERVICE);
1891 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001892 }
1893 });
1894
1895 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 }
1897
1898 private void changeFolderName() {
1899 final String name = mInput.getText().toString();
1900 if (!TextUtils.isEmpty(name)) {
1901 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001902 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 mFolderInfo.title = name;
1904 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1905
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001907 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001908 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 } else {
1910 final FolderIcon folderIcon = (FolderIcon)
1911 mWorkspace.getViewForTag(mFolderInfo);
1912 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001913 // TODO: At some point we'll probably want some version of setting
1914 // the text for a folder icon.
1915 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 getWorkspace().requestLayout();
1917 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001918 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 mWorkspaceLoading = true;
1920 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922 }
1923 }
1924 cleanup();
1925 }
1926
1927 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 dismissDialog(DIALOG_RENAME_FOLDER);
1929 mWaitingForResult = false;
1930 mFolderInfo = null;
1931 }
1932 }
1933
Daniel Sandler843e8602010-06-07 14:59:01 -04001934 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1935 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001936 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001937 }
1938
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001939 // AllAppsView.Watcher
1940 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001941 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001942 mWorkspace.setVisibility(View.GONE);
1943 }
1944 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001945
1946 /**
1947 * Helper method for the cameraZoomIn/cameraZoomOut animations
1948 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07001949 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001950 * @param scaleFactor The scale factor used for the zoom
1951 */
1952 private void setPivotsForZoom(View view, State state, float scaleFactor) {
1953 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08001954
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001955 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08001956 // Set pivotY so that at the starting zoom factor, the view is partially
1957 // visible. Modifying initialHeightFactor changes how much of the view is
1958 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001959 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08001960 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08001961 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001962 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08001963 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08001964 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001965 }
1966 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001967
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001968 /**
1969 * Zoom the camera out from the workspace to reveal 'toView'.
1970 * Assumes that the view to show is anchored at either the very top or very bottom
1971 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001972 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001973 */
Winson Chungc07918d2011-07-01 15:35:26 -07001974 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001975 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08001976
Winson Chungf0ea4d32011-06-06 14:27:16 -07001977 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
1978 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
1979 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
1980 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07001981
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001982 setPivotsForZoom(toView, toState, scale);
1983
Michael Jurkad74c9842011-07-10 12:44:21 -07001984 // Shrink workspaces away if going to AppsCustomize from workspace
1985 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07001986 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07001987
1988 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08001989 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08001990 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07001991 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
1992 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08001993 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08001994 toView.setFastScaleX(a * scale + b * 1f);
1995 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08001996 }
1997 });
Adam Cohen61033d32010-11-15 18:29:44 -08001998
Winson Chungf0ea4d32011-06-06 14:27:16 -07001999 toView.setVisibility(View.VISIBLE);
2000 toView.setFastAlpha(0f);
2001 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2002 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2003 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2004 public void onAnimationUpdate(float a, float b) {
2005 // don't need to invalidate because we do so above
2006 toView.setFastAlpha(a * 0f + b * 1f);
2007 }
2008 });
2009 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002010
Michael Jurkaabded662011-03-04 12:06:57 -08002011 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002012 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002013 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002014 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002015 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002016 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002017 // Prepare the position
2018 toView.setTranslationX(0.0f);
2019 toView.setTranslationY(0.0f);
2020 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002021 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002022 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002023 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002024 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002025 // If we don't set the final scale values here, if this animation is cancelled
2026 // it will have the wrong scale value and subsequent cameraPan animations will
2027 // not fix that
2028 toView.setScaleX(1.0f);
2029 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002030 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002031 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002032 }
Winson Chung32174c82011-07-19 15:47:55 -07002033
2034 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2035 // Hide the workspace scrollbar
2036 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002037 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002038 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002039 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002040 });
2041
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002042 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002043 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002044
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002045 if (mStateAnimation != null) mStateAnimation.cancel();
2046 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002047 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002048 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002049 } else {
2050 toView.setTranslationX(0.0f);
2051 toView.setTranslationY(0.0f);
2052 toView.setScaleX(1.0f);
2053 toView.setScaleY(1.0f);
2054 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002055 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002056 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002057 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2058 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002059
Winson Chungc07918d2011-07-01 15:35:26 -07002060 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2061 // Hide the workspace scrollbar
2062 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002063 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002064 }
Michael Jurkaabded662011-03-04 12:06:57 -08002065 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002066 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002067 }
2068
2069 /**
2070 * Zoom the camera back into the workspace, hiding 'fromView'.
2071 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002072 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002073 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002074 */
Winson Chungc07918d2011-07-01 15:35:26 -07002075 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002076 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002077
Winson Chungf0ea4d32011-06-06 14:27:16 -07002078 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2079 final float scaleFactor = (float)
2080 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2081 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002082
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002083 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002084
Michael Jurkad3ef3062010-11-23 16:23:58 -08002085 if (!springLoaded) {
2086 mWorkspace.unshrink(animated);
2087 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002088 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002089 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002090 if (mStateAnimation != null) mStateAnimation.cancel();
2091 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002092
Michael Jurka742574b2011-02-02 23:51:01 -08002093 final float oldScaleX = fromView.getScaleX();
2094 final float oldScaleY = fromView.getScaleY();
2095
2096 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2097 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002098 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2099 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002100 ((View)fromView.getParent()).fastInvalidate();
2101 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2102 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2103 }
2104 });
Michael Jurkaabded662011-03-04 12:06:57 -08002105 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002106 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002107 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002108 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2109 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002110 // don't need to invalidate because we do so above
2111 fromView.setFastAlpha(a * 1f + b * 0f);
2112 }
2113 });
Michael Jurkaabded662011-03-04 12:06:57 -08002114 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002115 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002116 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002117 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002118 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002119 public void onAnimationStart(android.animation.Animator animation) {
2120 if (!springLoaded) {
2121 mWorkspace.showDockDivider(false);
2122 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002123 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002124 }
2125 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002126 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002127 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002128 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002129 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002130 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002131 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002132 }
2133 });
2134
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002136 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002137 } else {
2138 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002139 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002140 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2141 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002142
Winson Chungc07918d2011-07-01 15:35:26 -07002143 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002144 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002145 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002146 mWorkspace.flashScrollingIndicator();
2147 }
Michael Jurkaabded662011-03-04 12:06:57 -08002148 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002149 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002150 }
2151
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002152 void showWorkspace(boolean animated) {
2153 showWorkspace(animated, null);
2154 }
2155
2156 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002157 if (layout != null) {
2158 // always animated, but that's ok since we never specify a layout and
2159 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002160 mWorkspace.unshrink(layout);
2161 } else {
2162 mWorkspace.unshrink(animated);
2163 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002164 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002165 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002166 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002167
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002168 // Change the state *after* we've called all the transition code
2169 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002170
Winson Chung5fb63472011-02-02 17:03:37 -08002171 // Resume the auto-advance of widgets
2172 mUserPresent = true;
2173 updateRunning();
2174
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002175 // send an accessibility event to announce the context change
2176 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002177 }
2178
Michael Jurkad3ef3062010-11-23 16:23:58 -08002179 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002180 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002181 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002182 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002183 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002184 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002185 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002186 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002187 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002188 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002189 @Override
2190 public void run() {
2191 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002192
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002193 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002194 // Before we show workspace, hide all apps again because
2195 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2196 // clean up our state transition functions
2197 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002198 showWorkspace(true);
2199 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002200 }
2201 }, (extendedDelay ?
2202 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2203 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2204 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002205 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002206 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002207 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002208 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2209 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002210 }
2211 // Otherwise, we are not in spring loaded mode, so don't do anything.
2212 }
2213
Adam Cohenfc53cd22011-07-20 15:45:11 -07002214 public boolean isAllAppsCustomizeOpen() {
2215 return mState == State.APPS_CUSTOMIZE;
2216 }
2217
Winson Chungf0ea4d32011-06-06 14:27:16 -07002218 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002219 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002220 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002221 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002222 if (!LauncherApplication.isScreenLarge()) {
2223 if (animated) {
2224 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002225 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002226 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002228 }
2229 }
2230 }
2231
2232 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002233 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002234 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002235 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002236 if (!LauncherApplication.isScreenLarge()) {
2237 if (animated) {
2238 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002240 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002242 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002243 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002244 }
2245
Winson Chung785d2eb2011-04-14 16:08:02 -07002246 void showAllApps(boolean animated) {
2247 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002248
Winson Chungf0ea4d32011-06-06 14:27:16 -07002249 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2250 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002251
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002253 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002254
Winson Chungf0ea4d32011-06-06 14:27:16 -07002255 // Change the state *after* we've called all the transition code
2256 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002257
2258 // Pause the auto-advance of widgets until we are out of AllApps
2259 mUserPresent = false;
2260 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002261 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002262
2263 // Send an accessibility event to announce the context change
2264 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2265 }
2266
Joe Onoratob2061212009-11-24 16:13:54 -05002267 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002268 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002269 * - Home from workspace
2270 * - from center screen
2271 * - from other screens
2272 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002273 * - from center screen
2274 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002275 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002276 * - from center screen
2277 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002278 * - Launch app from workspace and quit
2279 * - with back
2280 * - with home
2281 * - Launch app from all apps and quit
2282 * - with back
2283 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002284 * - Go to a screen that's not the default, then all
2285 * apps, and launch and app, and go back
2286 * - with back
2287 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002288 * - On workspace, long press power and go back
2289 * - with back
2290 * - with home
2291 * - On all apps, long press power and go back
2292 * - with back
2293 * - with home
2294 * - On workspace, power off
2295 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002296 * - Launch an app and turn off the screen while in that app
2297 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002298 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002299 * - From all apps
2300 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002301 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2302 * - From all apps
2303 * - From the center workspace
2304 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002305 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002306 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002307 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2308 mWorkspace.setVisibility(View.VISIBLE);
2309 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002310
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002313
Winson Chungf0ea4d32011-06-06 14:27:16 -07002314 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 if (mAllAppsButton != null) {
2316 mAllAppsButton.requestFocus();
2317 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002318 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002319 }
2320
Joe Onorato7c312c12009-08-13 21:36:53 -07002321 void lockAllApps() {
2322 // TODO
2323 }
2324
2325 void unlockAllApps() {
2326 // TODO
2327 }
2328
Patrick Dubroy5f445422011-02-18 14:35:21 -08002329 /**
2330 * Add an item from all apps or customize onto the given workspace screen.
2331 * If layout is null, add to the current screen.
2332 */
2333 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002334 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2335 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002336 } else {
2337 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002338 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002339 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002340
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002341 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002342 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002343 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002344 // Look for the toolbar icon specified in the activity meta-data
2345 Bundle metaData = packageManager.getActivityInfo(
2346 activityName, PackageManager.GET_META_DATA).metaData;
2347 if (metaData != null) {
2348 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2349 if (iconResId != 0) {
2350 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002351 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002352 }
2353 }
2354 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002355 // This can happen if the activity defines an invalid drawable
2356 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2357 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002358 } catch (Resources.NotFoundException nfe) {
2359 // This can happen if the activity defines an invalid drawable
2360 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2361 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002362 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002363 return null;
2364 }
2365
2366 // if successful in getting icon, return it; otherwise, set button to use default drawable
2367 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2368 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2369 TextView button = (TextView) findViewById(buttonId);
2370 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2371
2372 // If we were unable to find the icon via the meta-data, use a generic one
2373 if (toolbarIcon == null) {
2374 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2375 return null;
2376 } else {
2377 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2378 return toolbarIcon.getConstantState();
2379 }
2380 }
2381
2382 // if successful in getting icon, return it; otherwise, set button to use default drawable
2383 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2384 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2385 ImageView button = (ImageView) findViewById(buttonId);
2386 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2387
Michael Jurka19e0fc52011-07-22 18:00:21 -07002388 if (button != null) {
2389 // If we were unable to find the icon via the meta-data, use a
2390 // generic one
2391 if (toolbarIcon == null) {
2392 button.setImageResource(fallbackDrawableId);
2393 } else {
2394 button.setImageDrawable(toolbarIcon);
2395 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002396 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002397
2398 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2399
Michael Jurka0423dcf2010-10-05 14:56:18 -07002400 }
2401
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002402 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2403 TextView button = (TextView) findViewById(buttonId);
2404 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2405 }
2406
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002407 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002408 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002409 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002410 }
2411
Michael Jurka0423dcf2010-10-05 14:56:18 -07002412 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002413 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2414 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002415
Winson Chung1cad91e2011-05-25 17:41:01 -07002416 final SearchManager searchManager =
2417 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2418 ComponentName activityName = searchManager.getGlobalSearchActivity();
2419 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002420 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002421 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002422 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002423 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002424 } else {
2425 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002426 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002427 }
2428 }
2429
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002430 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002431 updateButtonWithDrawable(R.id.search_button, d);
2432 }
2433
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002434 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002435 final View searchDivider = findViewById(R.id.search_divider);
2436 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002437
Winson Chung1cad91e2011-05-25 17:41:01 -07002438 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2439 ComponentName activityName = intent.resolveActivity(getPackageManager());
2440 if (activityName != null) {
2441 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002442 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2443 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002444 voiceButton.setVisibility(View.VISIBLE);
2445 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002446 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002447 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002448 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002449 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002450
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002451 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002452 updateButtonWithDrawable(R.id.voice_button, d);
2453 }
2454
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002455 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002456 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002457 */
2458 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002459 final View marketButton = findViewById(R.id.market_button);
2460 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2461 // Find the app market activity by resolving an intent.
2462 // (If multiple app markets are installed, it will return the ResolverActivity.)
2463 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002464 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002465 mAppMarketIntent = intent;
2466 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002467 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002468 marketButton.setVisibility(View.VISIBLE);
2469 } else {
2470 // We should hide and disable the view so that we don't try and restore the visibility
2471 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2472 marketButton.setVisibility(View.GONE);
2473 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002474 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002475 }
2476
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002477 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002478 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002479 }
2480
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002481 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 * Displays the shortcut creation dialog and launches, if necessary, the
2483 * appropriate activity.
2484 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002485 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002486 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2487 DialogInterface.OnShowListener {
2488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002493
Winson Chung55b65502011-05-26 12:03:43 -07002494 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2495 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002496 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002498 AlertDialog dialog = builder.create();
2499 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002500 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002501 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 return dialog;
2504 }
2505
2506 public void onCancel(DialogInterface dialog) {
2507 mWaitingForResult = false;
2508 cleanup();
2509 }
2510
Romain Guycbb89e42009-06-08 15:52:54 -07002511 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002512 mWaitingForResult = false;
2513 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002514 }
2515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002516 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002517 try {
2518 dismissDialog(DIALOG_CREATE_SHORTCUT);
2519 } catch (Exception e) {
2520 // An exception is thrown if the dialog is not visible, which is fine
2521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 }
2523
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002524 /**
2525 * Handle the action clicked in the "Add to home" dialog.
2526 */
2527 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002528 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002529
Winson Chung55b65502011-05-26 12:03:43 -07002530 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2531 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002532 case AddAdapter.ITEM_APPLICATION: {
2533 if (mAppsCustomizeTabHost != null) {
2534 mAppsCustomizeTabHost.selectAppsTab();
2535 }
2536 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002537 break;
2538 }
Winson Chung55b65502011-05-26 12:03:43 -07002539 case AddAdapter.ITEM_APPWIDGET: {
2540 if (mAppsCustomizeTabHost != null) {
2541 mAppsCustomizeTabHost.selectWidgetsTab();
2542 }
2543 showAllApps(true);
2544 break;
2545 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002546 case AddAdapter.ITEM_WALLPAPER: {
2547 startWallpaper();
2548 break;
2549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 }
2551 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002552
2553 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002554 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002555 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 }
2557
2558 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002559 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002560 */
2561 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2562 @Override
2563 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002564 closeSystemDialogs();
2565 }
2566 }
2567
2568 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002569 * Receives notifications whenever the appwidgets are reset.
2570 */
2571 private class AppWidgetResetObserver extends ContentObserver {
2572 public AppWidgetResetObserver() {
2573 super(new Handler());
2574 }
2575
2576 @Override
2577 public void onChange(boolean selfChange) {
2578 onAppWidgetReset();
2579 }
2580 }
2581
2582 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002583 * If the activity is currently paused, signal that we need to re-run the loader
2584 * in onResume.
2585 *
2586 * This needs to be called from incoming places where resources might have been loaded
2587 * while we are paused. That is becaues the Configuration might be wrong
2588 * when we're not running, and if it comes back to what it was when we
2589 * were paused, we are not restarted.
2590 *
2591 * Implementation of the method from LauncherModel.Callbacks.
2592 *
2593 * @return true if we are currently paused. The caller might be able to
2594 * skip some work in that case since we will come back again.
2595 */
2596 public boolean setLoadOnResume() {
2597 if (mPaused) {
2598 Log.i(TAG, "setLoadOnResume");
2599 mOnResumeNeedsLoad = true;
2600 return true;
2601 } else {
2602 return false;
2603 }
2604 }
2605
2606 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002607 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002608 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002610 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002611 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002612 } else {
2613 return SCREEN_COUNT / 2;
2614 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002615 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002616
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002617
Joe Onorato9c1289c2009-08-17 11:03:03 -04002618 /**
2619 * Refreshes the shortcuts shown on the workspace.
2620 *
2621 * Implementation of the method from LauncherModel.Callbacks.
2622 */
2623 public void startBinding() {
2624 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002625
2626 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002627 int count = workspace.getChildCount();
2628 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002629 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002630 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2631 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002632 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002633 if (mHotseat != null) {
2634 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002636
Adam Cohen4eac29a2011-07-11 17:53:37 -07002637 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 }
2640
2641 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002642 * Bind the items start-end from the list.
2643 *
2644 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002646 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002647 setLoadOnResume();
2648
Joe Onorato9c1289c2009-08-17 11:03:03 -04002649 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002650 for (int i=start; i<end; i++) {
2651 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002652
2653 // Short circuit if we are loading dock items for a configuration which has no dock
2654 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2655 mHotseat == null) {
2656 continue;
2657 }
2658
Joe Onorato9c1289c2009-08-17 11:03:03 -04002659 switch (item.itemType) {
2660 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2661 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002662 View shortcut = createShortcut((ShortcutInfo)item);
2663 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2664 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002665 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002666 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002668 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002669 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002670 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2671 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002672 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002675 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002676 }
2677
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 /**
2679 * Implementation of the method from LauncherModel.Callbacks.
2680 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002681 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002682 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002683 sFolders.clear();
2684 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002686
2687 /**
2688 * Add the views for a widget to the workspace.
2689 *
2690 * Implementation of the method from LauncherModel.Callbacks.
2691 */
2692 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002693 setLoadOnResume();
2694
Daniel Sandler843e8602010-06-07 14:59:01 -04002695 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2696 if (DEBUG_WIDGETS) {
2697 Log.d(TAG, "bindAppWidget: " + item);
2698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699 final Workspace workspace = mWorkspace;
2700
2701 final int appWidgetId = item.appWidgetId;
2702 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002703 if (DEBUG_WIDGETS) {
2704 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2705 }
2706
Joe Onorato9c1289c2009-08-17 11:03:03 -04002707 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2708
Joe Onorato9c1289c2009-08-17 11:03:03 -04002709 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2710 item.hostView.setTag(item);
2711
Winson Chung3d503fb2011-07-13 17:25:49 -07002712 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 item.cellY, item.spanX, item.spanY, false);
2714
Adam Cohended9f8d2010-11-03 13:25:16 -07002715 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2716
Joe Onorato9c1289c2009-08-17 11:03:03 -04002717 workspace.requestLayout();
2718
Daniel Sandler843e8602010-06-07 14:59:01 -04002719 if (DEBUG_WIDGETS) {
2720 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2721 + (SystemClock.uptimeMillis()-start) + "ms");
2722 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002723 }
2724
2725 /**
2726 * Callback saying that there aren't any more items to bind.
2727 *
2728 * Implementation of the method from LauncherModel.Callbacks.
2729 */
2730 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002731 setLoadOnResume();
2732
Joe Onorato9c1289c2009-08-17 11:03:03 -04002733 if (mSavedState != null) {
2734 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002735 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002736 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002737 mSavedState = null;
2738 }
2739
2740 if (mSavedInstanceState != null) {
2741 super.onRestoreInstanceState(mSavedInstanceState);
2742 mSavedInstanceState = null;
2743 }
2744
Joe Onorato9c1289c2009-08-17 11:03:03 -04002745 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002746
2747 // If we received the result of any pending adds while the loader was running (e.g. the
2748 // widget configuration forced an orientation change), process them now.
2749 for (int i = 0; i < sPendingAddList.size(); i++) {
2750 completeAdd(sPendingAddList.get(i));
2751 }
2752 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 }
2754
2755 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002756 * Updates the icons on the launcher that are affected by changes to the package list
2757 * on the device.
2758 */
2759 private void updateIconsAffectedByPackageManagerChanges() {
2760 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002761 updateVoiceSearchIcon();
2762 }
2763
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002764 @Override
2765 public void bindSearchablesChanged() {
2766 updateGlobalSearchIcon();
2767 }
2768
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 * Add the icons for all apps.
2771 *
2772 * Implementation of the method from LauncherModel.Callbacks.
2773 */
2774 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002775 if (mAppsCustomizeContent != null) {
2776 mAppsCustomizeContent.setApps(apps);
2777 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002778 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002779 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 }
2781
2782 /**
2783 * A package was installed.
2784 *
2785 * Implementation of the method from LauncherModel.Callbacks.
2786 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002787 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002788 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002789 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002790
Winson Chung785d2eb2011-04-14 16:08:02 -07002791 if (mAppsCustomizeContent != null) {
2792 mAppsCustomizeContent.addApps(apps);
2793 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002794 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 }
2796
2797 /**
2798 * A package was updated.
2799 *
2800 * Implementation of the method from LauncherModel.Callbacks.
2801 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002802 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002803 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002805 if (mWorkspace != null) {
2806 mWorkspace.updateShortcuts(apps);
2807 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002808
Winson Chung785d2eb2011-04-14 16:08:02 -07002809 if (mAppsCustomizeContent != null) {
2810 mAppsCustomizeContent.updateApps(apps);
2811 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002812 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 }
2814
2815 /**
2816 * A package was uninstalled.
2817 *
2818 * Implementation of the method from LauncherModel.Callbacks.
2819 */
Joe Onorato36115782010-06-17 13:28:48 -04002820 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002821 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002822 if (permanent) {
2823 mWorkspace.removeItems(apps);
2824 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002825
Winson Chung785d2eb2011-04-14 16:08:02 -07002826 if (mAppsCustomizeContent != null) {
2827 mAppsCustomizeContent.removeApps(apps);
2828 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002829 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 }
Joe Onoratobe386092009-11-17 17:32:16 -08002831
2832 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002833 * A number of packages were updated.
2834 */
2835 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002836
Winson Chung785d2eb2011-04-14 16:08:02 -07002837 if (mAppsCustomizeContent != null) {
2838 mAppsCustomizeContent.onPackagesUpdated();
2839 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002840 }
2841
Winson Chung400438b2011-01-16 17:53:48 -08002842 private int mapConfigurationOriActivityInfoOri(int configOri) {
2843 final Display d = getWindowManager().getDefaultDisplay();
2844 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2845 switch (d.getRotation()) {
2846 case Surface.ROTATION_0:
2847 case Surface.ROTATION_180:
2848 // We are currently in the same basic orientation as the natural orientation
2849 naturalOri = configOri;
2850 break;
2851 case Surface.ROTATION_90:
2852 case Surface.ROTATION_270:
2853 // We are currently in the other basic orientation to the natural orientation
2854 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2855 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2856 break;
2857 }
2858
2859 int[] oriMap = {
2860 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2861 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2862 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2863 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2864 };
2865 // Since the map starts at portrait, we need to offset if this device's natural orientation
2866 // is landscape.
2867 int indexOffset = 0;
2868 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2869 indexOffset = 1;
2870 }
2871 return oriMap[(d.getRotation() + indexOffset) % 4];
2872 }
2873 public void lockScreenOrientation() {
2874 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2875 .getConfiguration().orientation));
2876 }
2877 public void unlockScreenOrientation() {
2878 mHandler.postDelayed(new Runnable() {
2879 public void run() {
2880 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2881 }
2882 }, mRestoreScreenOrientationDelay);
2883 }
2884
Winson Chung80baf5a2010-08-09 16:03:15 -07002885 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002886 * Prints out out state for debugging.
2887 */
2888 public void dumpState() {
2889 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002890 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002891 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2892 Log.d(TAG, "mRestoring=" + mRestoring);
2893 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2894 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002895 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002896 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002897
Winson Chung785d2eb2011-04-14 16:08:02 -07002898 if (mAppsCustomizeContent != null) {
2899 mAppsCustomizeContent.dumpState();
2900 }
Joe Onoratobe386092009-11-17 17:32:16 -08002901 Log.d(TAG, "END launcher2 dump state");
2902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903}
Michael Jurka2763be32011-02-24 11:19:57 -08002904
Michael Jurkaabded662011-03-04 12:06:57 -08002905interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002906 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2907 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002908}