blob: b1f56d064dd04c1f04c3a16921439ec6a904cecb [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
Michael Jurkac57b7a82011-08-09 22:02:20 -0700294 if (!mModel.isAllAppsLoaded()) {
295 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
296 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
297 }
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 // For handling default keys
300 mDefaultKeySsb = new SpannableStringBuilder();
301 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800302
303 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
304 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800305
306 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700307 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
308 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100309 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700310 }
311 if (sGlobalSearchIcon != null) {
312 updateGlobalSearchIcon(sGlobalSearchIcon);
313 }
314 if (sVoiceSearchIcon != null) {
315 updateVoiceSearchIcon(sVoiceSearchIcon);
316 }
317 if (sAppMarketIcon != null) {
318 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 }
Romain Guycbb89e42009-06-08 15:52:54 -0700321
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700323 if (sLocaleConfiguration == null) {
324 new AsyncTask<Void, Void, LocaleConfiguration>() {
325 @Override
326 protected LocaleConfiguration doInBackground(Void... unused) {
327 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
328 readConfiguration(Launcher.this, localeConfiguration);
329 return localeConfiguration;
330 }
331
332 @Override
333 protected void onPostExecute(LocaleConfiguration result) {
334 sLocaleConfiguration = result;
335 checkForLocaleChange(); // recursive, but now with a locale configuration
336 }
337 }.execute();
338 return;
339 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700340
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 final Configuration configuration = getResources().getConfiguration();
342
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700343 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 final String locale = configuration.locale.toString();
345
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700346 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 final int mcc = configuration.mcc;
348
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700349 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 final int mnc = configuration.mnc;
351
Romain Guy84f296c2009-11-04 15:00:44 -0800352 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353
Romain Guy84f296c2009-11-04 15:00:44 -0800354 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 sLocaleConfiguration.locale = locale;
356 sLocaleConfiguration.mcc = mcc;
357 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700358
Joe Onorato0589f0f2010-02-08 13:44:00 -0800359 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360
361 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
362 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700363 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 public void run() {
365 writeConfiguration(Launcher.this, localeConfiguration);
366 }
367 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700368 }
369 }
370
371 private static class LocaleConfiguration {
372 public String locale;
373 public int mcc = -1;
374 public int mnc = -1;
375 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700376
Romain Guy98d01652009-06-30 16:21:04 -0700377 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
378 DataInputStream in = null;
379 try {
380 in = new DataInputStream(context.openFileInput(PREFERENCES));
381 configuration.locale = in.readUTF();
382 configuration.mcc = in.readInt();
383 configuration.mnc = in.readInt();
384 } catch (FileNotFoundException e) {
385 // Ignore
386 } catch (IOException e) {
387 // Ignore
388 } finally {
389 if (in != null) {
390 try {
391 in.close();
392 } catch (IOException e) {
393 // Ignore
394 }
395 }
396 }
397 }
398
399 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
400 DataOutputStream out = null;
401 try {
402 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
403 out.writeUTF(configuration.locale);
404 out.writeInt(configuration.mcc);
405 out.writeInt(configuration.mnc);
406 out.flush();
407 } catch (FileNotFoundException e) {
408 // Ignore
409 } catch (IOException e) {
410 //noinspection ResultOfMethodCallIgnored
411 context.getFileStreamPath(PREFERENCES).delete();
412 } finally {
413 if (out != null) {
414 try {
415 out.close();
416 } catch (IOException e) {
417 // Ignore
418 }
419 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 }
421 }
422
Adam Cohen716b51e2011-06-30 12:09:54 -0700423 public DragLayer getDragLayer() {
424 return mDragLayer;
425 }
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 static int getScreen() {
428 synchronized (sLock) {
429 return sScreen;
430 }
431 }
432
433 static void setScreen(int screen) {
434 synchronized (sLock) {
435 sScreen = screen;
436 }
437 }
438
Winson Chung557d6ed2011-07-08 15:34:52 -0700439 /**
440 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
441 * a configuration step, this allows the proper animations to run after other transitions.
442 */
443 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700444 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800445 switch (args.requestCode) {
446 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700447 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
448 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800449 break;
450 case REQUEST_PICK_SHORTCUT:
451 processShortcut(args.intent);
452 break;
453 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700454 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
455 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700456 result = true;
457 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800458 case REQUEST_PICK_APPWIDGET:
459 addAppWidgetFromPick(args.intent);
460 break;
461 case REQUEST_CREATE_APPWIDGET:
462 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700463 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700464 result = true;
465 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800466 case REQUEST_PICK_WALLPAPER:
467 // We just wanted the activity result here so we can clear mWaitingForResult
468 break;
469 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700470 // In any situation where we have a multi-step drop, we should reset the add info only after
471 // we complete the drop
472 resetAddInfo();
473 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800474 }
475
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800477 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700478 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700479 mWaitingForResult = false;
480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 // The pattern used here is that a user PICKs a specific application,
482 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
485 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700486 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
487 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488 final PendingAddArguments args = new PendingAddArguments();
489 args.requestCode = requestCode;
490 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700491 args.container = mPendingAddInfo.container;
492 args.screen = mPendingAddInfo.screen;
493 args.cellX = mPendingAddInfo.cellX;
494 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495
496 // If the loader is still running, defer the add until it is done.
497 if (isWorkspaceLocked()) {
498 sPendingAddList.add(args);
499 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700500 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
Romain Guy18042c82009-11-06 11:44:55 -0800502 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700503 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
504 if (data != null) {
505 // Clean up the appWidgetId if we canceled
506 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
507 if (appWidgetId != -1) {
508 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
511 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700512
513 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700514 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
516
517 @Override
518 protected void onResume() {
519 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800520 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700521 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 mWorkspaceLoading = true;
523 mModel.startLoader(this, true);
524 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700525 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800527 if (mWaitingForResume != null) {
528 mWaitingForResume.setStayPressed(false);
529 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700530 // When we resume Launcher, a different Activity might be responsible for the app
531 // market intent, so refresh the icon
532 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 }
534
535 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 protected void onPause() {
537 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700538 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800539 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 }
Romain Guycbb89e42009-06-08 15:52:54 -0700541
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700542 @Override
543 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 // Flag the loader to stop early before switching
545 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 if (mAppsCustomizeContent != null) {
547 mAppsCustomizeContent.surrender();
548 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800549 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700550 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800552 // We can't hide the IME if it was forced open. So don't bother
553 /*
554 @Override
555 public void onWindowFocusChanged(boolean hasFocus) {
556 super.onWindowFocusChanged(hasFocus);
557
558 if (hasFocus) {
559 final InputMethodManager inputManager = (InputMethodManager)
560 getSystemService(Context.INPUT_METHOD_SERVICE);
561 WindowManager.LayoutParams lp = getWindow().getAttributes();
562 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
563 android.os.Handler()) {
564 protected void onReceiveResult(int resultCode, Bundle resultData) {
565 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
566 }
567 });
568 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
569 }
570 }
571 */
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 private boolean acceptFilter() {
574 final InputMethodManager inputManager = (InputMethodManager)
575 getSystemService(Context.INPUT_METHOD_SERVICE);
576 return !inputManager.isFullscreenMode();
577 }
578
579 @Override
580 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700581 final int uniChar = event.getUnicodeChar();
582 final boolean handled = super.onKeyDown(keyCode, event);
583 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
584 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
586 keyCode, event);
587 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700588 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700589 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700590 // showSearchDialog()
591 // If there are multiple keystrokes before the search dialog takes focus,
592 // onSearchRequested() will be called for every keystroke,
593 // but it is idempotent, so it's fine.
594 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596 }
597
Joe Onorato8a9625e2010-01-28 15:55:35 -0800598 // Eat the long press event so the keyboard doesn't come up.
599 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
600 return true;
601 }
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 return handled;
604 }
605
Karl Rosaen138a0412009-04-23 19:00:21 -0700606 private String getTypedText() {
607 return mDefaultKeySsb.toString();
608 }
609
610 private void clearTypedText() {
611 mDefaultKeySsb.clear();
612 mDefaultKeySsb.clearSpans();
613 Selection.setSelection(mDefaultKeySsb, 0);
614 }
615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700617 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
618 * State
619 */
620 private static State intToState(int stateOrdinal) {
621 State state = State.WORKSPACE;
622 final State[] stateValues = State.values();
623 for (int i = 0; i < stateValues.length; i++) {
624 if (stateValues[i].ordinal() == stateOrdinal) {
625 state = stateValues[i];
626 break;
627 }
628 }
629 return state;
630 }
631
632 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 * Restores the previous state, if it exists.
634 *
635 * @param savedState The previous state.
636 */
637 private void restoreState(Bundle savedState) {
638 if (savedState == null) {
639 return;
640 }
641
Michael Jurka883f55b2010-10-21 15:47:14 -0700642 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700643 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 showAllApps(false);
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
648 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700649 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
653 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
656 mPendingAddInfo.container = pendingAddContainer;
657 mPendingAddInfo.screen = pendingAddScreen;
658 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
659 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 mRestoring = true;
661 }
662
663 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
664 if (renameFolder) {
665 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700666 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 mRestoring = true;
668 }
Winson Chunga12a2502010-12-20 14:41:35 -0800669
Winson Chung785d2eb2011-04-14 16:08:02 -0700670
671 // Restore the AppsCustomize tab
672 if (mAppsCustomizeTabHost != null) {
673 String curTab = savedState.getString("apps_customize_currentTab");
674 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700675 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 mAppsCustomizeContent.setContentType(
677 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
678 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
679 }
680
681 // Note: currently we do not restore the page for the AppsCustomize pane because the
682 // change in layout can drastically affect the saved page index
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 /**
687 * Finds all the views we need and configure them properly.
688 */
689 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700690 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400691
Winson Chung4c98d922011-05-31 16:50:48 -0700692 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
693 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694
Winson Chung4c98d922011-05-31 16:50:48 -0700695 // Setup the drag layer
696 mDragLayer.setup(this, dragController);
697
Winson Chung3d503fb2011-07-13 17:25:49 -0700698 // Setup the hotseat
699 mHotseat = (Hotseat) findViewById(R.id.hotseat);
700 if (mHotseat != null) {
701 mHotseat.setup(this);
702 }
703
Winson Chung4c98d922011-05-31 16:50:48 -0700704 // Setup the workspace
705 mWorkspace.setHapticFeedbackEnabled(false);
706 mWorkspace.setOnLongClickListener(this);
707 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700708 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700709
Winson Chungf0ea4d32011-06-06 14:27:16 -0700710 // Get the search/delete bar
711 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700712
Winson Chungf0ea4d32011-06-06 14:27:16 -0700713 // Setup AppsCustomize
714 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
715 findViewById(R.id.apps_customize_pane);
716 mAppsCustomizeContent = (AppsCustomizePagedView)
717 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
718 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400719
Romain Guy1fbc1c82009-11-09 20:43:08 -0800720
Winson Chung4c98d922011-05-31 16:50:48 -0700721
Winson Chung3d503fb2011-07-13 17:25:49 -0700722 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700723 dragController.setDragScoller(mWorkspace);
724 dragController.setScrollView(mDragLayer);
725 dragController.setMoveTarget(mWorkspace);
726 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700727 if (mSearchDeleteBar != null) {
728 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
Romain Guy8a73c512009-11-09 19:19:59 -0800732
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 /**
735 * Creates a view representing a shortcut.
736 *
737 * @param info The data structure describing the shortcut.
738 *
739 * @return A View inflated from R.layout.application.
740 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800741 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700743 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
746 /**
747 * Creates a view representing a shortcut inflated from the specified resource.
748 *
749 * @param layoutResId The id of the XML layout used to create the shortcut.
750 * @param parent The group the shortcut belongs to.
751 * @param info The data structure describing the shortcut.
752 *
753 * @return A View inflated from layoutResId.
754 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800755 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800756 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
757 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 return favorite;
760 }
761
762 /**
763 * Add an application shortcut to the workspace.
764 *
765 * @param data The intent describing the application.
766 * @param cellInfo The position on screen where to create the shortcut.
767 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700768 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700769 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700770 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771
Adam Cohenfbba09b2011-07-18 21:43:05 -0700772 // First we check if we already know the exact location where we want to add this item.
773 if (cellX >= 0 && cellY >= 0) {
774 cellXY[0] = cellX;
775 cellXY[1] = cellY;
776 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777 showOutOfSpaceMessage();
778 return;
779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800781 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800782
Romain Guy73b979d2009-06-09 12:57:21 -0700783 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800784 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800786 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700788 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800789 } else {
790 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 }
792 }
Romain Guycbb89e42009-06-08 15:52:54 -0700793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 /**
795 * Add a shortcut to the workspace.
796 *
797 * @param data The intent describing the shortcut.
798 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
801 int cellY) {
802 int[] cellXY = mTmpAddItemCellCoordinates;
803 int[] touchXY = mPendingAddInfo.dropPos;
804 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700805
Michael Jurkad3ef3062010-11-23 16:23:58 -0800806 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700807
808 // First we check if we already know the exact location where we want to add this item.
809 if (cellX >= 0 && cellY >= 0) {
810 cellXY[0] = cellX;
811 cellXY[1] = cellY;
812 foundCellSpan = true;
813 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800814 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700815 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800816 foundCellSpan = (result != null);
817 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700818 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800819 }
820
821 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700822 showOutOfSpaceMessage();
823 return;
824 }
825
826 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700827 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
829 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
832 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 }
834 }
835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700837 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700839 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700840 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700842 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700843 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700844
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700845 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700847
848 // We want to account for the extra amount of padding that we are adding to the widget
849 // to ensure that it gets the full amount of space that it has requested
850 Resources r = getResources();
851 int requiredWidth = appWidgetInfo.minWidth +
852 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
853 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
854 int requiredHeight = appWidgetInfo.minHeight +
855 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
856 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
857 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700860 // We have saved the position to which the widget was dragged-- this really only matters
861 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 int[] cellXY = mTmpAddItemCellCoordinates;
863 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700864 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
866 cellXY[0] = mPendingAddInfo.cellX;
867 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700868 foundCellSpan = true;
869 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700870 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700871 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700872 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800873 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700874 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700875 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700876 }
877
Michael Jurka0280c3b2010-09-17 15:00:07 -0700878 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800879 if (appWidgetId != -1) {
880 // Deleting an app widget ID is a void call but writes to disk before returning
881 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800882 new Thread("deleteAppWidgetId") {
883 public void run() {
884 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
885 }
886 }.start();
887 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700888 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800889 return;
890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700892 // Build Launcher-specific widget info and save to database
893 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700894 launcherInfo.spanX = spanXY[0];
895 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700898 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899
900 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700902 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700903
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700904 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700906
Winson Chung3d503fb2011-07-13 17:25:49 -0700907 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400908 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700909
910 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 }
912 }
Romain Guycbb89e42009-06-08 15:52:54 -0700913
Adam Cohended9f8d2010-11-03 13:25:16 -0700914 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
915 @Override
916 public void onReceive(Context context, Intent intent) {
917 final String action = intent.getAction();
918 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
919 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700920 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700921 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700922
Winson Chungc100e8e2011-08-09 16:02:43 -0700923 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700924 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700925 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
926 mAppsCustomizeTabHost.reset();
927 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700928 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700929 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
930 mUserPresent = true;
931 updateRunning();
932 }
933 }
934 };
935
936 @Override
937 public void onAttachedToWindow() {
938 super.onAttachedToWindow();
939
940 // Listen for broadcasts related to user-presence
941 final IntentFilter filter = new IntentFilter();
942 filter.addAction(Intent.ACTION_SCREEN_OFF);
943 filter.addAction(Intent.ACTION_USER_PRESENT);
944 registerReceiver(mReceiver, filter);
945
Adam Cohend113e0c2010-11-11 10:48:05 -0800946 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700947 mVisible = true;
948 }
949
950 @Override
951 public void onDetachedFromWindow() {
952 super.onDetachedFromWindow();
953 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700954 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700955
Adam Cohend113e0c2010-11-11 10:48:05 -0800956 if (mAttached) {
957 unregisterReceiver(mReceiver);
958 mAttached = false;
959 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700960 updateRunning();
961 }
962
963 public void onWindowVisibilityChanged(int visibility) {
964 mVisible = visibility == View.VISIBLE;
965 updateRunning();
966 }
967
968 private void sendAdvanceMessage(long delay) {
969 mHandler.removeMessages(ADVANCE_MSG);
970 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
971 mHandler.sendMessageDelayed(msg, delay);
972 mAutoAdvanceSentTime = System.currentTimeMillis();
973 }
974
975 private void updateRunning() {
976 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
977 if (autoAdvanceRunning != mAutoAdvanceRunning) {
978 mAutoAdvanceRunning = autoAdvanceRunning;
979 if (autoAdvanceRunning) {
980 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
981 sendAdvanceMessage(delay);
982 } else {
983 if (!mWidgetsToAdvance.isEmpty()) {
984 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
985 (System.currentTimeMillis() - mAutoAdvanceSentTime));
986 }
987 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -0800988 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -0700989 }
990 }
991 }
992
993 private final Handler mHandler = new Handler() {
994 @Override
995 public void handleMessage(Message msg) {
996 if (msg.what == ADVANCE_MSG) {
997 int i = 0;
998 for (View key: mWidgetsToAdvance.keySet()) {
999 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1000 final int delay = mAdvanceStagger * i;
1001 if (v instanceof Advanceable) {
1002 postDelayed(new Runnable() {
1003 public void run() {
1004 ((Advanceable) v).advance();
1005 }
1006 }, delay);
1007 }
1008 i++;
1009 }
1010 sendAdvanceMessage(mAdvanceInterval);
1011 }
1012 }
1013 };
1014
1015 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001016 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001017 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1018 if (v instanceof Advanceable) {
1019 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001020 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001021 updateRunning();
1022 }
1023 }
1024
1025 void removeWidgetToAutoAdvance(View hostView) {
1026 if (mWidgetsToAdvance.containsKey(hostView)) {
1027 mWidgetsToAdvance.remove(hostView);
1028 updateRunning();
1029 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001030 }
1031
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001033 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001034 launcherInfo.hostView = null;
1035 }
1036
Adam Cohended9f8d2010-11-03 13:25:16 -07001037 void showOutOfSpaceMessage() {
1038 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1039 }
1040
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001041 public LauncherAppWidgetHost getAppWidgetHost() {
1042 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
Romain Guycbb89e42009-06-08 15:52:54 -07001044
Winson Chunga9abd0e2010-10-27 17:18:37 -07001045 public LauncherModel getModel() {
1046 return mModel;
1047 }
1048
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001049 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001050 getWindow().closeAllPanels();
1051
1052 try {
1053 dismissDialog(DIALOG_CREATE_SHORTCUT);
1054 // Unlock the workspace if the dialog was showing
1055 } catch (Exception e) {
1056 // An exception is thrown if the dialog is not visible, which is fine
1057 }
1058
1059 try {
1060 dismissDialog(DIALOG_RENAME_FOLDER);
1061 // Unlock the workspace if the dialog was showing
1062 } catch (Exception e) {
1063 // An exception is thrown if the dialog is not visible, which is fine
1064 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001065
1066 // Whatever we were doing is hereby canceled.
1067 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001068 }
1069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 @Override
1071 protected void onNewIntent(Intent intent) {
1072 super.onNewIntent(intent);
1073
1074 // Close the menu
1075 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001076 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001077 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001078
Adam Cohen95bb8002011-07-03 23:40:28 -07001079 closeFolder();
1080
Joe Onorato14f122b2009-11-19 14:06:36 -08001081 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1082 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001083
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001084 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001085 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001086 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001087 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001088 }
Winson Chungde1af762011-07-21 16:44:07 -07001089 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001090 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001091
Joe Onorato3a8820b2009-11-10 15:06:42 -08001092 final View v = getWindow().peekDecorView();
1093 if (v != null && v.getWindowToken() != null) {
1094 InputMethodManager imm = (InputMethodManager)getSystemService(
1095 INPUT_METHOD_SERVICE);
1096 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001098
Michael Jurka35aa14d2011-07-07 17:01:08 -07001099 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001100 if (mAppsCustomizeTabHost != null) {
1101 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 }
1104 }
1105
1106 @Override
1107 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1108 // Do not call super here
1109 mSavedInstanceState = savedInstanceState;
1110 }
1111
1112 @Override
1113 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001114 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001115 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116
Michael Jurka883f55b2010-10-21 15:47:14 -07001117 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001118 // We close any open folder since it will not be re-opened, and we need to make sure
1119 // this state is reflected.
1120 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121
Winson Chung3d503fb2011-07-13 17:25:49 -07001122 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1123 mWaitingForResult) {
1124 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1125 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1126 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1127 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
1129
1130 if (mFolderInfo != null && mWaitingForResult) {
1131 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1132 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1133 }
Michael Jurka946ad472010-07-09 18:05:18 -07001134
Winson Chung785d2eb2011-04-14 16:08:02 -07001135 // Save the current AppsCustomize tab
1136 if (mAppsCustomizeTabHost != null) {
1137 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1138 if (currentTabTag != null) {
1139 outState.putString("apps_customize_currentTab", currentTabTag);
1140 }
1141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143
1144 @Override
1145 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001147
Winson Chunge7a03942011-08-05 15:05:12 -07001148 // Remove all pending runnables
1149 mHandler.removeMessages(ADVANCE_MSG);
1150 mHandler.removeMessages(0);
1151
Winson Chungcd2b0142011-06-08 16:02:26 -07001152 // Stop callbacks from LauncherModel
1153 LauncherApplication app = ((LauncherApplication) getApplication());
1154 mModel.stopLoader();
1155 app.setLauncher(null);
1156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001158 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001160 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001162 mAppWidgetHost = null;
1163
1164 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
1166 TextKeyListener.getInstance().release();
1167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168
Winson Chung3d503fb2011-07-13 17:25:49 -07001169 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001170
1171 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001172 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001173
1174 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1175 mWorkspace.removeAllViews();
1176 mWorkspace = null;
1177 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001178
1179 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181
Adam Cohena9cf38f2011-05-02 15:36:58 -07001182 public DragController getDragController() {
1183 return mDragController;
1184 }
1185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 @Override
1187 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001188 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 super.startActivityForResult(intent, requestCode);
1190 }
1191
1192 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001193 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001195
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001196 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001197
Karl Rosaen138a0412009-04-23 19:00:21 -07001198 if (initialQuery == null) {
1199 // Use any text typed in the launcher as the initial query
1200 initialQuery = getTypedText();
1201 clearTypedText();
1202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 if (appSearchData == null) {
1204 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001205 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 final SearchManager searchManager =
1209 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001210 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001211 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213
Karl Rosaen138a0412009-04-23 19:00:21 -07001214 /**
1215 * Indicates that we want global search for this activity by setting the globalSearch
1216 * argument for {@link #startSearch} to true.
1217 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001219 @Override
1220 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001221 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001222 // Use a custom animation for launching search
1223 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001224 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001225 }
1226
Joe Onorato9c1289c2009-08-17 11:03:03 -04001227 public boolean isWorkspaceLocked() {
1228 return mWorkspaceLoading || mWaitingForResult;
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001232 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001233 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
Michael Jurka0280c3b2010-09-17 15:00:07 -07001236 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001237 mPendingAddInfo.container = ItemInfo.NO_ID;
1238 mPendingAddInfo.screen = -1;
1239 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1240 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1241 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001243
Winson Chung7ad01412010-09-27 11:33:03 -07001244 private void manageApps() {
1245 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1246 }
1247
Michael Jurkaaf442092010-06-10 17:01:57 -07001248 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001249 // TODO: catch bad widget exception when sent
1250 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001251 // TODO: Is this log message meaningful?
1252 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001253 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001254 }
1255
Winson Chung55cef262010-10-28 14:14:18 -07001256 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001257 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258
Bjorn Bringert7984c942009-12-09 15:38:25 +00001259 if (appWidget.configure != null) {
1260 // Launch over to configure widget, if needed
1261 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1262 intent.setComponent(appWidget.configure);
1263 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001264 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001265 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1266 intent.putExtra(
1267 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1268 info.mimeType);
1269
1270 final String mimeType = info.mimeType;
1271 final ClipData clipData = (ClipData) info.configurationData;
1272 final ClipDescription clipDesc = clipData.getDescription();
1273 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1274 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001275 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001276 final CharSequence stringData = item.getText();
1277 final Uri uriData = item.getUri();
1278 final Intent intentData = item.getIntent();
1279 final String key =
1280 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1281 if (uriData != null) {
1282 intent.putExtra(key, uriData);
1283 } else if (intentData != null) {
1284 intent.putExtra(key, intentData);
1285 } else if (stringData != null) {
1286 intent.putExtra(key, stringData);
1287 }
1288 break;
1289 }
1290 }
1291 }
Winson Chung55cef262010-10-28 14:14:18 -07001292 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001293
Romain Guy8e633c52010-03-04 12:51:36 -08001294 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001296 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001297 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001298
1299 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001300 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
1302 }
Romain Guycbb89e42009-06-08 15:52:54 -07001303
Adam Cohenfbba09b2011-07-18 21:43:05 -07001304 /**
1305 * Process a shortcut drop.
1306 *
1307 * @param componentName The name of the component
1308 * @param screen The screen where it should be added
1309 * @param cell The cell it should be added to, optional
1310 * @param position The location on the screen where it was dropped, optional
1311 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1313 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 mPendingAddInfo.container = container;
1316 mPendingAddInfo.screen = screen;
1317 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001318
1319 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001320 mPendingAddInfo.cellX = cell[0];
1321 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001322 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323
1324 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1325 createShortcutIntent.setComponent(componentName);
1326 processShortcut(createShortcutIntent);
1327 }
1328
Adam Cohenfbba09b2011-07-18 21:43:05 -07001329 /**
1330 * Process a widget drop.
1331 *
1332 * @param info The PendingAppWidgetInfo of the widget being added.
1333 * @param screen The screen where it should be added
1334 * @param cell The cell it should be added to, optional
1335 * @param position The location on the screen where it was dropped, optional
1336 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1338 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001339 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001340 mPendingAddInfo.container = info.container = container;
1341 mPendingAddInfo.screen = info.screen = screen;
1342 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001343 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001344 mPendingAddInfo.cellX = cell[0];
1345 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001346 }
1347
1348 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1349 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1350 addAppWidgetImpl(appWidgetId, info);
1351 }
1352
Joe Onoratodeb98af2010-02-19 14:59:39 -08001353 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001354 // Handle case where user selected "Applications"
1355 String applicationName = getResources().getString(R.string.group_applications);
1356 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001357
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001358 if (applicationName != null && applicationName.equals(shortcutName)) {
1359 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1360 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001361
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001362 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1363 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001364 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001365 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001366 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001367 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
1370
Winson Chung24ab2f12010-09-16 14:10:47 -07001371 void processWallpaper(Intent intent) {
1372 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1373 }
1374
Winson Chung3d503fb2011-07-13 17:25:49 -07001375 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1376 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001377 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 folderInfo.title = getText(R.string.folder_name);
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001381 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1382 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001383 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384
1385 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 FolderIcon newFolder =
1387 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1388 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1389 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001390 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
Romain Guycbb89e42009-06-08 15:52:54 -07001392
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001394 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 }
1396
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 private void showNotifications() {
1398 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1399 if (statusBar != null) {
1400 statusBar.expand();
1401 }
1402 }
1403
1404 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001405 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001407 Intent chooser = Intent.createChooser(pickWallpaper,
1408 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001409 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1410 // Removed in Eclair MR1
1411// WallpaperManager wm = (WallpaperManager)
1412// getSystemService(Context.WALLPAPER_SERVICE);
1413// WallpaperInfo wi = wm.getWallpaperInfo();
1414// if (wi != null && wi.getSettingsActivity() != null) {
1415// LabeledIntent li = new LabeledIntent(getPackageName(),
1416// R.string.configure_wallpaper, 0);
1417// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1418// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1419// }
Mike Clerona0618e42009-10-22 13:55:21 -07001420 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001423 /**
1424 * Registers various content observers. The current implementation registers
1425 * only a favorites observer to keep track of the favorites applications.
1426 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001427 private void registerContentObservers() {
1428 ContentResolver resolver = getContentResolver();
1429 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1430 true, mWidgetObserver);
1431 }
1432
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 @Override
1434 public boolean dispatchKeyEvent(KeyEvent event) {
1435 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1436 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001438 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001439 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001440 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001441 dumpState();
1442 return true;
1443 }
1444 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001445 }
1446 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1447 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001448 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 return true;
1450 }
1451 }
1452
1453 return super.dispatchKeyEvent(event);
1454 }
1455
Joe Onorato88ec0992009-11-19 13:16:06 -08001456 @Override
1457 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001458 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001459 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001460 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001461 Folder openFolder = mWorkspace.getOpenFolder();
1462 if (openFolder.isEditingName()) {
1463 openFolder.dismissEditingName();
1464 } else {
1465 closeFolder();
1466 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001467 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001468 mWorkspace.exitWidgetResizeMode();
1469
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001470 // Back button is a no-op here, but give at least some feedback for the button press
1471 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001472 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001473 }
1474
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001476 * Re-listen when widgets are reset.
1477 */
1478 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001479 if (mAppWidgetHost != null) {
1480 mAppWidgetHost.startListening();
1481 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001482 }
1483
1484 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1486 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001488 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001489 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 /**
1493 * Launches the intent referred by the clicked shortcut.
1494 *
1495 * @param v The view representing the clicked shortcut.
1496 */
1497 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001498 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1499 // view has detached (it's possible for this to happen if the view is removed mid touch).
1500 if (v.getWindowToken() == null) {
1501 return;
1502 }
1503
1504 if (mWorkspace.isSwitchingState()) {
1505 return;
1506 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001509 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001511 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001512 int[] pos = new int[2];
1513 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001514 intent.setSourceBounds(new Rect(pos[0], pos[1],
1515 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001516 boolean success = startActivitySafely(intent, tag);
1517
1518 if (success && v instanceof BubbleTextView) {
1519 mWaitingForResume = (BubbleTextView) v;
1520 mWaitingForResume.setStayPressed(true);
1521 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001523 if (v instanceof FolderIcon) {
1524 FolderIcon fi = (FolderIcon) v;
1525 handleFolderClick(fi);
1526 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001527 } else if (v == mAllAppsButton) {
1528 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001529 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001530 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001531 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 }
1534 }
1535
Michael Jurka0e260592010-06-30 17:07:39 -07001536 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001537 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001538 // clicking anywhere on the workspace causes the customization drawer to slide down
1539 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001540 return false;
1541 }
1542
Michael Jurkaaf442092010-06-10 17:01:57 -07001543 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001544 * Event handler for the search button
1545 *
1546 * @param v The view that was clicked.
1547 */
1548 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001549 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001550 }
1551
1552 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001553 * Event handler for the voice button
1554 *
1555 * @param v The view that was clicked.
1556 */
1557 public void onClickVoiceButton(View v) {
1558 startVoiceSearch();
1559 }
1560
1561 private void startVoiceSearch() {
1562 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1563 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1564 startActivity(intent);
1565 }
1566
1567 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001568 * Event handler for the "grid" button that appears on the home screen, which
1569 * enters all apps mode.
1570 *
1571 * @param v The view that was clicked.
1572 */
1573 public void onClickAllAppsButton(View v) {
1574 showAllApps(true);
1575 }
1576
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001577 public void onClickAppMarketButton(View v) {
1578 if (mAppMarketIntent != null) {
1579 startActivitySafely(mAppMarketIntent, "app market");
1580 }
1581 }
1582
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001583 void startApplicationDetailsActivity(ComponentName componentName) {
1584 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001585 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1586 Uri.fromParts("package", packageName, null));
1587 startActivity(intent);
1588 }
1589
Patrick Dubroy5539af72010-09-07 15:22:01 -07001590 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1591 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1592 // System applications cannot be installed. For now, show a toast explaining that.
1593 // We may give them the option of disabling apps this way.
1594 int messageId = R.string.uninstall_system_app_text;
1595 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1596 } else {
1597 String packageName = appInfo.componentName.getPackageName();
1598 String className = appInfo.componentName.getClassName();
1599 Intent intent = new Intent(
1600 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1601 startActivity(intent);
1602 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001603 }
1604
Michael Jurkaddd62e92011-02-16 17:49:14 -08001605 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1607 try {
1608 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001609 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 } catch (ActivityNotFoundException e) {
1611 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001612 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 } catch (SecurityException e) {
1614 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001615 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001617 "or use the exported attribute for this activity. "
1618 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001620 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001622
Romain Guy8e633c52010-03-04 12:51:36 -08001623 void startActivityForResultSafely(Intent intent, int requestCode) {
1624 try {
1625 startActivityForResult(intent, requestCode);
1626 } catch (ActivityNotFoundException e) {
1627 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1628 } catch (SecurityException e) {
1629 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1630 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1631 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1632 "or use the exported attribute for this activity.", e);
1633 }
1634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635
Adam Cohena9cf38f2011-05-02 15:36:58 -07001636 private void handleFolderClick(FolderIcon folderIcon) {
1637 final FolderInfo info = folderIcon.mInfo;
1638 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 // Close any open folder
1640 closeFolder();
1641 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001642 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 } else {
1644 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001645 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 int folderScreen;
1647 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001648 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 // .. and close it
1650 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001651 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 // Close any folder open on the current screen
1653 closeFolder();
1654 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001655 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657 }
1658 }
1659 }
1660
Adam Cohen2801caf2011-05-13 20:57:39 -07001661 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001662 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001663 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1664 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1665 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1666
Adam Cohenc51934b2011-07-26 21:07:43 -07001667 FolderInfo info = (FolderInfo) fi.getTag();
1668 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1669 CellLayout cl = (CellLayout) fi.getParent().getParent();
1670 cl.setFolderLeaveBehindCell(info.cellX, info.cellY);
1671 }
1672
Adam Cohen2801caf2011-05-13 20:57:39 -07001673 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1674 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1675 oa.start();
1676 }
1677
1678 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001679 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001680 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1681 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1682 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1683
Adam Cohenc51934b2011-07-26 21:07:43 -07001684 FolderInfo info = (FolderInfo) fi.getTag();
1685 CellLayout cl = null;
1686 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1687 cl = (CellLayout) fi.getParent().getParent();
1688 }
1689
1690 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001691 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1692 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001693 oa.addListener(new AnimatorListenerAdapter() {
1694 @Override
1695 public void onAnimationEnd(Animator animation) {
1696 if (layout != null) {
1697 layout.clearFolderLeaveBehind();
1698 }
1699 }
1700 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001701 oa.start();
1702 }
1703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001705 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 * is animated relative to the specified View. If the View is null, no animation
1707 * is played.
1708 *
1709 * @param folderInfo The FolderInfo describing the folder to open.
1710 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001711 public void openFolder(FolderIcon folderIcon) {
1712 Folder folder = folderIcon.mFolder;
1713 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714
Adam Cohen2801caf2011-05-13 20:57:39 -07001715 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001716 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717
Adam Cohen4554ee12011-08-03 16:13:21 -07001718 // Just verify that the folder hasn't already been added to the DragLayer.
1719 // There was a one-off crash where the folder had a parent already.
1720 if (folder.getParent() == null) {
1721 mDragLayer.addView(folder);
1722 mDragController.addDropTarget((DropTarget) folder);
1723 } else {
1724 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1725 folder.getParent() + ").");
1726 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001727 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001728 }
1729
1730 public void closeFolder() {
1731 Folder folder = mWorkspace.getOpenFolder();
1732 if (folder != null) {
1733 closeFolder(folder);
1734 }
1735 }
1736
1737 void closeFolder(Folder folder) {
1738 folder.getInfo().opened = false;
1739
1740 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1741 if (parent != null) {
1742 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1743 shrinkAndFadeInFolderIcon(fi);
1744 }
1745 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001749 if (mState != State.WORKSPACE) {
1750 return false;
1751 }
1752
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 return false;
1755 }
1756
1757 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001758 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
1760
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 resetAddInfo();
1762 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001764 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 return true;
1766 }
1767
Winson Chung3d503fb2011-07-13 17:25:49 -07001768 // The hotseat touch handling does not go through Workspace, and we always allow long press
1769 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001771 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1772 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001773 if (itemUnderLongClick == null) {
1774 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1776 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001777 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001779 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001781 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783 }
1784 }
1785 return true;
1786 }
1787
Winson Chung3d503fb2011-07-13 17:25:49 -07001788 boolean isHotseatLayout(View layout) {
1789 return mHotseat != null && layout != null &&
1790 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1791 }
1792 Hotseat getHotseat() {
1793 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001794 }
1795
Winson Chung3d503fb2011-07-13 17:25:49 -07001796 /**
1797 * Returns the CellLayout of the specified container at the specified screen.
1798 */
1799 CellLayout getCellLayout(long container, int screen) {
1800 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1801 if (mHotseat != null) {
1802 return mHotseat.getLayout();
1803 } else {
1804 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001805 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001806 } else {
1807 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001808 }
1809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 Workspace getWorkspace() {
1812 return mWorkspace;
1813 }
1814
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 @Override
1816 protected Dialog onCreateDialog(int id) {
1817 switch (id) {
1818 case DIALOG_CREATE_SHORTCUT:
1819 return new CreateShortcut().createDialog();
1820 case DIALOG_RENAME_FOLDER:
1821 return new RenameFolder().createDialog();
1822 }
1823
1824 return super.onCreateDialog(id);
1825 }
1826
1827 @Override
1828 protected void onPrepareDialog(int id, Dialog dialog) {
1829 switch (id) {
1830 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 break;
1832 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001833 if (mFolderInfo != null) {
1834 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1835 final CharSequence text = mFolderInfo.title;
1836 input.setText(text);
1837 input.setSelection(0, text.length());
1838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 break;
1840 }
1841 }
1842
1843 void showRenameDialog(FolderInfo info) {
1844 mFolderInfo = info;
1845 mWaitingForResult = true;
1846 showDialog(DIALOG_RENAME_FOLDER);
1847 }
1848
Adam Cohenfbba09b2011-07-18 21:43:05 -07001849 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001850 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001851 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1852 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 mWaitingForResult = true;
1854 showDialog(DIALOG_CREATE_SHORTCUT);
1855 }
1856
1857 private class RenameFolder {
1858 private EditText mInput;
1859
1860 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1862 mInput = (EditText) layout.findViewById(R.id.folder_name);
1863
1864 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1865 builder.setIcon(0);
1866 builder.setTitle(getString(R.string.rename_folder_title));
1867 builder.setCancelable(true);
1868 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1869 public void onCancel(DialogInterface dialog) {
1870 cleanup();
1871 }
1872 });
1873 builder.setNegativeButton(getString(R.string.cancel_action),
1874 new Dialog.OnClickListener() {
1875 public void onClick(DialogInterface dialog, int which) {
1876 cleanup();
1877 }
1878 }
1879 );
1880 builder.setPositiveButton(getString(R.string.rename_action),
1881 new Dialog.OnClickListener() {
1882 public void onClick(DialogInterface dialog, int which) {
1883 changeFolderName();
1884 }
1885 }
1886 );
1887 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001888
1889 final AlertDialog dialog = builder.create();
1890 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1891 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001892 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001893 mInput.requestFocus();
1894 InputMethodManager inputManager = (InputMethodManager)
1895 getSystemService(Context.INPUT_METHOD_SERVICE);
1896 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001897 }
1898 });
1899
1900 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 }
1902
1903 private void changeFolderName() {
1904 final String name = mInput.getText().toString();
1905 if (!TextUtils.isEmpty(name)) {
1906 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001907 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 mFolderInfo.title = name;
1909 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1910
Joe Onorato9c1289c2009-08-17 11:03:03 -04001911 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001912 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001913 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 } else {
1915 final FolderIcon folderIcon = (FolderIcon)
1916 mWorkspace.getViewForTag(mFolderInfo);
1917 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001918 // TODO: At some point we'll probably want some version of setting
1919 // the text for a folder icon.
1920 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 getWorkspace().requestLayout();
1922 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001923 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001924 mWorkspaceLoading = true;
1925 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 }
1927 }
1928 }
1929 cleanup();
1930 }
1931
1932 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 dismissDialog(DIALOG_RENAME_FOLDER);
1934 mWaitingForResult = false;
1935 mFolderInfo = null;
1936 }
1937 }
1938
Daniel Sandler843e8602010-06-07 14:59:01 -04001939 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
1940 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001941 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04001942 }
1943
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001944 // AllAppsView.Watcher
1945 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001946 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001947 mWorkspace.setVisibility(View.GONE);
1948 }
1949 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001950
1951 /**
1952 * Helper method for the cameraZoomIn/cameraZoomOut animations
1953 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07001954 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001955 * @param scaleFactor The scale factor used for the zoom
1956 */
1957 private void setPivotsForZoom(View view, State state, float scaleFactor) {
1958 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08001959
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001960 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08001961 // Set pivotY so that at the starting zoom factor, the view is partially
1962 // visible. Modifying initialHeightFactor changes how much of the view is
1963 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001964 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08001965 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08001966 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001967 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08001968 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08001969 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001970 }
1971 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05001972
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001973 /**
1974 * Zoom the camera out from the workspace to reveal 'toView'.
1975 * Assumes that the view to show is anchored at either the very top or very bottom
1976 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07001977 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001978 */
Winson Chungc07918d2011-07-01 15:35:26 -07001979 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001980 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08001981
Winson Chungf0ea4d32011-06-06 14:27:16 -07001982 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
1983 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
1984 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
1985 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07001986
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001987 setPivotsForZoom(toView, toState, scale);
1988
Michael Jurkad74c9842011-07-10 12:44:21 -07001989 // Shrink workspaces away if going to AppsCustomize from workspace
1990 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07001991 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07001992
1993 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08001994 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08001995 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07001996 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
1997 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08001998 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08001999 toView.setFastScaleX(a * scale + b * 1f);
2000 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002001 }
2002 });
Adam Cohen61033d32010-11-15 18:29:44 -08002003
Winson Chungf0ea4d32011-06-06 14:27:16 -07002004 toView.setVisibility(View.VISIBLE);
2005 toView.setFastAlpha(0f);
2006 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2007 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2008 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2009 public void onAnimationUpdate(float a, float b) {
2010 // don't need to invalidate because we do so above
2011 toView.setFastAlpha(a * 0f + b * 1f);
2012 }
2013 });
2014 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002015
Michael Jurkaabded662011-03-04 12:06:57 -08002016 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002017 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002018 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002019 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002020 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002021 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002022 // Prepare the position
2023 toView.setTranslationX(0.0f);
2024 toView.setTranslationY(0.0f);
2025 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002026 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002027 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002028 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002029 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002030 // If we don't set the final scale values here, if this animation is cancelled
2031 // it will have the wrong scale value and subsequent cameraPan animations will
2032 // not fix that
2033 toView.setScaleX(1.0f);
2034 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002035 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002036 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002037 }
Winson Chung32174c82011-07-19 15:47:55 -07002038
2039 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2040 // Hide the workspace scrollbar
2041 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002042 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002043 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002044 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002045 });
2046
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002047 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002048 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002049
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002050 if (mStateAnimation != null) mStateAnimation.cancel();
2051 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002052 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002053 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002054 } else {
2055 toView.setTranslationX(0.0f);
2056 toView.setTranslationY(0.0f);
2057 toView.setScaleX(1.0f);
2058 toView.setScaleY(1.0f);
2059 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002060 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002061 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002062 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2063 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002064
Winson Chungc07918d2011-07-01 15:35:26 -07002065 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2066 // Hide the workspace scrollbar
2067 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002068 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002069 }
Michael Jurkaabded662011-03-04 12:06:57 -08002070 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002071 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002072 }
2073
2074 /**
2075 * Zoom the camera back into the workspace, hiding 'fromView'.
2076 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002077 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002078 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002079 */
Winson Chungc07918d2011-07-01 15:35:26 -07002080 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002081 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002082
Winson Chungf0ea4d32011-06-06 14:27:16 -07002083 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2084 final float scaleFactor = (float)
2085 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2086 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002087
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002088 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002089
Michael Jurkad3ef3062010-11-23 16:23:58 -08002090 if (!springLoaded) {
2091 mWorkspace.unshrink(animated);
2092 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002093 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002094 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002095 if (mStateAnimation != null) mStateAnimation.cancel();
2096 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002097
Michael Jurka742574b2011-02-02 23:51:01 -08002098 final float oldScaleX = fromView.getScaleX();
2099 final float oldScaleY = fromView.getScaleY();
2100
2101 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2102 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002103 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2104 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002105 ((View)fromView.getParent()).fastInvalidate();
2106 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2107 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2108 }
2109 });
Michael Jurkaabded662011-03-04 12:06:57 -08002110 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002111 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002112 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002113 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2114 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002115 // don't need to invalidate because we do so above
2116 fromView.setFastAlpha(a * 1f + b * 0f);
2117 }
2118 });
Michael Jurkaabded662011-03-04 12:06:57 -08002119 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002120 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002121 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002122 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002123 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002124 public void onAnimationStart(android.animation.Animator animation) {
2125 if (!springLoaded) {
2126 mWorkspace.showDockDivider(false);
2127 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002128 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002129 }
2130 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002131 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002132 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002133 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002134 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002135 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002136 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002137 }
2138 });
2139
Winson Chungf0ea4d32011-06-06 14:27:16 -07002140 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002141 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002142 } else {
2143 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002144 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002145 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2146 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002147
Winson Chungc07918d2011-07-01 15:35:26 -07002148 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002149 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002150 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002151 mWorkspace.flashScrollingIndicator();
2152 }
Michael Jurkaabded662011-03-04 12:06:57 -08002153 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002154 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002155 }
2156
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002157 void showWorkspace(boolean animated) {
2158 showWorkspace(animated, null);
2159 }
2160
2161 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002162 if (layout != null) {
2163 // always animated, but that's ok since we never specify a layout and
2164 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002165 mWorkspace.unshrink(layout);
2166 } else {
2167 mWorkspace.unshrink(animated);
2168 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002169 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002170 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002171 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002172
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002173 // Change the state *after* we've called all the transition code
2174 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002175
Winson Chung5fb63472011-02-02 17:03:37 -08002176 // Resume the auto-advance of widgets
2177 mUserPresent = true;
2178 updateRunning();
2179
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002180 // send an accessibility event to announce the context change
2181 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002182 }
2183
Michael Jurkad3ef3062010-11-23 16:23:58 -08002184 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002185 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002186 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002187 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002188 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002189 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002190 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002191 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002192 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002193 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002194 @Override
2195 public void run() {
2196 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002197
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002198 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002199 // Before we show workspace, hide all apps again because
2200 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2201 // clean up our state transition functions
2202 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002203 showWorkspace(true);
2204 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002205 }
2206 }, (extendedDelay ?
2207 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2208 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2209 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002210 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002211 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002212 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002213 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2214 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002215 }
2216 // Otherwise, we are not in spring loaded mode, so don't do anything.
2217 }
2218
Adam Cohenfc53cd22011-07-20 15:45:11 -07002219 public boolean isAllAppsCustomizeOpen() {
2220 return mState == State.APPS_CUSTOMIZE;
2221 }
2222
Winson Chungf0ea4d32011-06-06 14:27:16 -07002223 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002224 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002225 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002226 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002227 if (!LauncherApplication.isScreenLarge()) {
2228 if (animated) {
2229 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002231 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002232 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002233 }
2234 }
2235 }
2236
2237 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002238 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002239 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002240 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002241 if (!LauncherApplication.isScreenLarge()) {
2242 if (animated) {
2243 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002245 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002246 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002247 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002248 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002249 }
2250
Winson Chung785d2eb2011-04-14 16:08:02 -07002251 void showAllApps(boolean animated) {
2252 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002253
Winson Chungf0ea4d32011-06-06 14:27:16 -07002254 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2255 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002256
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002258 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002259
Winson Chungf0ea4d32011-06-06 14:27:16 -07002260 // Change the state *after* we've called all the transition code
2261 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002262
2263 // Pause the auto-advance of widgets until we are out of AllApps
2264 mUserPresent = false;
2265 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002266 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002267
2268 // Send an accessibility event to announce the context change
2269 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2270 }
2271
Joe Onoratob2061212009-11-24 16:13:54 -05002272 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002273 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002274 * - Home from workspace
2275 * - from center screen
2276 * - from other screens
2277 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002278 * - from center screen
2279 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002280 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002281 * - from center screen
2282 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002283 * - Launch app from workspace and quit
2284 * - with back
2285 * - with home
2286 * - Launch app from all apps and quit
2287 * - with back
2288 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002289 * - Go to a screen that's not the default, then all
2290 * apps, and launch and app, and go back
2291 * - with back
2292 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002293 * - On workspace, long press power and go back
2294 * - with back
2295 * - with home
2296 * - On all apps, long press power and go back
2297 * - with back
2298 * - with home
2299 * - On workspace, power off
2300 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002301 * - Launch an app and turn off the screen while in that app
2302 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002303 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002304 * - From all apps
2305 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002306 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2307 * - From all apps
2308 * - From the center workspace
2309 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002310 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002311 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2313 mWorkspace.setVisibility(View.VISIBLE);
2314 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002315
Winson Chung3d503fb2011-07-13 17:25:49 -07002316 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002317 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002318
Winson Chungf0ea4d32011-06-06 14:27:16 -07002319 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 if (mAllAppsButton != null) {
2321 mAllAppsButton.requestFocus();
2322 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002323 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002324 }
2325
Joe Onorato7c312c12009-08-13 21:36:53 -07002326 void lockAllApps() {
2327 // TODO
2328 }
2329
2330 void unlockAllApps() {
2331 // TODO
2332 }
2333
Patrick Dubroy5f445422011-02-18 14:35:21 -08002334 /**
2335 * Add an item from all apps or customize onto the given workspace screen.
2336 * If layout is null, add to the current screen.
2337 */
2338 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002339 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2340 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002341 } else {
2342 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002343 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002344 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002345
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002346 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002347 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002348 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002349 // Look for the toolbar icon specified in the activity meta-data
2350 Bundle metaData = packageManager.getActivityInfo(
2351 activityName, PackageManager.GET_META_DATA).metaData;
2352 if (metaData != null) {
2353 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2354 if (iconResId != 0) {
2355 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002356 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002357 }
2358 }
2359 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002360 // This can happen if the activity defines an invalid drawable
2361 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2362 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002363 } catch (Resources.NotFoundException nfe) {
2364 // This can happen if the activity defines an invalid drawable
2365 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2366 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002367 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002368 return null;
2369 }
2370
2371 // if successful in getting icon, return it; otherwise, set button to use default drawable
2372 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2373 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2374 TextView button = (TextView) findViewById(buttonId);
2375 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2376
2377 // If we were unable to find the icon via the meta-data, use a generic one
2378 if (toolbarIcon == null) {
2379 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2380 return null;
2381 } else {
2382 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2383 return toolbarIcon.getConstantState();
2384 }
2385 }
2386
2387 // if successful in getting icon, return it; otherwise, set button to use default drawable
2388 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2389 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2390 ImageView button = (ImageView) findViewById(buttonId);
2391 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2392
Michael Jurka19e0fc52011-07-22 18:00:21 -07002393 if (button != null) {
2394 // If we were unable to find the icon via the meta-data, use a
2395 // generic one
2396 if (toolbarIcon == null) {
2397 button.setImageResource(fallbackDrawableId);
2398 } else {
2399 button.setImageDrawable(toolbarIcon);
2400 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002401 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002402
2403 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2404
Michael Jurka0423dcf2010-10-05 14:56:18 -07002405 }
2406
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002407 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2408 TextView button = (TextView) findViewById(buttonId);
2409 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2410 }
2411
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002412 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002413 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002414 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002415 }
2416
Michael Jurka0423dcf2010-10-05 14:56:18 -07002417 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002418 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2419 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002420
Winson Chung1cad91e2011-05-25 17:41:01 -07002421 final SearchManager searchManager =
2422 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2423 ComponentName activityName = searchManager.getGlobalSearchActivity();
2424 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002425 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002426 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002427 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002428 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002429 } else {
2430 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002431 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002432 }
2433 }
2434
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002435 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002436 updateButtonWithDrawable(R.id.search_button, d);
2437 }
2438
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002439 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002440 final View searchDivider = findViewById(R.id.search_divider);
2441 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002442
Winson Chung1cad91e2011-05-25 17:41:01 -07002443 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2444 ComponentName activityName = intent.resolveActivity(getPackageManager());
2445 if (activityName != null) {
2446 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002447 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2448 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002449 voiceButton.setVisibility(View.VISIBLE);
2450 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002451 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002452 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002453 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002454 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002455
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002456 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002457 updateButtonWithDrawable(R.id.voice_button, d);
2458 }
2459
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002460 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002461 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002462 */
2463 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002464 final View marketButton = findViewById(R.id.market_button);
2465 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2466 // Find the app market activity by resolving an intent.
2467 // (If multiple app markets are installed, it will return the ResolverActivity.)
2468 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002469 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002470 mAppMarketIntent = intent;
2471 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002472 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002473 marketButton.setVisibility(View.VISIBLE);
2474 } else {
2475 // We should hide and disable the view so that we don't try and restore the visibility
2476 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2477 marketButton.setVisibility(View.GONE);
2478 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002479 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002480 }
2481
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002482 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002483 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002484 }
2485
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002486 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 * Displays the shortcut creation dialog and launches, if necessary, the
2488 * appropriate activity.
2489 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002490 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002491 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2492 DialogInterface.OnShowListener {
2493
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002498
Winson Chung55b65502011-05-26 12:03:43 -07002499 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2500 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002501 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002502
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 AlertDialog dialog = builder.create();
2504 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002505 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002506 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 return dialog;
2509 }
2510
2511 public void onCancel(DialogInterface dialog) {
2512 mWaitingForResult = false;
2513 cleanup();
2514 }
2515
Romain Guycbb89e42009-06-08 15:52:54 -07002516 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002517 mWaitingForResult = false;
2518 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002519 }
2520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002522 try {
2523 dismissDialog(DIALOG_CREATE_SHORTCUT);
2524 } catch (Exception e) {
2525 // An exception is thrown if the dialog is not visible, which is fine
2526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 }
2528
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002529 /**
2530 * Handle the action clicked in the "Add to home" dialog.
2531 */
2532 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002533 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002534
Winson Chung55b65502011-05-26 12:03:43 -07002535 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2536 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002537 case AddAdapter.ITEM_APPLICATION: {
2538 if (mAppsCustomizeTabHost != null) {
2539 mAppsCustomizeTabHost.selectAppsTab();
2540 }
2541 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002542 break;
2543 }
Winson Chung55b65502011-05-26 12:03:43 -07002544 case AddAdapter.ITEM_APPWIDGET: {
2545 if (mAppsCustomizeTabHost != null) {
2546 mAppsCustomizeTabHost.selectWidgetsTab();
2547 }
2548 showAllApps(true);
2549 break;
2550 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002551 case AddAdapter.ITEM_WALLPAPER: {
2552 startWallpaper();
2553 break;
2554 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 }
2556 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002557
2558 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002559 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 }
2562
2563 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002564 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002565 */
2566 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2567 @Override
2568 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002569 closeSystemDialogs();
2570 }
2571 }
2572
2573 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002574 * Receives notifications whenever the appwidgets are reset.
2575 */
2576 private class AppWidgetResetObserver extends ContentObserver {
2577 public AppWidgetResetObserver() {
2578 super(new Handler());
2579 }
2580
2581 @Override
2582 public void onChange(boolean selfChange) {
2583 onAppWidgetReset();
2584 }
2585 }
2586
2587 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002588 * If the activity is currently paused, signal that we need to re-run the loader
2589 * in onResume.
2590 *
2591 * This needs to be called from incoming places where resources might have been loaded
2592 * while we are paused. That is becaues the Configuration might be wrong
2593 * when we're not running, and if it comes back to what it was when we
2594 * were paused, we are not restarted.
2595 *
2596 * Implementation of the method from LauncherModel.Callbacks.
2597 *
2598 * @return true if we are currently paused. The caller might be able to
2599 * skip some work in that case since we will come back again.
2600 */
2601 public boolean setLoadOnResume() {
2602 if (mPaused) {
2603 Log.i(TAG, "setLoadOnResume");
2604 mOnResumeNeedsLoad = true;
2605 return true;
2606 } else {
2607 return false;
2608 }
2609 }
2610
2611 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002612 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002614 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002615 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002616 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002617 } else {
2618 return SCREEN_COUNT / 2;
2619 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002620 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002621
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002622
Joe Onorato9c1289c2009-08-17 11:03:03 -04002623 /**
2624 * Refreshes the shortcuts shown on the workspace.
2625 *
2626 * Implementation of the method from LauncherModel.Callbacks.
2627 */
2628 public void startBinding() {
2629 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002630
2631 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002632 int count = workspace.getChildCount();
2633 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002634 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002635 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2636 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002637 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002638 if (mHotseat != null) {
2639 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002641
Adam Cohen4eac29a2011-07-11 17:53:37 -07002642 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002643 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002644 }
2645
2646 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002647 * Bind the items start-end from the list.
2648 *
2649 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002651 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002652 setLoadOnResume();
2653
Joe Onorato9c1289c2009-08-17 11:03:03 -04002654 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002655 for (int i=start; i<end; i++) {
2656 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002657
2658 // Short circuit if we are loading dock items for a configuration which has no dock
2659 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2660 mHotseat == null) {
2661 continue;
2662 }
2663
Joe Onorato9c1289c2009-08-17 11:03:03 -04002664 switch (item.itemType) {
2665 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2666 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002667 View shortcut = createShortcut((ShortcutInfo)item);
2668 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2669 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002670 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002671 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002672 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002673 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002674 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002675 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2676 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002677 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002680 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 }
2682
Joe Onorato9c1289c2009-08-17 11:03:03 -04002683 /**
2684 * Implementation of the method from LauncherModel.Callbacks.
2685 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002686 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002687 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002688 sFolders.clear();
2689 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002691
2692 /**
2693 * Add the views for a widget to the workspace.
2694 *
2695 * Implementation of the method from LauncherModel.Callbacks.
2696 */
2697 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002698 setLoadOnResume();
2699
Daniel Sandler843e8602010-06-07 14:59:01 -04002700 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2701 if (DEBUG_WIDGETS) {
2702 Log.d(TAG, "bindAppWidget: " + item);
2703 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002704 final Workspace workspace = mWorkspace;
2705
2706 final int appWidgetId = item.appWidgetId;
2707 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002708 if (DEBUG_WIDGETS) {
2709 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2710 }
2711
Joe Onorato9c1289c2009-08-17 11:03:03 -04002712 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2713
Joe Onorato9c1289c2009-08-17 11:03:03 -04002714 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2715 item.hostView.setTag(item);
2716
Winson Chung3d503fb2011-07-13 17:25:49 -07002717 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002718 item.cellY, item.spanX, item.spanY, false);
2719
Adam Cohended9f8d2010-11-03 13:25:16 -07002720 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2721
Joe Onorato9c1289c2009-08-17 11:03:03 -04002722 workspace.requestLayout();
2723
Daniel Sandler843e8602010-06-07 14:59:01 -04002724 if (DEBUG_WIDGETS) {
2725 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2726 + (SystemClock.uptimeMillis()-start) + "ms");
2727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002728 }
2729
2730 /**
2731 * Callback saying that there aren't any more items to bind.
2732 *
2733 * Implementation of the method from LauncherModel.Callbacks.
2734 */
2735 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002736 setLoadOnResume();
2737
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 if (mSavedState != null) {
2739 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002740 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002742 mSavedState = null;
2743 }
2744
2745 if (mSavedInstanceState != null) {
2746 super.onRestoreInstanceState(mSavedInstanceState);
2747 mSavedInstanceState = null;
2748 }
2749
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002751
2752 // If we received the result of any pending adds while the loader was running (e.g. the
2753 // widget configuration forced an orientation change), process them now.
2754 for (int i = 0; i < sPendingAddList.size(); i++) {
2755 completeAdd(sPendingAddList.get(i));
2756 }
2757 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758 }
2759
2760 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002761 * Updates the icons on the launcher that are affected by changes to the package list
2762 * on the device.
2763 */
2764 private void updateIconsAffectedByPackageManagerChanges() {
2765 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002766 updateVoiceSearchIcon();
2767 }
2768
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002769 @Override
2770 public void bindSearchablesChanged() {
2771 updateGlobalSearchIcon();
2772 }
2773
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002774 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002775 * Add the icons for all apps.
2776 *
2777 * Implementation of the method from LauncherModel.Callbacks.
2778 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002779 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2780 // Remove the progress bar entirely; we could also make it GONE
2781 // but better to remove it since we know it's not going to be used
2782 View progressBar = mAppsCustomizeTabHost.
2783 findViewById(R.id.apps_customize_progress_bar);
2784 if (progressBar != null) {
2785 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002786 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002787 // We just post the call to setApps so the user sees the progress bar
2788 // disappear-- otherwise, it just looks like the progress bar froze
2789 // which doesn't look great
2790 mAppsCustomizeTabHost.post(new Runnable() {
2791 public void run() {
2792 if (mAppsCustomizeContent != null) {
2793 mAppsCustomizeContent.setApps(apps);
2794 }
2795 }
2796 });
2797
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002798 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002799 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002800 }
2801
2802 /**
2803 * A package was installed.
2804 *
2805 * Implementation of the method from LauncherModel.Callbacks.
2806 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002807 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002808 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002809 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002810
Winson Chung785d2eb2011-04-14 16:08:02 -07002811 if (mAppsCustomizeContent != null) {
2812 mAppsCustomizeContent.addApps(apps);
2813 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002814 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002815 }
2816
2817 /**
2818 * A package was updated.
2819 *
2820 * Implementation of the method from LauncherModel.Callbacks.
2821 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002822 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002823 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002825 if (mWorkspace != null) {
2826 mWorkspace.updateShortcuts(apps);
2827 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002828
Winson Chung785d2eb2011-04-14 16:08:02 -07002829 if (mAppsCustomizeContent != null) {
2830 mAppsCustomizeContent.updateApps(apps);
2831 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002832 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 }
2834
2835 /**
2836 * A package was uninstalled.
2837 *
2838 * Implementation of the method from LauncherModel.Callbacks.
2839 */
Joe Onorato36115782010-06-17 13:28:48 -04002840 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002842 if (permanent) {
2843 mWorkspace.removeItems(apps);
2844 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002845
Winson Chung785d2eb2011-04-14 16:08:02 -07002846 if (mAppsCustomizeContent != null) {
2847 mAppsCustomizeContent.removeApps(apps);
2848 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002849 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 }
Joe Onoratobe386092009-11-17 17:32:16 -08002851
2852 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002853 * A number of packages were updated.
2854 */
2855 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002856
Winson Chung785d2eb2011-04-14 16:08:02 -07002857 if (mAppsCustomizeContent != null) {
2858 mAppsCustomizeContent.onPackagesUpdated();
2859 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002860 }
2861
Winson Chung400438b2011-01-16 17:53:48 -08002862 private int mapConfigurationOriActivityInfoOri(int configOri) {
2863 final Display d = getWindowManager().getDefaultDisplay();
2864 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2865 switch (d.getRotation()) {
2866 case Surface.ROTATION_0:
2867 case Surface.ROTATION_180:
2868 // We are currently in the same basic orientation as the natural orientation
2869 naturalOri = configOri;
2870 break;
2871 case Surface.ROTATION_90:
2872 case Surface.ROTATION_270:
2873 // We are currently in the other basic orientation to the natural orientation
2874 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2875 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2876 break;
2877 }
2878
2879 int[] oriMap = {
2880 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2881 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2882 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2883 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2884 };
2885 // Since the map starts at portrait, we need to offset if this device's natural orientation
2886 // is landscape.
2887 int indexOffset = 0;
2888 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2889 indexOffset = 1;
2890 }
2891 return oriMap[(d.getRotation() + indexOffset) % 4];
2892 }
2893 public void lockScreenOrientation() {
2894 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2895 .getConfiguration().orientation));
2896 }
2897 public void unlockScreenOrientation() {
2898 mHandler.postDelayed(new Runnable() {
2899 public void run() {
2900 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2901 }
2902 }, mRestoreScreenOrientationDelay);
2903 }
2904
Winson Chung80baf5a2010-08-09 16:03:15 -07002905 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002906 * Prints out out state for debugging.
2907 */
2908 public void dumpState() {
2909 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002910 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002911 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2912 Log.d(TAG, "mRestoring=" + mRestoring);
2913 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2914 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002915 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002916 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002917
Winson Chung785d2eb2011-04-14 16:08:02 -07002918 if (mAppsCustomizeContent != null) {
2919 mAppsCustomizeContent.dumpState();
2920 }
Joe Onoratobe386092009-11-17 17:32:16 -08002921 Log.d(TAG, "END launcher2 dump state");
2922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923}
Michael Jurka2763be32011-02-24 11:19:57 -08002924
Michael Jurkaabded662011-03-04 12:06:57 -08002925interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002926 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2927 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002928}