blob: 637d9560774404580e66323bb742db6fdbff6083 [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 Jurkab60fd0e2011-08-29 14:02:47 -0700180 private ItemInfo mPendingAddInfo = new ItemInfo("1");
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 Jurkab60fd0e2011-08-29 14:02:47 -0700949 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId, "2");
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 Jurkab60fd0e2011-08-29 14:02:47 -07001493 final FolderInfo folderInfo = new FolderInfo("3");
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() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001605 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 /**
1609 * Launches the intent referred by the clicked shortcut.
1610 *
1611 * @param v The view representing the clicked shortcut.
1612 */
1613 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001614 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1615 // view has detached (it's possible for this to happen if the view is removed mid touch).
1616 if (v.getWindowToken() == null) {
1617 return;
1618 }
1619
1620 if (mWorkspace.isSwitchingState()) {
1621 return;
1622 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001625 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001627 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001628 int[] pos = new int[2];
1629 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001630 intent.setSourceBounds(new Rect(pos[0], pos[1],
1631 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001632 boolean success = startActivitySafely(intent, tag);
1633
1634 if (success && v instanceof BubbleTextView) {
1635 mWaitingForResume = (BubbleTextView) v;
1636 mWaitingForResume.setStayPressed(true);
1637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001639 if (v instanceof FolderIcon) {
1640 FolderIcon fi = (FolderIcon) v;
1641 handleFolderClick(fi);
1642 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001643 } else if (v == mAllAppsButton) {
1644 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001645 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001646 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001647 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
1650 }
1651
Michael Jurka0e260592010-06-30 17:07:39 -07001652 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001653 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001654 // clicking anywhere on the workspace causes the customization drawer to slide down
1655 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001656 return false;
1657 }
1658
Michael Jurkaaf442092010-06-10 17:01:57 -07001659 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001660 * Event handler for the search button
1661 *
1662 * @param v The view that was clicked.
1663 */
1664 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001665 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001666 }
1667
1668 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001669 * Event handler for the voice button
1670 *
1671 * @param v The view that was clicked.
1672 */
1673 public void onClickVoiceButton(View v) {
1674 startVoiceSearch();
1675 }
1676
1677 private void startVoiceSearch() {
1678 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1679 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1680 startActivity(intent);
1681 }
1682
1683 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001684 * Event handler for the "grid" button that appears on the home screen, which
1685 * enters all apps mode.
1686 *
1687 * @param v The view that was clicked.
1688 */
1689 public void onClickAllAppsButton(View v) {
1690 showAllApps(true);
1691 }
1692
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001693 public void onClickAppMarketButton(View v) {
1694 if (mAppMarketIntent != null) {
1695 startActivitySafely(mAppMarketIntent, "app market");
1696 }
1697 }
1698
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001699 void startApplicationDetailsActivity(ComponentName componentName) {
1700 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001701 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1702 Uri.fromParts("package", packageName, null));
1703 startActivity(intent);
1704 }
1705
Patrick Dubroy5539af72010-09-07 15:22:01 -07001706 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1707 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1708 // System applications cannot be installed. For now, show a toast explaining that.
1709 // We may give them the option of disabling apps this way.
1710 int messageId = R.string.uninstall_system_app_text;
1711 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1712 } else {
1713 String packageName = appInfo.componentName.getPackageName();
1714 String className = appInfo.componentName.getClassName();
1715 Intent intent = new Intent(
1716 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1717 startActivity(intent);
1718 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001719 }
1720
Michael Jurkaddd62e92011-02-16 17:49:14 -08001721 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1723 try {
1724 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001725 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 } catch (ActivityNotFoundException e) {
1727 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001728 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 } catch (SecurityException e) {
1730 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001731 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001733 "or use the exported attribute for this activity. "
1734 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001736 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001738
Romain Guy8e633c52010-03-04 12:51:36 -08001739 void startActivityForResultSafely(Intent intent, int requestCode) {
1740 try {
1741 startActivityForResult(intent, requestCode);
1742 } catch (ActivityNotFoundException e) {
1743 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1744 } catch (SecurityException e) {
1745 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1746 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1747 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1748 "or use the exported attribute for this activity.", e);
1749 }
1750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751
Adam Cohena9cf38f2011-05-02 15:36:58 -07001752 private void handleFolderClick(FolderIcon folderIcon) {
1753 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001754 Folder openFolder = mWorkspace.getFolderForTag(info);
1755
1756 // If the folder info reports that the associated folder is open, then verify that
1757 // it is actually opened. There have been a few instances where this gets out of sync.
1758 if (info.opened && openFolder == null) {
1759 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1760 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1761 info.opened = false;
1762 }
1763
Adam Cohena9cf38f2011-05-02 15:36:58 -07001764 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 // Close any open folder
1766 closeFolder();
1767 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001768 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 } else {
1770 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 int folderScreen;
1772 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001773 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 // .. and close it
1775 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001776 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 // Close any folder open on the current screen
1778 closeFolder();
1779 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001780 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782 }
1783 }
1784 }
1785
Adam Cohen2801caf2011-05-13 20:57:39 -07001786 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001787 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001788 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1789 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1790 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1791
Adam Cohenc51934b2011-07-26 21:07:43 -07001792 FolderInfo info = (FolderInfo) fi.getTag();
1793 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1794 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001795 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1796 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001797 }
1798
Adam Cohen2801caf2011-05-13 20:57:39 -07001799 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1800 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1801 oa.start();
1802 }
1803
1804 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001805 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001806 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1807 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1808 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1809
Adam Cohenc51934b2011-07-26 21:07:43 -07001810 FolderInfo info = (FolderInfo) fi.getTag();
1811 CellLayout cl = null;
1812 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1813 cl = (CellLayout) fi.getParent().getParent();
1814 }
1815
1816 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001817 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1818 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001819 oa.addListener(new AnimatorListenerAdapter() {
1820 @Override
1821 public void onAnimationEnd(Animator animation) {
1822 if (layout != null) {
1823 layout.clearFolderLeaveBehind();
1824 }
1825 }
1826 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001827 oa.start();
1828 }
1829
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001831 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 * is animated relative to the specified View. If the View is null, no animation
1833 * is played.
1834 *
1835 * @param folderInfo The FolderInfo describing the folder to open.
1836 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001837 public void openFolder(FolderIcon folderIcon) {
1838 Folder folder = folderIcon.mFolder;
1839 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840
Adam Cohen2801caf2011-05-13 20:57:39 -07001841 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001842 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843
Adam Cohen4554ee12011-08-03 16:13:21 -07001844 // Just verify that the folder hasn't already been added to the DragLayer.
1845 // There was a one-off crash where the folder had a parent already.
1846 if (folder.getParent() == null) {
1847 mDragLayer.addView(folder);
1848 mDragController.addDropTarget((DropTarget) folder);
1849 } else {
1850 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1851 folder.getParent() + ").");
1852 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001853 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001854 }
1855
1856 public void closeFolder() {
1857 Folder folder = mWorkspace.getOpenFolder();
1858 if (folder != null) {
1859 closeFolder(folder);
1860 }
1861 }
1862
1863 void closeFolder(Folder folder) {
1864 folder.getInfo().opened = false;
1865
1866 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1867 if (parent != null) {
1868 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1869 shrinkAndFadeInFolderIcon(fi);
1870 }
1871 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
1873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001875 if (mState != State.WORKSPACE) {
1876 return false;
1877 }
1878
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 return false;
1881 }
1882
1883 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001884 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
Michael Jurka0280c3b2010-09-17 15:00:07 -07001887 resetAddInfo();
1888 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001890 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return true;
1892 }
1893
Winson Chung3d503fb2011-07-13 17:25:49 -07001894 // The hotseat touch handling does not go through Workspace, and we always allow long press
1895 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001896 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001897 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1898 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001899 if (itemUnderLongClick == null) {
1900 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001901 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1902 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001903 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001905 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001907 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909 }
1910 }
1911 return true;
1912 }
1913
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 boolean isHotseatLayout(View layout) {
1915 return mHotseat != null && layout != null &&
1916 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1917 }
1918 Hotseat getHotseat() {
1919 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001920 }
1921
Winson Chung3d503fb2011-07-13 17:25:49 -07001922 /**
1923 * Returns the CellLayout of the specified container at the specified screen.
1924 */
1925 CellLayout getCellLayout(long container, int screen) {
1926 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1927 if (mHotseat != null) {
1928 return mHotseat.getLayout();
1929 } else {
1930 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001931 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 } else {
1933 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001934 }
1935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 Workspace getWorkspace() {
1938 return mWorkspace;
1939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 @Override
1942 protected Dialog onCreateDialog(int id) {
1943 switch (id) {
1944 case DIALOG_CREATE_SHORTCUT:
1945 return new CreateShortcut().createDialog();
1946 case DIALOG_RENAME_FOLDER:
1947 return new RenameFolder().createDialog();
1948 }
1949
1950 return super.onCreateDialog(id);
1951 }
1952
1953 @Override
1954 protected void onPrepareDialog(int id, Dialog dialog) {
1955 switch (id) {
1956 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 break;
1958 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001959 if (mFolderInfo != null) {
1960 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1961 final CharSequence text = mFolderInfo.title;
1962 input.setText(text);
1963 input.setSelection(0, text.length());
1964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 break;
1966 }
1967 }
1968
1969 void showRenameDialog(FolderInfo info) {
1970 mFolderInfo = info;
1971 mWaitingForResult = true;
1972 showDialog(DIALOG_RENAME_FOLDER);
1973 }
1974
Adam Cohenfbba09b2011-07-18 21:43:05 -07001975 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001976 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001977 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1978 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 mWaitingForResult = true;
1980 showDialog(DIALOG_CREATE_SHORTCUT);
1981 }
1982
1983 private class RenameFolder {
1984 private EditText mInput;
1985
1986 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1988 mInput = (EditText) layout.findViewById(R.id.folder_name);
1989
1990 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1991 builder.setIcon(0);
1992 builder.setTitle(getString(R.string.rename_folder_title));
1993 builder.setCancelable(true);
1994 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1995 public void onCancel(DialogInterface dialog) {
1996 cleanup();
1997 }
1998 });
1999 builder.setNegativeButton(getString(R.string.cancel_action),
2000 new Dialog.OnClickListener() {
2001 public void onClick(DialogInterface dialog, int which) {
2002 cleanup();
2003 }
2004 }
2005 );
2006 builder.setPositiveButton(getString(R.string.rename_action),
2007 new Dialog.OnClickListener() {
2008 public void onClick(DialogInterface dialog, int which) {
2009 changeFolderName();
2010 }
2011 }
2012 );
2013 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002014
2015 final AlertDialog dialog = builder.create();
2016 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2017 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002018 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002019 mInput.requestFocus();
2020 InputMethodManager inputManager = (InputMethodManager)
2021 getSystemService(Context.INPUT_METHOD_SERVICE);
2022 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002023 }
2024 });
2025
2026 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
2029 private void changeFolderName() {
2030 final String name = mInput.getText().toString();
2031 if (!TextUtils.isEmpty(name)) {
2032 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002033 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 mFolderInfo.title = name;
2035 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2036
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002038 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 } else {
2041 final FolderIcon folderIcon = (FolderIcon)
2042 mWorkspace.getViewForTag(mFolderInfo);
2043 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002044 // TODO: At some point we'll probably want some version of setting
2045 // the text for a folder icon.
2046 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 getWorkspace().requestLayout();
2048 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002049 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 mWorkspaceLoading = true;
2051 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 }
2053 }
2054 }
2055 cleanup();
2056 }
2057
2058 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 dismissDialog(DIALOG_RENAME_FOLDER);
2060 mWaitingForResult = false;
2061 mFolderInfo = null;
2062 }
2063 }
2064
Daniel Sandler843e8602010-06-07 14:59:01 -04002065 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2066 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002067 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002068 }
2069
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002070 // AllAppsView.Watcher
2071 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002072 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002073 mWorkspace.setVisibility(View.GONE);
2074 }
2075 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002076
2077 /**
2078 * Helper method for the cameraZoomIn/cameraZoomOut animations
2079 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002080 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002081 * @param scaleFactor The scale factor used for the zoom
2082 */
2083 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2084 final int height = view.getHeight();
2085 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002086 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002087 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002088
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002089 void updateWallpaperVisibility(boolean visible) {
2090 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2091 int curflags = getWindow().getAttributes().flags
2092 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2093 if (wpflags != curflags) {
2094 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2095 }
2096 }
2097
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002098 /**
2099 * Zoom the camera out from the workspace to reveal 'toView'.
2100 * Assumes that the view to show is anchored at either the very top or very bottom
2101 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002102 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002103 */
Winson Chungc07918d2011-07-01 15:35:26 -07002104 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002105 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002106
Winson Chungf0ea4d32011-06-06 14:27:16 -07002107 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2108 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2109 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2110 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002111
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002112 setPivotsForZoom(toView, toState, scale);
2113
Michael Jurkad74c9842011-07-10 12:44:21 -07002114 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002115 mWorkspace.changeState(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002116 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002117
2118 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002119 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002120 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002121 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2122 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002123 ((View) toView.getParent()).invalidate();
2124 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002125 toView.setFastScaleX(a * scale + b * 1f);
2126 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002127 }
2128 });
Adam Cohen61033d32010-11-15 18:29:44 -08002129
Winson Chungf0ea4d32011-06-06 14:27:16 -07002130 toView.setVisibility(View.VISIBLE);
2131 toView.setFastAlpha(0f);
2132 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2133 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2134 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2135 public void onAnimationUpdate(float a, float b) {
2136 // don't need to invalidate because we do so above
2137 toView.setFastAlpha(a * 0f + b * 1f);
2138 }
2139 });
2140 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002141
Michael Jurkaabded662011-03-04 12:06:57 -08002142 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002143 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002144 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002145 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002146 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002147 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002148 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002149 // Prepare the position
2150 toView.setTranslationX(0.0f);
2151 toView.setTranslationY(0.0f);
2152 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002153 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002154 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002155 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002156 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002157 // If we don't set the final scale values here, if this animation is cancelled
2158 // it will have the wrong scale value and subsequent cameraPan animations will
2159 // not fix that
2160 toView.setScaleX(1.0f);
2161 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002162 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002163 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002164 }
Winson Chung32174c82011-07-19 15:47:55 -07002165
2166 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2167 // Hide the workspace scrollbar
2168 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002169 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002170 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002171 updateWallpaperVisibility(false);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002172 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002173 });
2174
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002175 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002176 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002177
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002178 if (mStateAnimation != null) mStateAnimation.cancel();
2179 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002180 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002181 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002182 } else {
2183 toView.setTranslationX(0.0f);
2184 toView.setTranslationY(0.0f);
2185 toView.setScaleX(1.0f);
2186 toView.setScaleY(1.0f);
2187 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002188 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002189 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002190 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2191 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002192
Winson Chungc07918d2011-07-01 15:35:26 -07002193 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2194 // Hide the workspace scrollbar
2195 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002196 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002197 }
Michael Jurkaabded662011-03-04 12:06:57 -08002198 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002199 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002200 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002201 }
2202
2203 /**
2204 * Zoom the camera back into the workspace, hiding 'fromView'.
2205 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002206 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002207 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002208 */
Winson Chungc07918d2011-07-01 15:35:26 -07002209 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002210 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002211
Winson Chungf0ea4d32011-06-06 14:27:16 -07002212 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2213 final float scaleFactor = (float)
2214 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2215 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002216
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002217 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002218 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002219 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002220 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002221 if (mStateAnimation != null) mStateAnimation.cancel();
2222 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002223
Michael Jurka742574b2011-02-02 23:51:01 -08002224 final float oldScaleX = fromView.getScaleX();
2225 final float oldScaleY = fromView.getScaleY();
2226
2227 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2228 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002229 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2230 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002231 ((View)fromView.getParent()).fastInvalidate();
2232 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2233 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2234 }
2235 });
Michael Jurkaabded662011-03-04 12:06:57 -08002236 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002237 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002238 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002239 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2240 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002241 // don't need to invalidate because we do so above
2242 fromView.setFastAlpha(a * 1f + b * 0f);
2243 }
2244 });
Michael Jurkaabded662011-03-04 12:06:57 -08002245 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002246 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002247 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002248 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002249 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002250 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002251 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002252 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002253 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002254 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002255 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002256 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002257 }
2258 });
2259
Winson Chungf0ea4d32011-06-06 14:27:16 -07002260 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002261 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002262 } else {
2263 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002264 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002265 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2266 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002267 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002268 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002269 }
2270
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002271 void showWorkspace(boolean animated) {
Adam Cohen7777d962011-08-18 18:58:38 -07002272 mWorkspace.changeState(Workspace.State.NORMAL, animated);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002273 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002274 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002275 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002276
Adam Cohen7777d962011-08-18 18:58:38 -07002277 mWorkspace.showDockDivider(!animated);
2278 mWorkspace.flashScrollingIndicator();
2279
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002280 // Change the state *after* we've called all the transition code
2281 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002282
Winson Chung5fb63472011-02-02 17:03:37 -08002283 // Resume the auto-advance of widgets
2284 mUserPresent = true;
2285 updateRunning();
2286
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002287 // send an accessibility event to announce the context change
2288 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002289 }
2290
Adam Cohen7777d962011-08-18 18:58:38 -07002291 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002292 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002293 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002294 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002295 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002296 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002297 }
Adam Cohen7777d962011-08-18 18:58:38 -07002298
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002299 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chunge7a03942011-08-05 15:05:12 -07002300 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002301 @Override
2302 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002303 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002304 // Before we show workspace, hide all apps again because
2305 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2306 // clean up our state transition functions
2307 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002308 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002309 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002310 } else {
2311 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002312 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002313 }
2314 }, (extendedDelay ?
2315 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2316 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2317 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002318 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002319 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002320 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2321 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002322 }
2323 // Otherwise, we are not in spring loaded mode, so don't do anything.
2324 }
2325
Adam Cohenfc53cd22011-07-20 15:45:11 -07002326 public boolean isAllAppsCustomizeOpen() {
2327 return mState == State.APPS_CUSTOMIZE;
2328 }
2329
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002334 if (!LauncherApplication.isScreenLarge()) {
2335 if (animated) {
2336 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002338 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002339 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 }
2341 }
2342 }
2343
2344 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002345 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002346 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002347 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 if (!LauncherApplication.isScreenLarge()) {
2349 if (animated) {
2350 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002352 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002354 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002355 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002356 }
2357
Winson Chung785d2eb2011-04-14 16:08:02 -07002358 void showAllApps(boolean animated) {
2359 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002360
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2362 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002363
Winson Chung3d503fb2011-07-13 17:25:49 -07002364 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002365 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002366
Winson Chungf0ea4d32011-06-06 14:27:16 -07002367 // Change the state *after* we've called all the transition code
2368 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002369
2370 // Pause the auto-advance of widgets until we are out of AllApps
2371 mUserPresent = false;
2372 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002373 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002374
2375 // Send an accessibility event to announce the context change
2376 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2377 }
2378
Joe Onoratob2061212009-11-24 16:13:54 -05002379 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002380 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002381 * - Home from workspace
2382 * - from center screen
2383 * - from other screens
2384 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002385 * - from center screen
2386 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002387 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002388 * - from center screen
2389 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002390 * - Launch app from workspace and quit
2391 * - with back
2392 * - with home
2393 * - Launch app from all apps and quit
2394 * - with back
2395 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002396 * - Go to a screen that's not the default, then all
2397 * apps, and launch and app, and go back
2398 * - with back
2399 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002400 * - On workspace, long press power and go back
2401 * - with back
2402 * - with home
2403 * - On all apps, long press power and go back
2404 * - with back
2405 * - with home
2406 * - On workspace, power off
2407 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002408 * - Launch an app and turn off the screen while in that app
2409 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002410 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002411 * - From all apps
2412 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002413 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2414 * - From all apps
2415 * - From the center workspace
2416 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002417 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002418 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002419 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2420 mWorkspace.setVisibility(View.VISIBLE);
2421 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002422
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002425
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 if (mAllAppsButton != null) {
2428 mAllAppsButton.requestFocus();
2429 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002430 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002431 }
2432
Joe Onorato7c312c12009-08-13 21:36:53 -07002433 void lockAllApps() {
2434 // TODO
2435 }
2436
2437 void unlockAllApps() {
2438 // TODO
2439 }
2440
Patrick Dubroy5f445422011-02-18 14:35:21 -08002441 /**
2442 * Add an item from all apps or customize onto the given workspace screen.
2443 * If layout is null, add to the current screen.
2444 */
2445 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002446 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2447 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002448 } else {
2449 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002450 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002451 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002452
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002453 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002454 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002455 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002456 // Look for the toolbar icon specified in the activity meta-data
2457 Bundle metaData = packageManager.getActivityInfo(
2458 activityName, PackageManager.GET_META_DATA).metaData;
2459 if (metaData != null) {
2460 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2461 if (iconResId != 0) {
2462 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002463 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002464 }
2465 }
2466 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002467 // This can happen if the activity defines an invalid drawable
2468 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2469 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002470 } catch (Resources.NotFoundException nfe) {
2471 // This can happen if the activity defines an invalid drawable
2472 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2473 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002474 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002475 return null;
2476 }
2477
2478 // if successful in getting icon, return it; otherwise, set button to use default drawable
2479 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2480 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2481 TextView button = (TextView) findViewById(buttonId);
2482 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002483 Resources r = getResources();
2484 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2485 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002486
2487 // If we were unable to find the icon via the meta-data, use a generic one
2488 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002489 toolbarIcon = r.getDrawable(fallbackDrawableId);
2490 toolbarIcon.setBounds(0, 0, w, h);
2491 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002492 return null;
2493 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002494 toolbarIcon.setBounds(0, 0, w, h);
2495 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002496 return toolbarIcon.getConstantState();
2497 }
2498 }
2499
2500 // if successful in getting icon, return it; otherwise, set button to use default drawable
2501 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2502 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2503 ImageView button = (ImageView) findViewById(buttonId);
2504 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2505
Michael Jurka19e0fc52011-07-22 18:00:21 -07002506 if (button != null) {
2507 // If we were unable to find the icon via the meta-data, use a
2508 // generic one
2509 if (toolbarIcon == null) {
2510 button.setImageResource(fallbackDrawableId);
2511 } else {
2512 button.setImageDrawable(toolbarIcon);
2513 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002514 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002515
2516 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2517
Michael Jurka0423dcf2010-10-05 14:56:18 -07002518 }
2519
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002520 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2521 TextView button = (TextView) findViewById(buttonId);
2522 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2523 }
2524
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002525 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002526 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002527 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002528 }
2529
Michael Jurka0423dcf2010-10-05 14:56:18 -07002530 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002531 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2532 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002533 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002534
Winson Chung1cad91e2011-05-25 17:41:01 -07002535 final SearchManager searchManager =
2536 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2537 ComponentName activityName = searchManager.getGlobalSearchActivity();
2538 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002539 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002540 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002541 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002542 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002543 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002544 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002545 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002546 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002547 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002548 }
2549 }
2550
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002551 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002552 updateButtonWithDrawable(R.id.search_button, d);
2553 }
2554
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002555 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002556 final View searchDivider = findViewById(R.id.search_divider);
2557 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002558
Winson Chung1cad91e2011-05-25 17:41:01 -07002559 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2560 ComponentName activityName = intent.resolveActivity(getPackageManager());
2561 if (activityName != null) {
2562 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002563 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2564 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002565 voiceButton.setVisibility(View.VISIBLE);
2566 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002567 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002568 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002569 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002570 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002571
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002572 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002573 updateButtonWithDrawable(R.id.voice_button, d);
2574 }
2575
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002576 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002577 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002578 */
2579 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002580 final View marketButton = findViewById(R.id.market_button);
2581 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2582 // Find the app market activity by resolving an intent.
2583 // (If multiple app markets are installed, it will return the ResolverActivity.)
2584 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002585 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002586 mAppMarketIntent = intent;
2587 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002588 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002589 marketButton.setVisibility(View.VISIBLE);
2590 } else {
2591 // We should hide and disable the view so that we don't try and restore the visibility
2592 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2593 marketButton.setVisibility(View.GONE);
2594 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002595 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002596 }
2597
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002598 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002599 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002600 }
2601
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002602 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 * Displays the shortcut creation dialog and launches, if necessary, the
2604 * appropriate activity.
2605 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002606 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002607 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2608 DialogInterface.OnShowListener {
2609
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002610 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002612 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002613 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002614
Winson Chung55b65502011-05-26 12:03:43 -07002615 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2616 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002617 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002619 AlertDialog dialog = builder.create();
2620 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002621 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002622 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002624 return dialog;
2625 }
2626
2627 public void onCancel(DialogInterface dialog) {
2628 mWaitingForResult = false;
2629 cleanup();
2630 }
2631
Romain Guycbb89e42009-06-08 15:52:54 -07002632 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002633 mWaitingForResult = false;
2634 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002635 }
2636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002637 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002638 try {
2639 dismissDialog(DIALOG_CREATE_SHORTCUT);
2640 } catch (Exception e) {
2641 // An exception is thrown if the dialog is not visible, which is fine
2642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002643 }
2644
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002645 /**
2646 * Handle the action clicked in the "Add to home" dialog.
2647 */
2648 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002649 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002650
Winson Chung55b65502011-05-26 12:03:43 -07002651 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2652 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002653 case AddAdapter.ITEM_APPLICATION: {
2654 if (mAppsCustomizeTabHost != null) {
2655 mAppsCustomizeTabHost.selectAppsTab();
2656 }
2657 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002658 break;
2659 }
Winson Chung55b65502011-05-26 12:03:43 -07002660 case AddAdapter.ITEM_APPWIDGET: {
2661 if (mAppsCustomizeTabHost != null) {
2662 mAppsCustomizeTabHost.selectWidgetsTab();
2663 }
2664 showAllApps(true);
2665 break;
2666 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002667 case AddAdapter.ITEM_WALLPAPER: {
2668 startWallpaper();
2669 break;
2670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002671 }
2672 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002673
2674 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002675 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002676 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002677 }
2678
2679 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002680 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002681 */
2682 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2683 @Override
2684 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002685 closeSystemDialogs();
2686 }
2687 }
2688
2689 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002690 * Receives notifications whenever the appwidgets are reset.
2691 */
2692 private class AppWidgetResetObserver extends ContentObserver {
2693 public AppWidgetResetObserver() {
2694 super(new Handler());
2695 }
2696
2697 @Override
2698 public void onChange(boolean selfChange) {
2699 onAppWidgetReset();
2700 }
2701 }
2702
2703 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002704 * If the activity is currently paused, signal that we need to re-run the loader
2705 * in onResume.
2706 *
2707 * This needs to be called from incoming places where resources might have been loaded
2708 * while we are paused. That is becaues the Configuration might be wrong
2709 * when we're not running, and if it comes back to what it was when we
2710 * were paused, we are not restarted.
2711 *
2712 * Implementation of the method from LauncherModel.Callbacks.
2713 *
2714 * @return true if we are currently paused. The caller might be able to
2715 * skip some work in that case since we will come back again.
2716 */
2717 public boolean setLoadOnResume() {
2718 if (mPaused) {
2719 Log.i(TAG, "setLoadOnResume");
2720 mOnResumeNeedsLoad = true;
2721 return true;
2722 } else {
2723 return false;
2724 }
2725 }
2726
2727 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002728 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002731 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002732 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002733 } else {
2734 return SCREEN_COUNT / 2;
2735 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002736 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002737
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002738
Joe Onorato9c1289c2009-08-17 11:03:03 -04002739 /**
2740 * Refreshes the shortcuts shown on the workspace.
2741 *
2742 * Implementation of the method from LauncherModel.Callbacks.
2743 */
2744 public void startBinding() {
2745 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002746
2747 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002748 int count = workspace.getChildCount();
2749 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002750 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002751 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2752 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002754 if (mHotseat != null) {
2755 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002757
Adam Cohen4eac29a2011-07-11 17:53:37 -07002758 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002759 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 }
2761
2762 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002763 * Bind the items start-end from the list.
2764 *
2765 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002767 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002768 setLoadOnResume();
2769
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002771 for (int i=start; i<end; i++) {
2772 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002773
2774 // Short circuit if we are loading dock items for a configuration which has no dock
2775 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2776 mHotseat == null) {
2777 continue;
2778 }
2779
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 switch (item.itemType) {
2781 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2782 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002783 View shortcut = createShortcut((ShortcutInfo)item);
2784 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2785 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002787 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002788 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002789 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002790 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002791 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2792 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002795 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002796 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 }
2798
Joe Onorato9c1289c2009-08-17 11:03:03 -04002799 /**
2800 * Implementation of the method from LauncherModel.Callbacks.
2801 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002802 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002803 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002804 sFolders.clear();
2805 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002806 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002807
2808 /**
2809 * Add the views for a widget to the workspace.
2810 *
2811 * Implementation of the method from LauncherModel.Callbacks.
2812 */
2813 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002814 setLoadOnResume();
2815
Daniel Sandler843e8602010-06-07 14:59:01 -04002816 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2817 if (DEBUG_WIDGETS) {
2818 Log.d(TAG, "bindAppWidget: " + item);
2819 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 final Workspace workspace = mWorkspace;
2821
2822 final int appWidgetId = item.appWidgetId;
2823 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002824 if (DEBUG_WIDGETS) {
2825 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2826 }
2827
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2829
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2831 item.hostView.setTag(item);
2832
Winson Chung3d503fb2011-07-13 17:25:49 -07002833 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 item.cellY, item.spanX, item.spanY, false);
2835
Adam Cohended9f8d2010-11-03 13:25:16 -07002836 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2837
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 workspace.requestLayout();
2839
Daniel Sandler843e8602010-06-07 14:59:01 -04002840 if (DEBUG_WIDGETS) {
2841 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2842 + (SystemClock.uptimeMillis()-start) + "ms");
2843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 }
2845
2846 /**
2847 * Callback saying that there aren't any more items to bind.
2848 *
2849 * Implementation of the method from LauncherModel.Callbacks.
2850 */
2851 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002852 setLoadOnResume();
2853
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 if (mSavedState != null) {
2855 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002856 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 mSavedState = null;
2859 }
2860
2861 if (mSavedInstanceState != null) {
2862 super.onRestoreInstanceState(mSavedInstanceState);
2863 mSavedInstanceState = null;
2864 }
2865
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002867
2868 // If we received the result of any pending adds while the loader was running (e.g. the
2869 // widget configuration forced an orientation change), process them now.
2870 for (int i = 0; i < sPendingAddList.size(); i++) {
2871 completeAdd(sPendingAddList.get(i));
2872 }
2873 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002874 }
2875
2876 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002877 * Updates the icons on the launcher that are affected by changes to the package list
2878 * on the device.
2879 */
2880 private void updateIconsAffectedByPackageManagerChanges() {
2881 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002882 updateVoiceSearchIcon();
2883 }
2884
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002885 @Override
2886 public void bindSearchablesChanged() {
2887 updateGlobalSearchIcon();
2888 }
2889
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002890 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 * Add the icons for all apps.
2892 *
2893 * Implementation of the method from LauncherModel.Callbacks.
2894 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002895 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2896 // Remove the progress bar entirely; we could also make it GONE
2897 // but better to remove it since we know it's not going to be used
2898 View progressBar = mAppsCustomizeTabHost.
2899 findViewById(R.id.apps_customize_progress_bar);
2900 if (progressBar != null) {
2901 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002902 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002903 // We just post the call to setApps so the user sees the progress bar
2904 // disappear-- otherwise, it just looks like the progress bar froze
2905 // which doesn't look great
2906 mAppsCustomizeTabHost.post(new Runnable() {
2907 public void run() {
2908 if (mAppsCustomizeContent != null) {
2909 mAppsCustomizeContent.setApps(apps);
2910 }
2911 }
2912 });
2913
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002914 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002915 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 }
2917
2918 /**
2919 * A package was installed.
2920 *
2921 * Implementation of the method from LauncherModel.Callbacks.
2922 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002923 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002924 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002926
Winson Chung785d2eb2011-04-14 16:08:02 -07002927 if (mAppsCustomizeContent != null) {
2928 mAppsCustomizeContent.addApps(apps);
2929 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002930 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 }
2932
2933 /**
2934 * A package was updated.
2935 *
2936 * Implementation of the method from LauncherModel.Callbacks.
2937 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002938 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002939 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002941 if (mWorkspace != null) {
2942 mWorkspace.updateShortcuts(apps);
2943 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002944
Winson Chung785d2eb2011-04-14 16:08:02 -07002945 if (mAppsCustomizeContent != null) {
2946 mAppsCustomizeContent.updateApps(apps);
2947 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002948 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 }
2950
2951 /**
2952 * A package was uninstalled.
2953 *
2954 * Implementation of the method from LauncherModel.Callbacks.
2955 */
Joe Onorato36115782010-06-17 13:28:48 -04002956 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002958 if (permanent) {
2959 mWorkspace.removeItems(apps);
2960 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002961
Winson Chung785d2eb2011-04-14 16:08:02 -07002962 if (mAppsCustomizeContent != null) {
2963 mAppsCustomizeContent.removeApps(apps);
2964 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002965 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 }
Joe Onoratobe386092009-11-17 17:32:16 -08002967
2968 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002969 * A number of packages were updated.
2970 */
2971 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002972
Winson Chung785d2eb2011-04-14 16:08:02 -07002973 if (mAppsCustomizeContent != null) {
2974 mAppsCustomizeContent.onPackagesUpdated();
2975 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002976 }
2977
Winson Chung400438b2011-01-16 17:53:48 -08002978 private int mapConfigurationOriActivityInfoOri(int configOri) {
2979 final Display d = getWindowManager().getDefaultDisplay();
2980 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2981 switch (d.getRotation()) {
2982 case Surface.ROTATION_0:
2983 case Surface.ROTATION_180:
2984 // We are currently in the same basic orientation as the natural orientation
2985 naturalOri = configOri;
2986 break;
2987 case Surface.ROTATION_90:
2988 case Surface.ROTATION_270:
2989 // We are currently in the other basic orientation to the natural orientation
2990 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2991 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2992 break;
2993 }
2994
2995 int[] oriMap = {
2996 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2997 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2998 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2999 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3000 };
3001 // Since the map starts at portrait, we need to offset if this device's natural orientation
3002 // is landscape.
3003 int indexOffset = 0;
3004 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3005 indexOffset = 1;
3006 }
3007 return oriMap[(d.getRotation() + indexOffset) % 4];
3008 }
3009 public void lockScreenOrientation() {
3010 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3011 .getConfiguration().orientation));
3012 }
3013 public void unlockScreenOrientation() {
3014 mHandler.postDelayed(new Runnable() {
3015 public void run() {
3016 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3017 }
3018 }, mRestoreScreenOrientationDelay);
3019 }
3020
Winson Chung82f55532011-08-09 14:14:23 -07003021 /* Cling related */
3022 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3023 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3024 private void enableClingsIfNecessary() {
3025 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3026 if (LauncherApplication.isScreenLarge()) return;
3027
Brett Chabot2a9e2282011-08-23 15:03:13 -07003028 // disable clings when running in a test harness
3029 if(ActivityManager.isRunningInTestHarness()) return;
3030
Winson Chung82f55532011-08-09 14:14:23 -07003031 // Enable the clings only if they have not been dismissed before
3032 SharedPreferences prefs =
3033 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3034 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3035 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3036 cling.init(this);
3037 cling.setVisibility(View.VISIBLE);
3038 }
3039 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3040 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3041 cling.init(this);
3042 cling.setVisibility(View.VISIBLE);
3043 }
3044 }
3045 private void dismissCling(final Cling cling, final String flag) {
3046 if (cling != null) {
3047 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3048 anim.setDuration(DISMISS_CLING_DURATION);
3049 anim.addListener(new AnimatorListenerAdapter() {
3050 public void onAnimationEnd(Animator animation) {
3051 cling.setVisibility(View.GONE);
3052 cling.cleanup();
3053 SharedPreferences prefs =
3054 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3055 SharedPreferences.Editor editor = prefs.edit();
3056 editor.putBoolean(flag, true);
3057 editor.commit();
3058 };
3059 });
3060 anim.start();
3061 }
3062 }
3063 public void dismissWorkspaceCling(View v) {
3064 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3065 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3066 }
3067 public void dismissAllAppsCling(View v) {
3068 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3069 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3070 }
3071
Winson Chung80baf5a2010-08-09 16:03:15 -07003072 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003073 * Prints out out state for debugging.
3074 */
3075 public void dumpState() {
3076 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003077 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003078 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3079 Log.d(TAG, "mRestoring=" + mRestoring);
3080 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3081 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003082 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003083 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003084
Winson Chung785d2eb2011-04-14 16:08:02 -07003085 if (mAppsCustomizeContent != null) {
3086 mAppsCustomizeContent.dumpState();
3087 }
Joe Onoratobe386092009-11-17 17:32:16 -08003088 Log.d(TAG, "END launcher2 dump state");
3089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003090}
Michael Jurka2763be32011-02-24 11:19:57 -08003091
Michael Jurkaabded662011-03-04 12:06:57 -08003092interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003093 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3094 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003095}