blob: f05850b36b37371e66f89bb99f0f488130a12b91 [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;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Adam Cohen50370f32011-08-25 18:57:14 -070079import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080080import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080081import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070083import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070085import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070086import android.widget.TextView;
87import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070088
Adam Cohendf2cc412011-04-27 16:56:57 -070089import com.android.common.Search;
90import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070091import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080092
Adam Cohenc0dcf592011-06-01 15:30:43 -070093import java.io.DataInputStream;
94import java.io.DataOutputStream;
95import java.io.FileNotFoundException;
96import java.io.IOException;
97import java.util.ArrayList;
98import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Default launcher application.
102 */
Michael Jurka946ad472010-07-09 18:05:18 -0700103public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700104 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
105 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400110 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700111
Winson Chung70d72102011-08-12 11:24:35 -0700112 private static final int MENU_GROUP_WALLPAPER = 1;
113 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
114 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700115 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
116 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
118 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700119 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int REQUEST_PICK_APPLICATION = 6;
121 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700122 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700123 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
126
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800127 static final int SCREEN_COUNT = 5;
128 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
Joe Onorato7c312c12009-08-13 21:36:53 -0700130 static final int DIALOG_CREATE_SHORTCUT = 1;
131 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Romain Guy98d01652009-06-30 16:21:04 -0700133 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 // Type: int
136 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700137 // Type: int
138 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700140 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
141 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
143 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700144 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: boolean
148 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
149 // Type: long
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
151
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700152 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
153
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700154 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700155 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700156 private State mState = State.WORKSPACE;
157 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700158
Joe Onorato9c1289c2009-08-17 11:03:03 -0400159 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700160 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
161 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700162 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800167 private final BroadcastReceiver mCloseSystemDialogsReceiver
168 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800169 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private LayoutInflater mInflater;
172
Joe Onorato00acb122009-08-04 16:04:30 -0400173 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700175
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700176 private AppWidgetManager mAppWidgetManager;
177 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700178
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700179 private ItemInfo mPendingAddInfo = new ItemInfo("1");
Michael Jurka0280c3b2010-09-17 15:00:07 -0700180 private int[] mTmpAddItemCellCoordinates = new int[2];
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private FolderInfo mFolderInfo;
183
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800185 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700186
Winson Chungf0ea4d32011-06-06 14:27:16 -0700187 private SearchDropTargetBar mSearchDeleteBar;
188 private AppsCustomizeTabHost mAppsCustomizeTabHost;
189 private AppsCustomizePagedView mAppsCustomizeContent;
190 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private Bundle mSavedState;
193
194 private SpannableStringBuilder mDefaultKeySsb = null;
195
Joe Onorato9c1289c2009-08-17 11:03:03 -0400196 private boolean mWorkspaceLoading = true;
197
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800198 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private boolean mRestoring;
200 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700201 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
203 private Bundle mSavedInstanceState;
204
Joe Onorato9c1289c2009-08-17 11:03:03 -0400205 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800206 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800207 private boolean mUserPresent = true;
208 private boolean mVisible = false;
209 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700211 private static LocaleConfiguration sLocaleConfiguration = null;
212
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700213 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700215 private Intent mAppMarketIntent = null;
216
Adam Cohended9f8d2010-11-03 13:25:16 -0700217 // Related to the auto-advancing of widgets
218 private final int ADVANCE_MSG = 1;
219 private final int mAdvanceInterval = 20000;
220 private final int mAdvanceStagger = 250;
221 private long mAutoAdvanceSentTime;
222 private long mAutoAdvanceTimeLeft = -1;
223 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
224 new HashMap<View, AppWidgetProviderInfo>();
225
Winson Chung400438b2011-01-16 17:53:48 -0800226 // Determines how long to wait after a rotation before restoring the screen orientation to
227 // match the sensor state.
228 private final int mRestoreScreenOrientationDelay = 500;
229
Michael Jurka4ef207b2010-11-29 17:05:45 -0800230 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800231 private static Drawable.ConstantState sGlobalSearchIcon;
232 private static Drawable.ConstantState sVoiceSearchIcon;
233 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800234
Adam Cohen2801caf2011-05-13 20:57:39 -0700235 private DragLayer mDragLayer;
236
Michael Jurkaddd62e92011-02-16 17:49:14 -0800237 private BubbleTextView mWaitingForResume;
238
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800239 private static ArrayList<PendingAddArguments> sPendingAddList
240 = new ArrayList<PendingAddArguments>();
241
242 private static class PendingAddArguments {
243 int requestCode;
244 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700245 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800246 int screen;
247 int cellX;
248 int cellY;
249 }
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 @Override
252 protected void onCreate(Bundle savedInstanceState) {
253 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800254 LauncherApplication app = ((LauncherApplication)getApplication());
255 mModel = app.setLauncher(this);
256 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400257 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700259
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
262 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200265 android.os.Debug.startMethodTracing(
266 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 }
268
269 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 setContentView(R.layout.launcher);
271 setupViews();
Winson Chung82f55532011-08-09 14:14:23 -0700272 enableClingsIfNecessary();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273
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 Chungc7da5552011-08-10 15:28:45 -0700486 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800487 final PendingAddArguments args = new PendingAddArguments();
488 args.requestCode = requestCode;
489 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700490 args.container = mPendingAddInfo.container;
491 args.screen = mPendingAddInfo.screen;
492 args.cellX = mPendingAddInfo.cellX;
493 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800494
495 // If the loader is still running, defer the add until it is done.
496 if (isWorkspaceLocked()) {
497 sPendingAddList.add(args);
498 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700499 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 }
Romain Guy18042c82009-11-06 11:44:55 -0800501 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
503 if (data != null) {
504 // Clean up the appWidgetId if we canceled
505 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
506 if (appWidgetId != -1) {
507 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 }
510 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700511
512 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700513 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 }
515
516 @Override
517 protected void onResume() {
518 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800519 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700520 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400521 mWorkspaceLoading = true;
522 mModel.startLoader(this, true);
523 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700524 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800526 if (mWaitingForResume != null) {
527 mWaitingForResume.setStayPressed(false);
528 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700529 // When we resume Launcher, a different Activity might be responsible for the app
530 // market intent, so refresh the icon
531 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 }
533
534 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700535 protected void onPause() {
536 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700537 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800538 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 }
Romain Guycbb89e42009-06-08 15:52:54 -0700540
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700541 @Override
542 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400543 // Flag the loader to stop early before switching
544 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700545 if (mAppsCustomizeContent != null) {
546 mAppsCustomizeContent.surrender();
547 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800548 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700549 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800551 // We can't hide the IME if it was forced open. So don't bother
552 /*
553 @Override
554 public void onWindowFocusChanged(boolean hasFocus) {
555 super.onWindowFocusChanged(hasFocus);
556
557 if (hasFocus) {
558 final InputMethodManager inputManager = (InputMethodManager)
559 getSystemService(Context.INPUT_METHOD_SERVICE);
560 WindowManager.LayoutParams lp = getWindow().getAttributes();
561 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
562 android.os.Handler()) {
563 protected void onReceiveResult(int resultCode, Bundle resultData) {
564 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
565 }
566 });
567 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
568 }
569 }
570 */
571
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800572 private boolean acceptFilter() {
573 final InputMethodManager inputManager = (InputMethodManager)
574 getSystemService(Context.INPUT_METHOD_SERVICE);
575 return !inputManager.isFullscreenMode();
576 }
577
578 @Override
579 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700580 final int uniChar = event.getUnicodeChar();
581 final boolean handled = super.onKeyDown(keyCode, event);
582 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
583 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800584 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
585 keyCode, event);
586 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700587 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700588 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700589 // showSearchDialog()
590 // If there are multiple keystrokes before the search dialog takes focus,
591 // onSearchRequested() will be called for every keystroke,
592 // but it is idempotent, so it's fine.
593 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800594 }
595 }
596
Joe Onorato8a9625e2010-01-28 15:55:35 -0800597 // Eat the long press event so the keyboard doesn't come up.
598 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
599 return true;
600 }
601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 return handled;
603 }
604
Karl Rosaen138a0412009-04-23 19:00:21 -0700605 private String getTypedText() {
606 return mDefaultKeySsb.toString();
607 }
608
609 private void clearTypedText() {
610 mDefaultKeySsb.clear();
611 mDefaultKeySsb.clearSpans();
612 Selection.setSelection(mDefaultKeySsb, 0);
613 }
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700616 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
617 * State
618 */
619 private static State intToState(int stateOrdinal) {
620 State state = State.WORKSPACE;
621 final State[] stateValues = State.values();
622 for (int i = 0; i < stateValues.length; i++) {
623 if (stateValues[i].ordinal() == stateOrdinal) {
624 state = stateValues[i];
625 break;
626 }
627 }
628 return state;
629 }
630
631 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 * Restores the previous state, if it exists.
633 *
634 * @param savedState The previous state.
635 */
636 private void restoreState(Bundle savedState) {
637 if (savedState == null) {
638 return;
639 }
640
Michael Jurka883f55b2010-10-21 15:47:14 -0700641 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700642 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800643 showAllApps(false);
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
647 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700648 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
650
Winson Chung3d503fb2011-07-13 17:25:49 -0700651 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
652 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700653
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
655 mPendingAddInfo.container = pendingAddContainer;
656 mPendingAddInfo.screen = pendingAddScreen;
657 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
658 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 mRestoring = true;
660 }
661
662 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
663 if (renameFolder) {
664 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700665 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 mRestoring = true;
667 }
Winson Chunga12a2502010-12-20 14:41:35 -0800668
Winson Chung785d2eb2011-04-14 16:08:02 -0700669
670 // Restore the AppsCustomize tab
671 if (mAppsCustomizeTabHost != null) {
672 String curTab = savedState.getString("apps_customize_currentTab");
673 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700674 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700675 mAppsCustomizeContent.setContentType(
676 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
677 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700678 mAppsCustomizeContent.loadAssociatedPages(
679 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700680 }
681
Winson Chung5afbf7b2011-07-25 11:53:08 -0700682 int currentIndex = savedState.getInt("apps_customize_currentIndex");
683 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 }
686
687 /**
688 * Finds all the views we need and configure them properly.
689 */
690 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700691 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400692
Winson Chung4c98d922011-05-31 16:50:48 -0700693 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
694 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695
Winson Chung4c98d922011-05-31 16:50:48 -0700696 // Setup the drag layer
697 mDragLayer.setup(this, dragController);
698
Winson Chung3d503fb2011-07-13 17:25:49 -0700699 // Setup the hotseat
700 mHotseat = (Hotseat) findViewById(R.id.hotseat);
701 if (mHotseat != null) {
702 mHotseat.setup(this);
703 }
704
Winson Chung4c98d922011-05-31 16:50:48 -0700705 // Setup the workspace
706 mWorkspace.setHapticFeedbackEnabled(false);
707 mWorkspace.setOnLongClickListener(this);
708 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700709 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700710
Winson Chungf0ea4d32011-06-06 14:27:16 -0700711 // Get the search/delete bar
712 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700713
Winson Chungf0ea4d32011-06-06 14:27:16 -0700714 // Setup AppsCustomize
715 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
716 findViewById(R.id.apps_customize_pane);
717 mAppsCustomizeContent = (AppsCustomizePagedView)
718 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
719 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400720
Romain Guy1fbc1c82009-11-09 20:43:08 -0800721
Winson Chung4c98d922011-05-31 16:50:48 -0700722
Winson Chung3d503fb2011-07-13 17:25:49 -0700723 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700724 dragController.setDragScoller(mWorkspace);
725 dragController.setScrollView(mDragLayer);
726 dragController.setMoveTarget(mWorkspace);
727 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700728 if (mSearchDeleteBar != null) {
729 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
732
733 /**
734 * Creates a view representing a shortcut.
735 *
736 * @param info The data structure describing the shortcut.
737 *
738 * @return A View inflated from R.layout.application.
739 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800740 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700742 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744
745 /**
746 * Creates a view representing a shortcut inflated from the specified resource.
747 *
748 * @param layoutResId The id of the XML layout used to create the shortcut.
749 * @param parent The group the shortcut belongs to.
750 * @param info The data structure describing the shortcut.
751 *
752 * @return A View inflated from layoutResId.
753 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800754 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800755 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
756 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 return favorite;
759 }
760
761 /**
762 * Add an application shortcut to the workspace.
763 *
764 * @param data The intent describing the application.
765 * @param cellInfo The position on screen where to create the shortcut.
766 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700767 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700768 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700769 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700770
Adam Cohenfbba09b2011-07-18 21:43:05 -0700771 // First we check if we already know the exact location where we want to add this item.
772 if (cellX >= 0 && cellY >= 0) {
773 cellXY[0] = cellX;
774 cellXY[1] = cellY;
775 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700776 showOutOfSpaceMessage();
777 return;
778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800780 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800781
Romain Guy73b979d2009-06-09 12:57:21 -0700782 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800783 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800785 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700787 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800788 } else {
789 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
791 }
Romain Guycbb89e42009-06-08 15:52:54 -0700792
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 /**
794 * Add a shortcut to the workspace.
795 *
796 * @param data The intent describing the shortcut.
797 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700799 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
800 int cellY) {
801 int[] cellXY = mTmpAddItemCellCoordinates;
802 int[] touchXY = mPendingAddInfo.dropPos;
803 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700804
Michael Jurkad3ef3062010-11-23 16:23:58 -0800805 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700806
Adam Cohen558baaf2011-08-15 15:22:57 -0700807 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
808 final View view = createShortcut(info);
809
Adam Cohenfbba09b2011-07-18 21:43:05 -0700810 // First we check if we already know the exact location where we want to add this item.
811 if (cellX >= 0 && cellY >= 0) {
812 cellXY[0] = cellX;
813 cellXY[1] = cellY;
814 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700815
816 // If appropriate, either create a folder or add to an existing folder
817 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
818 true, null,null)) {
819 return;
820 }
821 DragObject dragObject = new DragObject();
822 dragObject.dragInfo = info;
823 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
824 return;
825 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700826 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800827 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700828 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800829 foundCellSpan = (result != null);
830 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700831 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800832 }
833
834 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700835 showOutOfSpaceMessage();
836 return;
837 }
838
Adam Cohen558baaf2011-08-15 15:22:57 -0700839 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
841 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700842 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
843 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 }
845 }
846
Adam Cohen50370f32011-08-25 18:57:14 -0700847 class Padding {
848 int left = 0;
849 int right = 0;
850 int top = 0;
851 int bottom = 0;
852 }
853
854 Padding getPaddingForWidget(AppWidgetProviderInfo widgetInfo) {
855 PackageManager packageManager = getPackageManager();
856 Padding p = new Padding();
857 android.content.pm.ApplicationInfo appInfo;
858
859 try {
860 appInfo = packageManager.getApplicationInfo(
861 widgetInfo.provider.getPackageName(), 0);
862 } catch (Exception e) {
863 // if we can't find the package, return 0 padding
864 return p;
865 }
866
867 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
868 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
869 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
870 Resources r = getResources();
871 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
872 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
873 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
874 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
875 }
876
877 return p;
878 }
879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700881 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700883 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700884 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700887 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700888
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700889 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700891
Adam Cohen50370f32011-08-25 18:57:14 -0700892 Padding padding = getPaddingForWidget(appWidgetInfo);
Adam Cohen09353862011-07-14 16:10:03 -0700893 // We want to account for the extra amount of padding that we are adding to the widget
894 // to ensure that it gets the full amount of space that it has requested
Adam Cohen50370f32011-08-25 18:57:14 -0700895 int requiredWidth = appWidgetInfo.minWidth + padding.left + padding.right;
896 int requiredHeight = appWidgetInfo.minHeight + padding.top + padding.bottom;
Adam Cohen09353862011-07-14 16:10:03 -0700897 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700898
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700900 // We have saved the position to which the widget was dragged-- this really only matters
901 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700902 int[] cellXY = mTmpAddItemCellCoordinates;
903 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700904 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700905 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
906 cellXY[0] = mPendingAddInfo.cellX;
907 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700908 foundCellSpan = true;
909 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700910 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700912 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800913 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700914 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700915 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700916 }
917
Michael Jurka0280c3b2010-09-17 15:00:07 -0700918 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800919 if (appWidgetId != -1) {
920 // Deleting an app widget ID is a void call but writes to disk before returning
921 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800922 new Thread("deleteAppWidgetId") {
923 public void run() {
924 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
925 }
926 }.start();
927 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700928 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800929 return;
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700932 // Build Launcher-specific widget info and save to database
Michael Jurkab60fd0e2011-08-29 14:02:47 -0700933 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId, "2");
Michael Jurka0280c3b2010-09-17 15:00:07 -0700934 launcherInfo.spanX = spanXY[0];
935 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700936
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700938 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939
940 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700942 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700943
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700944 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700946
Winson Chung3d503fb2011-07-13 17:25:49 -0700947 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400948 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700949
950 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952 }
Romain Guycbb89e42009-06-08 15:52:54 -0700953
Adam Cohended9f8d2010-11-03 13:25:16 -0700954 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
955 @Override
956 public void onReceive(Context context, Intent intent) {
957 final String action = intent.getAction();
958 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
959 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700960 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700961 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700962
Winson Chungc100e8e2011-08-09 16:02:43 -0700963 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700964 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700965 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
966 mAppsCustomizeTabHost.reset();
967 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700968 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700969 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
970 mUserPresent = true;
971 updateRunning();
972 }
973 }
974 };
975
976 @Override
977 public void onAttachedToWindow() {
978 super.onAttachedToWindow();
979
980 // Listen for broadcasts related to user-presence
981 final IntentFilter filter = new IntentFilter();
982 filter.addAction(Intent.ACTION_SCREEN_OFF);
983 filter.addAction(Intent.ACTION_USER_PRESENT);
984 registerReceiver(mReceiver, filter);
985
Adam Cohend113e0c2010-11-11 10:48:05 -0800986 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700987 mVisible = true;
988 }
989
990 @Override
991 public void onDetachedFromWindow() {
992 super.onDetachedFromWindow();
993 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700994 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700995
Adam Cohend113e0c2010-11-11 10:48:05 -0800996 if (mAttached) {
997 unregisterReceiver(mReceiver);
998 mAttached = false;
999 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001000 updateRunning();
1001 }
1002
1003 public void onWindowVisibilityChanged(int visibility) {
1004 mVisible = visibility == View.VISIBLE;
1005 updateRunning();
1006 }
1007
1008 private void sendAdvanceMessage(long delay) {
1009 mHandler.removeMessages(ADVANCE_MSG);
1010 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1011 mHandler.sendMessageDelayed(msg, delay);
1012 mAutoAdvanceSentTime = System.currentTimeMillis();
1013 }
1014
1015 private void updateRunning() {
1016 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1017 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1018 mAutoAdvanceRunning = autoAdvanceRunning;
1019 if (autoAdvanceRunning) {
1020 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1021 sendAdvanceMessage(delay);
1022 } else {
1023 if (!mWidgetsToAdvance.isEmpty()) {
1024 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1025 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1026 }
1027 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001028 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001029 }
1030 }
1031 }
1032
1033 private final Handler mHandler = new Handler() {
1034 @Override
1035 public void handleMessage(Message msg) {
1036 if (msg.what == ADVANCE_MSG) {
1037 int i = 0;
1038 for (View key: mWidgetsToAdvance.keySet()) {
1039 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1040 final int delay = mAdvanceStagger * i;
1041 if (v instanceof Advanceable) {
1042 postDelayed(new Runnable() {
1043 public void run() {
1044 ((Advanceable) v).advance();
1045 }
1046 }, delay);
1047 }
1048 i++;
1049 }
1050 sendAdvanceMessage(mAdvanceInterval);
1051 }
1052 }
1053 };
1054
1055 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001056 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001057 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1058 if (v instanceof Advanceable) {
1059 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001060 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001061 updateRunning();
1062 }
1063 }
1064
1065 void removeWidgetToAutoAdvance(View hostView) {
1066 if (mWidgetsToAdvance.containsKey(hostView)) {
1067 mWidgetsToAdvance.remove(hostView);
1068 updateRunning();
1069 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 }
1071
Joe Onorato9c1289c2009-08-17 11:03:03 -04001072 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001073 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001074 launcherInfo.hostView = null;
1075 }
1076
Adam Cohended9f8d2010-11-03 13:25:16 -07001077 void showOutOfSpaceMessage() {
1078 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1079 }
1080
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 public LauncherAppWidgetHost getAppWidgetHost() {
1082 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 }
Romain Guycbb89e42009-06-08 15:52:54 -07001084
Winson Chunga9abd0e2010-10-27 17:18:37 -07001085 public LauncherModel getModel() {
1086 return mModel;
1087 }
1088
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001089 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001090 getWindow().closeAllPanels();
1091
Winson Chung87acb482011-08-23 17:28:05 -07001092 /**
1093 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001094 try {
1095 dismissDialog(DIALOG_CREATE_SHORTCUT);
1096 // Unlock the workspace if the dialog was showing
1097 } catch (Exception e) {
1098 // An exception is thrown if the dialog is not visible, which is fine
1099 }
1100
1101 try {
1102 dismissDialog(DIALOG_RENAME_FOLDER);
1103 // Unlock the workspace if the dialog was showing
1104 } catch (Exception e) {
1105 // An exception is thrown if the dialog is not visible, which is fine
1106 }
Winson Chung87acb482011-08-23 17:28:05 -07001107 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001108
1109 // Whatever we were doing is hereby canceled.
1110 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001111 }
1112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 @Override
1114 protected void onNewIntent(Intent intent) {
1115 super.onNewIntent(intent);
1116
1117 // Close the menu
1118 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001119 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001120 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001121
Adam Cohen95bb8002011-07-03 23:40:28 -07001122 closeFolder();
1123
Joe Onorato14f122b2009-11-19 14:06:36 -08001124 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1125 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001126
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001127 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001128 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001129 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001130 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001131 }
Winson Chungde1af762011-07-21 16:44:07 -07001132 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001133 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001134
Joe Onorato3a8820b2009-11-10 15:06:42 -08001135 final View v = getWindow().peekDecorView();
1136 if (v != null && v.getWindowToken() != null) {
1137 InputMethodManager imm = (InputMethodManager)getSystemService(
1138 INPUT_METHOD_SERVICE);
1139 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001141
Michael Jurka35aa14d2011-07-07 17:01:08 -07001142 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001143 if (mAppsCustomizeTabHost != null) {
1144 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 }
1147 }
1148
1149 @Override
1150 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1151 // Do not call super here
1152 mSavedInstanceState = savedInstanceState;
1153 }
1154
1155 @Override
1156 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001157 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001158 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159
Michael Jurka883f55b2010-10-21 15:47:14 -07001160 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001161 // We close any open folder since it will not be re-opened, and we need to make sure
1162 // this state is reflected.
1163 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164
Winson Chung3d503fb2011-07-13 17:25:49 -07001165 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1166 mWaitingForResult) {
1167 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1168 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1169 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1170 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
1172
1173 if (mFolderInfo != null && mWaitingForResult) {
1174 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1175 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1176 }
Michael Jurka946ad472010-07-09 18:05:18 -07001177
Winson Chung785d2eb2011-04-14 16:08:02 -07001178 // Save the current AppsCustomize tab
1179 if (mAppsCustomizeTabHost != null) {
1180 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1181 if (currentTabTag != null) {
1182 outState.putString("apps_customize_currentTab", currentTabTag);
1183 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001184 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1185 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188
1189 @Override
1190 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001192
Winson Chunge7a03942011-08-05 15:05:12 -07001193 // Remove all pending runnables
1194 mHandler.removeMessages(ADVANCE_MSG);
1195 mHandler.removeMessages(0);
1196
Winson Chungcd2b0142011-06-08 16:02:26 -07001197 // Stop callbacks from LauncherModel
1198 LauncherApplication app = ((LauncherApplication) getApplication());
1199 mModel.stopLoader();
1200 app.setLauncher(null);
1201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001203 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001205 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001207 mAppWidgetHost = null;
1208
1209 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210
1211 TextKeyListener.getInstance().release();
1212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213
Winson Chung3d503fb2011-07-13 17:25:49 -07001214 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001215
1216 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001217 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001218
1219 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1220 mWorkspace.removeAllViews();
1221 mWorkspace = null;
1222 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001223
1224 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
1226
Adam Cohena9cf38f2011-05-02 15:36:58 -07001227 public DragController getDragController() {
1228 return mDragController;
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 @Override
1232 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001233 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 super.startActivityForResult(intent, requestCode);
1235 }
1236
Winson Chung70d72102011-08-12 11:24:35 -07001237 /**
1238 * Indicates that we want global search for this activity by setting the globalSearch
1239 * argument for {@link #startSearch} to true.
1240 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001242 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001244
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001245 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Karl Rosaen138a0412009-04-23 19:00:21 -07001247 if (initialQuery == null) {
1248 // Use any text typed in the launcher as the initial query
1249 initialQuery = getTypedText();
1250 clearTypedText();
1251 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 if (appSearchData == null) {
1253 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001254 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 final SearchManager searchManager =
1258 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001259 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001260 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262
Winson Chung70d72102011-08-12 11:24:35 -07001263 @Override
1264 public boolean onCreateOptionsMenu(Menu menu) {
1265 if (isWorkspaceLocked()) {
1266 return false;
1267 }
1268
1269 super.onCreateOptionsMenu(menu);
Winson Chung236d4312011-08-22 15:12:27 -07001270 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1271 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1272 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chung70d72102011-08-12 11:24:35 -07001273 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1274 .setIcon(android.R.drawable.ic_menu_gallery)
1275 .setAlphabeticShortcut('W');
1276 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1277 .setIcon(android.R.drawable.ic_menu_manage)
1278 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001279 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1280 .setIcon(android.R.drawable.ic_menu_preferences)
1281 .setIntent(settings)
1282 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001283 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1284 .setIcon(android.R.drawable.ic_menu_help)
1285 .setAlphabeticShortcut('H');
1286 return true;
1287 }
1288
1289 @Override
1290 public boolean onPrepareOptionsMenu(Menu menu) {
1291 super.onPrepareOptionsMenu(menu);
1292
1293 if (mAppsCustomizeTabHost.isTransitioning()) {
1294 return false;
1295 }
1296 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1297 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1298
1299 return true;
1300 }
1301
1302 @Override
1303 public boolean onOptionsItemSelected(MenuItem item) {
1304 switch (item.getItemId()) {
1305 case MENU_WALLPAPER_SETTINGS:
1306 startWallpaper();
1307 return true;
1308 case MENU_MANAGE_APPS:
1309 manageApps();
1310 return true;
1311 case MENU_HELP:
1312 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url))));
1313 return true;
1314 }
1315
1316 return super.onOptionsItemSelected(item);
1317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001319 @Override
1320 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001321 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001322 // Use a custom animation for launching search
1323 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001324 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001325 }
1326
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 public boolean isWorkspaceLocked() {
1328 return mWorkspaceLoading || mWaitingForResult;
1329 }
1330
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001332 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001333 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335
Michael Jurka0280c3b2010-09-17 15:00:07 -07001336 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001337 mPendingAddInfo.container = ItemInfo.NO_ID;
1338 mPendingAddInfo.screen = -1;
1339 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1340 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1341 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001342 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001343
Winson Chung7ad01412010-09-27 11:33:03 -07001344 private void manageApps() {
1345 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1346 }
1347
Michael Jurkaaf442092010-06-10 17:01:57 -07001348 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001349 // TODO: catch bad widget exception when sent
1350 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001351 // TODO: Is this log message meaningful?
1352 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001353 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001354 }
1355
Winson Chung55cef262010-10-28 14:14:18 -07001356 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001357 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Bjorn Bringert7984c942009-12-09 15:38:25 +00001359 if (appWidget.configure != null) {
1360 // Launch over to configure widget, if needed
1361 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1362 intent.setComponent(appWidget.configure);
1363 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001364 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001365 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1366 intent.putExtra(
1367 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1368 info.mimeType);
1369
1370 final String mimeType = info.mimeType;
1371 final ClipData clipData = (ClipData) info.configurationData;
1372 final ClipDescription clipDesc = clipData.getDescription();
1373 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1374 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001375 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001376 final CharSequence stringData = item.getText();
1377 final Uri uriData = item.getUri();
1378 final Intent intentData = item.getIntent();
1379 final String key =
1380 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1381 if (uriData != null) {
1382 intent.putExtra(key, uriData);
1383 } else if (intentData != null) {
1384 intent.putExtra(key, intentData);
1385 } else if (stringData != null) {
1386 intent.putExtra(key, stringData);
1387 }
1388 break;
1389 }
1390 }
1391 }
Winson Chung55cef262010-10-28 14:14:18 -07001392 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001393
Romain Guy8e633c52010-03-04 12:51:36 -08001394 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001396 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001397 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001398
1399 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001400 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 }
1402 }
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Adam Cohenfbba09b2011-07-18 21:43:05 -07001404 /**
1405 * Process a shortcut drop.
1406 *
1407 * @param componentName The name of the component
1408 * @param screen The screen where it should be added
1409 * @param cell The cell it should be added to, optional
1410 * @param position The location on the screen where it was dropped, optional
1411 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001412 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1413 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001414 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001415 mPendingAddInfo.container = container;
1416 mPendingAddInfo.screen = screen;
1417 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001418
1419 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 mPendingAddInfo.cellX = cell[0];
1421 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001422 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423
1424 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1425 createShortcutIntent.setComponent(componentName);
1426 processShortcut(createShortcutIntent);
1427 }
1428
Adam Cohenfbba09b2011-07-18 21:43:05 -07001429 /**
1430 * Process a widget drop.
1431 *
1432 * @param info The PendingAppWidgetInfo of the widget being added.
1433 * @param screen The screen where it should be added
1434 * @param cell The cell it should be added to, optional
1435 * @param position The location on the screen where it was dropped, optional
1436 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001437 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1438 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001439 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 mPendingAddInfo.container = info.container = container;
1441 mPendingAddInfo.screen = info.screen = screen;
1442 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001443 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001444 mPendingAddInfo.cellX = cell[0];
1445 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 }
1447
1448 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1449 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1450 addAppWidgetImpl(appWidgetId, info);
1451 }
1452
Joe Onoratodeb98af2010-02-19 14:59:39 -08001453 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001454 // Handle case where user selected "Applications"
1455 String applicationName = getResources().getString(R.string.group_applications);
1456 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001457
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001458 if (applicationName != null && applicationName.equals(shortcutName)) {
1459 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1460 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001461
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001462 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1463 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001464 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001465 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001466 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001467 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001468 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470
Winson Chung24ab2f12010-09-16 14:10:47 -07001471 void processWallpaper(Intent intent) {
1472 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1473 }
1474
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1476 int cellY) {
Michael Jurkab60fd0e2011-08-29 14:02:47 -07001477 final FolderInfo folderInfo = new FolderInfo("3");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 folderInfo.title = getText(R.string.folder_name);
1479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001481 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1482 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001483 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001486 FolderIcon newFolder =
1487 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1488 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1489 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001490 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
Romain Guycbb89e42009-06-08 15:52:54 -07001492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001494 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 }
1496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 private void showNotifications() {
1498 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1499 if (statusBar != null) {
1500 statusBar.expand();
1501 }
1502 }
1503
1504 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001505 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001507 Intent chooser = Intent.createChooser(pickWallpaper,
1508 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001509 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1510 // Removed in Eclair MR1
1511// WallpaperManager wm = (WallpaperManager)
1512// getSystemService(Context.WALLPAPER_SERVICE);
1513// WallpaperInfo wi = wm.getWallpaperInfo();
1514// if (wi != null && wi.getSettingsActivity() != null) {
1515// LabeledIntent li = new LabeledIntent(getPackageName(),
1516// R.string.configure_wallpaper, 0);
1517// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1518// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1519// }
Mike Clerona0618e42009-10-22 13:55:21 -07001520 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001523 /**
1524 * Registers various content observers. The current implementation registers
1525 * only a favorites observer to keep track of the favorites applications.
1526 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001527 private void registerContentObservers() {
1528 ContentResolver resolver = getContentResolver();
1529 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1530 true, mWidgetObserver);
1531 }
1532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 @Override
1534 public boolean dispatchKeyEvent(KeyEvent event) {
1535 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1536 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001538 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001539 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001540 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001541 dumpState();
1542 return true;
1543 }
1544 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001545 }
1546 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1547 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001548 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 return true;
1550 }
1551 }
1552
1553 return super.dispatchKeyEvent(event);
1554 }
1555
Joe Onorato88ec0992009-11-19 13:16:06 -08001556 @Override
1557 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001558 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001559 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001560 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001561 Folder openFolder = mWorkspace.getOpenFolder();
1562 if (openFolder.isEditingName()) {
1563 openFolder.dismissEditingName();
1564 } else {
1565 closeFolder();
1566 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001567 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001568 mWorkspace.exitWidgetResizeMode();
1569
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001570 // Back button is a no-op here, but give at least some feedback for the button press
1571 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001572 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001573 }
1574
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001576 * Re-listen when widgets are reset.
1577 */
1578 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001579 if (mAppWidgetHost != null) {
1580 mAppWidgetHost.startListening();
1581 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001582 }
1583
1584 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001585 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1586 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001589 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 /**
1593 * Launches the intent referred by the clicked shortcut.
1594 *
1595 * @param v The view representing the clicked shortcut.
1596 */
1597 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001598 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1599 // view has detached (it's possible for this to happen if the view is removed mid touch).
1600 if (v.getWindowToken() == null) {
1601 return;
1602 }
1603
1604 if (mWorkspace.isSwitchingState()) {
1605 return;
1606 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001609 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001611 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001612 int[] pos = new int[2];
1613 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001614 intent.setSourceBounds(new Rect(pos[0], pos[1],
1615 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001616 boolean success = startActivitySafely(intent, tag);
1617
1618 if (success && v instanceof BubbleTextView) {
1619 mWaitingForResume = (BubbleTextView) v;
1620 mWaitingForResume.setStayPressed(true);
1621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001623 if (v instanceof FolderIcon) {
1624 FolderIcon fi = (FolderIcon) v;
1625 handleFolderClick(fi);
1626 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001627 } else if (v == mAllAppsButton) {
1628 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001629 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001630 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001631 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001632 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
1634 }
1635
Michael Jurka0e260592010-06-30 17:07:39 -07001636 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001637 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001638 // clicking anywhere on the workspace causes the customization drawer to slide down
1639 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001640 return false;
1641 }
1642
Michael Jurkaaf442092010-06-10 17:01:57 -07001643 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001644 * Event handler for the search button
1645 *
1646 * @param v The view that was clicked.
1647 */
1648 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001649 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001650 }
1651
1652 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001653 * Event handler for the voice button
1654 *
1655 * @param v The view that was clicked.
1656 */
1657 public void onClickVoiceButton(View v) {
1658 startVoiceSearch();
1659 }
1660
1661 private void startVoiceSearch() {
1662 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1663 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1664 startActivity(intent);
1665 }
1666
1667 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001668 * Event handler for the "grid" button that appears on the home screen, which
1669 * enters all apps mode.
1670 *
1671 * @param v The view that was clicked.
1672 */
1673 public void onClickAllAppsButton(View v) {
1674 showAllApps(true);
1675 }
1676
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001677 public void onClickAppMarketButton(View v) {
1678 if (mAppMarketIntent != null) {
1679 startActivitySafely(mAppMarketIntent, "app market");
1680 }
1681 }
1682
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001683 void startApplicationDetailsActivity(ComponentName componentName) {
1684 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001685 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1686 Uri.fromParts("package", packageName, null));
1687 startActivity(intent);
1688 }
1689
Patrick Dubroy5539af72010-09-07 15:22:01 -07001690 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1691 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1692 // System applications cannot be installed. For now, show a toast explaining that.
1693 // We may give them the option of disabling apps this way.
1694 int messageId = R.string.uninstall_system_app_text;
1695 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1696 } else {
1697 String packageName = appInfo.componentName.getPackageName();
1698 String className = appInfo.componentName.getClassName();
1699 Intent intent = new Intent(
1700 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1701 startActivity(intent);
1702 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001703 }
1704
Michael Jurkaddd62e92011-02-16 17:49:14 -08001705 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1707 try {
1708 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001709 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001710 } catch (ActivityNotFoundException e) {
1711 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001712 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 } catch (SecurityException e) {
1714 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001715 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001717 "or use the exported attribute for this activity. "
1718 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001720 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001722
Romain Guy8e633c52010-03-04 12:51:36 -08001723 void startActivityForResultSafely(Intent intent, int requestCode) {
1724 try {
1725 startActivityForResult(intent, requestCode);
1726 } catch (ActivityNotFoundException e) {
1727 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1728 } catch (SecurityException e) {
1729 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1730 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1731 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1732 "or use the exported attribute for this activity.", e);
1733 }
1734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735
Adam Cohena9cf38f2011-05-02 15:36:58 -07001736 private void handleFolderClick(FolderIcon folderIcon) {
1737 final FolderInfo info = folderIcon.mInfo;
1738 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 // Close any open folder
1740 closeFolder();
1741 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001742 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 } else {
1744 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001745 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 int folderScreen;
1747 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001748 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 // .. and close it
1750 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001751 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 // Close any folder open on the current screen
1753 closeFolder();
1754 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001755 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757 }
1758 }
1759 }
1760
Adam Cohen2801caf2011-05-13 20:57:39 -07001761 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001762 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001763 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1764 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1765 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1766
Adam Cohenc51934b2011-07-26 21:07:43 -07001767 FolderInfo info = (FolderInfo) fi.getTag();
1768 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1769 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001770 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1771 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001772 }
1773
Adam Cohen2801caf2011-05-13 20:57:39 -07001774 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1775 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1776 oa.start();
1777 }
1778
1779 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001780 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001781 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1782 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1783 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1784
Adam Cohenc51934b2011-07-26 21:07:43 -07001785 FolderInfo info = (FolderInfo) fi.getTag();
1786 CellLayout cl = null;
1787 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1788 cl = (CellLayout) fi.getParent().getParent();
1789 }
1790
1791 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001792 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1793 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001794 oa.addListener(new AnimatorListenerAdapter() {
1795 @Override
1796 public void onAnimationEnd(Animator animation) {
1797 if (layout != null) {
1798 layout.clearFolderLeaveBehind();
1799 }
1800 }
1801 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001802 oa.start();
1803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001806 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 * is animated relative to the specified View. If the View is null, no animation
1808 * is played.
1809 *
1810 * @param folderInfo The FolderInfo describing the folder to open.
1811 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001812 public void openFolder(FolderIcon folderIcon) {
1813 Folder folder = folderIcon.mFolder;
1814 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815
Adam Cohen2801caf2011-05-13 20:57:39 -07001816 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001817 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818
Adam Cohen4554ee12011-08-03 16:13:21 -07001819 // Just verify that the folder hasn't already been added to the DragLayer.
1820 // There was a one-off crash where the folder had a parent already.
1821 if (folder.getParent() == null) {
1822 mDragLayer.addView(folder);
1823 mDragController.addDropTarget((DropTarget) folder);
1824 } else {
1825 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1826 folder.getParent() + ").");
1827 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001828 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001829 }
1830
1831 public void closeFolder() {
1832 Folder folder = mWorkspace.getOpenFolder();
1833 if (folder != null) {
1834 closeFolder(folder);
1835 }
1836 }
1837
1838 void closeFolder(Folder folder) {
1839 folder.getInfo().opened = false;
1840
1841 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1842 if (parent != null) {
1843 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1844 shrinkAndFadeInFolderIcon(fi);
1845 }
1846 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001850 if (mState != State.WORKSPACE) {
1851 return false;
1852 }
1853
Joe Onorato9c1289c2009-08-17 11:03:03 -04001854 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 return false;
1856 }
1857
1858 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001859 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 }
1861
Michael Jurka0280c3b2010-09-17 15:00:07 -07001862 resetAddInfo();
1863 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001865 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 return true;
1867 }
1868
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 // The hotseat touch handling does not go through Workspace, and we always allow long press
1870 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001871 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001872 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1873 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001874 if (itemUnderLongClick == null) {
1875 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001876 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1877 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001878 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001880 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001882 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
1884 }
1885 }
1886 return true;
1887 }
1888
Winson Chung3d503fb2011-07-13 17:25:49 -07001889 boolean isHotseatLayout(View layout) {
1890 return mHotseat != null && layout != null &&
1891 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1892 }
1893 Hotseat getHotseat() {
1894 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001895 }
1896
Winson Chung3d503fb2011-07-13 17:25:49 -07001897 /**
1898 * Returns the CellLayout of the specified container at the specified screen.
1899 */
1900 CellLayout getCellLayout(long container, int screen) {
1901 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1902 if (mHotseat != null) {
1903 return mHotseat.getLayout();
1904 } else {
1905 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001906 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 } else {
1908 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001909 }
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 Workspace getWorkspace() {
1913 return mWorkspace;
1914 }
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 @Override
1917 protected Dialog onCreateDialog(int id) {
1918 switch (id) {
1919 case DIALOG_CREATE_SHORTCUT:
1920 return new CreateShortcut().createDialog();
1921 case DIALOG_RENAME_FOLDER:
1922 return new RenameFolder().createDialog();
1923 }
1924
1925 return super.onCreateDialog(id);
1926 }
1927
1928 @Override
1929 protected void onPrepareDialog(int id, Dialog dialog) {
1930 switch (id) {
1931 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 break;
1933 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001934 if (mFolderInfo != null) {
1935 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1936 final CharSequence text = mFolderInfo.title;
1937 input.setText(text);
1938 input.setSelection(0, text.length());
1939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 break;
1941 }
1942 }
1943
1944 void showRenameDialog(FolderInfo info) {
1945 mFolderInfo = info;
1946 mWaitingForResult = true;
1947 showDialog(DIALOG_RENAME_FOLDER);
1948 }
1949
Adam Cohenfbba09b2011-07-18 21:43:05 -07001950 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1953 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 mWaitingForResult = true;
1955 showDialog(DIALOG_CREATE_SHORTCUT);
1956 }
1957
1958 private class RenameFolder {
1959 private EditText mInput;
1960
1961 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1963 mInput = (EditText) layout.findViewById(R.id.folder_name);
1964
1965 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1966 builder.setIcon(0);
1967 builder.setTitle(getString(R.string.rename_folder_title));
1968 builder.setCancelable(true);
1969 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1970 public void onCancel(DialogInterface dialog) {
1971 cleanup();
1972 }
1973 });
1974 builder.setNegativeButton(getString(R.string.cancel_action),
1975 new Dialog.OnClickListener() {
1976 public void onClick(DialogInterface dialog, int which) {
1977 cleanup();
1978 }
1979 }
1980 );
1981 builder.setPositiveButton(getString(R.string.rename_action),
1982 new Dialog.OnClickListener() {
1983 public void onClick(DialogInterface dialog, int which) {
1984 changeFolderName();
1985 }
1986 }
1987 );
1988 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001989
1990 final AlertDialog dialog = builder.create();
1991 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1992 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001993 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001994 mInput.requestFocus();
1995 InputMethodManager inputManager = (InputMethodManager)
1996 getSystemService(Context.INPUT_METHOD_SERVICE);
1997 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001998 }
1999 });
2000
2001 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003
2004 private void changeFolderName() {
2005 final String name = mInput.getText().toString();
2006 if (!TextUtils.isEmpty(name)) {
2007 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002008 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 mFolderInfo.title = name;
2010 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2011
Joe Onorato9c1289c2009-08-17 11:03:03 -04002012 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002013 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002014 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 } else {
2016 final FolderIcon folderIcon = (FolderIcon)
2017 mWorkspace.getViewForTag(mFolderInfo);
2018 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002019 // TODO: At some point we'll probably want some version of setting
2020 // the text for a folder icon.
2021 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 getWorkspace().requestLayout();
2023 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002024 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025 mWorkspaceLoading = true;
2026 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028 }
2029 }
2030 cleanup();
2031 }
2032
2033 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 dismissDialog(DIALOG_RENAME_FOLDER);
2035 mWaitingForResult = false;
2036 mFolderInfo = null;
2037 }
2038 }
2039
Daniel Sandler843e8602010-06-07 14:59:01 -04002040 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2041 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002042 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002043 }
2044
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002045 // AllAppsView.Watcher
2046 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002047 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002048 mWorkspace.setVisibility(View.GONE);
2049 }
2050 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002051
2052 /**
2053 * Helper method for the cameraZoomIn/cameraZoomOut animations
2054 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002055 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002056 * @param scaleFactor The scale factor used for the zoom
2057 */
2058 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2059 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002060
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002061 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002062 // Set pivotY so that at the starting zoom factor, the view is partially
2063 // visible. Modifying initialHeightFactor changes how much of the view is
2064 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002065 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002066 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002067 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002068 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002069 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002070 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002071 }
2072 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002073
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002074 /**
2075 * Zoom the camera out from the workspace to reveal 'toView'.
2076 * Assumes that the view to show is anchored at either the very top or very bottom
2077 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002078 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002079 */
Winson Chungc07918d2011-07-01 15:35:26 -07002080 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002081 final 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_appsCustomizeZoomInTime);
2084 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2085 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2086 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002087
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002088 setPivotsForZoom(toView, toState, scale);
2089
Michael Jurkad74c9842011-07-10 12:44:21 -07002090 // Shrink workspaces away if going to AppsCustomize from workspace
2091 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002092 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002093
2094 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002095 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002096 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002097 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2098 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002099 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002100 toView.setFastScaleX(a * scale + b * 1f);
2101 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002102 }
2103 });
Adam Cohen61033d32010-11-15 18:29:44 -08002104
Winson Chungf0ea4d32011-06-06 14:27:16 -07002105 toView.setVisibility(View.VISIBLE);
2106 toView.setFastAlpha(0f);
2107 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2108 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2109 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2110 public void onAnimationUpdate(float a, float b) {
2111 // don't need to invalidate because we do so above
2112 toView.setFastAlpha(a * 0f + b * 1f);
2113 }
2114 });
2115 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002116
Michael Jurkaabded662011-03-04 12:06:57 -08002117 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002118 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002119 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002120 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002121 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002122 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002123 // Prepare the position
2124 toView.setTranslationX(0.0f);
2125 toView.setTranslationY(0.0f);
2126 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002127 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002128 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002129 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002130 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002131 // If we don't set the final scale values here, if this animation is cancelled
2132 // it will have the wrong scale value and subsequent cameraPan animations will
2133 // not fix that
2134 toView.setScaleX(1.0f);
2135 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002136 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002137 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002138 }
Winson Chung32174c82011-07-19 15:47:55 -07002139
2140 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2141 // Hide the workspace scrollbar
2142 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002143 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002144 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002145 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002146 });
2147
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002148 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002149 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002150
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002151 if (mStateAnimation != null) mStateAnimation.cancel();
2152 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002153 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002154 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002155 } else {
2156 toView.setTranslationX(0.0f);
2157 toView.setTranslationY(0.0f);
2158 toView.setScaleX(1.0f);
2159 toView.setScaleY(1.0f);
2160 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002161 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002162 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002163 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2164 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002165
Winson Chungc07918d2011-07-01 15:35:26 -07002166 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2167 // Hide the workspace scrollbar
2168 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002169 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002170 }
Michael Jurkaabded662011-03-04 12:06:57 -08002171 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002172 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002173 }
2174
2175 /**
2176 * Zoom the camera back into the workspace, hiding 'fromView'.
2177 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002178 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002179 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002180 */
Winson Chungc07918d2011-07-01 15:35:26 -07002181 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002182 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002183
Winson Chungf0ea4d32011-06-06 14:27:16 -07002184 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2185 final float scaleFactor = (float)
2186 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2187 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002188
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002189 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002190
Michael Jurkad3ef3062010-11-23 16:23:58 -08002191 if (!springLoaded) {
2192 mWorkspace.unshrink(animated);
2193 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002194 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002195 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002196 if (mStateAnimation != null) mStateAnimation.cancel();
2197 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002198
Michael Jurka742574b2011-02-02 23:51:01 -08002199 final float oldScaleX = fromView.getScaleX();
2200 final float oldScaleY = fromView.getScaleY();
2201
2202 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2203 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002204 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2205 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002206 ((View)fromView.getParent()).fastInvalidate();
2207 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2208 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2209 }
2210 });
Michael Jurkaabded662011-03-04 12:06:57 -08002211 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002212 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002213 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002214 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2215 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002216 // don't need to invalidate because we do so above
2217 fromView.setFastAlpha(a * 1f + b * 0f);
2218 }
2219 });
Michael Jurkaabded662011-03-04 12:06:57 -08002220 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002221 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002222 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002223 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002224 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002225 public void onAnimationStart(android.animation.Animator animation) {
2226 if (!springLoaded) {
2227 mWorkspace.showDockDivider(false);
2228 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002229 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002230 }
2231 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002232 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002233 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002234 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002235 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002236 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002237 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002238 }
2239 });
2240
Winson Chungf0ea4d32011-06-06 14:27:16 -07002241 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002242 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002243 } else {
2244 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002245 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002246 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2247 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002248
Winson Chungc07918d2011-07-01 15:35:26 -07002249 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002250 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002251 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002252 mWorkspace.flashScrollingIndicator();
2253 }
Michael Jurkaabded662011-03-04 12:06:57 -08002254 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002255 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002256 }
2257
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002258 void showWorkspace(boolean animated) {
2259 showWorkspace(animated, null);
2260 }
2261
2262 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002263 if (layout != null) {
2264 // always animated, but that's ok since we never specify a layout and
2265 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002266 mWorkspace.unshrink(layout);
2267 } else {
2268 mWorkspace.unshrink(animated);
2269 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002270 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002271 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002272 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002273
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002274 // Change the state *after* we've called all the transition code
2275 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002276
Winson Chung5fb63472011-02-02 17:03:37 -08002277 // Resume the auto-advance of widgets
2278 mUserPresent = true;
2279 updateRunning();
2280
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002281 // send an accessibility event to announce the context change
2282 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002283 }
2284
Michael Jurkad3ef3062010-11-23 16:23:58 -08002285 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002286 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002287 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002288 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002289 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002290 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002291 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002292 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002293 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002294 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002295 @Override
2296 public void run() {
2297 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002298
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002299 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002300 // Before we show workspace, hide all apps again because
2301 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2302 // clean up our state transition functions
2303 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002304 showWorkspace(true);
2305 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002306 }
2307 }, (extendedDelay ?
2308 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2309 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2310 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002311 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002312 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002313 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002314 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2315 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 }
2317 // Otherwise, we are not in spring loaded mode, so don't do anything.
2318 }
2319
Adam Cohenfc53cd22011-07-20 15:45:11 -07002320 public boolean isAllAppsCustomizeOpen() {
2321 return mState == State.APPS_CUSTOMIZE;
2322 }
2323
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002326 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002328 if (!LauncherApplication.isScreenLarge()) {
2329 if (animated) {
2330 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002334 }
2335 }
2336 }
2337
2338 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 if (!LauncherApplication.isScreenLarge()) {
2343 if (animated) {
2344 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002346 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002349 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002350 }
2351
Winson Chung785d2eb2011-04-14 16:08:02 -07002352 void showAllApps(boolean animated) {
2353 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002354
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2356 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002357
Winson Chung3d503fb2011-07-13 17:25:49 -07002358 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002359 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002360
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 // Change the state *after* we've called all the transition code
2362 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002363
2364 // Pause the auto-advance of widgets until we are out of AllApps
2365 mUserPresent = false;
2366 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002367 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002368
2369 // Send an accessibility event to announce the context change
2370 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2371 }
2372
Joe Onoratob2061212009-11-24 16:13:54 -05002373 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002374 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002375 * - Home from workspace
2376 * - from center screen
2377 * - from other screens
2378 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002379 * - from center screen
2380 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002381 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002382 * - from center screen
2383 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002384 * - Launch app from workspace and quit
2385 * - with back
2386 * - with home
2387 * - Launch app from all apps and quit
2388 * - with back
2389 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002390 * - Go to a screen that's not the default, then all
2391 * apps, and launch and app, and go back
2392 * - with back
2393 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002394 * - On workspace, long press power and go back
2395 * - with back
2396 * - with home
2397 * - On all apps, long press power and go back
2398 * - with back
2399 * - with home
2400 * - On workspace, power off
2401 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002402 * - Launch an app and turn off the screen while in that app
2403 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002404 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002405 * - From all apps
2406 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002407 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2408 * - From all apps
2409 * - From the center workspace
2410 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002411 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002412 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002413 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2414 mWorkspace.setVisibility(View.VISIBLE);
2415 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002416
Winson Chung3d503fb2011-07-13 17:25:49 -07002417 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002418 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002419
Winson Chungf0ea4d32011-06-06 14:27:16 -07002420 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 if (mAllAppsButton != null) {
2422 mAllAppsButton.requestFocus();
2423 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002424 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002425 }
2426
Joe Onorato7c312c12009-08-13 21:36:53 -07002427 void lockAllApps() {
2428 // TODO
2429 }
2430
2431 void unlockAllApps() {
2432 // TODO
2433 }
2434
Patrick Dubroy5f445422011-02-18 14:35:21 -08002435 /**
2436 * Add an item from all apps or customize onto the given workspace screen.
2437 * If layout is null, add to the current screen.
2438 */
2439 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002440 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2441 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002442 } else {
2443 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002444 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002445 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002446
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002447 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002448 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002449 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002450 // Look for the toolbar icon specified in the activity meta-data
2451 Bundle metaData = packageManager.getActivityInfo(
2452 activityName, PackageManager.GET_META_DATA).metaData;
2453 if (metaData != null) {
2454 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2455 if (iconResId != 0) {
2456 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002457 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002458 }
2459 }
2460 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002461 // This can happen if the activity defines an invalid drawable
2462 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2463 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002464 } catch (Resources.NotFoundException nfe) {
2465 // This can happen if the activity defines an invalid drawable
2466 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2467 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002468 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002469 return null;
2470 }
2471
2472 // if successful in getting icon, return it; otherwise, set button to use default drawable
2473 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2474 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2475 TextView button = (TextView) findViewById(buttonId);
2476 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2477
2478 // If we were unable to find the icon via the meta-data, use a generic one
2479 if (toolbarIcon == null) {
2480 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2481 return null;
2482 } else {
2483 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2484 return toolbarIcon.getConstantState();
2485 }
2486 }
2487
2488 // if successful in getting icon, return it; otherwise, set button to use default drawable
2489 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2490 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2491 ImageView button = (ImageView) findViewById(buttonId);
2492 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2493
Michael Jurka19e0fc52011-07-22 18:00:21 -07002494 if (button != null) {
2495 // If we were unable to find the icon via the meta-data, use a
2496 // generic one
2497 if (toolbarIcon == null) {
2498 button.setImageResource(fallbackDrawableId);
2499 } else {
2500 button.setImageDrawable(toolbarIcon);
2501 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002502 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002503
2504 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2505
Michael Jurka0423dcf2010-10-05 14:56:18 -07002506 }
2507
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002508 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2509 TextView button = (TextView) findViewById(buttonId);
2510 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2511 }
2512
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002513 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002514 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002515 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002516 }
2517
Michael Jurka0423dcf2010-10-05 14:56:18 -07002518 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002519 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2520 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002521 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002522
Winson Chung1cad91e2011-05-25 17:41:01 -07002523 final SearchManager searchManager =
2524 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2525 ComponentName activityName = searchManager.getGlobalSearchActivity();
2526 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002527 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002528 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002529 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002530 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002531 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002532 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002533 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002534 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002535 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002536 }
2537 }
2538
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002539 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002540 updateButtonWithDrawable(R.id.search_button, d);
2541 }
2542
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002543 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002544 final View searchDivider = findViewById(R.id.search_divider);
2545 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002546
Winson Chung1cad91e2011-05-25 17:41:01 -07002547 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2548 ComponentName activityName = intent.resolveActivity(getPackageManager());
2549 if (activityName != null) {
2550 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002551 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2552 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002553 voiceButton.setVisibility(View.VISIBLE);
2554 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002555 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002556 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002557 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002558 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002559
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002560 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002561 updateButtonWithDrawable(R.id.voice_button, d);
2562 }
2563
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002564 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002565 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002566 */
2567 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002568 final View marketButton = findViewById(R.id.market_button);
2569 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2570 // Find the app market activity by resolving an intent.
2571 // (If multiple app markets are installed, it will return the ResolverActivity.)
2572 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002573 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002574 mAppMarketIntent = intent;
2575 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002576 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002577 marketButton.setVisibility(View.VISIBLE);
2578 } else {
2579 // We should hide and disable the view so that we don't try and restore the visibility
2580 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2581 marketButton.setVisibility(View.GONE);
2582 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002583 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002584 }
2585
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002586 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002587 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002588 }
2589
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002590 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591 * Displays the shortcut creation dialog and launches, if necessary, the
2592 * appropriate activity.
2593 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002594 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002595 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2596 DialogInterface.OnShowListener {
2597
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002598 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002600 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002601 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002602
Winson Chung55b65502011-05-26 12:03:43 -07002603 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2604 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002605 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002606
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002607 AlertDialog dialog = builder.create();
2608 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002609 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002610 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002612 return dialog;
2613 }
2614
2615 public void onCancel(DialogInterface dialog) {
2616 mWaitingForResult = false;
2617 cleanup();
2618 }
2619
Romain Guycbb89e42009-06-08 15:52:54 -07002620 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002621 mWaitingForResult = false;
2622 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002623 }
2624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002626 try {
2627 dismissDialog(DIALOG_CREATE_SHORTCUT);
2628 } catch (Exception e) {
2629 // An exception is thrown if the dialog is not visible, which is fine
2630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 }
2632
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002633 /**
2634 * Handle the action clicked in the "Add to home" dialog.
2635 */
2636 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002637 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002638
Winson Chung55b65502011-05-26 12:03:43 -07002639 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2640 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002641 case AddAdapter.ITEM_APPLICATION: {
2642 if (mAppsCustomizeTabHost != null) {
2643 mAppsCustomizeTabHost.selectAppsTab();
2644 }
2645 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002646 break;
2647 }
Winson Chung55b65502011-05-26 12:03:43 -07002648 case AddAdapter.ITEM_APPWIDGET: {
2649 if (mAppsCustomizeTabHost != null) {
2650 mAppsCustomizeTabHost.selectWidgetsTab();
2651 }
2652 showAllApps(true);
2653 break;
2654 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002655 case AddAdapter.ITEM_WALLPAPER: {
2656 startWallpaper();
2657 break;
2658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 }
2660 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002661
2662 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002663 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 }
2666
2667 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002668 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002669 */
2670 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2671 @Override
2672 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002673 closeSystemDialogs();
2674 }
2675 }
2676
2677 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002678 * Receives notifications whenever the appwidgets are reset.
2679 */
2680 private class AppWidgetResetObserver extends ContentObserver {
2681 public AppWidgetResetObserver() {
2682 super(new Handler());
2683 }
2684
2685 @Override
2686 public void onChange(boolean selfChange) {
2687 onAppWidgetReset();
2688 }
2689 }
2690
2691 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002692 * If the activity is currently paused, signal that we need to re-run the loader
2693 * in onResume.
2694 *
2695 * This needs to be called from incoming places where resources might have been loaded
2696 * while we are paused. That is becaues the Configuration might be wrong
2697 * when we're not running, and if it comes back to what it was when we
2698 * were paused, we are not restarted.
2699 *
2700 * Implementation of the method from LauncherModel.Callbacks.
2701 *
2702 * @return true if we are currently paused. The caller might be able to
2703 * skip some work in that case since we will come back again.
2704 */
2705 public boolean setLoadOnResume() {
2706 if (mPaused) {
2707 Log.i(TAG, "setLoadOnResume");
2708 mOnResumeNeedsLoad = true;
2709 return true;
2710 } else {
2711 return false;
2712 }
2713 }
2714
2715 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002716 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002718 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002719 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002720 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002721 } else {
2722 return SCREEN_COUNT / 2;
2723 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002724 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002725
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002726
Joe Onorato9c1289c2009-08-17 11:03:03 -04002727 /**
2728 * Refreshes the shortcuts shown on the workspace.
2729 *
2730 * Implementation of the method from LauncherModel.Callbacks.
2731 */
2732 public void startBinding() {
2733 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002734
2735 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002736 int count = workspace.getChildCount();
2737 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002738 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002739 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2740 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002742 if (mHotseat != null) {
2743 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002745
Adam Cohen4eac29a2011-07-11 17:53:37 -07002746 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002747 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 }
2749
2750 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002751 * Bind the items start-end from the list.
2752 *
2753 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002755 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002756 setLoadOnResume();
2757
Joe Onorato9c1289c2009-08-17 11:03:03 -04002758 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 for (int i=start; i<end; i++) {
2760 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002761
2762 // Short circuit if we are loading dock items for a configuration which has no dock
2763 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2764 mHotseat == null) {
2765 continue;
2766 }
2767
Joe Onorato9c1289c2009-08-17 11:03:03 -04002768 switch (item.itemType) {
2769 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2770 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002771 View shortcut = createShortcut((ShortcutInfo)item);
2772 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2773 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002775 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002776 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002777 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002778 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002779 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2780 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002781 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002783 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002784 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002785 }
2786
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 /**
2788 * Implementation of the method from LauncherModel.Callbacks.
2789 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002790 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002791 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002792 sFolders.clear();
2793 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795
2796 /**
2797 * Add the views for a widget to the workspace.
2798 *
2799 * Implementation of the method from LauncherModel.Callbacks.
2800 */
2801 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002802 setLoadOnResume();
2803
Daniel Sandler843e8602010-06-07 14:59:01 -04002804 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2805 if (DEBUG_WIDGETS) {
2806 Log.d(TAG, "bindAppWidget: " + item);
2807 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 final Workspace workspace = mWorkspace;
2809
2810 final int appWidgetId = item.appWidgetId;
2811 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002812 if (DEBUG_WIDGETS) {
2813 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2814 }
2815
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2817
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2819 item.hostView.setTag(item);
2820
Winson Chung3d503fb2011-07-13 17:25:49 -07002821 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 item.cellY, item.spanX, item.spanY, false);
2823
Adam Cohended9f8d2010-11-03 13:25:16 -07002824 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2825
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 workspace.requestLayout();
2827
Daniel Sandler843e8602010-06-07 14:59:01 -04002828 if (DEBUG_WIDGETS) {
2829 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2830 + (SystemClock.uptimeMillis()-start) + "ms");
2831 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 }
2833
2834 /**
2835 * Callback saying that there aren't any more items to bind.
2836 *
2837 * Implementation of the method from LauncherModel.Callbacks.
2838 */
2839 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002840 setLoadOnResume();
2841
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 if (mSavedState != null) {
2843 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002844 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002846 mSavedState = null;
2847 }
2848
2849 if (mSavedInstanceState != null) {
2850 super.onRestoreInstanceState(mSavedInstanceState);
2851 mSavedInstanceState = null;
2852 }
2853
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002855
2856 // If we received the result of any pending adds while the loader was running (e.g. the
2857 // widget configuration forced an orientation change), process them now.
2858 for (int i = 0; i < sPendingAddList.size(); i++) {
2859 completeAdd(sPendingAddList.get(i));
2860 }
2861 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 }
2863
2864 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002865 * Updates the icons on the launcher that are affected by changes to the package list
2866 * on the device.
2867 */
2868 private void updateIconsAffectedByPackageManagerChanges() {
2869 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002870 updateVoiceSearchIcon();
2871 }
2872
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002873 @Override
2874 public void bindSearchablesChanged() {
2875 updateGlobalSearchIcon();
2876 }
2877
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 * Add the icons for all apps.
2880 *
2881 * Implementation of the method from LauncherModel.Callbacks.
2882 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002883 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2884 // Remove the progress bar entirely; we could also make it GONE
2885 // but better to remove it since we know it's not going to be used
2886 View progressBar = mAppsCustomizeTabHost.
2887 findViewById(R.id.apps_customize_progress_bar);
2888 if (progressBar != null) {
2889 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002890 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002891 // We just post the call to setApps so the user sees the progress bar
2892 // disappear-- otherwise, it just looks like the progress bar froze
2893 // which doesn't look great
2894 mAppsCustomizeTabHost.post(new Runnable() {
2895 public void run() {
2896 if (mAppsCustomizeContent != null) {
2897 mAppsCustomizeContent.setApps(apps);
2898 }
2899 }
2900 });
2901
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002902 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002903 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 }
2905
2906 /**
2907 * A package was installed.
2908 *
2909 * Implementation of the method from LauncherModel.Callbacks.
2910 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002911 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002912 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002914
Winson Chung785d2eb2011-04-14 16:08:02 -07002915 if (mAppsCustomizeContent != null) {
2916 mAppsCustomizeContent.addApps(apps);
2917 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002918 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 }
2920
2921 /**
2922 * A package was updated.
2923 *
2924 * Implementation of the method from LauncherModel.Callbacks.
2925 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002926 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002927 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002929 if (mWorkspace != null) {
2930 mWorkspace.updateShortcuts(apps);
2931 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002932
Winson Chung785d2eb2011-04-14 16:08:02 -07002933 if (mAppsCustomizeContent != null) {
2934 mAppsCustomizeContent.updateApps(apps);
2935 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002936 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002937 }
2938
2939 /**
2940 * A package was uninstalled.
2941 *
2942 * Implementation of the method from LauncherModel.Callbacks.
2943 */
Joe Onorato36115782010-06-17 13:28:48 -04002944 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002946 if (permanent) {
2947 mWorkspace.removeItems(apps);
2948 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002949
Winson Chung785d2eb2011-04-14 16:08:02 -07002950 if (mAppsCustomizeContent != null) {
2951 mAppsCustomizeContent.removeApps(apps);
2952 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002953 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 }
Joe Onoratobe386092009-11-17 17:32:16 -08002955
2956 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002957 * A number of packages were updated.
2958 */
2959 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960
Winson Chung785d2eb2011-04-14 16:08:02 -07002961 if (mAppsCustomizeContent != null) {
2962 mAppsCustomizeContent.onPackagesUpdated();
2963 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002964 }
2965
Winson Chung400438b2011-01-16 17:53:48 -08002966 private int mapConfigurationOriActivityInfoOri(int configOri) {
2967 final Display d = getWindowManager().getDefaultDisplay();
2968 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2969 switch (d.getRotation()) {
2970 case Surface.ROTATION_0:
2971 case Surface.ROTATION_180:
2972 // We are currently in the same basic orientation as the natural orientation
2973 naturalOri = configOri;
2974 break;
2975 case Surface.ROTATION_90:
2976 case Surface.ROTATION_270:
2977 // We are currently in the other basic orientation to the natural orientation
2978 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2979 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2980 break;
2981 }
2982
2983 int[] oriMap = {
2984 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2985 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2986 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2987 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2988 };
2989 // Since the map starts at portrait, we need to offset if this device's natural orientation
2990 // is landscape.
2991 int indexOffset = 0;
2992 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2993 indexOffset = 1;
2994 }
2995 return oriMap[(d.getRotation() + indexOffset) % 4];
2996 }
2997 public void lockScreenOrientation() {
2998 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2999 .getConfiguration().orientation));
3000 }
3001 public void unlockScreenOrientation() {
3002 mHandler.postDelayed(new Runnable() {
3003 public void run() {
3004 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3005 }
3006 }, mRestoreScreenOrientationDelay);
3007 }
3008
Winson Chung82f55532011-08-09 14:14:23 -07003009 /* Cling related */
3010 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3011 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3012 private void enableClingsIfNecessary() {
3013 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3014 if (LauncherApplication.isScreenLarge()) return;
3015
Brett Chabot2a9e2282011-08-23 15:03:13 -07003016 // disable clings when running in a test harness
3017 if(ActivityManager.isRunningInTestHarness()) return;
3018
Winson Chung82f55532011-08-09 14:14:23 -07003019 // Enable the clings only if they have not been dismissed before
3020 SharedPreferences prefs =
3021 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3022 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3023 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3024 cling.init(this);
3025 cling.setVisibility(View.VISIBLE);
3026 }
3027 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3028 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3029 cling.init(this);
3030 cling.setVisibility(View.VISIBLE);
3031 }
3032 }
3033 private void dismissCling(final Cling cling, final String flag) {
3034 if (cling != null) {
3035 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3036 anim.setDuration(DISMISS_CLING_DURATION);
3037 anim.addListener(new AnimatorListenerAdapter() {
3038 public void onAnimationEnd(Animator animation) {
3039 cling.setVisibility(View.GONE);
3040 cling.cleanup();
3041 SharedPreferences prefs =
3042 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3043 SharedPreferences.Editor editor = prefs.edit();
3044 editor.putBoolean(flag, true);
3045 editor.commit();
3046 };
3047 });
3048 anim.start();
3049 }
3050 }
3051 public void dismissWorkspaceCling(View v) {
3052 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3053 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3054 }
3055 public void dismissAllAppsCling(View v) {
3056 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3057 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3058 }
3059
Winson Chung80baf5a2010-08-09 16:03:15 -07003060 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003061 * Prints out out state for debugging.
3062 */
3063 public void dumpState() {
3064 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003065 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003066 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3067 Log.d(TAG, "mRestoring=" + mRestoring);
3068 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3069 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003070 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003071 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003072
Winson Chung785d2eb2011-04-14 16:08:02 -07003073 if (mAppsCustomizeContent != null) {
3074 mAppsCustomizeContent.dumpState();
3075 }
Joe Onoratobe386092009-11-17 17:32:16 -08003076 Log.d(TAG, "END launcher2 dump state");
3077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003078}
Michael Jurka2763be32011-02-24 11:19:57 -08003079
Michael Jurkaabded662011-03-04 12:06:57 -08003080interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003081 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3082 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003083}