blob: 6ff46f85e714fc96025fadacf7dcaa73fcdcc275 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Adam Cohen50370f32011-08-25 18:57:14 -070080import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080082import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070084import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070087import android.widget.TextView;
88import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089
Adam Cohendf2cc412011-04-27 16:56:57 -070090import com.android.common.Search;
91import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070092import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080093
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.io.DataInputStream;
95import java.io.DataOutputStream;
96import java.io.FileNotFoundException;
97import java.io.IOException;
98import java.util.ArrayList;
99import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Default launcher application.
103 */
Michael Jurka946ad472010-07-09 18:05:18 -0700104public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700105 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
106 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700108 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400111 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700112
Winson Chung70d72102011-08-12 11:24:35 -0700113 private static final int MENU_GROUP_WALLPAPER = 1;
114 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
115 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700116 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
117 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
119 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700124 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
127
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800128 static final int SCREEN_COUNT = 5;
129 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Joe Onorato7c312c12009-08-13 21:36:53 -0700131 static final int DIALOG_CREATE_SHORTCUT = 1;
132 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Romain Guy98d01652009-06-30 16:21:04 -0700134 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 // Type: int
137 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700138 // Type: int
139 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700141 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
142 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
144 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700153 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
154
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700155 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700156 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700157 private State mState = State.WORKSPACE;
158 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700161 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
162 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700163 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800168 private final BroadcastReceiver mCloseSystemDialogsReceiver
169 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800170 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private LayoutInflater mInflater;
173
Joe Onorato00acb122009-08-04 16:04:30 -0400174 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private AppWidgetManager mAppWidgetManager;
178 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
Michael Jurkac9d95c52011-08-29 14:03:34 -0700180 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700181 private int[] mTmpAddItemCellCoordinates = new int[2];
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private FolderInfo mFolderInfo;
184
Winson Chung3d503fb2011-07-13 17:25:49 -0700185 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800186 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187
Winson Chungf0ea4d32011-06-06 14:27:16 -0700188 private SearchDropTargetBar mSearchDeleteBar;
189 private AppsCustomizeTabHost mAppsCustomizeTabHost;
190 private AppsCustomizePagedView mAppsCustomizeContent;
191 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
204 private Bundle mSavedInstanceState;
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800208 private boolean mUserPresent = true;
209 private boolean mVisible = false;
210 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700212 private static LocaleConfiguration sLocaleConfiguration = null;
213
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700214 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700216 private Intent mAppMarketIntent = null;
217
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 // Related to the auto-advancing of widgets
219 private final int ADVANCE_MSG = 1;
220 private final int mAdvanceInterval = 20000;
221 private final int mAdvanceStagger = 250;
222 private long mAutoAdvanceSentTime;
223 private long mAutoAdvanceTimeLeft = -1;
224 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
225 new HashMap<View, AppWidgetProviderInfo>();
226
Winson Chung400438b2011-01-16 17:53:48 -0800227 // Determines how long to wait after a rotation before restoring the screen orientation to
228 // match the sensor state.
229 private final int mRestoreScreenOrientationDelay = 500;
230
Michael Jurka4ef207b2010-11-29 17:05:45 -0800231 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800232 private static Drawable.ConstantState sGlobalSearchIcon;
233 private static Drawable.ConstantState sVoiceSearchIcon;
234 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800235
Adam Cohen2801caf2011-05-13 20:57:39 -0700236 private DragLayer mDragLayer;
237
Michael Jurkaddd62e92011-02-16 17:49:14 -0800238 private BubbleTextView mWaitingForResume;
239
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800240 private static ArrayList<PendingAddArguments> sPendingAddList
241 = new ArrayList<PendingAddArguments>();
242
243 private static class PendingAddArguments {
244 int requestCode;
245 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700246 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800247 int screen;
248 int cellX;
249 int cellY;
250 }
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 @Override
253 protected void onCreate(Bundle savedInstanceState) {
254 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 LauncherApplication app = ((LauncherApplication)getApplication());
256 mModel = app.setLauncher(this);
257 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400258 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
263 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200266 android.os.Debug.startMethodTracing(
267 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 }
269
270 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 setContentView(R.layout.launcher);
272 setupViews();
Winson Chung82f55532011-08-09 14:14:23 -0700273 enableClingsIfNecessary();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800275 registerContentObservers();
276
Joe Onorato7c312c12009-08-13 21:36:53 -0700277 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 mSavedState = savedInstanceState;
280 restoreState(mSavedState);
281
Winson Chunga12a2502010-12-20 14:41:35 -0800282 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700283 if (mAppsCustomizeContent != null) {
284 mAppsCustomizeContent.onPackagesUpdated();
285 }
Winson Chunga12a2502010-12-20 14:41:35 -0800286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 if (PROFILE_STARTUP) {
288 android.os.Debug.stopMethodTracing();
289 }
290
291 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400292 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 }
294
Michael Jurkac57b7a82011-08-09 22:02:20 -0700295 if (!mModel.isAllAppsLoaded()) {
296 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
297 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
298 }
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 // For handling default keys
301 mDefaultKeySsb = new SpannableStringBuilder();
302 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800303
304 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
305 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800306
307 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700308 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
309 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100310 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700311 }
312 if (sGlobalSearchIcon != null) {
313 updateGlobalSearchIcon(sGlobalSearchIcon);
314 }
315 if (sVoiceSearchIcon != null) {
316 updateVoiceSearchIcon(sVoiceSearchIcon);
317 }
318 if (sAppMarketIcon != null) {
319 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 }
Romain Guycbb89e42009-06-08 15:52:54 -0700322
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700324 if (sLocaleConfiguration == null) {
325 new AsyncTask<Void, Void, LocaleConfiguration>() {
326 @Override
327 protected LocaleConfiguration doInBackground(Void... unused) {
328 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
329 readConfiguration(Launcher.this, localeConfiguration);
330 return localeConfiguration;
331 }
332
333 @Override
334 protected void onPostExecute(LocaleConfiguration result) {
335 sLocaleConfiguration = result;
336 checkForLocaleChange(); // recursive, but now with a locale configuration
337 }
338 }.execute();
339 return;
340 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700341
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800342 final Configuration configuration = getResources().getConfiguration();
343
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700344 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 final String locale = configuration.locale.toString();
346
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700347 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 final int mcc = configuration.mcc;
349
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700350 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final int mnc = configuration.mnc;
352
Romain Guy84f296c2009-11-04 15:00:44 -0800353 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354
Romain Guy84f296c2009-11-04 15:00:44 -0800355 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700356 sLocaleConfiguration.locale = locale;
357 sLocaleConfiguration.mcc = mcc;
358 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700359
Joe Onorato0589f0f2010-02-08 13:44:00 -0800360 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700361
362 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
363 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700364 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700365 public void run() {
366 writeConfiguration(Launcher.this, localeConfiguration);
367 }
368 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700369 }
370 }
371
372 private static class LocaleConfiguration {
373 public String locale;
374 public int mcc = -1;
375 public int mnc = -1;
376 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700377
Romain Guy98d01652009-06-30 16:21:04 -0700378 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
379 DataInputStream in = null;
380 try {
381 in = new DataInputStream(context.openFileInput(PREFERENCES));
382 configuration.locale = in.readUTF();
383 configuration.mcc = in.readInt();
384 configuration.mnc = in.readInt();
385 } catch (FileNotFoundException e) {
386 // Ignore
387 } catch (IOException e) {
388 // Ignore
389 } finally {
390 if (in != null) {
391 try {
392 in.close();
393 } catch (IOException e) {
394 // Ignore
395 }
396 }
397 }
398 }
399
400 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
401 DataOutputStream out = null;
402 try {
403 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
404 out.writeUTF(configuration.locale);
405 out.writeInt(configuration.mcc);
406 out.writeInt(configuration.mnc);
407 out.flush();
408 } catch (FileNotFoundException e) {
409 // Ignore
410 } catch (IOException e) {
411 //noinspection ResultOfMethodCallIgnored
412 context.getFileStreamPath(PREFERENCES).delete();
413 } finally {
414 if (out != null) {
415 try {
416 out.close();
417 } catch (IOException e) {
418 // Ignore
419 }
420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421 }
422 }
423
Adam Cohen716b51e2011-06-30 12:09:54 -0700424 public DragLayer getDragLayer() {
425 return mDragLayer;
426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 static int getScreen() {
429 synchronized (sLock) {
430 return sScreen;
431 }
432 }
433
434 static void setScreen(int screen) {
435 synchronized (sLock) {
436 sScreen = screen;
437 }
438 }
439
Winson Chung557d6ed2011-07-08 15:34:52 -0700440 /**
441 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
442 * a configuration step, this allows the proper animations to run after other transitions.
443 */
444 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700445 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800446 switch (args.requestCode) {
447 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700448 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
449 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800450 break;
451 case REQUEST_PICK_SHORTCUT:
452 processShortcut(args.intent);
453 break;
454 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700455 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
456 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700457 result = true;
458 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800459 case REQUEST_PICK_APPWIDGET:
460 addAppWidgetFromPick(args.intent);
461 break;
462 case REQUEST_CREATE_APPWIDGET:
463 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700464 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 result = true;
466 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800467 case REQUEST_PICK_WALLPAPER:
468 // We just wanted the activity result here so we can clear mWaitingForResult
469 break;
470 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700471 // In any situation where we have a multi-step drop, we should reset the add info only after
472 // we complete the drop
473 resetAddInfo();
474 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800475 }
476
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800478 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700479 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700480 mWaitingForResult = false;
481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 // The pattern used here is that a user PICKs a specific application,
483 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
486 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700487 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488 final PendingAddArguments args = new PendingAddArguments();
489 args.requestCode = requestCode;
490 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700491 args.container = mPendingAddInfo.container;
492 args.screen = mPendingAddInfo.screen;
493 args.cellX = mPendingAddInfo.cellX;
494 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495
496 // If the loader is still running, defer the add until it is done.
497 if (isWorkspaceLocked()) {
498 sPendingAddList.add(args);
499 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700500 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 }
Romain Guy18042c82009-11-06 11:44:55 -0800502 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700503 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
504 if (data != null) {
505 // Clean up the appWidgetId if we canceled
506 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
507 if (appWidgetId != -1) {
508 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
511 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700512
513 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700514 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
516
517 @Override
518 protected void onResume() {
519 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800520 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700521 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400522 mWorkspaceLoading = true;
523 mModel.startLoader(this, true);
524 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700525 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800527 if (mWaitingForResume != null) {
528 mWaitingForResume.setStayPressed(false);
529 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700530 // When we resume Launcher, a different Activity might be responsible for the app
531 // market intent, so refresh the icon
532 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 }
534
535 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700536 protected void onPause() {
537 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700538 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800539 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700540 }
Romain Guycbb89e42009-06-08 15:52:54 -0700541
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700542 @Override
543 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400544 // Flag the loader to stop early before switching
545 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700546 if (mAppsCustomizeContent != null) {
547 mAppsCustomizeContent.surrender();
548 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800549 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700550 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700551
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800552 // We can't hide the IME if it was forced open. So don't bother
553 /*
554 @Override
555 public void onWindowFocusChanged(boolean hasFocus) {
556 super.onWindowFocusChanged(hasFocus);
557
558 if (hasFocus) {
559 final InputMethodManager inputManager = (InputMethodManager)
560 getSystemService(Context.INPUT_METHOD_SERVICE);
561 WindowManager.LayoutParams lp = getWindow().getAttributes();
562 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
563 android.os.Handler()) {
564 protected void onReceiveResult(int resultCode, Bundle resultData) {
565 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
566 }
567 });
568 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
569 }
570 }
571 */
572
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 private boolean acceptFilter() {
574 final InputMethodManager inputManager = (InputMethodManager)
575 getSystemService(Context.INPUT_METHOD_SERVICE);
576 return !inputManager.isFullscreenMode();
577 }
578
579 @Override
580 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700581 final int uniChar = event.getUnicodeChar();
582 final boolean handled = super.onKeyDown(keyCode, event);
583 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
584 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
586 keyCode, event);
587 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700588 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700589 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700590 // showSearchDialog()
591 // If there are multiple keystrokes before the search dialog takes focus,
592 // onSearchRequested() will be called for every keystroke,
593 // but it is idempotent, so it's fine.
594 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596 }
597
Joe Onorato8a9625e2010-01-28 15:55:35 -0800598 // Eat the long press event so the keyboard doesn't come up.
599 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
600 return true;
601 }
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 return handled;
604 }
605
Karl Rosaen138a0412009-04-23 19:00:21 -0700606 private String getTypedText() {
607 return mDefaultKeySsb.toString();
608 }
609
610 private void clearTypedText() {
611 mDefaultKeySsb.clear();
612 mDefaultKeySsb.clearSpans();
613 Selection.setSelection(mDefaultKeySsb, 0);
614 }
615
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700617 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
618 * State
619 */
620 private static State intToState(int stateOrdinal) {
621 State state = State.WORKSPACE;
622 final State[] stateValues = State.values();
623 for (int i = 0; i < stateValues.length; i++) {
624 if (stateValues[i].ordinal() == stateOrdinal) {
625 state = stateValues[i];
626 break;
627 }
628 }
629 return state;
630 }
631
632 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 * Restores the previous state, if it exists.
634 *
635 * @param savedState The previous state.
636 */
637 private void restoreState(Bundle savedState) {
638 if (savedState == null) {
639 return;
640 }
641
Michael Jurka883f55b2010-10-21 15:47:14 -0700642 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700643 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800644 showAllApps(false);
645 }
646
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
648 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700649 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651
Winson Chung3d503fb2011-07-13 17:25:49 -0700652 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
653 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700654
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
656 mPendingAddInfo.container = pendingAddContainer;
657 mPendingAddInfo.screen = pendingAddScreen;
658 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
659 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 mRestoring = true;
661 }
662
663 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
664 if (renameFolder) {
665 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700666 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 mRestoring = true;
668 }
Winson Chunga12a2502010-12-20 14:41:35 -0800669
Winson Chung785d2eb2011-04-14 16:08:02 -0700670
671 // Restore the AppsCustomize tab
672 if (mAppsCustomizeTabHost != null) {
673 String curTab = savedState.getString("apps_customize_currentTab");
674 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700675 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700676 mAppsCustomizeContent.setContentType(
677 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
678 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700679 mAppsCustomizeContent.loadAssociatedPages(
680 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700681 }
682
Winson Chung5afbf7b2011-07-25 11:53:08 -0700683 int currentIndex = savedState.getInt("apps_customize_currentIndex");
684 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700685 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800686 }
687
688 /**
689 * Finds all the views we need and configure them properly.
690 */
691 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700692 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400693
Winson Chung4c98d922011-05-31 16:50:48 -0700694 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
695 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696
Winson Chung4c98d922011-05-31 16:50:48 -0700697 // Setup the drag layer
698 mDragLayer.setup(this, dragController);
699
Winson Chung3d503fb2011-07-13 17:25:49 -0700700 // Setup the hotseat
701 mHotseat = (Hotseat) findViewById(R.id.hotseat);
702 if (mHotseat != null) {
703 mHotseat.setup(this);
704 }
705
Winson Chung4c98d922011-05-31 16:50:48 -0700706 // Setup the workspace
707 mWorkspace.setHapticFeedbackEnabled(false);
708 mWorkspace.setOnLongClickListener(this);
709 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700710 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700711
Winson Chungf0ea4d32011-06-06 14:27:16 -0700712 // Get the search/delete bar
713 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700714
Winson Chungf0ea4d32011-06-06 14:27:16 -0700715 // Setup AppsCustomize
716 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
717 findViewById(R.id.apps_customize_pane);
718 mAppsCustomizeContent = (AppsCustomizePagedView)
719 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
720 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400721
Romain Guy1fbc1c82009-11-09 20:43:08 -0800722
Winson Chung4c98d922011-05-31 16:50:48 -0700723
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700725 dragController.setDragScoller(mWorkspace);
726 dragController.setScrollView(mDragLayer);
727 dragController.setMoveTarget(mWorkspace);
728 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700729 if (mSearchDeleteBar != null) {
730 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 }
733
734 /**
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
Adam Cohen50370f32011-08-25 18:57:14 -0700848 class Padding {
849 int left = 0;
850 int right = 0;
851 int top = 0;
852 int bottom = 0;
853 }
854
Adam Cohenf814aa02011-09-01 13:48:05 -0700855 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700856 PackageManager packageManager = getPackageManager();
857 Padding p = new Padding();
858 android.content.pm.ApplicationInfo appInfo;
859
860 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700861 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700862 } catch (Exception e) {
863 // if we can't find the package, return 0 padding
864 return p;
865 }
866
867 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
868 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
869 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
870 Resources r = getResources();
871 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
872 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
873 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
874 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
875 }
876
877 return p;
878 }
879
Adam Cohenf814aa02011-09-01 13:48:05 -0700880 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
881 if (spanXY == null) {
882 spanXY = new int[2];
883 }
884
885 Padding padding = getPaddingForWidget(component);
886 // We want to account for the extra amount of padding that we are adding to the widget
887 // to ensure that it gets the full amount of space that it has requested
888 int requiredWidth = minWidth + padding.left + padding.right;
889 int requiredHeight = minHeight + padding.top + padding.bottom;
890 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
891 }
892
893 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
894 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
895 }
896
897 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
898 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
899 }
900
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700902 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700904 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700905 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700907 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700908 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700909
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700912
Adam Cohenf814aa02011-09-01 13:48:05 -0700913 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700916 // We have saved the position to which the widget was dragged-- this really only matters
917 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700918 int[] cellXY = mTmpAddItemCellCoordinates;
919 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700920 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
922 cellXY[0] = mPendingAddInfo.cellX;
923 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700924 foundCellSpan = true;
925 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700926 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700927 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700928 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800929 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700930 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700931 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700932 }
933
Michael Jurka0280c3b2010-09-17 15:00:07 -0700934 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800935 if (appWidgetId != -1) {
936 // Deleting an app widget ID is a void call but writes to disk before returning
937 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800938 new Thread("deleteAppWidgetId") {
939 public void run() {
940 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
941 }
942 }.start();
943 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800945 return;
946 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700948 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700949 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700950 launcherInfo.spanX = spanXY[0];
951 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
956 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700959
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700962
Winson Chung3d503fb2011-07-13 17:25:49 -0700963 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400964 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700965
966 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968 }
Romain Guycbb89e42009-06-08 15:52:54 -0700969
Adam Cohended9f8d2010-11-03 13:25:16 -0700970 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
971 @Override
972 public void onReceive(Context context, Intent intent) {
973 final String action = intent.getAction();
974 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
975 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700976 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700977 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700978
Winson Chungc100e8e2011-08-09 16:02:43 -0700979 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700980 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700981 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
982 mAppsCustomizeTabHost.reset();
983 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700984 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700985 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
986 mUserPresent = true;
987 updateRunning();
988 }
989 }
990 };
991
992 @Override
993 public void onAttachedToWindow() {
994 super.onAttachedToWindow();
995
996 // Listen for broadcasts related to user-presence
997 final IntentFilter filter = new IntentFilter();
998 filter.addAction(Intent.ACTION_SCREEN_OFF);
999 filter.addAction(Intent.ACTION_USER_PRESENT);
1000 registerReceiver(mReceiver, filter);
1001
Adam Cohend113e0c2010-11-11 10:48:05 -08001002 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001003 mVisible = true;
1004 }
1005
1006 @Override
1007 public void onDetachedFromWindow() {
1008 super.onDetachedFromWindow();
1009 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001010 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001011
Adam Cohend113e0c2010-11-11 10:48:05 -08001012 if (mAttached) {
1013 unregisterReceiver(mReceiver);
1014 mAttached = false;
1015 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001016 updateRunning();
1017 }
1018
1019 public void onWindowVisibilityChanged(int visibility) {
1020 mVisible = visibility == View.VISIBLE;
1021 updateRunning();
1022 }
1023
1024 private void sendAdvanceMessage(long delay) {
1025 mHandler.removeMessages(ADVANCE_MSG);
1026 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1027 mHandler.sendMessageDelayed(msg, delay);
1028 mAutoAdvanceSentTime = System.currentTimeMillis();
1029 }
1030
1031 private void updateRunning() {
1032 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1033 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1034 mAutoAdvanceRunning = autoAdvanceRunning;
1035 if (autoAdvanceRunning) {
1036 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1037 sendAdvanceMessage(delay);
1038 } else {
1039 if (!mWidgetsToAdvance.isEmpty()) {
1040 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1041 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1042 }
1043 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001044 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001045 }
1046 }
1047 }
1048
1049 private final Handler mHandler = new Handler() {
1050 @Override
1051 public void handleMessage(Message msg) {
1052 if (msg.what == ADVANCE_MSG) {
1053 int i = 0;
1054 for (View key: mWidgetsToAdvance.keySet()) {
1055 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1056 final int delay = mAdvanceStagger * i;
1057 if (v instanceof Advanceable) {
1058 postDelayed(new Runnable() {
1059 public void run() {
1060 ((Advanceable) v).advance();
1061 }
1062 }, delay);
1063 }
1064 i++;
1065 }
1066 sendAdvanceMessage(mAdvanceInterval);
1067 }
1068 }
1069 };
1070
1071 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001072 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001073 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1074 if (v instanceof Advanceable) {
1075 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001076 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001077 updateRunning();
1078 }
1079 }
1080
1081 void removeWidgetToAutoAdvance(View hostView) {
1082 if (mWidgetsToAdvance.containsKey(hostView)) {
1083 mWidgetsToAdvance.remove(hostView);
1084 updateRunning();
1085 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 }
1087
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001089 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 launcherInfo.hostView = null;
1091 }
1092
Adam Cohended9f8d2010-11-03 13:25:16 -07001093 void showOutOfSpaceMessage() {
1094 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1095 }
1096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 public LauncherAppWidgetHost getAppWidgetHost() {
1098 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 }
Romain Guycbb89e42009-06-08 15:52:54 -07001100
Winson Chunga9abd0e2010-10-27 17:18:37 -07001101 public LauncherModel getModel() {
1102 return mModel;
1103 }
1104
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001105 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001106 getWindow().closeAllPanels();
1107
Winson Chung87acb482011-08-23 17:28:05 -07001108 /**
1109 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001110 try {
1111 dismissDialog(DIALOG_CREATE_SHORTCUT);
1112 // Unlock the workspace if the dialog was showing
1113 } catch (Exception e) {
1114 // An exception is thrown if the dialog is not visible, which is fine
1115 }
1116
1117 try {
1118 dismissDialog(DIALOG_RENAME_FOLDER);
1119 // Unlock the workspace if the dialog was showing
1120 } catch (Exception e) {
1121 // An exception is thrown if the dialog is not visible, which is fine
1122 }
Winson Chung87acb482011-08-23 17:28:05 -07001123 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001124
1125 // Whatever we were doing is hereby canceled.
1126 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001127 }
1128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 @Override
1130 protected void onNewIntent(Intent intent) {
1131 super.onNewIntent(intent);
1132
1133 // Close the menu
1134 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001135 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001136 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001137
Adam Cohen95bb8002011-07-03 23:40:28 -07001138 closeFolder();
1139
Joe Onorato14f122b2009-11-19 14:06:36 -08001140 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1141 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001142
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001143 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001144 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001145 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001146 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001147 }
Winson Chungde1af762011-07-21 16:44:07 -07001148 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001149 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001150
Joe Onorato3a8820b2009-11-10 15:06:42 -08001151 final View v = getWindow().peekDecorView();
1152 if (v != null && v.getWindowToken() != null) {
1153 InputMethodManager imm = (InputMethodManager)getSystemService(
1154 INPUT_METHOD_SERVICE);
1155 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001157
Michael Jurka35aa14d2011-07-07 17:01:08 -07001158 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001159 if (mAppsCustomizeTabHost != null) {
1160 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163 }
1164
1165 @Override
1166 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1167 // Do not call super here
1168 mSavedInstanceState = savedInstanceState;
1169 }
1170
1171 @Override
1172 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001173 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001174 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
Michael Jurka883f55b2010-10-21 15:47:14 -07001176 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001177 // We close any open folder since it will not be re-opened, and we need to make sure
1178 // this state is reflected.
1179 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180
Winson Chung3d503fb2011-07-13 17:25:49 -07001181 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1182 mWaitingForResult) {
1183 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1184 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1185 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1186 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 }
1188
1189 if (mFolderInfo != null && mWaitingForResult) {
1190 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1191 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1192 }
Michael Jurka946ad472010-07-09 18:05:18 -07001193
Winson Chung785d2eb2011-04-14 16:08:02 -07001194 // Save the current AppsCustomize tab
1195 if (mAppsCustomizeTabHost != null) {
1196 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1197 if (currentTabTag != null) {
1198 outState.putString("apps_customize_currentTab", currentTabTag);
1199 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001200 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1201 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
1204
1205 @Override
1206 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Winson Chunge7a03942011-08-05 15:05:12 -07001209 // Remove all pending runnables
1210 mHandler.removeMessages(ADVANCE_MSG);
1211 mHandler.removeMessages(0);
1212
Winson Chungcd2b0142011-06-08 16:02:26 -07001213 // Stop callbacks from LauncherModel
1214 LauncherApplication app = ((LauncherApplication) getApplication());
1215 mModel.stopLoader();
1216 app.setLauncher(null);
1217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001221 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001223 mAppWidgetHost = null;
1224
1225 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
1227 TextKeyListener.getInstance().release();
1228
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
Winson Chung3d503fb2011-07-13 17:25:49 -07001230 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001231
1232 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001233 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001234
1235 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1236 mWorkspace.removeAllViews();
1237 mWorkspace = null;
1238 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001239
1240 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
1242
Adam Cohena9cf38f2011-05-02 15:36:58 -07001243 public DragController getDragController() {
1244 return mDragController;
1245 }
1246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 @Override
1248 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001249 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 super.startActivityForResult(intent, requestCode);
1251 }
1252
Winson Chung70d72102011-08-12 11:24:35 -07001253 /**
1254 * Indicates that we want global search for this activity by setting the globalSearch
1255 * argument for {@link #startSearch} to true.
1256 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001258 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001260
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001261 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Karl Rosaen138a0412009-04-23 19:00:21 -07001263 if (initialQuery == null) {
1264 // Use any text typed in the launcher as the initial query
1265 initialQuery = getTypedText();
1266 clearTypedText();
1267 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 if (appSearchData == null) {
1269 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001270 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
Romain Guycbb89e42009-06-08 15:52:54 -07001272
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 final SearchManager searchManager =
1274 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001276 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
1278
Winson Chung70d72102011-08-12 11:24:35 -07001279 @Override
1280 public boolean onCreateOptionsMenu(Menu menu) {
1281 if (isWorkspaceLocked()) {
1282 return false;
1283 }
1284
1285 super.onCreateOptionsMenu(menu);
Winson Chung236d4312011-08-22 15:12:27 -07001286 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1287 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1288 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chung70d72102011-08-12 11:24:35 -07001289 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1290 .setIcon(android.R.drawable.ic_menu_gallery)
1291 .setAlphabeticShortcut('W');
1292 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1293 .setIcon(android.R.drawable.ic_menu_manage)
1294 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001295 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1296 .setIcon(android.R.drawable.ic_menu_preferences)
1297 .setIntent(settings)
1298 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001299 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1300 .setIcon(android.R.drawable.ic_menu_help)
1301 .setAlphabeticShortcut('H');
1302 return true;
1303 }
1304
1305 @Override
1306 public boolean onPrepareOptionsMenu(Menu menu) {
1307 super.onPrepareOptionsMenu(menu);
1308
1309 if (mAppsCustomizeTabHost.isTransitioning()) {
1310 return false;
1311 }
1312 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1313 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1314
1315 return true;
1316 }
1317
1318 @Override
1319 public boolean onOptionsItemSelected(MenuItem item) {
1320 switch (item.getItemId()) {
1321 case MENU_WALLPAPER_SETTINGS:
1322 startWallpaper();
1323 return true;
1324 case MENU_MANAGE_APPS:
1325 manageApps();
1326 return true;
1327 case MENU_HELP:
1328 startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url))));
1329 return true;
1330 }
1331
1332 return super.onOptionsItemSelected(item);
1333 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001335 @Override
1336 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001337 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001338 // Use a custom animation for launching search
1339 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001340 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001341 }
1342
Joe Onorato9c1289c2009-08-17 11:03:03 -04001343 public boolean isWorkspaceLocked() {
1344 return mWorkspaceLoading || mWaitingForResult;
1345 }
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001348 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001349 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001353 mPendingAddInfo.container = ItemInfo.NO_ID;
1354 mPendingAddInfo.screen = -1;
1355 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1356 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1357 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001359
Winson Chung7ad01412010-09-27 11:33:03 -07001360 private void manageApps() {
1361 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1362 }
1363
Michael Jurkaaf442092010-06-10 17:01:57 -07001364 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001365 // TODO: catch bad widget exception when sent
1366 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001367 // TODO: Is this log message meaningful?
1368 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001369 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001370 }
1371
Winson Chung55cef262010-10-28 14:14:18 -07001372 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001373 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374
Bjorn Bringert7984c942009-12-09 15:38:25 +00001375 if (appWidget.configure != null) {
1376 // Launch over to configure widget, if needed
1377 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1378 intent.setComponent(appWidget.configure);
1379 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001380 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001381 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1382 intent.putExtra(
1383 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1384 info.mimeType);
1385
1386 final String mimeType = info.mimeType;
1387 final ClipData clipData = (ClipData) info.configurationData;
1388 final ClipDescription clipDesc = clipData.getDescription();
1389 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1390 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001391 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001392 final CharSequence stringData = item.getText();
1393 final Uri uriData = item.getUri();
1394 final Intent intentData = item.getIntent();
1395 final String key =
1396 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1397 if (uriData != null) {
1398 intent.putExtra(key, uriData);
1399 } else if (intentData != null) {
1400 intent.putExtra(key, intentData);
1401 } else if (stringData != null) {
1402 intent.putExtra(key, stringData);
1403 }
1404 break;
1405 }
1406 }
1407 }
Winson Chung55cef262010-10-28 14:14:18 -07001408 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001409
Romain Guy8e633c52010-03-04 12:51:36 -08001410 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001412 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001413 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001414
1415 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001416 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418 }
Romain Guycbb89e42009-06-08 15:52:54 -07001419
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420 /**
1421 * Process a shortcut drop.
1422 *
1423 * @param componentName The name of the component
1424 * @param screen The screen where it should be added
1425 * @param cell The cell it should be added to, optional
1426 * @param position The location on the screen where it was dropped, optional
1427 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001428 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1429 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 mPendingAddInfo.container = container;
1432 mPendingAddInfo.screen = screen;
1433 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001434
1435 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001436 mPendingAddInfo.cellX = cell[0];
1437 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001438 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439
1440 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1441 createShortcutIntent.setComponent(componentName);
1442 processShortcut(createShortcutIntent);
1443 }
1444
Adam Cohenfbba09b2011-07-18 21:43:05 -07001445 /**
1446 * Process a widget drop.
1447 *
1448 * @param info The PendingAppWidgetInfo of the widget being added.
1449 * @param screen The screen where it should be added
1450 * @param cell The cell it should be added to, optional
1451 * @param position The location on the screen where it was dropped, optional
1452 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1454 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001455 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001456 mPendingAddInfo.container = info.container = container;
1457 mPendingAddInfo.screen = info.screen = screen;
1458 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001459 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001460 mPendingAddInfo.cellX = cell[0];
1461 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 }
1463
1464 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1465 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1466 addAppWidgetImpl(appWidgetId, info);
1467 }
1468
Joe Onoratodeb98af2010-02-19 14:59:39 -08001469 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001470 // Handle case where user selected "Applications"
1471 String applicationName = getResources().getString(R.string.group_applications);
1472 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001473
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001474 if (applicationName != null && applicationName.equals(shortcutName)) {
1475 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1476 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001477
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001478 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1479 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001480 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001481 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001482 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001483 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
Winson Chung24ab2f12010-09-16 14:10:47 -07001487 void processWallpaper(Intent intent) {
1488 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1489 }
1490
Winson Chung3d503fb2011-07-13 17:25:49 -07001491 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1492 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001493 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 folderInfo.title = getText(R.string.folder_name);
1495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001497 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1498 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001499 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
1501 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 FolderIcon newFolder =
1503 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1504 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1505 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001506 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001510 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 }
1512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 private void showNotifications() {
1514 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1515 if (statusBar != null) {
1516 statusBar.expand();
1517 }
1518 }
1519
1520 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001521 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001523 Intent chooser = Intent.createChooser(pickWallpaper,
1524 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001525 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1526 // Removed in Eclair MR1
1527// WallpaperManager wm = (WallpaperManager)
1528// getSystemService(Context.WALLPAPER_SERVICE);
1529// WallpaperInfo wi = wm.getWallpaperInfo();
1530// if (wi != null && wi.getSettingsActivity() != null) {
1531// LabeledIntent li = new LabeledIntent(getPackageName(),
1532// R.string.configure_wallpaper, 0);
1533// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1534// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1535// }
Mike Clerona0618e42009-10-22 13:55:21 -07001536 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
1538
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001539 /**
1540 * Registers various content observers. The current implementation registers
1541 * only a favorites observer to keep track of the favorites applications.
1542 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001543 private void registerContentObservers() {
1544 ContentResolver resolver = getContentResolver();
1545 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1546 true, mWidgetObserver);
1547 }
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 @Override
1550 public boolean dispatchKeyEvent(KeyEvent event) {
1551 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1552 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001554 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001555 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001556 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001557 dumpState();
1558 return true;
1559 }
1560 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001561 }
1562 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1563 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001564 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 return true;
1566 }
1567 }
1568
1569 return super.dispatchKeyEvent(event);
1570 }
1571
Joe Onorato88ec0992009-11-19 13:16:06 -08001572 @Override
1573 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001574 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001575 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001576 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001577 Folder openFolder = mWorkspace.getOpenFolder();
1578 if (openFolder.isEditingName()) {
1579 openFolder.dismissEditingName();
1580 } else {
1581 closeFolder();
1582 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001583 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001584 mWorkspace.exitWidgetResizeMode();
1585
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001586 // Back button is a no-op here, but give at least some feedback for the button press
1587 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001588 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001589 }
1590
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001592 * Re-listen when widgets are reset.
1593 */
1594 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001595 if (mAppWidgetHost != null) {
1596 mAppWidgetHost.startListening();
1597 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001598 }
1599
1600 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001601 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1602 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001605 if (mModel != null) {
1606 mModel.unbindWorkspaceItems();
1607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 /**
1611 * Launches the intent referred by the clicked shortcut.
1612 *
1613 * @param v The view representing the clicked shortcut.
1614 */
1615 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001616 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1617 // view has detached (it's possible for this to happen if the view is removed mid touch).
1618 if (v.getWindowToken() == null) {
1619 return;
1620 }
1621
1622 if (mWorkspace.isSwitchingState()) {
1623 return;
1624 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001627 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001629 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001630 int[] pos = new int[2];
1631 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001632 intent.setSourceBounds(new Rect(pos[0], pos[1],
1633 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001634 boolean success = startActivitySafely(intent, tag);
1635
1636 if (success && v instanceof BubbleTextView) {
1637 mWaitingForResume = (BubbleTextView) v;
1638 mWaitingForResume.setStayPressed(true);
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001641 if (v instanceof FolderIcon) {
1642 FolderIcon fi = (FolderIcon) v;
1643 handleFolderClick(fi);
1644 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001645 } else if (v == mAllAppsButton) {
1646 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001647 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001648 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001649 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652 }
1653
Michael Jurka0e260592010-06-30 17:07:39 -07001654 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001655 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001656 // clicking anywhere on the workspace causes the customization drawer to slide down
1657 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001658 return false;
1659 }
1660
Michael Jurkaaf442092010-06-10 17:01:57 -07001661 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001662 * Event handler for the search button
1663 *
1664 * @param v The view that was clicked.
1665 */
1666 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001667 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001668 }
1669
1670 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001671 * Event handler for the voice button
1672 *
1673 * @param v The view that was clicked.
1674 */
1675 public void onClickVoiceButton(View v) {
1676 startVoiceSearch();
1677 }
1678
1679 private void startVoiceSearch() {
1680 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1681 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1682 startActivity(intent);
1683 }
1684
1685 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001686 * Event handler for the "grid" button that appears on the home screen, which
1687 * enters all apps mode.
1688 *
1689 * @param v The view that was clicked.
1690 */
1691 public void onClickAllAppsButton(View v) {
1692 showAllApps(true);
1693 }
1694
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001695 public void onClickAppMarketButton(View v) {
1696 if (mAppMarketIntent != null) {
1697 startActivitySafely(mAppMarketIntent, "app market");
1698 }
1699 }
1700
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001701 void startApplicationDetailsActivity(ComponentName componentName) {
1702 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001703 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1704 Uri.fromParts("package", packageName, null));
1705 startActivity(intent);
1706 }
1707
Patrick Dubroy5539af72010-09-07 15:22:01 -07001708 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1709 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1710 // System applications cannot be installed. For now, show a toast explaining that.
1711 // We may give them the option of disabling apps this way.
1712 int messageId = R.string.uninstall_system_app_text;
1713 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1714 } else {
1715 String packageName = appInfo.componentName.getPackageName();
1716 String className = appInfo.componentName.getClassName();
1717 Intent intent = new Intent(
1718 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1719 startActivity(intent);
1720 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001721 }
1722
Michael Jurkaddd62e92011-02-16 17:49:14 -08001723 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1725 try {
1726 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001727 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 } catch (ActivityNotFoundException e) {
1729 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001730 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 } catch (SecurityException e) {
1732 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001733 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001735 "or use the exported attribute for this activity. "
1736 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001738 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001740
Romain Guy8e633c52010-03-04 12:51:36 -08001741 void startActivityForResultSafely(Intent intent, int requestCode) {
1742 try {
1743 startActivityForResult(intent, requestCode);
1744 } catch (ActivityNotFoundException e) {
1745 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1746 } catch (SecurityException e) {
1747 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1748 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1749 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1750 "or use the exported attribute for this activity.", e);
1751 }
1752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753
Adam Cohena9cf38f2011-05-02 15:36:58 -07001754 private void handleFolderClick(FolderIcon folderIcon) {
1755 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001756 Folder openFolder = mWorkspace.getFolderForTag(info);
1757
1758 // If the folder info reports that the associated folder is open, then verify that
1759 // it is actually opened. There have been a few instances where this gets out of sync.
1760 if (info.opened && openFolder == null) {
1761 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1762 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1763 info.opened = false;
1764 }
1765
Adam Cohena9cf38f2011-05-02 15:36:58 -07001766 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 // Close any open folder
1768 closeFolder();
1769 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001770 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 } else {
1772 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 int folderScreen;
1774 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001775 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 // .. and close it
1777 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001778 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 // Close any folder open on the current screen
1780 closeFolder();
1781 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001782 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
1784 }
1785 }
1786 }
1787
Adam Cohen2801caf2011-05-13 20:57:39 -07001788 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001789 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001790 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1791 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1792 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1793
Adam Cohenc51934b2011-07-26 21:07:43 -07001794 FolderInfo info = (FolderInfo) fi.getTag();
1795 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1796 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001797 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1798 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001799 }
1800
Adam Cohen2801caf2011-05-13 20:57:39 -07001801 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1802 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1803 oa.start();
1804 }
1805
1806 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001807 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001808 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1809 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1810 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1811
Adam Cohenc51934b2011-07-26 21:07:43 -07001812 FolderInfo info = (FolderInfo) fi.getTag();
1813 CellLayout cl = null;
1814 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1815 cl = (CellLayout) fi.getParent().getParent();
1816 }
1817
1818 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001819 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1820 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001821 oa.addListener(new AnimatorListenerAdapter() {
1822 @Override
1823 public void onAnimationEnd(Animator animation) {
1824 if (layout != null) {
1825 layout.clearFolderLeaveBehind();
1826 }
1827 }
1828 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001829 oa.start();
1830 }
1831
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001833 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 * is animated relative to the specified View. If the View is null, no animation
1835 * is played.
1836 *
1837 * @param folderInfo The FolderInfo describing the folder to open.
1838 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001839 public void openFolder(FolderIcon folderIcon) {
1840 Folder folder = folderIcon.mFolder;
1841 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842
Adam Cohen2801caf2011-05-13 20:57:39 -07001843 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001844 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845
Adam Cohen4554ee12011-08-03 16:13:21 -07001846 // Just verify that the folder hasn't already been added to the DragLayer.
1847 // There was a one-off crash where the folder had a parent already.
1848 if (folder.getParent() == null) {
1849 mDragLayer.addView(folder);
1850 mDragController.addDropTarget((DropTarget) folder);
1851 } else {
1852 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1853 folder.getParent() + ").");
1854 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001855 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001856 }
1857
1858 public void closeFolder() {
1859 Folder folder = mWorkspace.getOpenFolder();
1860 if (folder != null) {
1861 closeFolder(folder);
1862 }
1863 }
1864
1865 void closeFolder(Folder folder) {
1866 folder.getInfo().opened = false;
1867
1868 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1869 if (parent != null) {
1870 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1871 shrinkAndFadeInFolderIcon(fi);
1872 }
1873 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001877 if (mState != State.WORKSPACE) {
1878 return false;
1879 }
1880
Joe Onorato9c1289c2009-08-17 11:03:03 -04001881 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 return false;
1883 }
1884
1885 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001886 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 resetAddInfo();
1890 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001892 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 return true;
1894 }
1895
Winson Chung3d503fb2011-07-13 17:25:49 -07001896 // The hotseat touch handling does not go through Workspace, and we always allow long press
1897 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001898 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001899 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1900 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001901 if (itemUnderLongClick == null) {
1902 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001903 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1904 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001905 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001907 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001909 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911 }
1912 }
1913 return true;
1914 }
1915
Winson Chung3d503fb2011-07-13 17:25:49 -07001916 boolean isHotseatLayout(View layout) {
1917 return mHotseat != null && layout != null &&
1918 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1919 }
1920 Hotseat getHotseat() {
1921 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001922 }
1923
Winson Chung3d503fb2011-07-13 17:25:49 -07001924 /**
1925 * Returns the CellLayout of the specified container at the specified screen.
1926 */
1927 CellLayout getCellLayout(long container, int screen) {
1928 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1929 if (mHotseat != null) {
1930 return mHotseat.getLayout();
1931 } else {
1932 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001933 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 } else {
1935 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001936 }
1937 }
1938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 Workspace getWorkspace() {
1940 return mWorkspace;
1941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 @Override
1944 protected Dialog onCreateDialog(int id) {
1945 switch (id) {
1946 case DIALOG_CREATE_SHORTCUT:
1947 return new CreateShortcut().createDialog();
1948 case DIALOG_RENAME_FOLDER:
1949 return new RenameFolder().createDialog();
1950 }
1951
1952 return super.onCreateDialog(id);
1953 }
1954
1955 @Override
1956 protected void onPrepareDialog(int id, Dialog dialog) {
1957 switch (id) {
1958 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 break;
1960 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001961 if (mFolderInfo != null) {
1962 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1963 final CharSequence text = mFolderInfo.title;
1964 input.setText(text);
1965 input.setSelection(0, text.length());
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 break;
1968 }
1969 }
1970
1971 void showRenameDialog(FolderInfo info) {
1972 mFolderInfo = info;
1973 mWaitingForResult = true;
1974 showDialog(DIALOG_RENAME_FOLDER);
1975 }
1976
Adam Cohenfbba09b2011-07-18 21:43:05 -07001977 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001978 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1980 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 mWaitingForResult = true;
1982 showDialog(DIALOG_CREATE_SHORTCUT);
1983 }
1984
1985 private class RenameFolder {
1986 private EditText mInput;
1987
1988 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1990 mInput = (EditText) layout.findViewById(R.id.folder_name);
1991
1992 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1993 builder.setIcon(0);
1994 builder.setTitle(getString(R.string.rename_folder_title));
1995 builder.setCancelable(true);
1996 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1997 public void onCancel(DialogInterface dialog) {
1998 cleanup();
1999 }
2000 });
2001 builder.setNegativeButton(getString(R.string.cancel_action),
2002 new Dialog.OnClickListener() {
2003 public void onClick(DialogInterface dialog, int which) {
2004 cleanup();
2005 }
2006 }
2007 );
2008 builder.setPositiveButton(getString(R.string.rename_action),
2009 new Dialog.OnClickListener() {
2010 public void onClick(DialogInterface dialog, int which) {
2011 changeFolderName();
2012 }
2013 }
2014 );
2015 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002016
2017 final AlertDialog dialog = builder.create();
2018 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2019 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002020 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002021 mInput.requestFocus();
2022 InputMethodManager inputManager = (InputMethodManager)
2023 getSystemService(Context.INPUT_METHOD_SERVICE);
2024 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002025 }
2026 });
2027
2028 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
2030
2031 private void changeFolderName() {
2032 final String name = mInput.getText().toString();
2033 if (!TextUtils.isEmpty(name)) {
2034 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002035 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 mFolderInfo.title = name;
2037 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2038
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002040 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002041 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } else {
2043 final FolderIcon folderIcon = (FolderIcon)
2044 mWorkspace.getViewForTag(mFolderInfo);
2045 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002046 // TODO: At some point we'll probably want some version of setting
2047 // the text for a folder icon.
2048 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 getWorkspace().requestLayout();
2050 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002051 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 mWorkspaceLoading = true;
2053 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 }
2055 }
2056 }
2057 cleanup();
2058 }
2059
2060 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 dismissDialog(DIALOG_RENAME_FOLDER);
2062 mWaitingForResult = false;
2063 mFolderInfo = null;
2064 }
2065 }
2066
Daniel Sandler843e8602010-06-07 14:59:01 -04002067 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2068 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002069 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002070 }
2071
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002072 // AllAppsView.Watcher
2073 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002074 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002075 mWorkspace.setVisibility(View.GONE);
2076 }
2077 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002078
2079 /**
2080 * Helper method for the cameraZoomIn/cameraZoomOut animations
2081 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002082 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002083 * @param scaleFactor The scale factor used for the zoom
2084 */
2085 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2086 final int height = view.getHeight();
2087 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002088 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002089 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002090
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002091 void updateWallpaperVisibility(boolean visible) {
2092 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2093 int curflags = getWindow().getAttributes().flags
2094 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2095 if (wpflags != curflags) {
2096 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2097 }
2098 }
2099
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002100 /**
2101 * Zoom the camera out from the workspace to reveal 'toView'.
2102 * Assumes that the view to show is anchored at either the very top or very bottom
2103 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002104 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002105 */
Winson Chungc07918d2011-07-01 15:35:26 -07002106 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002107 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002108
Winson Chungf0ea4d32011-06-06 14:27:16 -07002109 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2110 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2111 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2112 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002113
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002114 setPivotsForZoom(toView, toState, scale);
2115
Michael Jurkad74c9842011-07-10 12:44:21 -07002116 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002117 mWorkspace.changeState(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002118 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002119
2120 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002121 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002122 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002123 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2124 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002125 ((View) toView.getParent()).invalidate();
2126 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002127 toView.setFastScaleX(a * scale + b * 1f);
2128 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002129 }
2130 });
Adam Cohen61033d32010-11-15 18:29:44 -08002131
Winson Chungf0ea4d32011-06-06 14:27:16 -07002132 toView.setVisibility(View.VISIBLE);
2133 toView.setFastAlpha(0f);
2134 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2135 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2136 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2137 public void onAnimationUpdate(float a, float b) {
2138 // don't need to invalidate because we do so above
2139 toView.setFastAlpha(a * 0f + b * 1f);
2140 }
2141 });
2142 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002143
Michael Jurkaabded662011-03-04 12:06:57 -08002144 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002145 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002146 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002147 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002148 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002149 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002150 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002151 // Prepare the position
2152 toView.setTranslationX(0.0f);
2153 toView.setTranslationY(0.0f);
2154 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002155 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002156 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002157 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002158 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002159 // If we don't set the final scale values here, if this animation is cancelled
2160 // it will have the wrong scale value and subsequent cameraPan animations will
2161 // not fix that
2162 toView.setScaleX(1.0f);
2163 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002164 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002165 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002166 }
Winson Chung32174c82011-07-19 15:47:55 -07002167
2168 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2169 // Hide the workspace scrollbar
2170 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002171 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002172 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002173 updateWallpaperVisibility(false);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002174 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002175 });
2176
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002177 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002178 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002179
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002180 if (mStateAnimation != null) mStateAnimation.cancel();
2181 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002182 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002183 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002184 } else {
2185 toView.setTranslationX(0.0f);
2186 toView.setTranslationY(0.0f);
2187 toView.setScaleX(1.0f);
2188 toView.setScaleY(1.0f);
2189 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002190 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002191 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002192 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2193 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002194
Winson Chungc07918d2011-07-01 15:35:26 -07002195 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2196 // Hide the workspace scrollbar
2197 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002198 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002199 }
Michael Jurkaabded662011-03-04 12:06:57 -08002200 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002201 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002202 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002203 }
2204
2205 /**
2206 * Zoom the camera back into the workspace, hiding 'fromView'.
2207 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002208 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002209 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002210 */
Winson Chungc07918d2011-07-01 15:35:26 -07002211 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002212 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002213
Winson Chungf0ea4d32011-06-06 14:27:16 -07002214 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2215 final float scaleFactor = (float)
2216 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2217 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002218
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002219 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002220 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002221 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002222 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002223 if (mStateAnimation != null) mStateAnimation.cancel();
2224 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002225
Michael Jurka742574b2011-02-02 23:51:01 -08002226 final float oldScaleX = fromView.getScaleX();
2227 final float oldScaleY = fromView.getScaleY();
2228
2229 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2230 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002231 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2232 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002233 ((View)fromView.getParent()).fastInvalidate();
2234 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2235 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2236 }
2237 });
Michael Jurkaabded662011-03-04 12:06:57 -08002238 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002239 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002240 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002241 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2242 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002243 // don't need to invalidate because we do so above
2244 fromView.setFastAlpha(a * 1f + b * 0f);
2245 }
2246 });
Michael Jurkaabded662011-03-04 12:06:57 -08002247 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002248 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002249 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002250 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002251 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002252 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002253 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002254 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002255 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002256 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002257 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002258 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 }
2260 });
2261
Winson Chungf0ea4d32011-06-06 14:27:16 -07002262 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002263 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264 } else {
2265 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002266 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002267 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2268 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002269 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002270 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002271 }
2272
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002273 void showWorkspace(boolean animated) {
Adam Cohen7777d962011-08-18 18:58:38 -07002274 mWorkspace.changeState(Workspace.State.NORMAL, animated);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002275 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002276 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002277 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002278
Adam Cohen7777d962011-08-18 18:58:38 -07002279 mWorkspace.showDockDivider(!animated);
2280 mWorkspace.flashScrollingIndicator();
2281
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002282 // Change the state *after* we've called all the transition code
2283 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002284
Winson Chung5fb63472011-02-02 17:03:37 -08002285 // Resume the auto-advance of widgets
2286 mUserPresent = true;
2287 updateRunning();
2288
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002289 // send an accessibility event to announce the context change
2290 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002291 }
2292
Adam Cohen7777d962011-08-18 18:58:38 -07002293 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002294 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002295 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002296 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002297 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002298 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002299 }
Adam Cohen7777d962011-08-18 18:58:38 -07002300
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002301 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002302 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002303 @Override
2304 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002305 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002306 // Before we show workspace, hide all apps again because
2307 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2308 // clean up our state transition functions
2309 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002310 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002311 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002312 } else {
2313 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002314 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002315 }
2316 }, (extendedDelay ?
2317 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2318 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2319 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002320 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002321 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002322 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2323 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 }
2325 // Otherwise, we are not in spring loaded mode, so don't do anything.
2326 }
2327
Adam Cohenfc53cd22011-07-20 15:45:11 -07002328 public boolean isAllAppsCustomizeOpen() {
2329 return mState == State.APPS_CUSTOMIZE;
2330 }
2331
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002334 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002336 if (!LauncherApplication.isScreenLarge()) {
2337 if (animated) {
2338 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002341 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 }
2343 }
2344 }
2345
2346 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 if (!LauncherApplication.isScreenLarge()) {
2351 if (animated) {
2352 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002354 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002355 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002356 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002357 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002358 }
2359
Winson Chung785d2eb2011-04-14 16:08:02 -07002360 void showAllApps(boolean animated) {
2361 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002362
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2364 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002365
Winson Chung3d503fb2011-07-13 17:25:49 -07002366 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002367 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002368
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 // Change the state *after* we've called all the transition code
2370 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002371
2372 // Pause the auto-advance of widgets until we are out of AllApps
2373 mUserPresent = false;
2374 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002375 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002376
2377 // Send an accessibility event to announce the context change
2378 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2379 }
2380
Joe Onoratob2061212009-11-24 16:13:54 -05002381 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002382 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002383 * - Home from workspace
2384 * - from center screen
2385 * - from other screens
2386 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002387 * - from center screen
2388 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002389 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002390 * - from center screen
2391 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002392 * - Launch app from workspace and quit
2393 * - with back
2394 * - with home
2395 * - Launch app from all apps and quit
2396 * - with back
2397 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002398 * - Go to a screen that's not the default, then all
2399 * apps, and launch and app, and go back
2400 * - with back
2401 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002402 * - On workspace, long press power and go back
2403 * - with back
2404 * - with home
2405 * - On all apps, long press power and go back
2406 * - with back
2407 * - with home
2408 * - On workspace, power off
2409 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002410 * - Launch an app and turn off the screen while in that app
2411 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002412 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002413 * - From all apps
2414 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002415 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2416 * - From all apps
2417 * - From the center workspace
2418 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002419 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002420 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002421 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2422 mWorkspace.setVisibility(View.VISIBLE);
2423 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002424
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002427
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 if (mAllAppsButton != null) {
2430 mAllAppsButton.requestFocus();
2431 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002432 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002433 }
2434
Joe Onorato7c312c12009-08-13 21:36:53 -07002435 void lockAllApps() {
2436 // TODO
2437 }
2438
2439 void unlockAllApps() {
2440 // TODO
2441 }
2442
Patrick Dubroy5f445422011-02-18 14:35:21 -08002443 /**
2444 * Add an item from all apps or customize onto the given workspace screen.
2445 * If layout is null, add to the current screen.
2446 */
2447 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002448 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2449 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002450 } else {
2451 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002452 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002453 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002454
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002455 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002456 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002457 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002458 // Look for the toolbar icon specified in the activity meta-data
2459 Bundle metaData = packageManager.getActivityInfo(
2460 activityName, PackageManager.GET_META_DATA).metaData;
2461 if (metaData != null) {
2462 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2463 if (iconResId != 0) {
2464 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002465 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002466 }
2467 }
2468 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002469 // This can happen if the activity defines an invalid drawable
2470 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2471 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002472 } catch (Resources.NotFoundException nfe) {
2473 // This can happen if the activity defines an invalid drawable
2474 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2475 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002476 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002477 return null;
2478 }
2479
2480 // if successful in getting icon, return it; otherwise, set button to use default drawable
2481 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2482 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2483 TextView button = (TextView) findViewById(buttonId);
2484 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002485 Resources r = getResources();
2486 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2487 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002488
2489 // If we were unable to find the icon via the meta-data, use a generic one
2490 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002491 toolbarIcon = r.getDrawable(fallbackDrawableId);
2492 toolbarIcon.setBounds(0, 0, w, h);
2493 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002494 return null;
2495 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002496 toolbarIcon.setBounds(0, 0, w, h);
2497 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002498 return toolbarIcon.getConstantState();
2499 }
2500 }
2501
2502 // if successful in getting icon, return it; otherwise, set button to use default drawable
2503 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2504 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2505 ImageView button = (ImageView) findViewById(buttonId);
2506 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2507
Michael Jurka19e0fc52011-07-22 18:00:21 -07002508 if (button != null) {
2509 // If we were unable to find the icon via the meta-data, use a
2510 // generic one
2511 if (toolbarIcon == null) {
2512 button.setImageResource(fallbackDrawableId);
2513 } else {
2514 button.setImageDrawable(toolbarIcon);
2515 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002516 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002517
2518 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2519
Michael Jurka0423dcf2010-10-05 14:56:18 -07002520 }
2521
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002522 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2523 TextView button = (TextView) findViewById(buttonId);
2524 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2525 }
2526
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002527 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002528 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002529 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002530 }
2531
Michael Jurka0423dcf2010-10-05 14:56:18 -07002532 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002533 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2534 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002535 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002536
Winson Chung1cad91e2011-05-25 17:41:01 -07002537 final SearchManager searchManager =
2538 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2539 ComponentName activityName = searchManager.getGlobalSearchActivity();
2540 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002541 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002542 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002543 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002544 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002545 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002546 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002547 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002548 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002549 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002550 }
2551 }
2552
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002553 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002554 updateButtonWithDrawable(R.id.search_button, d);
2555 }
2556
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002557 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002558 final View searchDivider = findViewById(R.id.search_divider);
2559 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002560
Winson Chung1cad91e2011-05-25 17:41:01 -07002561 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2562 ComponentName activityName = intent.resolveActivity(getPackageManager());
2563 if (activityName != null) {
2564 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002565 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2566 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002567 voiceButton.setVisibility(View.VISIBLE);
2568 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002569 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002570 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002571 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002572 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002573
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002574 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002575 updateButtonWithDrawable(R.id.voice_button, d);
2576 }
2577
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002578 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002579 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002580 */
2581 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002582 final View marketButton = findViewById(R.id.market_button);
2583 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2584 // Find the app market activity by resolving an intent.
2585 // (If multiple app markets are installed, it will return the ResolverActivity.)
2586 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002587 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002588 mAppMarketIntent = intent;
2589 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002590 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002591 marketButton.setVisibility(View.VISIBLE);
2592 } else {
2593 // We should hide and disable the view so that we don't try and restore the visibility
2594 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2595 marketButton.setVisibility(View.GONE);
2596 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002597 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002598 }
2599
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002600 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002601 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002602 }
2603
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002604 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 * Displays the shortcut creation dialog and launches, if necessary, the
2606 * appropriate activity.
2607 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002608 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002609 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2610 DialogInterface.OnShowListener {
2611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002612 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002614 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002615 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002616
Winson Chung55b65502011-05-26 12:03:43 -07002617 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2618 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002619 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002620
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002621 AlertDialog dialog = builder.create();
2622 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002623 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002624 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 return dialog;
2627 }
2628
2629 public void onCancel(DialogInterface dialog) {
2630 mWaitingForResult = false;
2631 cleanup();
2632 }
2633
Romain Guycbb89e42009-06-08 15:52:54 -07002634 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002635 mWaitingForResult = false;
2636 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002637 }
2638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002639 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002640 try {
2641 dismissDialog(DIALOG_CREATE_SHORTCUT);
2642 } catch (Exception e) {
2643 // An exception is thrown if the dialog is not visible, which is fine
2644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 }
2646
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002647 /**
2648 * Handle the action clicked in the "Add to home" dialog.
2649 */
2650 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002651 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002652
Winson Chung55b65502011-05-26 12:03:43 -07002653 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2654 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002655 case AddAdapter.ITEM_APPLICATION: {
2656 if (mAppsCustomizeTabHost != null) {
2657 mAppsCustomizeTabHost.selectAppsTab();
2658 }
2659 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002660 break;
2661 }
Winson Chung55b65502011-05-26 12:03:43 -07002662 case AddAdapter.ITEM_APPWIDGET: {
2663 if (mAppsCustomizeTabHost != null) {
2664 mAppsCustomizeTabHost.selectWidgetsTab();
2665 }
2666 showAllApps(true);
2667 break;
2668 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002669 case AddAdapter.ITEM_WALLPAPER: {
2670 startWallpaper();
2671 break;
2672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 }
2674 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002675
2676 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002677 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 }
2680
2681 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002682 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002683 */
2684 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2685 @Override
2686 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002687 closeSystemDialogs();
2688 }
2689 }
2690
2691 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002692 * Receives notifications whenever the appwidgets are reset.
2693 */
2694 private class AppWidgetResetObserver extends ContentObserver {
2695 public AppWidgetResetObserver() {
2696 super(new Handler());
2697 }
2698
2699 @Override
2700 public void onChange(boolean selfChange) {
2701 onAppWidgetReset();
2702 }
2703 }
2704
2705 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002706 * If the activity is currently paused, signal that we need to re-run the loader
2707 * in onResume.
2708 *
2709 * This needs to be called from incoming places where resources might have been loaded
2710 * while we are paused. That is becaues the Configuration might be wrong
2711 * when we're not running, and if it comes back to what it was when we
2712 * were paused, we are not restarted.
2713 *
2714 * Implementation of the method from LauncherModel.Callbacks.
2715 *
2716 * @return true if we are currently paused. The caller might be able to
2717 * skip some work in that case since we will come back again.
2718 */
2719 public boolean setLoadOnResume() {
2720 if (mPaused) {
2721 Log.i(TAG, "setLoadOnResume");
2722 mOnResumeNeedsLoad = true;
2723 return true;
2724 } else {
2725 return false;
2726 }
2727 }
2728
2729 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002732 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002733 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002734 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002735 } else {
2736 return SCREEN_COUNT / 2;
2737 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002738 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002739
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002740
Joe Onorato9c1289c2009-08-17 11:03:03 -04002741 /**
2742 * Refreshes the shortcuts shown on the workspace.
2743 *
2744 * Implementation of the method from LauncherModel.Callbacks.
2745 */
2746 public void startBinding() {
2747 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002748
2749 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002750 int count = workspace.getChildCount();
2751 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002752 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002753 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2754 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002755 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002756 if (mHotseat != null) {
2757 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002758 }
2759 }
2760
2761 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002762 * Bind the items start-end from the list.
2763 *
2764 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002766 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002767 setLoadOnResume();
2768
Joe Onorato9c1289c2009-08-17 11:03:03 -04002769 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 for (int i=start; i<end; i++) {
2771 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002772
2773 // Short circuit if we are loading dock items for a configuration which has no dock
2774 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2775 mHotseat == null) {
2776 continue;
2777 }
2778
Joe Onorato9c1289c2009-08-17 11:03:03 -04002779 switch (item.itemType) {
2780 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2781 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002782 View shortcut = createShortcut((ShortcutInfo)item);
2783 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2784 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002785 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002786 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002787 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002788 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002789 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002790 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2791 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002792 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 }
2797
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 /**
2799 * Implementation of the method from LauncherModel.Callbacks.
2800 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002801 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002802 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002803 sFolders.clear();
2804 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806
2807 /**
2808 * Add the views for a widget to the workspace.
2809 *
2810 * Implementation of the method from LauncherModel.Callbacks.
2811 */
2812 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002813 setLoadOnResume();
2814
Daniel Sandler843e8602010-06-07 14:59:01 -04002815 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2816 if (DEBUG_WIDGETS) {
2817 Log.d(TAG, "bindAppWidget: " + item);
2818 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002819 final Workspace workspace = mWorkspace;
2820
2821 final int appWidgetId = item.appWidgetId;
2822 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002823 if (DEBUG_WIDGETS) {
2824 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2825 }
2826
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2828
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2830 item.hostView.setTag(item);
2831
Winson Chung3d503fb2011-07-13 17:25:49 -07002832 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 item.cellY, item.spanX, item.spanY, false);
2834
Adam Cohended9f8d2010-11-03 13:25:16 -07002835 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2836
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 workspace.requestLayout();
2838
Daniel Sandler843e8602010-06-07 14:59:01 -04002839 if (DEBUG_WIDGETS) {
2840 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2841 + (SystemClock.uptimeMillis()-start) + "ms");
2842 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 }
2844
2845 /**
2846 * Callback saying that there aren't any more items to bind.
2847 *
2848 * Implementation of the method from LauncherModel.Callbacks.
2849 */
2850 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002851 setLoadOnResume();
2852
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 if (mSavedState != null) {
2854 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002855 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 mSavedState = null;
2858 }
2859
2860 if (mSavedInstanceState != null) {
2861 super.onRestoreInstanceState(mSavedInstanceState);
2862 mSavedInstanceState = null;
2863 }
2864
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002866
2867 // If we received the result of any pending adds while the loader was running (e.g. the
2868 // widget configuration forced an orientation change), process them now.
2869 for (int i = 0; i < sPendingAddList.size(); i++) {
2870 completeAdd(sPendingAddList.get(i));
2871 }
2872 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 }
2874
2875 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002876 * Updates the icons on the launcher that are affected by changes to the package list
2877 * on the device.
2878 */
2879 private void updateIconsAffectedByPackageManagerChanges() {
2880 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002881 updateVoiceSearchIcon();
2882 }
2883
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002884 @Override
2885 public void bindSearchablesChanged() {
2886 updateGlobalSearchIcon();
2887 }
2888
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002889 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 * Add the icons for all apps.
2891 *
2892 * Implementation of the method from LauncherModel.Callbacks.
2893 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002894 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2895 // Remove the progress bar entirely; we could also make it GONE
2896 // but better to remove it since we know it's not going to be used
2897 View progressBar = mAppsCustomizeTabHost.
2898 findViewById(R.id.apps_customize_progress_bar);
2899 if (progressBar != null) {
2900 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002901 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002902 // We just post the call to setApps so the user sees the progress bar
2903 // disappear-- otherwise, it just looks like the progress bar froze
2904 // which doesn't look great
2905 mAppsCustomizeTabHost.post(new Runnable() {
2906 public void run() {
2907 if (mAppsCustomizeContent != null) {
2908 mAppsCustomizeContent.setApps(apps);
2909 }
2910 }
2911 });
2912
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002913 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002914 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 }
2916
2917 /**
2918 * A package was installed.
2919 *
2920 * Implementation of the method from LauncherModel.Callbacks.
2921 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002922 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002923 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002925
Winson Chung785d2eb2011-04-14 16:08:02 -07002926 if (mAppsCustomizeContent != null) {
2927 mAppsCustomizeContent.addApps(apps);
2928 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002929 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002930 }
2931
2932 /**
2933 * A package was updated.
2934 *
2935 * Implementation of the method from LauncherModel.Callbacks.
2936 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002937 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002938 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002940 if (mWorkspace != null) {
2941 mWorkspace.updateShortcuts(apps);
2942 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002943
Winson Chung785d2eb2011-04-14 16:08:02 -07002944 if (mAppsCustomizeContent != null) {
2945 mAppsCustomizeContent.updateApps(apps);
2946 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002947 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 }
2949
2950 /**
2951 * A package was uninstalled.
2952 *
2953 * Implementation of the method from LauncherModel.Callbacks.
2954 */
Joe Onorato36115782010-06-17 13:28:48 -04002955 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002957 if (permanent) {
2958 mWorkspace.removeItems(apps);
2959 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960
Winson Chung785d2eb2011-04-14 16:08:02 -07002961 if (mAppsCustomizeContent != null) {
2962 mAppsCustomizeContent.removeApps(apps);
2963 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002964 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 }
Joe Onoratobe386092009-11-17 17:32:16 -08002966
2967 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002968 * A number of packages were updated.
2969 */
2970 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002971
Winson Chung785d2eb2011-04-14 16:08:02 -07002972 if (mAppsCustomizeContent != null) {
2973 mAppsCustomizeContent.onPackagesUpdated();
2974 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002975 }
2976
Winson Chung400438b2011-01-16 17:53:48 -08002977 private int mapConfigurationOriActivityInfoOri(int configOri) {
2978 final Display d = getWindowManager().getDefaultDisplay();
2979 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2980 switch (d.getRotation()) {
2981 case Surface.ROTATION_0:
2982 case Surface.ROTATION_180:
2983 // We are currently in the same basic orientation as the natural orientation
2984 naturalOri = configOri;
2985 break;
2986 case Surface.ROTATION_90:
2987 case Surface.ROTATION_270:
2988 // We are currently in the other basic orientation to the natural orientation
2989 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2990 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2991 break;
2992 }
2993
2994 int[] oriMap = {
2995 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2996 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2997 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2998 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2999 };
3000 // Since the map starts at portrait, we need to offset if this device's natural orientation
3001 // is landscape.
3002 int indexOffset = 0;
3003 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3004 indexOffset = 1;
3005 }
3006 return oriMap[(d.getRotation() + indexOffset) % 4];
3007 }
3008 public void lockScreenOrientation() {
3009 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3010 .getConfiguration().orientation));
3011 }
3012 public void unlockScreenOrientation() {
3013 mHandler.postDelayed(new Runnable() {
3014 public void run() {
3015 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3016 }
3017 }, mRestoreScreenOrientationDelay);
3018 }
3019
Winson Chung82f55532011-08-09 14:14:23 -07003020 /* Cling related */
3021 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3022 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3023 private void enableClingsIfNecessary() {
3024 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3025 if (LauncherApplication.isScreenLarge()) return;
3026
Brett Chabot2a9e2282011-08-23 15:03:13 -07003027 // disable clings when running in a test harness
3028 if(ActivityManager.isRunningInTestHarness()) return;
3029
Winson Chung82f55532011-08-09 14:14:23 -07003030 // Enable the clings only if they have not been dismissed before
3031 SharedPreferences prefs =
3032 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3033 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3034 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3035 cling.init(this);
3036 cling.setVisibility(View.VISIBLE);
3037 }
3038 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3039 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3040 cling.init(this);
3041 cling.setVisibility(View.VISIBLE);
3042 }
3043 }
3044 private void dismissCling(final Cling cling, final String flag) {
3045 if (cling != null) {
3046 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3047 anim.setDuration(DISMISS_CLING_DURATION);
3048 anim.addListener(new AnimatorListenerAdapter() {
3049 public void onAnimationEnd(Animator animation) {
3050 cling.setVisibility(View.GONE);
3051 cling.cleanup();
3052 SharedPreferences prefs =
3053 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3054 SharedPreferences.Editor editor = prefs.edit();
3055 editor.putBoolean(flag, true);
3056 editor.commit();
3057 };
3058 });
3059 anim.start();
3060 }
3061 }
3062 public void dismissWorkspaceCling(View v) {
3063 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3064 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3065 }
3066 public void dismissAllAppsCling(View v) {
3067 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3068 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3069 }
3070
Winson Chung80baf5a2010-08-09 16:03:15 -07003071 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003072 * Prints out out state for debugging.
3073 */
3074 public void dumpState() {
3075 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003076 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003077 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3078 Log.d(TAG, "mRestoring=" + mRestoring);
3079 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3080 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003081 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003082 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003083
Winson Chung785d2eb2011-04-14 16:08:02 -07003084 if (mAppsCustomizeContent != null) {
3085 mAppsCustomizeContent.dumpState();
3086 }
Joe Onoratobe386092009-11-17 17:32:16 -08003087 Log.d(TAG, "END launcher2 dump state");
3088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089}
Michael Jurka2763be32011-02-24 11:19:57 -08003090
Michael Jurkaabded662011-03-04 12:06:57 -08003091interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003092 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3093 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003094}