blob: 22a34e6c7a94d8ebfa289438c8b166b7341a580c [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040059import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080060import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070061import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080062import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen860f4c52011-01-27 20:16:13 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080079import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080080import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070082import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070084import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070085import android.widget.TextView;
86import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070087
Adam Cohendf2cc412011-04-27 16:56:57 -070088import com.android.common.Search;
89import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070090import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080091
Adam Cohenc0dcf592011-06-01 15:30:43 -070092import java.io.DataInputStream;
93import java.io.DataOutputStream;
94import java.io.FileNotFoundException;
95import java.io.IOException;
96import java.util.ArrayList;
97import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -070098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Default launcher application.
101 */
Michael Jurka946ad472010-07-09 18:05:18 -0700102public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700103 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
104 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800105 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700106 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400109 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700110
Winson Chung70d72102011-08-12 11:24:35 -0700111 private static final int MENU_GROUP_WALLPAPER = 1;
112 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
113 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700114 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
115 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
117 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700118 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 private static final int REQUEST_PICK_APPLICATION = 6;
120 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700122 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
125
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800126 static final int SCREEN_COUNT = 5;
127 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
Joe Onorato7c312c12009-08-13 21:36:53 -0700129 static final int DIALOG_CREATE_SHORTCUT = 1;
130 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Romain Guy98d01652009-06-30 16:21:04 -0700132 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 // Type: int
135 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700136 // Type: int
137 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700139 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
140 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
142 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700143 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: boolean
147 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
148 // Type: long
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
150
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700151 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
152
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700153 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700154 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700155 private State mState = State.WORKSPACE;
156 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700157
Joe Onorato9c1289c2009-08-17 11:03:03 -0400158 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700159 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
160 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700161 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800166 private final BroadcastReceiver mCloseSystemDialogsReceiver
167 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800168 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private LayoutInflater mInflater;
171
Joe Onorato00acb122009-08-04 16:04:30 -0400172 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700174
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700175 private AppWidgetManager mAppWidgetManager;
176 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
Winson Chung3d503fb2011-07-13 17:25:49 -0700178 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700179 private int[] mTmpAddItemCellCoordinates = new int[2];
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private FolderInfo mFolderInfo;
182
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800184 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700185
Winson Chungf0ea4d32011-06-06 14:27:16 -0700186 private SearchDropTargetBar mSearchDeleteBar;
187 private AppsCustomizeTabHost mAppsCustomizeTabHost;
188 private AppsCustomizePagedView mAppsCustomizeContent;
189 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private Bundle mSavedState;
192
193 private SpannableStringBuilder mDefaultKeySsb = null;
194
Joe Onorato9c1289c2009-08-17 11:03:03 -0400195 private boolean mWorkspaceLoading = true;
196
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800197 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private boolean mRestoring;
199 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700200 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
202 private Bundle mSavedInstanceState;
203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800205 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800206 private boolean mUserPresent = true;
207 private boolean mVisible = false;
208 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700210 private static LocaleConfiguration sLocaleConfiguration = null;
211
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700212 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700214 private Intent mAppMarketIntent = null;
215
Adam Cohended9f8d2010-11-03 13:25:16 -0700216 // Related to the auto-advancing of widgets
217 private final int ADVANCE_MSG = 1;
218 private final int mAdvanceInterval = 20000;
219 private final int mAdvanceStagger = 250;
220 private long mAutoAdvanceSentTime;
221 private long mAutoAdvanceTimeLeft = -1;
222 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
223 new HashMap<View, AppWidgetProviderInfo>();
224
Winson Chung400438b2011-01-16 17:53:48 -0800225 // Determines how long to wait after a rotation before restoring the screen orientation to
226 // match the sensor state.
227 private final int mRestoreScreenOrientationDelay = 500;
228
Michael Jurka4ef207b2010-11-29 17:05:45 -0800229 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800230 private static Drawable.ConstantState sGlobalSearchIcon;
231 private static Drawable.ConstantState sVoiceSearchIcon;
232 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800233
Adam Cohen2801caf2011-05-13 20:57:39 -0700234 private DragLayer mDragLayer;
235
Michael Jurkaddd62e92011-02-16 17:49:14 -0800236 private BubbleTextView mWaitingForResume;
237
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800238 private static ArrayList<PendingAddArguments> sPendingAddList
239 = new ArrayList<PendingAddArguments>();
240
241 private static class PendingAddArguments {
242 int requestCode;
243 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700244 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800245 int screen;
246 int cellX;
247 int cellY;
248 }
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 @Override
251 protected void onCreate(Bundle savedInstanceState) {
252 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800253 LauncherApplication app = ((LauncherApplication)getApplication());
254 mModel = app.setLauncher(this);
255 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400256 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700258
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700259 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
261 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700262
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200264 android.os.Debug.startMethodTracing(
265 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 }
267
268 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 setContentView(R.layout.launcher);
270 setupViews();
Winson Chung82f55532011-08-09 14:14:23 -0700271 enableClingsIfNecessary();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800273 registerContentObservers();
274
Joe Onorato7c312c12009-08-13 21:36:53 -0700275 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 mSavedState = savedInstanceState;
278 restoreState(mSavedState);
279
Winson Chunga12a2502010-12-20 14:41:35 -0800280 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700281 if (mAppsCustomizeContent != null) {
282 mAppsCustomizeContent.onPackagesUpdated();
283 }
Winson Chunga12a2502010-12-20 14:41:35 -0800284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 if (PROFILE_STARTUP) {
286 android.os.Debug.stopMethodTracing();
287 }
288
289 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
292
Michael Jurkac57b7a82011-08-09 22:02:20 -0700293 if (!mModel.isAllAppsLoaded()) {
294 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
295 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
296 }
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 // For handling default keys
299 mDefaultKeySsb = new SpannableStringBuilder();
300 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800301
302 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
303 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800304
305 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700306 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
307 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100308 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700309 }
310 if (sGlobalSearchIcon != null) {
311 updateGlobalSearchIcon(sGlobalSearchIcon);
312 }
313 if (sVoiceSearchIcon != null) {
314 updateVoiceSearchIcon(sVoiceSearchIcon);
315 }
316 if (sAppMarketIcon != null) {
317 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
Romain Guycbb89e42009-06-08 15:52:54 -0700320
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700322 if (sLocaleConfiguration == null) {
323 new AsyncTask<Void, Void, LocaleConfiguration>() {
324 @Override
325 protected LocaleConfiguration doInBackground(Void... unused) {
326 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
327 readConfiguration(Launcher.this, localeConfiguration);
328 return localeConfiguration;
329 }
330
331 @Override
332 protected void onPostExecute(LocaleConfiguration result) {
333 sLocaleConfiguration = result;
334 checkForLocaleChange(); // recursive, but now with a locale configuration
335 }
336 }.execute();
337 return;
338 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 final Configuration configuration = getResources().getConfiguration();
341
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700342 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 final String locale = configuration.locale.toString();
344
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700345 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800346 final int mcc = configuration.mcc;
347
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700348 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 final int mnc = configuration.mnc;
350
Romain Guy84f296c2009-11-04 15:00:44 -0800351 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352
Romain Guy84f296c2009-11-04 15:00:44 -0800353 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354 sLocaleConfiguration.locale = locale;
355 sLocaleConfiguration.mcc = mcc;
356 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700357
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359
360 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
361 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700362 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700363 public void run() {
364 writeConfiguration(Launcher.this, localeConfiguration);
365 }
366 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700367 }
368 }
369
370 private static class LocaleConfiguration {
371 public String locale;
372 public int mcc = -1;
373 public int mnc = -1;
374 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700375
Romain Guy98d01652009-06-30 16:21:04 -0700376 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
377 DataInputStream in = null;
378 try {
379 in = new DataInputStream(context.openFileInput(PREFERENCES));
380 configuration.locale = in.readUTF();
381 configuration.mcc = in.readInt();
382 configuration.mnc = in.readInt();
383 } catch (FileNotFoundException e) {
384 // Ignore
385 } catch (IOException e) {
386 // Ignore
387 } finally {
388 if (in != null) {
389 try {
390 in.close();
391 } catch (IOException e) {
392 // Ignore
393 }
394 }
395 }
396 }
397
398 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
399 DataOutputStream out = null;
400 try {
401 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
402 out.writeUTF(configuration.locale);
403 out.writeInt(configuration.mcc);
404 out.writeInt(configuration.mnc);
405 out.flush();
406 } catch (FileNotFoundException e) {
407 // Ignore
408 } catch (IOException e) {
409 //noinspection ResultOfMethodCallIgnored
410 context.getFileStreamPath(PREFERENCES).delete();
411 } finally {
412 if (out != null) {
413 try {
414 out.close();
415 } catch (IOException e) {
416 // Ignore
417 }
418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 }
420 }
421
Adam Cohen716b51e2011-06-30 12:09:54 -0700422 public DragLayer getDragLayer() {
423 return mDragLayer;
424 }
425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 static int getScreen() {
427 synchronized (sLock) {
428 return sScreen;
429 }
430 }
431
432 static void setScreen(int screen) {
433 synchronized (sLock) {
434 sScreen = screen;
435 }
436 }
437
Winson Chung557d6ed2011-07-08 15:34:52 -0700438 /**
439 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
440 * a configuration step, this allows the proper animations to run after other transitions.
441 */
442 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700443 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800444 switch (args.requestCode) {
445 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700446 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
447 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800448 break;
449 case REQUEST_PICK_SHORTCUT:
450 processShortcut(args.intent);
451 break;
452 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700453 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
454 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700455 result = true;
456 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800457 case REQUEST_PICK_APPWIDGET:
458 addAppWidgetFromPick(args.intent);
459 break;
460 case REQUEST_CREATE_APPWIDGET:
461 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700462 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700463 result = true;
464 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800465 case REQUEST_PICK_WALLPAPER:
466 // We just wanted the activity result here so we can clear mWaitingForResult
467 break;
468 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700469 // In any situation where we have a multi-step drop, we should reset the add info only after
470 // we complete the drop
471 resetAddInfo();
472 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800473 }
474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800476 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700477 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700478 mWaitingForResult = false;
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 // The pattern used here is that a user PICKs a specific application,
481 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700482
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
484 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700485 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800486 final PendingAddArguments args = new PendingAddArguments();
487 args.requestCode = requestCode;
488 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700489 args.container = mPendingAddInfo.container;
490 args.screen = mPendingAddInfo.screen;
491 args.cellX = mPendingAddInfo.cellX;
492 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800493
494 // If the loader is still running, defer the add until it is done.
495 if (isWorkspaceLocked()) {
496 sPendingAddList.add(args);
497 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700498 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
Romain Guy18042c82009-11-06 11:44:55 -0800500 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700501 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
502 if (data != null) {
503 // Clean up the appWidgetId if we canceled
504 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
505 if (appWidgetId != -1) {
506 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
507 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 }
509 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700510
511 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700512 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 }
514
515 @Override
516 protected void onResume() {
517 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800518 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700519 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400520 mWorkspaceLoading = true;
521 mModel.startLoader(this, true);
522 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700523 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800525 if (mWaitingForResume != null) {
526 mWaitingForResume.setStayPressed(false);
527 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700528 // When we resume Launcher, a different Activity might be responsible for the app
529 // market intent, so refresh the icon
530 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
532
533 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700534 protected void onPause() {
535 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700536 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800537 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 }
Romain Guycbb89e42009-06-08 15:52:54 -0700539
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700540 @Override
541 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 // Flag the loader to stop early before switching
543 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700544 if (mAppsCustomizeContent != null) {
545 mAppsCustomizeContent.surrender();
546 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800547 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700548 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700549
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800550 // We can't hide the IME if it was forced open. So don't bother
551 /*
552 @Override
553 public void onWindowFocusChanged(boolean hasFocus) {
554 super.onWindowFocusChanged(hasFocus);
555
556 if (hasFocus) {
557 final InputMethodManager inputManager = (InputMethodManager)
558 getSystemService(Context.INPUT_METHOD_SERVICE);
559 WindowManager.LayoutParams lp = getWindow().getAttributes();
560 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
561 android.os.Handler()) {
562 protected void onReceiveResult(int resultCode, Bundle resultData) {
563 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
564 }
565 });
566 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
567 }
568 }
569 */
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 private boolean acceptFilter() {
572 final InputMethodManager inputManager = (InputMethodManager)
573 getSystemService(Context.INPUT_METHOD_SERVICE);
574 return !inputManager.isFullscreenMode();
575 }
576
577 @Override
578 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700579 final int uniChar = event.getUnicodeChar();
580 final boolean handled = super.onKeyDown(keyCode, event);
581 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
582 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
584 keyCode, event);
585 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700586 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700587 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700588 // showSearchDialog()
589 // If there are multiple keystrokes before the search dialog takes focus,
590 // onSearchRequested() will be called for every keystroke,
591 // but it is idempotent, so it's fine.
592 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 }
594 }
595
Joe Onorato8a9625e2010-01-28 15:55:35 -0800596 // Eat the long press event so the keyboard doesn't come up.
597 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
598 return true;
599 }
600
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 return handled;
602 }
603
Karl Rosaen138a0412009-04-23 19:00:21 -0700604 private String getTypedText() {
605 return mDefaultKeySsb.toString();
606 }
607
608 private void clearTypedText() {
609 mDefaultKeySsb.clear();
610 mDefaultKeySsb.clearSpans();
611 Selection.setSelection(mDefaultKeySsb, 0);
612 }
613
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700615 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
616 * State
617 */
618 private static State intToState(int stateOrdinal) {
619 State state = State.WORKSPACE;
620 final State[] stateValues = State.values();
621 for (int i = 0; i < stateValues.length; i++) {
622 if (stateValues[i].ordinal() == stateOrdinal) {
623 state = stateValues[i];
624 break;
625 }
626 }
627 return state;
628 }
629
630 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 * Restores the previous state, if it exists.
632 *
633 * @param savedState The previous state.
634 */
635 private void restoreState(Bundle savedState) {
636 if (savedState == null) {
637 return;
638 }
639
Michael Jurka883f55b2010-10-21 15:47:14 -0700640 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700641 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800642 showAllApps(false);
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
646 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700647 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
649
Winson Chung3d503fb2011-07-13 17:25:49 -0700650 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
651 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700652
Winson Chung3d503fb2011-07-13 17:25:49 -0700653 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
654 mPendingAddInfo.container = pendingAddContainer;
655 mPendingAddInfo.screen = pendingAddScreen;
656 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
657 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 mRestoring = true;
659 }
660
661 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
662 if (renameFolder) {
663 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700664 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 mRestoring = true;
666 }
Winson Chunga12a2502010-12-20 14:41:35 -0800667
Winson Chung785d2eb2011-04-14 16:08:02 -0700668
669 // Restore the AppsCustomize tab
670 if (mAppsCustomizeTabHost != null) {
671 String curTab = savedState.getString("apps_customize_currentTab");
672 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700673 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700674 mAppsCustomizeContent.setContentType(
675 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
676 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700677 mAppsCustomizeContent.loadAssociatedPages(
678 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 }
680
Winson Chung5afbf7b2011-07-25 11:53:08 -0700681 int currentIndex = savedState.getInt("apps_customize_currentIndex");
682 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 /**
687 * Finds all the views we need and configure them properly.
688 */
689 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700690 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400691
Winson Chung4c98d922011-05-31 16:50:48 -0700692 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
693 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694
Winson Chung4c98d922011-05-31 16:50:48 -0700695 // Setup the drag layer
696 mDragLayer.setup(this, dragController);
697
Winson Chung3d503fb2011-07-13 17:25:49 -0700698 // Setup the hotseat
699 mHotseat = (Hotseat) findViewById(R.id.hotseat);
700 if (mHotseat != null) {
701 mHotseat.setup(this);
702 }
703
Winson Chung4c98d922011-05-31 16:50:48 -0700704 // Setup the workspace
705 mWorkspace.setHapticFeedbackEnabled(false);
706 mWorkspace.setOnLongClickListener(this);
707 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700708 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700709
Winson Chungf0ea4d32011-06-06 14:27:16 -0700710 // Get the search/delete bar
711 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700712
Winson Chungf0ea4d32011-06-06 14:27:16 -0700713 // Setup AppsCustomize
714 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
715 findViewById(R.id.apps_customize_pane);
716 mAppsCustomizeContent = (AppsCustomizePagedView)
717 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
718 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400719
Romain Guy1fbc1c82009-11-09 20:43:08 -0800720
Winson Chung4c98d922011-05-31 16:50:48 -0700721
Winson Chung3d503fb2011-07-13 17:25:49 -0700722 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700723 dragController.setDragScoller(mWorkspace);
724 dragController.setScrollView(mDragLayer);
725 dragController.setMoveTarget(mWorkspace);
726 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700727 if (mSearchDeleteBar != null) {
728 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800729 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731
Romain Guy8a73c512009-11-09 19:19:59 -0800732
Winson Chungaafa03c2010-06-11 17:34:16 -0700733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 /**
735 * Creates a view representing a shortcut.
736 *
737 * @param info The data structure describing the shortcut.
738 *
739 * @return A View inflated from R.layout.application.
740 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800741 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700743 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745
746 /**
747 * Creates a view representing a shortcut inflated from the specified resource.
748 *
749 * @param layoutResId The id of the XML layout used to create the shortcut.
750 * @param parent The group the shortcut belongs to.
751 * @param info The data structure describing the shortcut.
752 *
753 * @return A View inflated from layoutResId.
754 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800755 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800756 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
757 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 return favorite;
760 }
761
762 /**
763 * Add an application shortcut to the workspace.
764 *
765 * @param data The intent describing the application.
766 * @param cellInfo The position on screen where to create the shortcut.
767 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700768 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700769 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700770 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771
Adam Cohenfbba09b2011-07-18 21:43:05 -0700772 // First we check if we already know the exact location where we want to add this item.
773 if (cellX >= 0 && cellY >= 0) {
774 cellXY[0] = cellX;
775 cellXY[1] = cellY;
776 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777 showOutOfSpaceMessage();
778 return;
779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800781 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800782
Romain Guy73b979d2009-06-09 12:57:21 -0700783 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800784 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800786 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700787 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700788 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800789 } else {
790 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 }
792 }
Romain Guycbb89e42009-06-08 15:52:54 -0700793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 /**
795 * Add a shortcut to the workspace.
796 *
797 * @param data The intent describing the shortcut.
798 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
801 int cellY) {
802 int[] cellXY = mTmpAddItemCellCoordinates;
803 int[] touchXY = mPendingAddInfo.dropPos;
804 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700805
Michael Jurkad3ef3062010-11-23 16:23:58 -0800806 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700807
Adam Cohen558baaf2011-08-15 15:22:57 -0700808 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
809 final View view = createShortcut(info);
810
Adam Cohenfbba09b2011-07-18 21:43:05 -0700811 // First we check if we already know the exact location where we want to add this item.
812 if (cellX >= 0 && cellY >= 0) {
813 cellXY[0] = cellX;
814 cellXY[1] = cellY;
815 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700816
817 // If appropriate, either create a folder or add to an existing folder
818 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
819 true, null,null)) {
820 return;
821 }
822 DragObject dragObject = new DragObject();
823 dragObject.dragInfo = info;
824 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
825 return;
826 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700827 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800828 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700829 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800830 foundCellSpan = (result != null);
831 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700832 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800833 }
834
835 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700836 showOutOfSpaceMessage();
837 return;
838 }
839
Adam Cohen558baaf2011-08-15 15:22:57 -0700840 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
842 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
844 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 }
846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700851 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700852 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700855 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700856
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700857 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700859
860 // We want to account for the extra amount of padding that we are adding to the widget
861 // to ensure that it gets the full amount of space that it has requested
862 Resources r = getResources();
863 int requiredWidth = appWidgetInfo.minWidth +
864 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
865 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
866 int requiredHeight = appWidgetInfo.minHeight +
867 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
868 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
869 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700872 // We have saved the position to which the widget was dragged-- this really only matters
873 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700874 int[] cellXY = mTmpAddItemCellCoordinates;
875 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700877 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
878 cellXY[0] = mPendingAddInfo.cellX;
879 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700880 foundCellSpan = true;
881 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700882 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700884 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800885 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700887 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700888 }
889
Michael Jurka0280c3b2010-09-17 15:00:07 -0700890 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800891 if (appWidgetId != -1) {
892 // Deleting an app widget ID is a void call but writes to disk before returning
893 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800894 new Thread("deleteAppWidgetId") {
895 public void run() {
896 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
897 }
898 }.start();
899 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700900 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800901 return;
902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700904 // Build Launcher-specific widget info and save to database
905 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700906 launcherInfo.spanX = spanXY[0];
907 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700910 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911
912 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700914 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700915
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700916 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700918
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400920 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700921
922 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924 }
Romain Guycbb89e42009-06-08 15:52:54 -0700925
Adam Cohended9f8d2010-11-03 13:25:16 -0700926 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
927 @Override
928 public void onReceive(Context context, Intent intent) {
929 final String action = intent.getAction();
930 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
931 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700932 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700933 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700934
Winson Chungc100e8e2011-08-09 16:02:43 -0700935 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700936 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700937 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
938 mAppsCustomizeTabHost.reset();
939 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700940 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700941 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
942 mUserPresent = true;
943 updateRunning();
944 }
945 }
946 };
947
948 @Override
949 public void onAttachedToWindow() {
950 super.onAttachedToWindow();
951
952 // Listen for broadcasts related to user-presence
953 final IntentFilter filter = new IntentFilter();
954 filter.addAction(Intent.ACTION_SCREEN_OFF);
955 filter.addAction(Intent.ACTION_USER_PRESENT);
956 registerReceiver(mReceiver, filter);
957
Adam Cohend113e0c2010-11-11 10:48:05 -0800958 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700959 mVisible = true;
960 }
961
962 @Override
963 public void onDetachedFromWindow() {
964 super.onDetachedFromWindow();
965 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700966 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700967
Adam Cohend113e0c2010-11-11 10:48:05 -0800968 if (mAttached) {
969 unregisterReceiver(mReceiver);
970 mAttached = false;
971 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700972 updateRunning();
973 }
974
975 public void onWindowVisibilityChanged(int visibility) {
976 mVisible = visibility == View.VISIBLE;
977 updateRunning();
978 }
979
980 private void sendAdvanceMessage(long delay) {
981 mHandler.removeMessages(ADVANCE_MSG);
982 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
983 mHandler.sendMessageDelayed(msg, delay);
984 mAutoAdvanceSentTime = System.currentTimeMillis();
985 }
986
987 private void updateRunning() {
988 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
989 if (autoAdvanceRunning != mAutoAdvanceRunning) {
990 mAutoAdvanceRunning = autoAdvanceRunning;
991 if (autoAdvanceRunning) {
992 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
993 sendAdvanceMessage(delay);
994 } else {
995 if (!mWidgetsToAdvance.isEmpty()) {
996 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
997 (System.currentTimeMillis() - mAutoAdvanceSentTime));
998 }
999 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001000 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001001 }
1002 }
1003 }
1004
1005 private final Handler mHandler = new Handler() {
1006 @Override
1007 public void handleMessage(Message msg) {
1008 if (msg.what == ADVANCE_MSG) {
1009 int i = 0;
1010 for (View key: mWidgetsToAdvance.keySet()) {
1011 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1012 final int delay = mAdvanceStagger * i;
1013 if (v instanceof Advanceable) {
1014 postDelayed(new Runnable() {
1015 public void run() {
1016 ((Advanceable) v).advance();
1017 }
1018 }, delay);
1019 }
1020 i++;
1021 }
1022 sendAdvanceMessage(mAdvanceInterval);
1023 }
1024 }
1025 };
1026
1027 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001028 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001029 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1030 if (v instanceof Advanceable) {
1031 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001032 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001033 updateRunning();
1034 }
1035 }
1036
1037 void removeWidgetToAutoAdvance(View hostView) {
1038 if (mWidgetsToAdvance.containsKey(hostView)) {
1039 mWidgetsToAdvance.remove(hostView);
1040 updateRunning();
1041 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 }
1043
Joe Onorato9c1289c2009-08-17 11:03:03 -04001044 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001045 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 launcherInfo.hostView = null;
1047 }
1048
Adam Cohended9f8d2010-11-03 13:25:16 -07001049 void showOutOfSpaceMessage() {
1050 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1051 }
1052
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 public LauncherAppWidgetHost getAppWidgetHost() {
1054 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
Winson Chunga9abd0e2010-10-27 17:18:37 -07001057 public LauncherModel getModel() {
1058 return mModel;
1059 }
1060
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001061 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001062 getWindow().closeAllPanels();
1063
Winson Chung87acb482011-08-23 17:28:05 -07001064 /**
1065 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001066 try {
1067 dismissDialog(DIALOG_CREATE_SHORTCUT);
1068 // Unlock the workspace if the dialog was showing
1069 } catch (Exception e) {
1070 // An exception is thrown if the dialog is not visible, which is fine
1071 }
1072
1073 try {
1074 dismissDialog(DIALOG_RENAME_FOLDER);
1075 // Unlock the workspace if the dialog was showing
1076 } catch (Exception e) {
1077 // An exception is thrown if the dialog is not visible, which is fine
1078 }
Winson Chung87acb482011-08-23 17:28:05 -07001079 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001080
1081 // Whatever we were doing is hereby canceled.
1082 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001083 }
1084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 @Override
1086 protected void onNewIntent(Intent intent) {
1087 super.onNewIntent(intent);
1088
1089 // Close the menu
1090 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001091 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001092 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001093
Adam Cohen95bb8002011-07-03 23:40:28 -07001094 closeFolder();
1095
Joe Onorato14f122b2009-11-19 14:06:36 -08001096 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1097 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001098
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001099 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001100 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001101 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001102 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001103 }
Winson Chungde1af762011-07-21 16:44:07 -07001104 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001105 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001106
Joe Onorato3a8820b2009-11-10 15:06:42 -08001107 final View v = getWindow().peekDecorView();
1108 if (v != null && v.getWindowToken() != null) {
1109 InputMethodManager imm = (InputMethodManager)getSystemService(
1110 INPUT_METHOD_SERVICE);
1111 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001113
Michael Jurka35aa14d2011-07-07 17:01:08 -07001114 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001115 if (mAppsCustomizeTabHost != null) {
1116 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001117 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 }
1119 }
1120
1121 @Override
1122 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1123 // Do not call super here
1124 mSavedInstanceState = savedInstanceState;
1125 }
1126
1127 @Override
1128 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001129 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001130 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
Michael Jurka883f55b2010-10-21 15:47:14 -07001132 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001133 // We close any open folder since it will not be re-opened, and we need to make sure
1134 // this state is reflected.
1135 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136
Winson Chung3d503fb2011-07-13 17:25:49 -07001137 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1138 mWaitingForResult) {
1139 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1140 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1141 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1142 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144
1145 if (mFolderInfo != null && mWaitingForResult) {
1146 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1147 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1148 }
Michael Jurka946ad472010-07-09 18:05:18 -07001149
Winson Chung785d2eb2011-04-14 16:08:02 -07001150 // Save the current AppsCustomize tab
1151 if (mAppsCustomizeTabHost != null) {
1152 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1153 if (currentTabTag != null) {
1154 outState.putString("apps_customize_currentTab", currentTabTag);
1155 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001156 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1157 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160
1161 @Override
1162 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001164
Winson Chunge7a03942011-08-05 15:05:12 -07001165 // Remove all pending runnables
1166 mHandler.removeMessages(ADVANCE_MSG);
1167 mHandler.removeMessages(0);
1168
Winson Chungcd2b0142011-06-08 16:02:26 -07001169 // Stop callbacks from LauncherModel
1170 LauncherApplication app = ((LauncherApplication) getApplication());
1171 mModel.stopLoader();
1172 app.setLauncher(null);
1173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001175 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001177 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001179 mAppWidgetHost = null;
1180
1181 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
1183 TextKeyListener.getInstance().release();
1184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185
Winson Chung3d503fb2011-07-13 17:25:49 -07001186 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001187
1188 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001189 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001190
1191 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1192 mWorkspace.removeAllViews();
1193 mWorkspace = null;
1194 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001195
1196 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198
Adam Cohena9cf38f2011-05-02 15:36:58 -07001199 public DragController getDragController() {
1200 return mDragController;
1201 }
1202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 @Override
1204 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001205 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 super.startActivityForResult(intent, requestCode);
1207 }
1208
Winson Chung70d72102011-08-12 11:24:35 -07001209 /**
1210 * Indicates that we want global search for this activity by setting the globalSearch
1211 * argument for {@link #startSearch} to true.
1212 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001214 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001216
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001217 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001218
Karl Rosaen138a0412009-04-23 19:00:21 -07001219 if (initialQuery == null) {
1220 // Use any text typed in the launcher as the initial query
1221 initialQuery = getTypedText();
1222 clearTypedText();
1223 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 if (appSearchData == null) {
1225 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001226 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Karl Rosaen138a0412009-04-23 19:00:21 -07001229 final SearchManager searchManager =
1230 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001231 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001232 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234
Winson Chung70d72102011-08-12 11:24:35 -07001235 @Override
1236 public boolean onCreateOptionsMenu(Menu menu) {
1237 if (isWorkspaceLocked()) {
1238 return false;
1239 }
1240
1241 super.onCreateOptionsMenu(menu);
Winson Chung236d4312011-08-22 15:12:27 -07001242 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1243 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1244 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chung70d72102011-08-12 11:24:35 -07001245 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1246 .setIcon(android.R.drawable.ic_menu_gallery)
1247 .setAlphabeticShortcut('W');
1248 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1249 .setIcon(android.R.drawable.ic_menu_manage)
1250 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001251 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1252 .setIcon(android.R.drawable.ic_menu_preferences)
1253 .setIntent(settings)
1254 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001255 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1256 .setIcon(android.R.drawable.ic_menu_help)
1257 .setAlphabeticShortcut('H');
1258 return true;
1259 }
1260
1261 @Override
1262 public boolean onPrepareOptionsMenu(Menu menu) {
1263 super.onPrepareOptionsMenu(menu);
1264
1265 if (mAppsCustomizeTabHost.isTransitioning()) {
1266 return false;
1267 }
1268 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1269 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1270
1271 return true;
1272 }
1273
1274 @Override
1275 public boolean onOptionsItemSelected(MenuItem item) {
1276 switch (item.getItemId()) {
1277 case MENU_WALLPAPER_SETTINGS:
1278 startWallpaper();
1279 return true;
1280 case MENU_MANAGE_APPS:
1281 manageApps();
1282 return true;
1283 case MENU_HELP:
1284 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url))));
1285 return true;
1286 }
1287
1288 return super.onOptionsItemSelected(item);
1289 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001291 @Override
1292 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001293 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001294 // Use a custom animation for launching search
1295 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001296 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001297 }
1298
Joe Onorato9c1289c2009-08-17 11:03:03 -04001299 public boolean isWorkspaceLocked() {
1300 return mWorkspaceLoading || mWaitingForResult;
1301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001304 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001305 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
1307
Michael Jurka0280c3b2010-09-17 15:00:07 -07001308 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 mPendingAddInfo.container = ItemInfo.NO_ID;
1310 mPendingAddInfo.screen = -1;
1311 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1312 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1313 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001314 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001315
Winson Chung7ad01412010-09-27 11:33:03 -07001316 private void manageApps() {
1317 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1318 }
1319
Michael Jurkaaf442092010-06-10 17:01:57 -07001320 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001321 // TODO: catch bad widget exception when sent
1322 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001323 // TODO: Is this log message meaningful?
1324 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001325 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001326 }
1327
Winson Chung55cef262010-10-28 14:14:18 -07001328 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001329 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
Bjorn Bringert7984c942009-12-09 15:38:25 +00001331 if (appWidget.configure != null) {
1332 // Launch over to configure widget, if needed
1333 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1334 intent.setComponent(appWidget.configure);
1335 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001336 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001337 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1338 intent.putExtra(
1339 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1340 info.mimeType);
1341
1342 final String mimeType = info.mimeType;
1343 final ClipData clipData = (ClipData) info.configurationData;
1344 final ClipDescription clipDesc = clipData.getDescription();
1345 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1346 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001347 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001348 final CharSequence stringData = item.getText();
1349 final Uri uriData = item.getUri();
1350 final Intent intentData = item.getIntent();
1351 final String key =
1352 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1353 if (uriData != null) {
1354 intent.putExtra(key, uriData);
1355 } else if (intentData != null) {
1356 intent.putExtra(key, intentData);
1357 } else if (stringData != null) {
1358 intent.putExtra(key, stringData);
1359 }
1360 break;
1361 }
1362 }
1363 }
Winson Chung55cef262010-10-28 14:14:18 -07001364 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001365
Romain Guy8e633c52010-03-04 12:51:36 -08001366 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001368 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001370
1371 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001372 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
1374 }
Romain Guycbb89e42009-06-08 15:52:54 -07001375
Adam Cohenfbba09b2011-07-18 21:43:05 -07001376 /**
1377 * Process a shortcut drop.
1378 *
1379 * @param componentName The name of the component
1380 * @param screen The screen where it should be added
1381 * @param cell The cell it should be added to, optional
1382 * @param position The location on the screen where it was dropped, optional
1383 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001384 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1385 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001386 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001387 mPendingAddInfo.container = container;
1388 mPendingAddInfo.screen = screen;
1389 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001390
1391 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 mPendingAddInfo.cellX = cell[0];
1393 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001394 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001395
1396 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1397 createShortcutIntent.setComponent(componentName);
1398 processShortcut(createShortcutIntent);
1399 }
1400
Adam Cohenfbba09b2011-07-18 21:43:05 -07001401 /**
1402 * Process a widget drop.
1403 *
1404 * @param info The PendingAppWidgetInfo of the widget being added.
1405 * @param screen The screen where it should be added
1406 * @param cell The cell it should be added to, optional
1407 * @param position The location on the screen where it was dropped, optional
1408 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001409 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1410 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001411 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001412 mPendingAddInfo.container = info.container = container;
1413 mPendingAddInfo.screen = info.screen = screen;
1414 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001415 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 mPendingAddInfo.cellX = cell[0];
1417 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001418 }
1419
1420 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1421 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1422 addAppWidgetImpl(appWidgetId, info);
1423 }
1424
Joe Onoratodeb98af2010-02-19 14:59:39 -08001425 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001426 // Handle case where user selected "Applications"
1427 String applicationName = getResources().getString(R.string.group_applications);
1428 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001430 if (applicationName != null && applicationName.equals(shortcutName)) {
1431 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1432 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001434 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1435 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001436 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001437 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001438 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001439 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
Winson Chung24ab2f12010-09-16 14:10:47 -07001443 void processWallpaper(Intent intent) {
1444 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1445 }
1446
Winson Chung3d503fb2011-07-13 17:25:49 -07001447 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1448 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001449 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 folderInfo.title = getText(R.string.folder_name);
1451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1454 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001455 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
1457 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 FolderIcon newFolder =
1459 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1460 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1461 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001462 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001463 }
Romain Guycbb89e42009-06-08 15:52:54 -07001464
Joe Onorato9c1289c2009-08-17 11:03:03 -04001465 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001466 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001467 }
1468
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 private void showNotifications() {
1470 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1471 if (statusBar != null) {
1472 statusBar.expand();
1473 }
1474 }
1475
1476 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001477 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001479 Intent chooser = Intent.createChooser(pickWallpaper,
1480 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001481 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1482 // Removed in Eclair MR1
1483// WallpaperManager wm = (WallpaperManager)
1484// getSystemService(Context.WALLPAPER_SERVICE);
1485// WallpaperInfo wi = wm.getWallpaperInfo();
1486// if (wi != null && wi.getSettingsActivity() != null) {
1487// LabeledIntent li = new LabeledIntent(getPackageName(),
1488// R.string.configure_wallpaper, 0);
1489// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1490// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1491// }
Mike Clerona0618e42009-10-22 13:55:21 -07001492 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001495 /**
1496 * Registers various content observers. The current implementation registers
1497 * only a favorites observer to keep track of the favorites applications.
1498 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001499 private void registerContentObservers() {
1500 ContentResolver resolver = getContentResolver();
1501 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1502 true, mWidgetObserver);
1503 }
1504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 @Override
1506 public boolean dispatchKeyEvent(KeyEvent event) {
1507 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1508 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001510 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001511 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001512 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001513 dumpState();
1514 return true;
1515 }
1516 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001517 }
1518 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1519 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001520 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 return true;
1522 }
1523 }
1524
1525 return super.dispatchKeyEvent(event);
1526 }
1527
Joe Onorato88ec0992009-11-19 13:16:06 -08001528 @Override
1529 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001530 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001531 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001532 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001533 Folder openFolder = mWorkspace.getOpenFolder();
1534 if (openFolder.isEditingName()) {
1535 openFolder.dismissEditingName();
1536 } else {
1537 closeFolder();
1538 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001539 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001540 mWorkspace.exitWidgetResizeMode();
1541
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001542 // Back button is a no-op here, but give at least some feedback for the button press
1543 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001544 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001545 }
1546
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001548 * Re-listen when widgets are reset.
1549 */
1550 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001551 if (mAppWidgetHost != null) {
1552 mAppWidgetHost.startListening();
1553 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001554 }
1555
1556 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1558 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001560 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001561 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 /**
1565 * Launches the intent referred by the clicked shortcut.
1566 *
1567 * @param v The view representing the clicked shortcut.
1568 */
1569 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001570 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1571 // view has detached (it's possible for this to happen if the view is removed mid touch).
1572 if (v.getWindowToken() == null) {
1573 return;
1574 }
1575
1576 if (mWorkspace.isSwitchingState()) {
1577 return;
1578 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001579
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001581 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001583 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001584 int[] pos = new int[2];
1585 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001586 intent.setSourceBounds(new Rect(pos[0], pos[1],
1587 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001588 boolean success = startActivitySafely(intent, tag);
1589
1590 if (success && v instanceof BubbleTextView) {
1591 mWaitingForResume = (BubbleTextView) v;
1592 mWaitingForResume.setStayPressed(true);
1593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001595 if (v instanceof FolderIcon) {
1596 FolderIcon fi = (FolderIcon) v;
1597 handleFolderClick(fi);
1598 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001599 } else if (v == mAllAppsButton) {
1600 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001601 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001602 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001603 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
1606 }
1607
Michael Jurka0e260592010-06-30 17:07:39 -07001608 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001609 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001610 // clicking anywhere on the workspace causes the customization drawer to slide down
1611 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001612 return false;
1613 }
1614
Michael Jurkaaf442092010-06-10 17:01:57 -07001615 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001616 * Event handler for the search button
1617 *
1618 * @param v The view that was clicked.
1619 */
1620 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001621 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001622 }
1623
1624 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001625 * Event handler for the voice button
1626 *
1627 * @param v The view that was clicked.
1628 */
1629 public void onClickVoiceButton(View v) {
1630 startVoiceSearch();
1631 }
1632
1633 private void startVoiceSearch() {
1634 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1635 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1636 startActivity(intent);
1637 }
1638
1639 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001640 * Event handler for the "grid" button that appears on the home screen, which
1641 * enters all apps mode.
1642 *
1643 * @param v The view that was clicked.
1644 */
1645 public void onClickAllAppsButton(View v) {
1646 showAllApps(true);
1647 }
1648
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001649 public void onClickAppMarketButton(View v) {
1650 if (mAppMarketIntent != null) {
1651 startActivitySafely(mAppMarketIntent, "app market");
1652 }
1653 }
1654
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001655 void startApplicationDetailsActivity(ComponentName componentName) {
1656 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001657 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1658 Uri.fromParts("package", packageName, null));
1659 startActivity(intent);
1660 }
1661
Patrick Dubroy5539af72010-09-07 15:22:01 -07001662 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1663 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1664 // System applications cannot be installed. For now, show a toast explaining that.
1665 // We may give them the option of disabling apps this way.
1666 int messageId = R.string.uninstall_system_app_text;
1667 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1668 } else {
1669 String packageName = appInfo.componentName.getPackageName();
1670 String className = appInfo.componentName.getClassName();
1671 Intent intent = new Intent(
1672 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1673 startActivity(intent);
1674 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001675 }
1676
Michael Jurkaddd62e92011-02-16 17:49:14 -08001677 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1679 try {
1680 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001681 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 } catch (ActivityNotFoundException e) {
1683 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001684 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 } catch (SecurityException e) {
1686 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001687 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001689 "or use the exported attribute for this activity. "
1690 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001692 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001694
Romain Guy8e633c52010-03-04 12:51:36 -08001695 void startActivityForResultSafely(Intent intent, int requestCode) {
1696 try {
1697 startActivityForResult(intent, requestCode);
1698 } catch (ActivityNotFoundException e) {
1699 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1700 } catch (SecurityException e) {
1701 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1702 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1703 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1704 "or use the exported attribute for this activity.", e);
1705 }
1706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707
Adam Cohena9cf38f2011-05-02 15:36:58 -07001708 private void handleFolderClick(FolderIcon folderIcon) {
1709 final FolderInfo info = folderIcon.mInfo;
1710 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 // Close any open folder
1712 closeFolder();
1713 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001714 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 } else {
1716 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001717 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 int folderScreen;
1719 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001720 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 // .. and close it
1722 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001723 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 // Close any folder open on the current screen
1725 closeFolder();
1726 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001727 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 }
1729 }
1730 }
1731 }
1732
Adam Cohen2801caf2011-05-13 20:57:39 -07001733 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001734 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001735 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1736 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1737 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1738
Adam Cohenc51934b2011-07-26 21:07:43 -07001739 FolderInfo info = (FolderInfo) fi.getTag();
1740 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1741 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001742 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1743 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001744 }
1745
Adam Cohen2801caf2011-05-13 20:57:39 -07001746 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1747 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1748 oa.start();
1749 }
1750
1751 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001752 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001753 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1754 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1755 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1756
Adam Cohenc51934b2011-07-26 21:07:43 -07001757 FolderInfo info = (FolderInfo) fi.getTag();
1758 CellLayout cl = null;
1759 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1760 cl = (CellLayout) fi.getParent().getParent();
1761 }
1762
1763 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001764 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1765 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001766 oa.addListener(new AnimatorListenerAdapter() {
1767 @Override
1768 public void onAnimationEnd(Animator animation) {
1769 if (layout != null) {
1770 layout.clearFolderLeaveBehind();
1771 }
1772 }
1773 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001774 oa.start();
1775 }
1776
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001778 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 * is animated relative to the specified View. If the View is null, no animation
1780 * is played.
1781 *
1782 * @param folderInfo The FolderInfo describing the folder to open.
1783 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001784 public void openFolder(FolderIcon folderIcon) {
1785 Folder folder = folderIcon.mFolder;
1786 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787
Adam Cohen2801caf2011-05-13 20:57:39 -07001788 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001789 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790
Adam Cohen4554ee12011-08-03 16:13:21 -07001791 // Just verify that the folder hasn't already been added to the DragLayer.
1792 // There was a one-off crash where the folder had a parent already.
1793 if (folder.getParent() == null) {
1794 mDragLayer.addView(folder);
1795 mDragController.addDropTarget((DropTarget) folder);
1796 } else {
1797 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1798 folder.getParent() + ").");
1799 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001800 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001801 }
1802
1803 public void closeFolder() {
1804 Folder folder = mWorkspace.getOpenFolder();
1805 if (folder != null) {
1806 closeFolder(folder);
1807 }
1808 }
1809
1810 void closeFolder(Folder folder) {
1811 folder.getInfo().opened = false;
1812
1813 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1814 if (parent != null) {
1815 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1816 shrinkAndFadeInFolderIcon(fi);
1817 }
1818 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001822 if (mState != State.WORKSPACE) {
1823 return false;
1824 }
1825
Joe Onorato9c1289c2009-08-17 11:03:03 -04001826 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 return false;
1828 }
1829
1830 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001831 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 }
1833
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 resetAddInfo();
1835 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001837 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 return true;
1839 }
1840
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 // The hotseat touch handling does not go through Workspace, and we always allow long press
1842 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001843 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001844 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1845 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001846 if (itemUnderLongClick == null) {
1847 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001848 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1849 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001850 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001854 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 }
1856 }
1857 }
1858 return true;
1859 }
1860
Winson Chung3d503fb2011-07-13 17:25:49 -07001861 boolean isHotseatLayout(View layout) {
1862 return mHotseat != null && layout != null &&
1863 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1864 }
1865 Hotseat getHotseat() {
1866 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001867 }
1868
Winson Chung3d503fb2011-07-13 17:25:49 -07001869 /**
1870 * Returns the CellLayout of the specified container at the specified screen.
1871 */
1872 CellLayout getCellLayout(long container, int screen) {
1873 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1874 if (mHotseat != null) {
1875 return mHotseat.getLayout();
1876 } else {
1877 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001878 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001879 } else {
1880 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001881 }
1882 }
1883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 Workspace getWorkspace() {
1885 return mWorkspace;
1886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 protected Dialog onCreateDialog(int id) {
1890 switch (id) {
1891 case DIALOG_CREATE_SHORTCUT:
1892 return new CreateShortcut().createDialog();
1893 case DIALOG_RENAME_FOLDER:
1894 return new RenameFolder().createDialog();
1895 }
1896
1897 return super.onCreateDialog(id);
1898 }
1899
1900 @Override
1901 protected void onPrepareDialog(int id, Dialog dialog) {
1902 switch (id) {
1903 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 break;
1905 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001906 if (mFolderInfo != null) {
1907 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1908 final CharSequence text = mFolderInfo.title;
1909 input.setText(text);
1910 input.setSelection(0, text.length());
1911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 break;
1913 }
1914 }
1915
1916 void showRenameDialog(FolderInfo info) {
1917 mFolderInfo = info;
1918 mWaitingForResult = true;
1919 showDialog(DIALOG_RENAME_FOLDER);
1920 }
1921
Adam Cohenfbba09b2011-07-18 21:43:05 -07001922 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001923 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1925 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 mWaitingForResult = true;
1927 showDialog(DIALOG_CREATE_SHORTCUT);
1928 }
1929
1930 private class RenameFolder {
1931 private EditText mInput;
1932
1933 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1935 mInput = (EditText) layout.findViewById(R.id.folder_name);
1936
1937 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1938 builder.setIcon(0);
1939 builder.setTitle(getString(R.string.rename_folder_title));
1940 builder.setCancelable(true);
1941 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1942 public void onCancel(DialogInterface dialog) {
1943 cleanup();
1944 }
1945 });
1946 builder.setNegativeButton(getString(R.string.cancel_action),
1947 new Dialog.OnClickListener() {
1948 public void onClick(DialogInterface dialog, int which) {
1949 cleanup();
1950 }
1951 }
1952 );
1953 builder.setPositiveButton(getString(R.string.rename_action),
1954 new Dialog.OnClickListener() {
1955 public void onClick(DialogInterface dialog, int which) {
1956 changeFolderName();
1957 }
1958 }
1959 );
1960 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001961
1962 final AlertDialog dialog = builder.create();
1963 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1964 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001965 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001966 mInput.requestFocus();
1967 InputMethodManager inputManager = (InputMethodManager)
1968 getSystemService(Context.INPUT_METHOD_SERVICE);
1969 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001970 }
1971 });
1972
1973 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
1976 private void changeFolderName() {
1977 final String name = mInput.getText().toString();
1978 if (!TextUtils.isEmpty(name)) {
1979 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001980 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 mFolderInfo.title = name;
1982 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1983
Joe Onorato9c1289c2009-08-17 11:03:03 -04001984 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001985 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 } else {
1988 final FolderIcon folderIcon = (FolderIcon)
1989 mWorkspace.getViewForTag(mFolderInfo);
1990 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 // TODO: At some point we'll probably want some version of setting
1992 // the text for a folder icon.
1993 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 getWorkspace().requestLayout();
1995 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001996 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001997 mWorkspaceLoading = true;
1998 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000 }
2001 }
2002 cleanup();
2003 }
2004
2005 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 dismissDialog(DIALOG_RENAME_FOLDER);
2007 mWaitingForResult = false;
2008 mFolderInfo = null;
2009 }
2010 }
2011
Daniel Sandler843e8602010-06-07 14:59:01 -04002012 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2013 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002014 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002015 }
2016
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002017 // AllAppsView.Watcher
2018 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002019 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002020 mWorkspace.setVisibility(View.GONE);
2021 }
2022 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002023
2024 /**
2025 * Helper method for the cameraZoomIn/cameraZoomOut animations
2026 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002027 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002028 * @param scaleFactor The scale factor used for the zoom
2029 */
2030 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2031 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002032
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002033 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002034 // Set pivotY so that at the starting zoom factor, the view is partially
2035 // visible. Modifying initialHeightFactor changes how much of the view is
2036 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002037 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002038 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002039 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002040 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002041 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002042 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002043 }
2044 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002045
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002046 /**
2047 * Zoom the camera out from the workspace to reveal 'toView'.
2048 * Assumes that the view to show is anchored at either the very top or very bottom
2049 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002050 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002051 */
Winson Chungc07918d2011-07-01 15:35:26 -07002052 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002053 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002054
Winson Chungf0ea4d32011-06-06 14:27:16 -07002055 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2056 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2057 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2058 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002059
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002060 setPivotsForZoom(toView, toState, scale);
2061
Michael Jurkad74c9842011-07-10 12:44:21 -07002062 // Shrink workspaces away if going to AppsCustomize from workspace
2063 mWorkspace.shrink(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002064 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002065
2066 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002067 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002068 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002069 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2070 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002071 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002072 toView.setFastScaleX(a * scale + b * 1f);
2073 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002074 }
2075 });
Adam Cohen61033d32010-11-15 18:29:44 -08002076
Winson Chungf0ea4d32011-06-06 14:27:16 -07002077 toView.setVisibility(View.VISIBLE);
2078 toView.setFastAlpha(0f);
2079 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2080 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2081 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2082 public void onAnimationUpdate(float a, float b) {
2083 // don't need to invalidate because we do so above
2084 toView.setFastAlpha(a * 0f + b * 1f);
2085 }
2086 });
2087 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002088
Michael Jurkaabded662011-03-04 12:06:57 -08002089 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002090 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002091 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002092 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002093 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002094 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002095 // Prepare the position
2096 toView.setTranslationX(0.0f);
2097 toView.setTranslationY(0.0f);
2098 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002099 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002100 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002101 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002102 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002103 // If we don't set the final scale values here, if this animation is cancelled
2104 // it will have the wrong scale value and subsequent cameraPan animations will
2105 // not fix that
2106 toView.setScaleX(1.0f);
2107 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002108 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002109 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002110 }
Winson Chung32174c82011-07-19 15:47:55 -07002111
2112 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2113 // Hide the workspace scrollbar
2114 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002115 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002116 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002117 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002118 });
2119
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002120 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002121 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002122
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002123 if (mStateAnimation != null) mStateAnimation.cancel();
2124 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002125 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002126 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002127 } else {
2128 toView.setTranslationX(0.0f);
2129 toView.setTranslationY(0.0f);
2130 toView.setScaleX(1.0f);
2131 toView.setScaleY(1.0f);
2132 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002133 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002134 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002135 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2136 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002137
Winson Chungc07918d2011-07-01 15:35:26 -07002138 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2139 // Hide the workspace scrollbar
2140 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002141 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002142 }
Michael Jurkaabded662011-03-04 12:06:57 -08002143 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002144 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002145 }
2146
2147 /**
2148 * Zoom the camera back into the workspace, hiding 'fromView'.
2149 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002150 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002151 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002152 */
Winson Chungc07918d2011-07-01 15:35:26 -07002153 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002154 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002155
Winson Chungf0ea4d32011-06-06 14:27:16 -07002156 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2157 final float scaleFactor = (float)
2158 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2159 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002160
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002161 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002162
Michael Jurkad3ef3062010-11-23 16:23:58 -08002163 if (!springLoaded) {
2164 mWorkspace.unshrink(animated);
2165 }
Winson Chung4afe9b32011-07-27 17:46:20 -07002166 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002167 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002168 if (mStateAnimation != null) mStateAnimation.cancel();
2169 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002170
Michael Jurka742574b2011-02-02 23:51:01 -08002171 final float oldScaleX = fromView.getScaleX();
2172 final float oldScaleY = fromView.getScaleY();
2173
2174 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2175 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002176 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2177 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002178 ((View)fromView.getParent()).fastInvalidate();
2179 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2180 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2181 }
2182 });
Michael Jurkaabded662011-03-04 12:06:57 -08002183 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002184 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002185 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002186 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2187 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002188 // don't need to invalidate because we do so above
2189 fromView.setFastAlpha(a * 1f + b * 0f);
2190 }
2191 });
Michael Jurkaabded662011-03-04 12:06:57 -08002192 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002193 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002194 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002195 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002196 @Override
Winson Chungbb6f6a52011-07-25 17:55:44 -07002197 public void onAnimationStart(android.animation.Animator animation) {
2198 if (!springLoaded) {
2199 mWorkspace.showDockDivider(false);
2200 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002201 mWorkspace.showScrollingIndicator(false);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002202 }
2203 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002204 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002205 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002206 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002207 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002208 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002209 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002210 }
2211 });
2212
Winson Chungf0ea4d32011-06-06 14:27:16 -07002213 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002214 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002215 } else {
2216 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002217 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002218 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2219 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002220
Winson Chungc07918d2011-07-01 15:35:26 -07002221 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002222 // Flash the workspace scrollbar
Winson Chungbb6f6a52011-07-25 17:55:44 -07002223 mWorkspace.showDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002224 mWorkspace.flashScrollingIndicator();
2225 }
Michael Jurkaabded662011-03-04 12:06:57 -08002226 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002228 }
2229
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002230 void showWorkspace(boolean animated) {
2231 showWorkspace(animated, null);
2232 }
2233
2234 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002235 if (layout != null) {
2236 // always animated, but that's ok since we never specify a layout and
2237 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002238 mWorkspace.unshrink(layout);
2239 } else {
2240 mWorkspace.unshrink(animated);
2241 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002242 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002243 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002244 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002245
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002246 // Change the state *after* we've called all the transition code
2247 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002248
Winson Chung5fb63472011-02-02 17:03:37 -08002249 // Resume the auto-advance of widgets
2250 mUserPresent = true;
2251 updateRunning();
2252
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002253 // send an accessibility event to announce the context change
2254 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002255 }
2256
Michael Jurkad3ef3062010-11-23 16:23:58 -08002257 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002258 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002259 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002260 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002261 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002262 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002263 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002264 }
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002265 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002266 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002267 @Override
2268 public void run() {
2269 exitSpringLoadedDragMode();
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002270
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002271 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002272 // Before we show workspace, hide all apps again because
2273 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2274 // clean up our state transition functions
2275 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002276 showWorkspace(true);
2277 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002278 }
2279 }, (extendedDelay ?
2280 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2281 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2282 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002283 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002284 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002285 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002286 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2287 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 }
2289 // Otherwise, we are not in spring loaded mode, so don't do anything.
2290 }
2291
Adam Cohenfc53cd22011-07-20 15:45:11 -07002292 public boolean isAllAppsCustomizeOpen() {
2293 return mState == State.APPS_CUSTOMIZE;
2294 }
2295
Winson Chungf0ea4d32011-06-06 14:27:16 -07002296 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002298 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002299 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002300 if (!LauncherApplication.isScreenLarge()) {
2301 if (animated) {
2302 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002304 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002305 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002306 }
2307 }
2308 }
2309
2310 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002314 if (!LauncherApplication.isScreenLarge()) {
2315 if (animated) {
2316 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002318 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002319 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002320 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002321 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002322 }
2323
Winson Chung785d2eb2011-04-14 16:08:02 -07002324 void showAllApps(boolean animated) {
2325 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002326
Winson Chungf0ea4d32011-06-06 14:27:16 -07002327 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2328 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002329
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002331 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002332
Winson Chungf0ea4d32011-06-06 14:27:16 -07002333 // Change the state *after* we've called all the transition code
2334 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002335
2336 // Pause the auto-advance of widgets until we are out of AllApps
2337 mUserPresent = false;
2338 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002339 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002340
2341 // Send an accessibility event to announce the context change
2342 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2343 }
2344
Joe Onoratob2061212009-11-24 16:13:54 -05002345 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002346 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002347 * - Home from workspace
2348 * - from center screen
2349 * - from other screens
2350 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002351 * - from center screen
2352 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002353 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002354 * - from center screen
2355 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002356 * - Launch app from workspace and quit
2357 * - with back
2358 * - with home
2359 * - Launch app from all apps and quit
2360 * - with back
2361 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002362 * - Go to a screen that's not the default, then all
2363 * apps, and launch and app, and go back
2364 * - with back
2365 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002366 * - On workspace, long press power and go back
2367 * - with back
2368 * - with home
2369 * - On all apps, long press power and go back
2370 * - with back
2371 * - with home
2372 * - On workspace, power off
2373 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002374 * - Launch an app and turn off the screen while in that app
2375 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002376 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002377 * - From all apps
2378 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002379 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2380 * - From all apps
2381 * - From the center workspace
2382 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002383 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002384 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002385 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2386 mWorkspace.setVisibility(View.VISIBLE);
2387 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002388
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002390 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002391
Winson Chungf0ea4d32011-06-06 14:27:16 -07002392 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002393 if (mAllAppsButton != null) {
2394 mAllAppsButton.requestFocus();
2395 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002396 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002397 }
2398
Joe Onorato7c312c12009-08-13 21:36:53 -07002399 void lockAllApps() {
2400 // TODO
2401 }
2402
2403 void unlockAllApps() {
2404 // TODO
2405 }
2406
Patrick Dubroy5f445422011-02-18 14:35:21 -08002407 /**
2408 * Add an item from all apps or customize onto the given workspace screen.
2409 * If layout is null, add to the current screen.
2410 */
2411 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002412 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2413 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002414 } else {
2415 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002416 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002417 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002418
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002419 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002420 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002421 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002422 // Look for the toolbar icon specified in the activity meta-data
2423 Bundle metaData = packageManager.getActivityInfo(
2424 activityName, PackageManager.GET_META_DATA).metaData;
2425 if (metaData != null) {
2426 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2427 if (iconResId != 0) {
2428 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002429 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002430 }
2431 }
2432 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002433 // This can happen if the activity defines an invalid drawable
2434 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2435 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002436 } catch (Resources.NotFoundException nfe) {
2437 // This can happen if the activity defines an invalid drawable
2438 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2439 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002440 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002441 return null;
2442 }
2443
2444 // if successful in getting icon, return it; otherwise, set button to use default drawable
2445 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2446 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2447 TextView button = (TextView) findViewById(buttonId);
2448 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2449
2450 // If we were unable to find the icon via the meta-data, use a generic one
2451 if (toolbarIcon == null) {
2452 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2453 return null;
2454 } else {
2455 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2456 return toolbarIcon.getConstantState();
2457 }
2458 }
2459
2460 // if successful in getting icon, return it; otherwise, set button to use default drawable
2461 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2462 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2463 ImageView button = (ImageView) findViewById(buttonId);
2464 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2465
Michael Jurka19e0fc52011-07-22 18:00:21 -07002466 if (button != null) {
2467 // If we were unable to find the icon via the meta-data, use a
2468 // generic one
2469 if (toolbarIcon == null) {
2470 button.setImageResource(fallbackDrawableId);
2471 } else {
2472 button.setImageDrawable(toolbarIcon);
2473 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002474 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002475
2476 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2477
Michael Jurka0423dcf2010-10-05 14:56:18 -07002478 }
2479
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002480 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2481 TextView button = (TextView) findViewById(buttonId);
2482 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2483 }
2484
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002485 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002486 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002487 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002488 }
2489
Michael Jurka0423dcf2010-10-05 14:56:18 -07002490 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002491 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2492 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002493 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002494
Winson Chung1cad91e2011-05-25 17:41:01 -07002495 final SearchManager searchManager =
2496 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2497 ComponentName activityName = searchManager.getGlobalSearchActivity();
2498 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002499 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002500 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002501 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002502 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002503 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002504 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002505 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002506 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002507 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002508 }
2509 }
2510
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002511 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002512 updateButtonWithDrawable(R.id.search_button, d);
2513 }
2514
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002515 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002516 final View searchDivider = findViewById(R.id.search_divider);
2517 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002518
Winson Chung1cad91e2011-05-25 17:41:01 -07002519 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2520 ComponentName activityName = intent.resolveActivity(getPackageManager());
2521 if (activityName != null) {
2522 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002523 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2524 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002525 voiceButton.setVisibility(View.VISIBLE);
2526 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002527 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002528 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002529 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002530 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002531
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002532 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002533 updateButtonWithDrawable(R.id.voice_button, d);
2534 }
2535
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002536 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002537 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002538 */
2539 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002540 final View marketButton = findViewById(R.id.market_button);
2541 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2542 // Find the app market activity by resolving an intent.
2543 // (If multiple app markets are installed, it will return the ResolverActivity.)
2544 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002545 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002546 mAppMarketIntent = intent;
2547 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002548 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002549 marketButton.setVisibility(View.VISIBLE);
2550 } else {
2551 // We should hide and disable the view so that we don't try and restore the visibility
2552 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2553 marketButton.setVisibility(View.GONE);
2554 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002555 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002556 }
2557
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002558 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002559 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002560 }
2561
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002562 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002563 * Displays the shortcut creation dialog and launches, if necessary, the
2564 * appropriate activity.
2565 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002566 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002567 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2568 DialogInterface.OnShowListener {
2569
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002570 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002572 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002573 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002574
Winson Chung55b65502011-05-26 12:03:43 -07002575 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2576 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002577 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002578
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002579 AlertDialog dialog = builder.create();
2580 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002581 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002582 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002583
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 return dialog;
2585 }
2586
2587 public void onCancel(DialogInterface dialog) {
2588 mWaitingForResult = false;
2589 cleanup();
2590 }
2591
Romain Guycbb89e42009-06-08 15:52:54 -07002592 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002593 mWaitingForResult = false;
2594 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002595 }
2596
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002597 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002598 try {
2599 dismissDialog(DIALOG_CREATE_SHORTCUT);
2600 } catch (Exception e) {
2601 // An exception is thrown if the dialog is not visible, which is fine
2602 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 }
2604
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002605 /**
2606 * Handle the action clicked in the "Add to home" dialog.
2607 */
2608 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002609 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002610
Winson Chung55b65502011-05-26 12:03:43 -07002611 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2612 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002613 case AddAdapter.ITEM_APPLICATION: {
2614 if (mAppsCustomizeTabHost != null) {
2615 mAppsCustomizeTabHost.selectAppsTab();
2616 }
2617 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002618 break;
2619 }
Winson Chung55b65502011-05-26 12:03:43 -07002620 case AddAdapter.ITEM_APPWIDGET: {
2621 if (mAppsCustomizeTabHost != null) {
2622 mAppsCustomizeTabHost.selectWidgetsTab();
2623 }
2624 showAllApps(true);
2625 break;
2626 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002627 case AddAdapter.ITEM_WALLPAPER: {
2628 startWallpaper();
2629 break;
2630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 }
2632 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002633
2634 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002635 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 }
2638
2639 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002640 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002641 */
2642 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2643 @Override
2644 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002645 closeSystemDialogs();
2646 }
2647 }
2648
2649 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002650 * Receives notifications whenever the appwidgets are reset.
2651 */
2652 private class AppWidgetResetObserver extends ContentObserver {
2653 public AppWidgetResetObserver() {
2654 super(new Handler());
2655 }
2656
2657 @Override
2658 public void onChange(boolean selfChange) {
2659 onAppWidgetReset();
2660 }
2661 }
2662
2663 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002664 * If the activity is currently paused, signal that we need to re-run the loader
2665 * in onResume.
2666 *
2667 * This needs to be called from incoming places where resources might have been loaded
2668 * while we are paused. That is becaues the Configuration might be wrong
2669 * when we're not running, and if it comes back to what it was when we
2670 * were paused, we are not restarted.
2671 *
2672 * Implementation of the method from LauncherModel.Callbacks.
2673 *
2674 * @return true if we are currently paused. The caller might be able to
2675 * skip some work in that case since we will come back again.
2676 */
2677 public boolean setLoadOnResume() {
2678 if (mPaused) {
2679 Log.i(TAG, "setLoadOnResume");
2680 mOnResumeNeedsLoad = true;
2681 return true;
2682 } else {
2683 return false;
2684 }
2685 }
2686
2687 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002688 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002690 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002691 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002692 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002693 } else {
2694 return SCREEN_COUNT / 2;
2695 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002696 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002697
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002698
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699 /**
2700 * Refreshes the shortcuts shown on the workspace.
2701 *
2702 * Implementation of the method from LauncherModel.Callbacks.
2703 */
2704 public void startBinding() {
2705 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002706
2707 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002708 int count = workspace.getChildCount();
2709 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002710 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002711 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2712 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002714 if (mHotseat != null) {
2715 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002717
Adam Cohen4eac29a2011-07-11 17:53:37 -07002718 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002719 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 }
2721
2722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002723 * Bind the items start-end from the list.
2724 *
2725 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002727 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002728 setLoadOnResume();
2729
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002731 for (int i=start; i<end; i++) {
2732 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002733
2734 // Short circuit if we are loading dock items for a configuration which has no dock
2735 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2736 mHotseat == null) {
2737 continue;
2738 }
2739
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 switch (item.itemType) {
2741 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2742 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002743 View shortcut = createShortcut((ShortcutInfo)item);
2744 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2745 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002747 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002748 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002749 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002750 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002751 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2752 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002756 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 }
2758
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 /**
2760 * Implementation of the method from LauncherModel.Callbacks.
2761 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002762 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002763 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002764 sFolders.clear();
2765 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002767
2768 /**
2769 * Add the views for a widget to the workspace.
2770 *
2771 * Implementation of the method from LauncherModel.Callbacks.
2772 */
2773 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002774 setLoadOnResume();
2775
Daniel Sandler843e8602010-06-07 14:59:01 -04002776 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2777 if (DEBUG_WIDGETS) {
2778 Log.d(TAG, "bindAppWidget: " + item);
2779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 final Workspace workspace = mWorkspace;
2781
2782 final int appWidgetId = item.appWidgetId;
2783 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002784 if (DEBUG_WIDGETS) {
2785 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2786 }
2787
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2789
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2791 item.hostView.setTag(item);
2792
Winson Chung3d503fb2011-07-13 17:25:49 -07002793 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 item.cellY, item.spanX, item.spanY, false);
2795
Adam Cohended9f8d2010-11-03 13:25:16 -07002796 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2797
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 workspace.requestLayout();
2799
Daniel Sandler843e8602010-06-07 14:59:01 -04002800 if (DEBUG_WIDGETS) {
2801 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2802 + (SystemClock.uptimeMillis()-start) + "ms");
2803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 }
2805
2806 /**
2807 * Callback saying that there aren't any more items to bind.
2808 *
2809 * Implementation of the method from LauncherModel.Callbacks.
2810 */
2811 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002812 setLoadOnResume();
2813
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 if (mSavedState != null) {
2815 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002816 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 mSavedState = null;
2819 }
2820
2821 if (mSavedInstanceState != null) {
2822 super.onRestoreInstanceState(mSavedInstanceState);
2823 mSavedInstanceState = null;
2824 }
2825
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002827
2828 // If we received the result of any pending adds while the loader was running (e.g. the
2829 // widget configuration forced an orientation change), process them now.
2830 for (int i = 0; i < sPendingAddList.size(); i++) {
2831 completeAdd(sPendingAddList.get(i));
2832 }
2833 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 }
2835
2836 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002837 * Updates the icons on the launcher that are affected by changes to the package list
2838 * on the device.
2839 */
2840 private void updateIconsAffectedByPackageManagerChanges() {
2841 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002842 updateVoiceSearchIcon();
2843 }
2844
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002845 @Override
2846 public void bindSearchablesChanged() {
2847 updateGlobalSearchIcon();
2848 }
2849
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 * Add the icons for all apps.
2852 *
2853 * Implementation of the method from LauncherModel.Callbacks.
2854 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002855 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2856 // Remove the progress bar entirely; we could also make it GONE
2857 // but better to remove it since we know it's not going to be used
2858 View progressBar = mAppsCustomizeTabHost.
2859 findViewById(R.id.apps_customize_progress_bar);
2860 if (progressBar != null) {
2861 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002862 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002863 // We just post the call to setApps so the user sees the progress bar
2864 // disappear-- otherwise, it just looks like the progress bar froze
2865 // which doesn't look great
2866 mAppsCustomizeTabHost.post(new Runnable() {
2867 public void run() {
2868 if (mAppsCustomizeContent != null) {
2869 mAppsCustomizeContent.setApps(apps);
2870 }
2871 }
2872 });
2873
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002874 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002875 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 }
2877
2878 /**
2879 * A package was installed.
2880 *
2881 * Implementation of the method from LauncherModel.Callbacks.
2882 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002883 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002884 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002886
Winson Chung785d2eb2011-04-14 16:08:02 -07002887 if (mAppsCustomizeContent != null) {
2888 mAppsCustomizeContent.addApps(apps);
2889 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002890 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 }
2892
2893 /**
2894 * A package was updated.
2895 *
2896 * Implementation of the method from LauncherModel.Callbacks.
2897 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002898 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002899 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002901 if (mWorkspace != null) {
2902 mWorkspace.updateShortcuts(apps);
2903 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002904
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 if (mAppsCustomizeContent != null) {
2906 mAppsCustomizeContent.updateApps(apps);
2907 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002908 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 }
2910
2911 /**
2912 * A package was uninstalled.
2913 *
2914 * Implementation of the method from LauncherModel.Callbacks.
2915 */
Joe Onorato36115782010-06-17 13:28:48 -04002916 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002918 if (permanent) {
2919 mWorkspace.removeItems(apps);
2920 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002921
Winson Chung785d2eb2011-04-14 16:08:02 -07002922 if (mAppsCustomizeContent != null) {
2923 mAppsCustomizeContent.removeApps(apps);
2924 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002925 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002926 }
Joe Onoratobe386092009-11-17 17:32:16 -08002927
2928 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002929 * A number of packages were updated.
2930 */
2931 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002932
Winson Chung785d2eb2011-04-14 16:08:02 -07002933 if (mAppsCustomizeContent != null) {
2934 mAppsCustomizeContent.onPackagesUpdated();
2935 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002936 }
2937
Winson Chung400438b2011-01-16 17:53:48 -08002938 private int mapConfigurationOriActivityInfoOri(int configOri) {
2939 final Display d = getWindowManager().getDefaultDisplay();
2940 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2941 switch (d.getRotation()) {
2942 case Surface.ROTATION_0:
2943 case Surface.ROTATION_180:
2944 // We are currently in the same basic orientation as the natural orientation
2945 naturalOri = configOri;
2946 break;
2947 case Surface.ROTATION_90:
2948 case Surface.ROTATION_270:
2949 // We are currently in the other basic orientation to the natural orientation
2950 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2951 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2952 break;
2953 }
2954
2955 int[] oriMap = {
2956 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2957 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2958 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2959 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2960 };
2961 // Since the map starts at portrait, we need to offset if this device's natural orientation
2962 // is landscape.
2963 int indexOffset = 0;
2964 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2965 indexOffset = 1;
2966 }
2967 return oriMap[(d.getRotation() + indexOffset) % 4];
2968 }
2969 public void lockScreenOrientation() {
2970 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2971 .getConfiguration().orientation));
2972 }
2973 public void unlockScreenOrientation() {
2974 mHandler.postDelayed(new Runnable() {
2975 public void run() {
2976 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2977 }
2978 }, mRestoreScreenOrientationDelay);
2979 }
2980
Winson Chung82f55532011-08-09 14:14:23 -07002981 /* Cling related */
2982 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
2983 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
2984 private void enableClingsIfNecessary() {
2985 // TEMPORARY: DISABLE CLINGS ON LARGE UI
2986 if (LauncherApplication.isScreenLarge()) return;
2987
Brett Chabot2a9e2282011-08-23 15:03:13 -07002988 // disable clings when running in a test harness
2989 if(ActivityManager.isRunningInTestHarness()) return;
2990
Winson Chung82f55532011-08-09 14:14:23 -07002991 // Enable the clings only if they have not been dismissed before
2992 SharedPreferences prefs =
2993 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
2994 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
2995 Cling cling = (Cling) findViewById(R.id.workspace_cling);
2996 cling.init(this);
2997 cling.setVisibility(View.VISIBLE);
2998 }
2999 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3000 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3001 cling.init(this);
3002 cling.setVisibility(View.VISIBLE);
3003 }
3004 }
3005 private void dismissCling(final Cling cling, final String flag) {
3006 if (cling != null) {
3007 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3008 anim.setDuration(DISMISS_CLING_DURATION);
3009 anim.addListener(new AnimatorListenerAdapter() {
3010 public void onAnimationEnd(Animator animation) {
3011 cling.setVisibility(View.GONE);
3012 cling.cleanup();
3013 SharedPreferences prefs =
3014 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3015 SharedPreferences.Editor editor = prefs.edit();
3016 editor.putBoolean(flag, true);
3017 editor.commit();
3018 };
3019 });
3020 anim.start();
3021 }
3022 }
3023 public void dismissWorkspaceCling(View v) {
3024 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3025 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3026 }
3027 public void dismissAllAppsCling(View v) {
3028 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3029 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3030 }
3031
Winson Chung80baf5a2010-08-09 16:03:15 -07003032 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003033 * Prints out out state for debugging.
3034 */
3035 public void dumpState() {
3036 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003037 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003038 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3039 Log.d(TAG, "mRestoring=" + mRestoring);
3040 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3041 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003042 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003043 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044
Winson Chung785d2eb2011-04-14 16:08:02 -07003045 if (mAppsCustomizeContent != null) {
3046 mAppsCustomizeContent.dumpState();
3047 }
Joe Onoratobe386092009-11-17 17:32:16 -08003048 Log.d(TAG, "END launcher2 dump state");
3049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050}
Michael Jurka2763be32011-02-24 11:19:57 -08003051
Michael Jurkaabded662011-03-04 12:06:57 -08003052interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003053 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3054 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003055}