blob: 255bf06d650be9640b8203b800c030d87c5bb94a [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 Chungdff8ebb2011-09-08 17:25:31 -070078import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070079import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070080import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080082import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070084import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070086import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070087import android.widget.TextView;
88import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070089
Adam Cohendf2cc412011-04-27 16:56:57 -070090import com.android.common.Search;
91import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070092import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080093
Adam Cohenc0dcf592011-06-01 15:30:43 -070094import java.io.DataInputStream;
95import java.io.DataOutputStream;
96import java.io.FileNotFoundException;
97import java.io.IOException;
98import java.util.ArrayList;
99import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800101/**
102 * Default launcher application.
103 */
Michael Jurka946ad472010-07-09 18:05:18 -0700104public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700105 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
106 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800107 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700108 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400111 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700112
Winson Chung70d72102011-08-12 11:24:35 -0700113 private static final int MENU_GROUP_WALLPAPER = 1;
114 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
115 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700116 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
117 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
119 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700120 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int REQUEST_PICK_APPLICATION = 6;
122 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700123 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700124 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
126 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
127
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800128 static final int SCREEN_COUNT = 5;
129 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
Joe Onorato7c312c12009-08-13 21:36:53 -0700131 static final int DIALOG_CREATE_SHORTCUT = 1;
132 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
Romain Guy98d01652009-06-30 16:21:04 -0700134 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 // Type: int
137 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700138 // Type: int
139 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700141 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
142 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
144 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: boolean
149 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
150 // Type: long
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
152
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700153 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
154
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700155 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700156 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700157 private State mState = State.WORKSPACE;
158 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700159
Joe Onorato9c1289c2009-08-17 11:03:03 -0400160 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700161 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
162 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700163 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800168 private final BroadcastReceiver mCloseSystemDialogsReceiver
169 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800170 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private LayoutInflater mInflater;
173
Joe Onorato00acb122009-08-04 16:04:30 -0400174 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700176
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700177 private AppWidgetManager mAppWidgetManager;
178 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
Michael Jurkac9d95c52011-08-29 14:03:34 -0700180 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700181 private int[] mTmpAddItemCellCoordinates = new int[2];
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private FolderInfo mFolderInfo;
184
Winson Chung3d503fb2011-07-13 17:25:49 -0700185 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800186 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700187
Winson Chungf0ea4d32011-06-06 14:27:16 -0700188 private SearchDropTargetBar mSearchDeleteBar;
189 private AppsCustomizeTabHost mAppsCustomizeTabHost;
190 private AppsCustomizePagedView mAppsCustomizeContent;
191 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private Bundle mSavedState;
194
195 private SpannableStringBuilder mDefaultKeySsb = null;
196
Joe Onorato9c1289c2009-08-17 11:03:03 -0400197 private boolean mWorkspaceLoading = true;
198
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800199 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private boolean mRestoring;
201 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700202 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
204 private Bundle mSavedInstanceState;
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800207 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800208 private boolean mUserPresent = true;
209 private boolean mVisible = false;
210 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700212 private static LocaleConfiguration sLocaleConfiguration = null;
213
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700214 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700216 private Intent mAppMarketIntent = null;
217
Adam Cohended9f8d2010-11-03 13:25:16 -0700218 // Related to the auto-advancing of widgets
219 private final int ADVANCE_MSG = 1;
220 private final int mAdvanceInterval = 20000;
221 private final int mAdvanceStagger = 250;
222 private long mAutoAdvanceSentTime;
223 private long mAutoAdvanceTimeLeft = -1;
224 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
225 new HashMap<View, AppWidgetProviderInfo>();
226
Winson Chung400438b2011-01-16 17:53:48 -0800227 // Determines how long to wait after a rotation before restoring the screen orientation to
228 // match the sensor state.
229 private final int mRestoreScreenOrientationDelay = 500;
230
Michael Jurka4ef207b2010-11-29 17:05:45 -0800231 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700232 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
233 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
234 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
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 Chungdff8ebb2011-09-08 17:25:31 -0700308 int coi = getCurrentOrientationIndexForGlobalIcons();
309 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
310 sAppMarketIcon[coi] == null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700311 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100312 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700313 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700314 if (sGlobalSearchIcon[coi] != null) {
315 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700316 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700317 if (sVoiceSearchIcon[coi] != null) {
318 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700319 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700320 if (sAppMarketIcon[coi] != null) {
321 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 }
Romain Guycbb89e42009-06-08 15:52:54 -0700324
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700326 if (sLocaleConfiguration == null) {
327 new AsyncTask<Void, Void, LocaleConfiguration>() {
328 @Override
329 protected LocaleConfiguration doInBackground(Void... unused) {
330 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
331 readConfiguration(Launcher.this, localeConfiguration);
332 return localeConfiguration;
333 }
334
335 @Override
336 protected void onPostExecute(LocaleConfiguration result) {
337 sLocaleConfiguration = result;
338 checkForLocaleChange(); // recursive, but now with a locale configuration
339 }
340 }.execute();
341 return;
342 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 final Configuration configuration = getResources().getConfiguration();
345
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700346 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 final String locale = configuration.locale.toString();
348
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700349 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 final int mcc = configuration.mcc;
351
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700352 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 final int mnc = configuration.mnc;
354
Romain Guy84f296c2009-11-04 15:00:44 -0800355 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356
Romain Guy84f296c2009-11-04 15:00:44 -0800357 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 sLocaleConfiguration.locale = locale;
359 sLocaleConfiguration.mcc = mcc;
360 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700361
Joe Onorato0589f0f2010-02-08 13:44:00 -0800362 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700363
364 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
365 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700366 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700367 public void run() {
368 writeConfiguration(Launcher.this, localeConfiguration);
369 }
370 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700371 }
372 }
373
374 private static class LocaleConfiguration {
375 public String locale;
376 public int mcc = -1;
377 public int mnc = -1;
378 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700379
Romain Guy98d01652009-06-30 16:21:04 -0700380 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
381 DataInputStream in = null;
382 try {
383 in = new DataInputStream(context.openFileInput(PREFERENCES));
384 configuration.locale = in.readUTF();
385 configuration.mcc = in.readInt();
386 configuration.mnc = in.readInt();
387 } catch (FileNotFoundException e) {
388 // Ignore
389 } catch (IOException e) {
390 // Ignore
391 } finally {
392 if (in != null) {
393 try {
394 in.close();
395 } catch (IOException e) {
396 // Ignore
397 }
398 }
399 }
400 }
401
402 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
403 DataOutputStream out = null;
404 try {
405 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
406 out.writeUTF(configuration.locale);
407 out.writeInt(configuration.mcc);
408 out.writeInt(configuration.mnc);
409 out.flush();
410 } catch (FileNotFoundException e) {
411 // Ignore
412 } catch (IOException e) {
413 //noinspection ResultOfMethodCallIgnored
414 context.getFileStreamPath(PREFERENCES).delete();
415 } finally {
416 if (out != null) {
417 try {
418 out.close();
419 } catch (IOException e) {
420 // Ignore
421 }
422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 }
424 }
425
Adam Cohen716b51e2011-06-30 12:09:54 -0700426 public DragLayer getDragLayer() {
427 return mDragLayer;
428 }
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 static int getScreen() {
431 synchronized (sLock) {
432 return sScreen;
433 }
434 }
435
436 static void setScreen(int screen) {
437 synchronized (sLock) {
438 sScreen = screen;
439 }
440 }
441
Winson Chung557d6ed2011-07-08 15:34:52 -0700442 /**
443 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
444 * a configuration step, this allows the proper animations to run after other transitions.
445 */
446 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700447 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800448 switch (args.requestCode) {
449 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700450 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
451 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800452 break;
453 case REQUEST_PICK_SHORTCUT:
454 processShortcut(args.intent);
455 break;
456 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700457 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
458 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700459 result = true;
460 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800461 case REQUEST_PICK_APPWIDGET:
462 addAppWidgetFromPick(args.intent);
463 break;
464 case REQUEST_CREATE_APPWIDGET:
465 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700466 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700467 result = true;
468 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800469 case REQUEST_PICK_WALLPAPER:
470 // We just wanted the activity result here so we can clear mWaitingForResult
471 break;
472 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700473 // In any situation where we have a multi-step drop, we should reset the add info only after
474 // we complete the drop
475 resetAddInfo();
476 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800477 }
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800480 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700481 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700482 mWaitingForResult = false;
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 // The pattern used here is that a user PICKs a specific application,
485 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
488 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700489 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800490 final PendingAddArguments args = new PendingAddArguments();
491 args.requestCode = requestCode;
492 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700493 args.container = mPendingAddInfo.container;
494 args.screen = mPendingAddInfo.screen;
495 args.cellX = mPendingAddInfo.cellX;
496 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800497
498 // If the loader is still running, defer the add until it is done.
499 if (isWorkspaceLocked()) {
500 sPendingAddList.add(args);
501 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700502 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 }
Romain Guy18042c82009-11-06 11:44:55 -0800504 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700505 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
506 if (data != null) {
507 // Clean up the appWidgetId if we canceled
508 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
509 if (appWidgetId != -1) {
510 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800512 }
513 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700514
515 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700516 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 }
518
519 @Override
520 protected void onResume() {
521 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800522 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700523 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400524 mWorkspaceLoading = true;
525 mModel.startLoader(this, true);
526 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700527 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800529 if (mWaitingForResume != null) {
530 mWaitingForResume.setStayPressed(false);
531 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700532 // When we resume Launcher, a different Activity might be responsible for the app
533 // market intent, so refresh the icon
534 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 }
536
537 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700538 protected void onPause() {
539 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700540 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800541 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700542 }
Romain Guycbb89e42009-06-08 15:52:54 -0700543
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700544 @Override
545 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400546 // Flag the loader to stop early before switching
547 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700548 if (mAppsCustomizeContent != null) {
549 mAppsCustomizeContent.surrender();
550 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800551 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700552 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700553
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800554 // We can't hide the IME if it was forced open. So don't bother
555 /*
556 @Override
557 public void onWindowFocusChanged(boolean hasFocus) {
558 super.onWindowFocusChanged(hasFocus);
559
560 if (hasFocus) {
561 final InputMethodManager inputManager = (InputMethodManager)
562 getSystemService(Context.INPUT_METHOD_SERVICE);
563 WindowManager.LayoutParams lp = getWindow().getAttributes();
564 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
565 android.os.Handler()) {
566 protected void onReceiveResult(int resultCode, Bundle resultData) {
567 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
568 }
569 });
570 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
571 }
572 }
573 */
574
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800575 private boolean acceptFilter() {
576 final InputMethodManager inputManager = (InputMethodManager)
577 getSystemService(Context.INPUT_METHOD_SERVICE);
578 return !inputManager.isFullscreenMode();
579 }
580
581 @Override
582 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700583 final int uniChar = event.getUnicodeChar();
584 final boolean handled = super.onKeyDown(keyCode, event);
585 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
586 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
588 keyCode, event);
589 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700590 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700591 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700592 // showSearchDialog()
593 // If there are multiple keystrokes before the search dialog takes focus,
594 // onSearchRequested() will be called for every keystroke,
595 // but it is idempotent, so it's fine.
596 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598 }
599
Joe Onorato8a9625e2010-01-28 15:55:35 -0800600 // Eat the long press event so the keyboard doesn't come up.
601 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
602 return true;
603 }
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 return handled;
606 }
607
Karl Rosaen138a0412009-04-23 19:00:21 -0700608 private String getTypedText() {
609 return mDefaultKeySsb.toString();
610 }
611
612 private void clearTypedText() {
613 mDefaultKeySsb.clear();
614 mDefaultKeySsb.clearSpans();
615 Selection.setSelection(mDefaultKeySsb, 0);
616 }
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700619 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
620 * State
621 */
622 private static State intToState(int stateOrdinal) {
623 State state = State.WORKSPACE;
624 final State[] stateValues = State.values();
625 for (int i = 0; i < stateValues.length; i++) {
626 if (stateValues[i].ordinal() == stateOrdinal) {
627 state = stateValues[i];
628 break;
629 }
630 }
631 return state;
632 }
633
634 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 * Restores the previous state, if it exists.
636 *
637 * @param savedState The previous state.
638 */
639 private void restoreState(Bundle savedState) {
640 if (savedState == null) {
641 return;
642 }
643
Michael Jurka883f55b2010-10-21 15:47:14 -0700644 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700645 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800646 showAllApps(false);
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
650 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700651 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
653
Winson Chung3d503fb2011-07-13 17:25:49 -0700654 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
655 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656
Winson Chung3d503fb2011-07-13 17:25:49 -0700657 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
658 mPendingAddInfo.container = pendingAddContainer;
659 mPendingAddInfo.screen = pendingAddScreen;
660 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
661 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 mRestoring = true;
663 }
664
665 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
666 if (renameFolder) {
667 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700668 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669 mRestoring = true;
670 }
Winson Chunga12a2502010-12-20 14:41:35 -0800671
Winson Chung785d2eb2011-04-14 16:08:02 -0700672
673 // Restore the AppsCustomize tab
674 if (mAppsCustomizeTabHost != null) {
675 String curTab = savedState.getString("apps_customize_currentTab");
676 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700677 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700678 mAppsCustomizeContent.setContentType(
679 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
680 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700681 mAppsCustomizeContent.loadAssociatedPages(
682 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700683 }
684
Winson Chung5afbf7b2011-07-25 11:53:08 -0700685 int currentIndex = savedState.getInt("apps_customize_currentIndex");
686 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700687 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
689
690 /**
691 * Finds all the views we need and configure them properly.
692 */
693 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700694 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400695
Winson Chung4c98d922011-05-31 16:50:48 -0700696 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
697 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698
Winson Chung4c98d922011-05-31 16:50:48 -0700699 // Setup the drag layer
700 mDragLayer.setup(this, dragController);
701
Winson Chung3d503fb2011-07-13 17:25:49 -0700702 // Setup the hotseat
703 mHotseat = (Hotseat) findViewById(R.id.hotseat);
704 if (mHotseat != null) {
705 mHotseat.setup(this);
706 }
707
Winson Chung4c98d922011-05-31 16:50:48 -0700708 // Setup the workspace
709 mWorkspace.setHapticFeedbackEnabled(false);
710 mWorkspace.setOnLongClickListener(this);
711 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700712 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700713
Winson Chungf0ea4d32011-06-06 14:27:16 -0700714 // Get the search/delete bar
715 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700716
Winson Chungf0ea4d32011-06-06 14:27:16 -0700717 // Setup AppsCustomize
718 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
719 findViewById(R.id.apps_customize_pane);
720 mAppsCustomizeContent = (AppsCustomizePagedView)
721 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
722 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400723
Romain Guy1fbc1c82009-11-09 20:43:08 -0800724
Winson Chung4c98d922011-05-31 16:50:48 -0700725
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700727 dragController.setDragScoller(mWorkspace);
728 dragController.setScrollView(mDragLayer);
729 dragController.setMoveTarget(mWorkspace);
730 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700731 if (mSearchDeleteBar != null) {
732 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735
736 /**
737 * Creates a view representing a shortcut.
738 *
739 * @param info The data structure describing the shortcut.
740 *
741 * @return A View inflated from R.layout.application.
742 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800743 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700745 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747
748 /**
749 * Creates a view representing a shortcut inflated from the specified resource.
750 *
751 * @param layoutResId The id of the XML layout used to create the shortcut.
752 * @param parent The group the shortcut belongs to.
753 * @param info The data structure describing the shortcut.
754 *
755 * @return A View inflated from layoutResId.
756 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800757 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800758 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
759 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 return favorite;
762 }
763
764 /**
765 * Add an application shortcut to the workspace.
766 *
767 * @param data The intent describing the application.
768 * @param cellInfo The position on screen where to create the shortcut.
769 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700770 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700771 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700772 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700773
Adam Cohenfbba09b2011-07-18 21:43:05 -0700774 // First we check if we already know the exact location where we want to add this item.
775 if (cellX >= 0 && cellY >= 0) {
776 cellXY[0] = cellX;
777 cellXY[1] = cellY;
778 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700779 showOutOfSpaceMessage();
780 return;
781 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800783 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800784
Romain Guy73b979d2009-06-09 12:57:21 -0700785 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800786 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800788 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700790 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800791 } else {
792 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794 }
Romain Guycbb89e42009-06-08 15:52:54 -0700795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 /**
797 * Add a shortcut to the workspace.
798 *
799 * @param data The intent describing the shortcut.
800 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700802 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
803 int cellY) {
804 int[] cellXY = mTmpAddItemCellCoordinates;
805 int[] touchXY = mPendingAddInfo.dropPos;
806 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700807
Michael Jurkad3ef3062010-11-23 16:23:58 -0800808 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700809
Adam Cohen558baaf2011-08-15 15:22:57 -0700810 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
811 final View view = createShortcut(info);
812
Adam Cohenfbba09b2011-07-18 21:43:05 -0700813 // First we check if we already know the exact location where we want to add this item.
814 if (cellX >= 0 && cellY >= 0) {
815 cellXY[0] = cellX;
816 cellXY[1] = cellY;
817 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700818
819 // If appropriate, either create a folder or add to an existing folder
820 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
821 true, null,null)) {
822 return;
823 }
824 DragObject dragObject = new DragObject();
825 dragObject.dragInfo = info;
826 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
827 return;
828 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700829 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800830 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800832 foundCellSpan = (result != null);
833 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700834 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800835 }
836
837 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838 showOutOfSpaceMessage();
839 return;
840 }
841
Adam Cohen558baaf2011-08-15 15:22:57 -0700842 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843
844 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
846 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 }
848 }
849
Adam Cohen50370f32011-08-25 18:57:14 -0700850 class Padding {
851 int left = 0;
852 int right = 0;
853 int top = 0;
854 int bottom = 0;
855 }
856
Adam Cohenf814aa02011-09-01 13:48:05 -0700857 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700858 PackageManager packageManager = getPackageManager();
859 Padding p = new Padding();
860 android.content.pm.ApplicationInfo appInfo;
861
862 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700863 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700864 } catch (Exception e) {
865 // if we can't find the package, return 0 padding
866 return p;
867 }
868
869 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
870 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
871 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
872 Resources r = getResources();
873 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
874 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
875 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
876 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
877 }
878
879 return p;
880 }
881
Adam Cohenf814aa02011-09-01 13:48:05 -0700882 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
883 if (spanXY == null) {
884 spanXY = new int[2];
885 }
886
887 Padding padding = getPaddingForWidget(component);
888 // We want to account for the extra amount of padding that we are adding to the widget
889 // to ensure that it gets the full amount of space that it has requested
890 int requiredWidth = minWidth + padding.left + padding.right;
891 int requiredHeight = minHeight + padding.top + padding.bottom;
892 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
893 }
894
895 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
896 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
897 }
898
899 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
900 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
901 }
902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700904 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700906 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700907 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700909 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700911
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700912 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700913 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700914
Adam Cohenf814aa02011-09-01 13:48:05 -0700915 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700918 // We have saved the position to which the widget was dragged-- this really only matters
919 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700920 int[] cellXY = mTmpAddItemCellCoordinates;
921 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700922 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
924 cellXY[0] = mPendingAddInfo.cellX;
925 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700926 foundCellSpan = true;
927 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700928 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700929 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700930 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800931 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700932 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700933 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700934 }
935
Michael Jurka0280c3b2010-09-17 15:00:07 -0700936 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800937 if (appWidgetId != -1) {
938 // Deleting an app widget ID is a void call but writes to disk before returning
939 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800940 new Thread("deleteAppWidgetId") {
941 public void run() {
942 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
943 }
944 }.start();
945 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800947 return;
948 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700950 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700951 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700952 launcherInfo.spanX = spanXY[0];
953 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700954
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957
958 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700961
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700962 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700964
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400966 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700967
968 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 }
970 }
Romain Guycbb89e42009-06-08 15:52:54 -0700971
Adam Cohended9f8d2010-11-03 13:25:16 -0700972 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
973 @Override
974 public void onReceive(Context context, Intent intent) {
975 final String action = intent.getAction();
976 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
977 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700978 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700979 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700980
Winson Chungc100e8e2011-08-09 16:02:43 -0700981 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700982 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700983 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
984 mAppsCustomizeTabHost.reset();
985 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700986 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700987 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
988 mUserPresent = true;
989 updateRunning();
990 }
991 }
992 };
993
994 @Override
995 public void onAttachedToWindow() {
996 super.onAttachedToWindow();
997
998 // Listen for broadcasts related to user-presence
999 final IntentFilter filter = new IntentFilter();
1000 filter.addAction(Intent.ACTION_SCREEN_OFF);
1001 filter.addAction(Intent.ACTION_USER_PRESENT);
1002 registerReceiver(mReceiver, filter);
1003
Adam Cohend113e0c2010-11-11 10:48:05 -08001004 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001005 mVisible = true;
1006 }
1007
1008 @Override
1009 public void onDetachedFromWindow() {
1010 super.onDetachedFromWindow();
1011 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001012 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001013
Adam Cohend113e0c2010-11-11 10:48:05 -08001014 if (mAttached) {
1015 unregisterReceiver(mReceiver);
1016 mAttached = false;
1017 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001018 updateRunning();
1019 }
1020
1021 public void onWindowVisibilityChanged(int visibility) {
1022 mVisible = visibility == View.VISIBLE;
1023 updateRunning();
1024 }
1025
1026 private void sendAdvanceMessage(long delay) {
1027 mHandler.removeMessages(ADVANCE_MSG);
1028 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1029 mHandler.sendMessageDelayed(msg, delay);
1030 mAutoAdvanceSentTime = System.currentTimeMillis();
1031 }
1032
1033 private void updateRunning() {
1034 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1035 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1036 mAutoAdvanceRunning = autoAdvanceRunning;
1037 if (autoAdvanceRunning) {
1038 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1039 sendAdvanceMessage(delay);
1040 } else {
1041 if (!mWidgetsToAdvance.isEmpty()) {
1042 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1043 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1044 }
1045 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001046 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001047 }
1048 }
1049 }
1050
1051 private final Handler mHandler = new Handler() {
1052 @Override
1053 public void handleMessage(Message msg) {
1054 if (msg.what == ADVANCE_MSG) {
1055 int i = 0;
1056 for (View key: mWidgetsToAdvance.keySet()) {
1057 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1058 final int delay = mAdvanceStagger * i;
1059 if (v instanceof Advanceable) {
1060 postDelayed(new Runnable() {
1061 public void run() {
1062 ((Advanceable) v).advance();
1063 }
1064 }, delay);
1065 }
1066 i++;
1067 }
1068 sendAdvanceMessage(mAdvanceInterval);
1069 }
1070 }
1071 };
1072
1073 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001074 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001075 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1076 if (v instanceof Advanceable) {
1077 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001078 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001079 updateRunning();
1080 }
1081 }
1082
1083 void removeWidgetToAutoAdvance(View hostView) {
1084 if (mWidgetsToAdvance.containsKey(hostView)) {
1085 mWidgetsToAdvance.remove(hostView);
1086 updateRunning();
1087 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 }
1089
Joe Onorato9c1289c2009-08-17 11:03:03 -04001090 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001091 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 launcherInfo.hostView = null;
1093 }
1094
Adam Cohended9f8d2010-11-03 13:25:16 -07001095 void showOutOfSpaceMessage() {
1096 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1097 }
1098
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 public LauncherAppWidgetHost getAppWidgetHost() {
1100 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Winson Chunga9abd0e2010-10-27 17:18:37 -07001103 public LauncherModel getModel() {
1104 return mModel;
1105 }
1106
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001107 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001108 getWindow().closeAllPanels();
1109
Winson Chung87acb482011-08-23 17:28:05 -07001110 /**
1111 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001112 try {
1113 dismissDialog(DIALOG_CREATE_SHORTCUT);
1114 // Unlock the workspace if the dialog was showing
1115 } catch (Exception e) {
1116 // An exception is thrown if the dialog is not visible, which is fine
1117 }
1118
1119 try {
1120 dismissDialog(DIALOG_RENAME_FOLDER);
1121 // Unlock the workspace if the dialog was showing
1122 } catch (Exception e) {
1123 // An exception is thrown if the dialog is not visible, which is fine
1124 }
Winson Chung87acb482011-08-23 17:28:05 -07001125 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001126
1127 // Whatever we were doing is hereby canceled.
1128 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001129 }
1130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 @Override
1132 protected void onNewIntent(Intent intent) {
1133 super.onNewIntent(intent);
1134
1135 // Close the menu
1136 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001137 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001138 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001139
Adam Cohen95bb8002011-07-03 23:40:28 -07001140 closeFolder();
1141
Joe Onorato14f122b2009-11-19 14:06:36 -08001142 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1143 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001144
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001145 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001146 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001147 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001148 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001149 }
Winson Chungde1af762011-07-21 16:44:07 -07001150 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001151 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001152
Joe Onorato3a8820b2009-11-10 15:06:42 -08001153 final View v = getWindow().peekDecorView();
1154 if (v != null && v.getWindowToken() != null) {
1155 InputMethodManager imm = (InputMethodManager)getSystemService(
1156 INPUT_METHOD_SERVICE);
1157 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001159
Michael Jurka35aa14d2011-07-07 17:01:08 -07001160 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001161 if (mAppsCustomizeTabHost != null) {
1162 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001163 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
1165 }
1166
1167 @Override
1168 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1169 // Do not call super here
1170 mSavedInstanceState = savedInstanceState;
1171 }
1172
1173 @Override
1174 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001175 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001176 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177
Michael Jurka883f55b2010-10-21 15:47:14 -07001178 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001179 // We close any open folder since it will not be re-opened, and we need to make sure
1180 // this state is reflected.
1181 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1184 mWaitingForResult) {
1185 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1186 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1187 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1188 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 }
1190
1191 if (mFolderInfo != null && mWaitingForResult) {
1192 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1193 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1194 }
Michael Jurka946ad472010-07-09 18:05:18 -07001195
Winson Chung785d2eb2011-04-14 16:08:02 -07001196 // Save the current AppsCustomize tab
1197 if (mAppsCustomizeTabHost != null) {
1198 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1199 if (currentTabTag != null) {
1200 outState.putString("apps_customize_currentTab", currentTabTag);
1201 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001202 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1203 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001204 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206
1207 @Override
1208 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Winson Chunge7a03942011-08-05 15:05:12 -07001211 // Remove all pending runnables
1212 mHandler.removeMessages(ADVANCE_MSG);
1213 mHandler.removeMessages(0);
1214
Winson Chungcd2b0142011-06-08 16:02:26 -07001215 // Stop callbacks from LauncherModel
1216 LauncherApplication app = ((LauncherApplication) getApplication());
1217 mModel.stopLoader();
1218 app.setLauncher(null);
1219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001221 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001223 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001225 mAppWidgetHost = null;
1226
1227 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
1229 TextKeyListener.getInstance().release();
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231
Winson Chung3d503fb2011-07-13 17:25:49 -07001232 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001233
1234 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001235 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001236
1237 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1238 mWorkspace.removeAllViews();
1239 mWorkspace = null;
1240 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001241
1242 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
Adam Cohena9cf38f2011-05-02 15:36:58 -07001245 public DragController getDragController() {
1246 return mDragController;
1247 }
1248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 @Override
1250 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001251 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 super.startActivityForResult(intent, requestCode);
1253 }
1254
Winson Chung70d72102011-08-12 11:24:35 -07001255 /**
1256 * Indicates that we want global search for this activity by setting the globalSearch
1257 * argument for {@link #startSearch} to true.
1258 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001260 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001262
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001263 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001264
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 if (initialQuery == null) {
1266 // Use any text typed in the launcher as the initial query
1267 initialQuery = getTypedText();
1268 clearTypedText();
1269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 if (appSearchData == null) {
1271 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001272 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 final SearchManager searchManager =
1276 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001277 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001278 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
1280
Winson Chung70d72102011-08-12 11:24:35 -07001281 @Override
1282 public boolean onCreateOptionsMenu(Menu menu) {
1283 if (isWorkspaceLocked()) {
1284 return false;
1285 }
1286
1287 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001288
1289 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1290 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1291 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001292 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1293 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1294 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001295 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
1296 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1297 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1298
Winson Chung70d72102011-08-12 11:24:35 -07001299 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1300 .setIcon(android.R.drawable.ic_menu_gallery)
1301 .setAlphabeticShortcut('W');
1302 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1303 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001304 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001305 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001306 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1307 .setIcon(android.R.drawable.ic_menu_preferences)
1308 .setIntent(settings)
1309 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001310 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1311 .setIcon(android.R.drawable.ic_menu_help)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001312 .setIntent(help)
Winson Chung70d72102011-08-12 11:24:35 -07001313 .setAlphabeticShortcut('H');
1314 return true;
1315 }
1316
1317 @Override
1318 public boolean onPrepareOptionsMenu(Menu menu) {
1319 super.onPrepareOptionsMenu(menu);
1320
1321 if (mAppsCustomizeTabHost.isTransitioning()) {
1322 return false;
1323 }
1324 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1325 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1326
1327 return true;
1328 }
1329
1330 @Override
1331 public boolean onOptionsItemSelected(MenuItem item) {
1332 switch (item.getItemId()) {
1333 case MENU_WALLPAPER_SETTINGS:
1334 startWallpaper();
1335 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001336 }
1337
1338 return super.onOptionsItemSelected(item);
1339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001341 @Override
1342 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001343 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001344 // Use a custom animation for launching search
1345 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001346 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001347 }
1348
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 public boolean isWorkspaceLocked() {
1350 return mWorkspaceLoading || mWaitingForResult;
1351 }
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001354 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001355 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 }
1357
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 mPendingAddInfo.container = ItemInfo.NO_ID;
1360 mPendingAddInfo.screen = -1;
1361 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1362 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1363 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001365
Michael Jurkaaf442092010-06-10 17:01:57 -07001366 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001367 // TODO: catch bad widget exception when sent
1368 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001369 // TODO: Is this log message meaningful?
1370 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001371 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001372 }
1373
Winson Chung55cef262010-10-28 14:14:18 -07001374 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001375 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376
Bjorn Bringert7984c942009-12-09 15:38:25 +00001377 if (appWidget.configure != null) {
1378 // Launch over to configure widget, if needed
1379 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1380 intent.setComponent(appWidget.configure);
1381 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001382 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001383 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1384 intent.putExtra(
1385 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1386 info.mimeType);
1387
1388 final String mimeType = info.mimeType;
1389 final ClipData clipData = (ClipData) info.configurationData;
1390 final ClipDescription clipDesc = clipData.getDescription();
1391 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1392 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001393 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001394 final CharSequence stringData = item.getText();
1395 final Uri uriData = item.getUri();
1396 final Intent intentData = item.getIntent();
1397 final String key =
1398 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1399 if (uriData != null) {
1400 intent.putExtra(key, uriData);
1401 } else if (intentData != null) {
1402 intent.putExtra(key, intentData);
1403 } else if (stringData != null) {
1404 intent.putExtra(key, stringData);
1405 }
1406 break;
1407 }
1408 }
1409 }
Winson Chung55cef262010-10-28 14:14:18 -07001410 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001411
Romain Guy8e633c52010-03-04 12:51:36 -08001412 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001414 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001415 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001416
1417 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001418 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420 }
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Adam Cohenfbba09b2011-07-18 21:43:05 -07001422 /**
1423 * Process a shortcut drop.
1424 *
1425 * @param componentName The name of the component
1426 * @param screen The screen where it should be added
1427 * @param cell The cell it should be added to, optional
1428 * @param position The location on the screen where it was dropped, optional
1429 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001430 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1431 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001433 mPendingAddInfo.container = container;
1434 mPendingAddInfo.screen = screen;
1435 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001436
1437 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001438 mPendingAddInfo.cellX = cell[0];
1439 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001440 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441
1442 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1443 createShortcutIntent.setComponent(componentName);
1444 processShortcut(createShortcutIntent);
1445 }
1446
Adam Cohenfbba09b2011-07-18 21:43:05 -07001447 /**
1448 * Process a widget drop.
1449 *
1450 * @param info The PendingAppWidgetInfo of the widget being added.
1451 * @param screen The screen where it should be added
1452 * @param cell The cell it should be added to, optional
1453 * @param position The location on the screen where it was dropped, optional
1454 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001455 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1456 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 mPendingAddInfo.container = info.container = container;
1459 mPendingAddInfo.screen = info.screen = screen;
1460 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001462 mPendingAddInfo.cellX = cell[0];
1463 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001464 }
1465
1466 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1467 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1468 addAppWidgetImpl(appWidgetId, info);
1469 }
1470
Joe Onoratodeb98af2010-02-19 14:59:39 -08001471 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001472 // Handle case where user selected "Applications"
1473 String applicationName = getResources().getString(R.string.group_applications);
1474 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001475
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001476 if (applicationName != null && applicationName.equals(shortcutName)) {
1477 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1478 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001479
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001480 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1481 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001482 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001483 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001484 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001485 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
Winson Chung24ab2f12010-09-16 14:10:47 -07001489 void processWallpaper(Intent intent) {
1490 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1491 }
1492
Winson Chung3d503fb2011-07-13 17:25:49 -07001493 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1494 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001495 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 folderInfo.title = getText(R.string.folder_name);
1497
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1500 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001501 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502
1503 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001504 FolderIcon newFolder =
1505 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1506 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1507 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001508 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001512 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001513 }
1514
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 private void showNotifications() {
1516 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1517 if (statusBar != null) {
1518 statusBar.expand();
1519 }
1520 }
1521
1522 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001523 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001525 Intent chooser = Intent.createChooser(pickWallpaper,
1526 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001527 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1528 // Removed in Eclair MR1
1529// WallpaperManager wm = (WallpaperManager)
1530// getSystemService(Context.WALLPAPER_SERVICE);
1531// WallpaperInfo wi = wm.getWallpaperInfo();
1532// if (wi != null && wi.getSettingsActivity() != null) {
1533// LabeledIntent li = new LabeledIntent(getPackageName(),
1534// R.string.configure_wallpaper, 0);
1535// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1536// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1537// }
Mike Clerona0618e42009-10-22 13:55:21 -07001538 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001541 /**
1542 * Registers various content observers. The current implementation registers
1543 * only a favorites observer to keep track of the favorites applications.
1544 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001545 private void registerContentObservers() {
1546 ContentResolver resolver = getContentResolver();
1547 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1548 true, mWidgetObserver);
1549 }
1550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 @Override
1552 public boolean dispatchKeyEvent(KeyEvent event) {
1553 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1554 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001556 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001557 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001558 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001559 dumpState();
1560 return true;
1561 }
1562 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001563 }
1564 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1565 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001566 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 return true;
1568 }
1569 }
1570
1571 return super.dispatchKeyEvent(event);
1572 }
1573
Joe Onorato88ec0992009-11-19 13:16:06 -08001574 @Override
1575 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001576 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001577 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001578 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001579 Folder openFolder = mWorkspace.getOpenFolder();
1580 if (openFolder.isEditingName()) {
1581 openFolder.dismissEditingName();
1582 } else {
1583 closeFolder();
1584 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001585 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001586 mWorkspace.exitWidgetResizeMode();
1587
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001588 // Back button is a no-op here, but give at least some feedback for the button press
1589 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001590 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001591 }
1592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001594 * Re-listen when widgets are reset.
1595 */
1596 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001597 if (mAppWidgetHost != null) {
1598 mAppWidgetHost.startListening();
1599 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001600 }
1601
1602 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001603 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1604 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001607 if (mModel != null) {
1608 mModel.unbindWorkspaceItems();
1609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 /**
1613 * Launches the intent referred by the clicked shortcut.
1614 *
1615 * @param v The view representing the clicked shortcut.
1616 */
1617 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001618 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1619 // view has detached (it's possible for this to happen if the view is removed mid touch).
1620 if (v.getWindowToken() == null) {
1621 return;
1622 }
1623
1624 if (mWorkspace.isSwitchingState()) {
1625 return;
1626 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001629 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001631 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001632 int[] pos = new int[2];
1633 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001634 intent.setSourceBounds(new Rect(pos[0], pos[1],
1635 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001636 boolean success = startActivitySafely(intent, tag);
1637
1638 if (success && v instanceof BubbleTextView) {
1639 mWaitingForResume = (BubbleTextView) v;
1640 mWaitingForResume.setStayPressed(true);
1641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001643 if (v instanceof FolderIcon) {
1644 FolderIcon fi = (FolderIcon) v;
1645 handleFolderClick(fi);
1646 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001647 } else if (v == mAllAppsButton) {
1648 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001649 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001650 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001651 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
1654 }
1655
Michael Jurka0e260592010-06-30 17:07:39 -07001656 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001657 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001658 // clicking anywhere on the workspace causes the customization drawer to slide down
1659 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001660 return false;
1661 }
1662
Michael Jurkaaf442092010-06-10 17:01:57 -07001663 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001664 * Event handler for the search button
1665 *
1666 * @param v The view that was clicked.
1667 */
1668 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001669 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001670 }
1671
1672 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001673 * Event handler for the voice button
1674 *
1675 * @param v The view that was clicked.
1676 */
1677 public void onClickVoiceButton(View v) {
1678 startVoiceSearch();
1679 }
1680
1681 private void startVoiceSearch() {
1682 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001683 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001684 startActivity(intent);
1685 }
1686
1687 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001688 * Event handler for the "grid" button that appears on the home screen, which
1689 * enters all apps mode.
1690 *
1691 * @param v The view that was clicked.
1692 */
1693 public void onClickAllAppsButton(View v) {
1694 showAllApps(true);
1695 }
1696
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001697 public void onClickAppMarketButton(View v) {
1698 if (mAppMarketIntent != null) {
1699 startActivitySafely(mAppMarketIntent, "app market");
1700 }
1701 }
1702
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001703 void startApplicationDetailsActivity(ComponentName componentName) {
1704 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001705 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1706 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001707 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001708 startActivity(intent);
1709 }
1710
Patrick Dubroy5539af72010-09-07 15:22:01 -07001711 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1712 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1713 // System applications cannot be installed. For now, show a toast explaining that.
1714 // We may give them the option of disabling apps this way.
1715 int messageId = R.string.uninstall_system_app_text;
1716 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1717 } else {
1718 String packageName = appInfo.componentName.getPackageName();
1719 String className = appInfo.componentName.getClassName();
1720 Intent intent = new Intent(
1721 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001722 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1723 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001724 startActivity(intent);
1725 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001726 }
1727
Michael Jurkaddd62e92011-02-16 17:49:14 -08001728 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1730 try {
1731 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001732 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 } catch (ActivityNotFoundException e) {
1734 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001735 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 } catch (SecurityException e) {
1737 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001738 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001740 "or use the exported attribute for this activity. "
1741 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001743 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001745
Romain Guy8e633c52010-03-04 12:51:36 -08001746 void startActivityForResultSafely(Intent intent, int requestCode) {
1747 try {
1748 startActivityForResult(intent, requestCode);
1749 } catch (ActivityNotFoundException e) {
1750 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1751 } catch (SecurityException e) {
1752 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1753 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1754 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1755 "or use the exported attribute for this activity.", e);
1756 }
1757 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758
Adam Cohena9cf38f2011-05-02 15:36:58 -07001759 private void handleFolderClick(FolderIcon folderIcon) {
1760 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001761 Folder openFolder = mWorkspace.getFolderForTag(info);
1762
1763 // If the folder info reports that the associated folder is open, then verify that
1764 // it is actually opened. There have been a few instances where this gets out of sync.
1765 if (info.opened && openFolder == null) {
1766 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1767 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1768 info.opened = false;
1769 }
1770
Adam Cohena9cf38f2011-05-02 15:36:58 -07001771 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 // Close any open folder
1773 closeFolder();
1774 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001775 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 } else {
1777 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 int folderScreen;
1779 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001780 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 // .. and close it
1782 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001783 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 // Close any folder open on the current screen
1785 closeFolder();
1786 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001787 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789 }
1790 }
1791 }
1792
Adam Cohen2801caf2011-05-13 20:57:39 -07001793 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001794 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001795 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1796 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1797 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1798
Adam Cohenc51934b2011-07-26 21:07:43 -07001799 FolderInfo info = (FolderInfo) fi.getTag();
1800 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1801 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001802 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1803 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001804 }
1805
Adam Cohen2801caf2011-05-13 20:57:39 -07001806 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1807 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1808 oa.start();
1809 }
1810
1811 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001812 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001813 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1814 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1815 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1816
Adam Cohenc51934b2011-07-26 21:07:43 -07001817 FolderInfo info = (FolderInfo) fi.getTag();
1818 CellLayout cl = null;
1819 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1820 cl = (CellLayout) fi.getParent().getParent();
1821 }
1822
1823 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001824 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1825 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001826 oa.addListener(new AnimatorListenerAdapter() {
1827 @Override
1828 public void onAnimationEnd(Animator animation) {
1829 if (layout != null) {
1830 layout.clearFolderLeaveBehind();
1831 }
1832 }
1833 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001834 oa.start();
1835 }
1836
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001838 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 * is animated relative to the specified View. If the View is null, no animation
1840 * is played.
1841 *
1842 * @param folderInfo The FolderInfo describing the folder to open.
1843 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001844 public void openFolder(FolderIcon folderIcon) {
1845 Folder folder = folderIcon.mFolder;
1846 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847
Adam Cohen2801caf2011-05-13 20:57:39 -07001848 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001849 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850
Adam Cohen4554ee12011-08-03 16:13:21 -07001851 // Just verify that the folder hasn't already been added to the DragLayer.
1852 // There was a one-off crash where the folder had a parent already.
1853 if (folder.getParent() == null) {
1854 mDragLayer.addView(folder);
1855 mDragController.addDropTarget((DropTarget) folder);
1856 } else {
1857 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1858 folder.getParent() + ").");
1859 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001860 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001861 }
1862
1863 public void closeFolder() {
1864 Folder folder = mWorkspace.getOpenFolder();
1865 if (folder != null) {
1866 closeFolder(folder);
1867 }
1868 }
1869
1870 void closeFolder(Folder folder) {
1871 folder.getInfo().opened = false;
1872
1873 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1874 if (parent != null) {
1875 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1876 shrinkAndFadeInFolderIcon(fi);
1877 }
1878 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
1880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001882 if (mState != State.WORKSPACE) {
1883 return false;
1884 }
1885
Joe Onorato9c1289c2009-08-17 11:03:03 -04001886 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 return false;
1888 }
1889
1890 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001891 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 }
1893
Michael Jurka0280c3b2010-09-17 15:00:07 -07001894 resetAddInfo();
1895 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001897 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 return true;
1899 }
1900
Winson Chung3d503fb2011-07-13 17:25:49 -07001901 // The hotseat touch handling does not go through Workspace, and we always allow long press
1902 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001903 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001904 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1905 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 if (itemUnderLongClick == null) {
1907 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001908 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1909 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001910 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001912 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001914 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 }
1916 }
1917 }
1918 return true;
1919 }
1920
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 boolean isHotseatLayout(View layout) {
1922 return mHotseat != null && layout != null &&
1923 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1924 }
1925 Hotseat getHotseat() {
1926 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001927 }
1928
Winson Chung3d503fb2011-07-13 17:25:49 -07001929 /**
1930 * Returns the CellLayout of the specified container at the specified screen.
1931 */
1932 CellLayout getCellLayout(long container, int screen) {
1933 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1934 if (mHotseat != null) {
1935 return mHotseat.getLayout();
1936 } else {
1937 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001938 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 } else {
1940 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001941 }
1942 }
1943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 Workspace getWorkspace() {
1945 return mWorkspace;
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
1949 protected Dialog onCreateDialog(int id) {
1950 switch (id) {
1951 case DIALOG_CREATE_SHORTCUT:
1952 return new CreateShortcut().createDialog();
1953 case DIALOG_RENAME_FOLDER:
1954 return new RenameFolder().createDialog();
1955 }
1956
1957 return super.onCreateDialog(id);
1958 }
1959
1960 @Override
1961 protected void onPrepareDialog(int id, Dialog dialog) {
1962 switch (id) {
1963 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 break;
1965 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001966 if (mFolderInfo != null) {
1967 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1968 final CharSequence text = mFolderInfo.title;
1969 input.setText(text);
1970 input.setSelection(0, text.length());
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 break;
1973 }
1974 }
1975
1976 void showRenameDialog(FolderInfo info) {
1977 mFolderInfo = info;
1978 mWaitingForResult = true;
1979 showDialog(DIALOG_RENAME_FOLDER);
1980 }
1981
Adam Cohenfbba09b2011-07-18 21:43:05 -07001982 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001983 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001984 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1985 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 mWaitingForResult = true;
1987 showDialog(DIALOG_CREATE_SHORTCUT);
1988 }
1989
1990 private class RenameFolder {
1991 private EditText mInput;
1992
1993 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1995 mInput = (EditText) layout.findViewById(R.id.folder_name);
1996
1997 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1998 builder.setIcon(0);
1999 builder.setTitle(getString(R.string.rename_folder_title));
2000 builder.setCancelable(true);
2001 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2002 public void onCancel(DialogInterface dialog) {
2003 cleanup();
2004 }
2005 });
2006 builder.setNegativeButton(getString(R.string.cancel_action),
2007 new Dialog.OnClickListener() {
2008 public void onClick(DialogInterface dialog, int which) {
2009 cleanup();
2010 }
2011 }
2012 );
2013 builder.setPositiveButton(getString(R.string.rename_action),
2014 new Dialog.OnClickListener() {
2015 public void onClick(DialogInterface dialog, int which) {
2016 changeFolderName();
2017 }
2018 }
2019 );
2020 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002021
2022 final AlertDialog dialog = builder.create();
2023 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2024 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002025 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002026 mInput.requestFocus();
2027 InputMethodManager inputManager = (InputMethodManager)
2028 getSystemService(Context.INPUT_METHOD_SERVICE);
2029 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002030 }
2031 });
2032
2033 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 }
2035
2036 private void changeFolderName() {
2037 final String name = mInput.getText().toString();
2038 if (!TextUtils.isEmpty(name)) {
2039 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002040 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 mFolderInfo.title = name;
2042 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2043
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002045 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002046 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 } else {
2048 final FolderIcon folderIcon = (FolderIcon)
2049 mWorkspace.getViewForTag(mFolderInfo);
2050 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002051 // TODO: At some point we'll probably want some version of setting
2052 // the text for a folder icon.
2053 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 getWorkspace().requestLayout();
2055 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002056 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002057 mWorkspaceLoading = true;
2058 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060 }
2061 }
2062 cleanup();
2063 }
2064
2065 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 dismissDialog(DIALOG_RENAME_FOLDER);
2067 mWaitingForResult = false;
2068 mFolderInfo = null;
2069 }
2070 }
2071
Daniel Sandler843e8602010-06-07 14:59:01 -04002072 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2073 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002074 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002075 }
2076
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002077 // AllAppsView.Watcher
2078 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002079 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002080 mWorkspace.setVisibility(View.GONE);
2081 }
2082 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002083
2084 /**
2085 * Helper method for the cameraZoomIn/cameraZoomOut animations
2086 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002087 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002088 * @param scaleFactor The scale factor used for the zoom
2089 */
2090 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2091 final int height = view.getHeight();
2092 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002093 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002094 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002095
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002096 void updateWallpaperVisibility(boolean visible) {
2097 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2098 int curflags = getWindow().getAttributes().flags
2099 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2100 if (wpflags != curflags) {
2101 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2102 }
2103 }
2104
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002105 /**
2106 * Zoom the camera out from the workspace to reveal 'toView'.
2107 * Assumes that the view to show is anchored at either the very top or very bottom
2108 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002109 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002110 */
Winson Chungc07918d2011-07-01 15:35:26 -07002111 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002112 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002113
Winson Chungf0ea4d32011-06-06 14:27:16 -07002114 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2115 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2116 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2117 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002118
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002119 setPivotsForZoom(toView, toState, scale);
2120
Michael Jurkad74c9842011-07-10 12:44:21 -07002121 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002122 mWorkspace.changeState(Workspace.State.SMALL, animated);
Winson Chung4afe9b32011-07-27 17:46:20 -07002123 hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002124
2125 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002126 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002127 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002128 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2129 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002130 ((View) toView.getParent()).invalidate();
2131 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002132 toView.setFastScaleX(a * scale + b * 1f);
2133 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002134 }
2135 });
Adam Cohen61033d32010-11-15 18:29:44 -08002136
Winson Chungf0ea4d32011-06-06 14:27:16 -07002137 toView.setVisibility(View.VISIBLE);
2138 toView.setFastAlpha(0f);
2139 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2140 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2141 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2142 public void onAnimationUpdate(float a, float b) {
2143 // don't need to invalidate because we do so above
2144 toView.setFastAlpha(a * 0f + b * 1f);
2145 }
2146 });
2147 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002148
Michael Jurkaabded662011-03-04 12:06:57 -08002149 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002150 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002151 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002152 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002153 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002154 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002155 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002156 // Prepare the position
2157 toView.setTranslationX(0.0f);
2158 toView.setTranslationY(0.0f);
2159 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002160 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002161 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002162 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002163 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002164 // If we don't set the final scale values here, if this animation is cancelled
2165 // it will have the wrong scale value and subsequent cameraPan animations will
2166 // not fix that
2167 toView.setScaleX(1.0f);
2168 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002169 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002170 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002171 }
Winson Chung32174c82011-07-19 15:47:55 -07002172
2173 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2174 // Hide the workspace scrollbar
2175 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002176 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002177 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002178 updateWallpaperVisibility(false);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002179 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002180 });
2181
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002182 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002183 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002184
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002185 if (mStateAnimation != null) mStateAnimation.cancel();
2186 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002187 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002188 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002189 } else {
2190 toView.setTranslationX(0.0f);
2191 toView.setTranslationY(0.0f);
2192 toView.setScaleX(1.0f);
2193 toView.setScaleY(1.0f);
2194 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002195 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002196 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002197 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2198 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002199
Winson Chungc07918d2011-07-01 15:35:26 -07002200 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2201 // Hide the workspace scrollbar
2202 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002203 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002204 }
Michael Jurkaabded662011-03-04 12:06:57 -08002205 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002206 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002207 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002208 }
2209
2210 /**
2211 * Zoom the camera back into the workspace, hiding 'fromView'.
2212 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002213 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002214 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002215 */
Winson Chungc07918d2011-07-01 15:35:26 -07002216 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002217 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002218
Winson Chungf0ea4d32011-06-06 14:27:16 -07002219 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2220 final float scaleFactor = (float)
2221 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2222 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002223
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002224 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002225 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002226 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 if (mStateAnimation != null) mStateAnimation.cancel();
2229 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002230
Michael Jurka742574b2011-02-02 23:51:01 -08002231 final float oldScaleX = fromView.getScaleX();
2232 final float oldScaleY = fromView.getScaleY();
2233
2234 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2235 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002236 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2237 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002238 ((View)fromView.getParent()).fastInvalidate();
2239 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2240 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2241 }
2242 });
Michael Jurkaabded662011-03-04 12:06:57 -08002243 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002244 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002245 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002246 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2247 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002248 // don't need to invalidate because we do so above
2249 fromView.setFastAlpha(a * 1f + b * 0f);
2250 }
2251 });
Michael Jurkaabded662011-03-04 12:06:57 -08002252 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002253 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002254 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002255 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002256 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002257 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002258 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002260 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002261 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002262 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002263 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264 }
2265 });
2266
Winson Chungf0ea4d32011-06-06 14:27:16 -07002267 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002268 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002269 } else {
2270 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002271 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002272 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2273 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002274 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002276 }
2277
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002278 void showWorkspace(boolean animated) {
Adam Cohen7777d962011-08-18 18:58:38 -07002279 mWorkspace.changeState(Workspace.State.NORMAL, animated);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002280 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002281 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002282 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002283
Adam Cohen7777d962011-08-18 18:58:38 -07002284 mWorkspace.showDockDivider(!animated);
2285 mWorkspace.flashScrollingIndicator();
2286
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002287 // Change the state *after* we've called all the transition code
2288 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002289
Winson Chung5fb63472011-02-02 17:03:37 -08002290 // Resume the auto-advance of widgets
2291 mUserPresent = true;
2292 updateRunning();
2293
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002294 // send an accessibility event to announce the context change
2295 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002296 }
2297
Adam Cohen7777d962011-08-18 18:58:38 -07002298 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002299 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002300 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002301 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002302 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002303 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002304 }
Adam Cohen7777d962011-08-18 18:58:38 -07002305
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002306 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002307 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2308
Winson Chunge7a03942011-08-05 15:05:12 -07002309 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002310 @Override
2311 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002312 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002313 // Before we show workspace, hide all apps again because
2314 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2315 // clean up our state transition functions
2316 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002317 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002318 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002319 } else {
2320 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002321 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002322 }
2323 }, (extendedDelay ?
2324 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2325 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2326 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002327 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002328 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002329 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2330 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002331 }
2332 // Otherwise, we are not in spring loaded mode, so don't do anything.
2333 }
2334
Adam Cohenfc53cd22011-07-20 15:45:11 -07002335 public boolean isAllAppsCustomizeOpen() {
2336 return mState == State.APPS_CUSTOMIZE;
2337 }
2338
Winson Chungf0ea4d32011-06-06 14:27:16 -07002339 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002341 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002342 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002343 if (!LauncherApplication.isScreenLarge()) {
2344 if (animated) {
2345 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002347 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002348 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002349 }
2350 }
2351 }
2352
2353 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002357 if (!LauncherApplication.isScreenLarge()) {
2358 if (animated) {
2359 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002364 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002365 }
2366
Winson Chung785d2eb2011-04-14 16:08:02 -07002367 void showAllApps(boolean animated) {
2368 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002369
Winson Chungf0ea4d32011-06-06 14:27:16 -07002370 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2371 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002372
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002375
Winson Chungf0ea4d32011-06-06 14:27:16 -07002376 // Change the state *after* we've called all the transition code
2377 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002378
2379 // Pause the auto-advance of widgets until we are out of AllApps
2380 mUserPresent = false;
2381 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002382 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002383
2384 // Send an accessibility event to announce the context change
2385 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2386 }
2387
Joe Onoratob2061212009-11-24 16:13:54 -05002388 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002389 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002390 * - Home from workspace
2391 * - from center screen
2392 * - from other screens
2393 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002394 * - from center screen
2395 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002396 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002397 * - from center screen
2398 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002399 * - Launch app from workspace and quit
2400 * - with back
2401 * - with home
2402 * - Launch app from all apps and quit
2403 * - with back
2404 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002405 * - Go to a screen that's not the default, then all
2406 * apps, and launch and app, and go back
2407 * - with back
2408 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002409 * - On workspace, long press power and go back
2410 * - with back
2411 * - with home
2412 * - On all apps, long press power and go back
2413 * - with back
2414 * - with home
2415 * - On workspace, power off
2416 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002417 * - Launch an app and turn off the screen while in that app
2418 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002419 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002420 * - From all apps
2421 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002422 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2423 * - From all apps
2424 * - From the center workspace
2425 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002426 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002427 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2429 mWorkspace.setVisibility(View.VISIBLE);
2430 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002431
Winson Chung3d503fb2011-07-13 17:25:49 -07002432 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002433 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002434
Winson Chungf0ea4d32011-06-06 14:27:16 -07002435 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002436 if (mAllAppsButton != null) {
2437 mAllAppsButton.requestFocus();
2438 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002439 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002440 }
2441
Joe Onorato7c312c12009-08-13 21:36:53 -07002442 void lockAllApps() {
2443 // TODO
2444 }
2445
2446 void unlockAllApps() {
2447 // TODO
2448 }
2449
Patrick Dubroy5f445422011-02-18 14:35:21 -08002450 /**
2451 * Add an item from all apps or customize onto the given workspace screen.
2452 * If layout is null, add to the current screen.
2453 */
2454 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002455 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2456 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002457 } else {
2458 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002459 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002460 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002461
Winson Chungdff8ebb2011-09-08 17:25:31 -07002462 /** Maps the current orientation to an index for referencing orientation correct global icons */
2463 private int getCurrentOrientationIndexForGlobalIcons() {
2464 // default - 0, landscape - 1
2465 switch (getResources().getConfiguration().orientation) {
2466 case Configuration.ORIENTATION_LANDSCAPE:
2467 return 1;
2468 default:
2469 return 0;
2470 }
2471 }
2472
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002473 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002474 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002475 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002476 // Look for the toolbar icon specified in the activity meta-data
2477 Bundle metaData = packageManager.getActivityInfo(
2478 activityName, PackageManager.GET_META_DATA).metaData;
2479 if (metaData != null) {
2480 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2481 if (iconResId != 0) {
2482 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002483 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002484 }
2485 }
2486 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002487 // This can happen if the activity defines an invalid drawable
2488 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2489 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002490 } catch (Resources.NotFoundException nfe) {
2491 // This can happen if the activity defines an invalid drawable
2492 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2493 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002494 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002495 return null;
2496 }
2497
2498 // if successful in getting icon, return it; otherwise, set button to use default drawable
2499 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2500 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2501 TextView button = (TextView) findViewById(buttonId);
2502 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002503 Resources r = getResources();
2504 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2505 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002506
2507 // If we were unable to find the icon via the meta-data, use a generic one
2508 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002509 toolbarIcon = r.getDrawable(fallbackDrawableId);
2510 toolbarIcon.setBounds(0, 0, w, h);
2511 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002512 return null;
2513 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002514 toolbarIcon.setBounds(0, 0, w, h);
2515 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002516 return toolbarIcon.getConstantState();
2517 }
2518 }
2519
2520 // if successful in getting icon, return it; otherwise, set button to use default drawable
2521 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2522 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2523 ImageView button = (ImageView) findViewById(buttonId);
2524 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2525
Michael Jurka19e0fc52011-07-22 18:00:21 -07002526 if (button != null) {
2527 // If we were unable to find the icon via the meta-data, use a
2528 // generic one
2529 if (toolbarIcon == null) {
2530 button.setImageResource(fallbackDrawableId);
2531 } else {
2532 button.setImageDrawable(toolbarIcon);
2533 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002534 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002535
2536 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2537
Michael Jurka0423dcf2010-10-05 14:56:18 -07002538 }
2539
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002540 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2541 TextView button = (TextView) findViewById(buttonId);
2542 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2543 }
2544
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002545 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002546 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002547 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002548 }
2549
Michael Jurka0423dcf2010-10-05 14:56:18 -07002550 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002551 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2552 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002553 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002554
Winson Chung1cad91e2011-05-25 17:41:01 -07002555 final SearchManager searchManager =
2556 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2557 ComponentName activityName = searchManager.getGlobalSearchActivity();
2558 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002559 int coi = getCurrentOrientationIndexForGlobalIcons();
2560 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002561 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002562 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002563 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002564 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002565 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002566 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002567 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002568 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002569 }
2570 }
2571
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002572 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002573 updateButtonWithDrawable(R.id.search_button, d);
2574 }
2575
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002576 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002577 final View searchDivider = findViewById(R.id.search_divider);
2578 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002579
Winson Chung1cad91e2011-05-25 17:41:01 -07002580 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2581 ComponentName activityName = intent.resolveActivity(getPackageManager());
2582 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002583 int coi = getCurrentOrientationIndexForGlobalIcons();
2584 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002585 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2586 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002587 voiceButton.setVisibility(View.VISIBLE);
2588 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002589 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002590 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002591 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002592 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002593
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002594 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002595 updateButtonWithDrawable(R.id.voice_button, d);
2596 }
2597
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002598 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002599 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002600 */
2601 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002602 final View marketButton = findViewById(R.id.market_button);
2603 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2604 // Find the app market activity by resolving an intent.
2605 // (If multiple app markets are installed, it will return the ResolverActivity.)
2606 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002607 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002608 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002609 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002610 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002611 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002612 marketButton.setVisibility(View.VISIBLE);
2613 } else {
2614 // We should hide and disable the view so that we don't try and restore the visibility
2615 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2616 marketButton.setVisibility(View.GONE);
2617 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002618 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002619 }
2620
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002621 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002622 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002623 }
2624
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002625 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002626 * Displays the shortcut creation dialog and launches, if necessary, the
2627 * appropriate activity.
2628 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002629 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002630 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2631 DialogInterface.OnShowListener {
2632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002633 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002635 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002636 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002637
Winson Chung55b65502011-05-26 12:03:43 -07002638 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2639 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002640 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002641
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002642 AlertDialog dialog = builder.create();
2643 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002644 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002645 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 return dialog;
2648 }
2649
2650 public void onCancel(DialogInterface dialog) {
2651 mWaitingForResult = false;
2652 cleanup();
2653 }
2654
Romain Guycbb89e42009-06-08 15:52:54 -07002655 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002656 mWaitingForResult = false;
2657 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002658 }
2659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002661 try {
2662 dismissDialog(DIALOG_CREATE_SHORTCUT);
2663 } catch (Exception e) {
2664 // An exception is thrown if the dialog is not visible, which is fine
2665 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 }
2667
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002668 /**
2669 * Handle the action clicked in the "Add to home" dialog.
2670 */
2671 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002672 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002673
Winson Chung55b65502011-05-26 12:03:43 -07002674 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2675 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002676 case AddAdapter.ITEM_APPLICATION: {
2677 if (mAppsCustomizeTabHost != null) {
2678 mAppsCustomizeTabHost.selectAppsTab();
2679 }
2680 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002681 break;
2682 }
Winson Chung55b65502011-05-26 12:03:43 -07002683 case AddAdapter.ITEM_APPWIDGET: {
2684 if (mAppsCustomizeTabHost != null) {
2685 mAppsCustomizeTabHost.selectWidgetsTab();
2686 }
2687 showAllApps(true);
2688 break;
2689 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002690 case AddAdapter.ITEM_WALLPAPER: {
2691 startWallpaper();
2692 break;
2693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002694 }
2695 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002696
2697 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002698 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 }
2701
2702 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002703 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002704 */
2705 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2706 @Override
2707 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002708 closeSystemDialogs();
2709 }
2710 }
2711
2712 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002713 * Receives notifications whenever the appwidgets are reset.
2714 */
2715 private class AppWidgetResetObserver extends ContentObserver {
2716 public AppWidgetResetObserver() {
2717 super(new Handler());
2718 }
2719
2720 @Override
2721 public void onChange(boolean selfChange) {
2722 onAppWidgetReset();
2723 }
2724 }
2725
2726 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002727 * If the activity is currently paused, signal that we need to re-run the loader
2728 * in onResume.
2729 *
2730 * This needs to be called from incoming places where resources might have been loaded
2731 * while we are paused. That is becaues the Configuration might be wrong
2732 * when we're not running, and if it comes back to what it was when we
2733 * were paused, we are not restarted.
2734 *
2735 * Implementation of the method from LauncherModel.Callbacks.
2736 *
2737 * @return true if we are currently paused. The caller might be able to
2738 * skip some work in that case since we will come back again.
2739 */
2740 public boolean setLoadOnResume() {
2741 if (mPaused) {
2742 Log.i(TAG, "setLoadOnResume");
2743 mOnResumeNeedsLoad = true;
2744 return true;
2745 } else {
2746 return false;
2747 }
2748 }
2749
2750 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002751 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002754 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002755 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002756 } else {
2757 return SCREEN_COUNT / 2;
2758 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002760
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002761
Joe Onorato9c1289c2009-08-17 11:03:03 -04002762 /**
2763 * Refreshes the shortcuts shown on the workspace.
2764 *
2765 * Implementation of the method from LauncherModel.Callbacks.
2766 */
2767 public void startBinding() {
2768 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002769
2770 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002771 int count = workspace.getChildCount();
2772 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002773 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002774 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2775 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002777 if (mHotseat != null) {
2778 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779 }
2780 }
2781
2782 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002783 * Bind the items start-end from the list.
2784 *
2785 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002787 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002788 setLoadOnResume();
2789
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002791 for (int i=start; i<end; i++) {
2792 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002793
2794 // Short circuit if we are loading dock items for a configuration which has no dock
2795 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2796 mHotseat == null) {
2797 continue;
2798 }
2799
Joe Onorato9c1289c2009-08-17 11:03:03 -04002800 switch (item.itemType) {
2801 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2802 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002803 View shortcut = createShortcut((ShortcutInfo)item);
2804 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2805 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002807 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002808 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002809 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002810 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002811 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2812 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002815 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 }
2818
Joe Onorato9c1289c2009-08-17 11:03:03 -04002819 /**
2820 * Implementation of the method from LauncherModel.Callbacks.
2821 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002822 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002823 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002824 sFolders.clear();
2825 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827
2828 /**
2829 * Add the views for a widget to the workspace.
2830 *
2831 * Implementation of the method from LauncherModel.Callbacks.
2832 */
2833 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002834 setLoadOnResume();
2835
Daniel Sandler843e8602010-06-07 14:59:01 -04002836 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2837 if (DEBUG_WIDGETS) {
2838 Log.d(TAG, "bindAppWidget: " + item);
2839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 final Workspace workspace = mWorkspace;
2841
2842 final int appWidgetId = item.appWidgetId;
2843 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002844 if (DEBUG_WIDGETS) {
2845 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2846 }
2847
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2849
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2851 item.hostView.setTag(item);
2852
Winson Chung3d503fb2011-07-13 17:25:49 -07002853 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 item.cellY, item.spanX, item.spanY, false);
2855
Adam Cohended9f8d2010-11-03 13:25:16 -07002856 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2857
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 workspace.requestLayout();
2859
Daniel Sandler843e8602010-06-07 14:59:01 -04002860 if (DEBUG_WIDGETS) {
2861 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2862 + (SystemClock.uptimeMillis()-start) + "ms");
2863 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 }
2865
2866 /**
2867 * Callback saying that there aren't any more items to bind.
2868 *
2869 * Implementation of the method from LauncherModel.Callbacks.
2870 */
2871 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002872 setLoadOnResume();
2873
Joe Onorato9c1289c2009-08-17 11:03:03 -04002874 if (mSavedState != null) {
2875 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002876 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 mSavedState = null;
2879 }
2880
2881 if (mSavedInstanceState != null) {
2882 super.onRestoreInstanceState(mSavedInstanceState);
2883 mSavedInstanceState = null;
2884 }
2885
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002887
2888 // If we received the result of any pending adds while the loader was running (e.g. the
2889 // widget configuration forced an orientation change), process them now.
2890 for (int i = 0; i < sPendingAddList.size(); i++) {
2891 completeAdd(sPendingAddList.get(i));
2892 }
2893 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 }
2895
2896 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002897 * Updates the icons on the launcher that are affected by changes to the package list
2898 * on the device.
2899 */
2900 private void updateIconsAffectedByPackageManagerChanges() {
2901 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002902 updateVoiceSearchIcon();
2903 }
2904
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002905 @Override
2906 public void bindSearchablesChanged() {
2907 updateGlobalSearchIcon();
2908 }
2909
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002910 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 * Add the icons for all apps.
2912 *
2913 * Implementation of the method from LauncherModel.Callbacks.
2914 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002915 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2916 // Remove the progress bar entirely; we could also make it GONE
2917 // but better to remove it since we know it's not going to be used
2918 View progressBar = mAppsCustomizeTabHost.
2919 findViewById(R.id.apps_customize_progress_bar);
2920 if (progressBar != null) {
2921 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002922 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002923 // We just post the call to setApps so the user sees the progress bar
2924 // disappear-- otherwise, it just looks like the progress bar froze
2925 // which doesn't look great
2926 mAppsCustomizeTabHost.post(new Runnable() {
2927 public void run() {
2928 if (mAppsCustomizeContent != null) {
2929 mAppsCustomizeContent.setApps(apps);
2930 }
2931 }
2932 });
2933
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002934 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002935 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 }
2937
2938 /**
2939 * A package was installed.
2940 *
2941 * Implementation of the method from LauncherModel.Callbacks.
2942 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002943 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002944 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002946
Winson Chung785d2eb2011-04-14 16:08:02 -07002947 if (mAppsCustomizeContent != null) {
2948 mAppsCustomizeContent.addApps(apps);
2949 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002950 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 }
2952
2953 /**
2954 * A package was updated.
2955 *
2956 * Implementation of the method from LauncherModel.Callbacks.
2957 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002958 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002959 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002961 if (mWorkspace != null) {
2962 mWorkspace.updateShortcuts(apps);
2963 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002964
Winson Chung785d2eb2011-04-14 16:08:02 -07002965 if (mAppsCustomizeContent != null) {
2966 mAppsCustomizeContent.updateApps(apps);
2967 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002968 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 }
2970
2971 /**
2972 * A package was uninstalled.
2973 *
2974 * Implementation of the method from LauncherModel.Callbacks.
2975 */
Joe Onorato36115782010-06-17 13:28:48 -04002976 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002978 if (permanent) {
2979 mWorkspace.removeItems(apps);
2980 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002981
Winson Chung785d2eb2011-04-14 16:08:02 -07002982 if (mAppsCustomizeContent != null) {
2983 mAppsCustomizeContent.removeApps(apps);
2984 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002985 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 }
Joe Onoratobe386092009-11-17 17:32:16 -08002987
2988 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002989 * A number of packages were updated.
2990 */
2991 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002992
Winson Chung785d2eb2011-04-14 16:08:02 -07002993 if (mAppsCustomizeContent != null) {
2994 mAppsCustomizeContent.onPackagesUpdated();
2995 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002996 }
2997
Winson Chung400438b2011-01-16 17:53:48 -08002998 private int mapConfigurationOriActivityInfoOri(int configOri) {
2999 final Display d = getWindowManager().getDefaultDisplay();
3000 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3001 switch (d.getRotation()) {
3002 case Surface.ROTATION_0:
3003 case Surface.ROTATION_180:
3004 // We are currently in the same basic orientation as the natural orientation
3005 naturalOri = configOri;
3006 break;
3007 case Surface.ROTATION_90:
3008 case Surface.ROTATION_270:
3009 // We are currently in the other basic orientation to the natural orientation
3010 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3011 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3012 break;
3013 }
3014
3015 int[] oriMap = {
3016 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3017 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3018 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3019 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3020 };
3021 // Since the map starts at portrait, we need to offset if this device's natural orientation
3022 // is landscape.
3023 int indexOffset = 0;
3024 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3025 indexOffset = 1;
3026 }
3027 return oriMap[(d.getRotation() + indexOffset) % 4];
3028 }
3029 public void lockScreenOrientation() {
3030 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3031 .getConfiguration().orientation));
3032 }
3033 public void unlockScreenOrientation() {
3034 mHandler.postDelayed(new Runnable() {
3035 public void run() {
3036 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3037 }
3038 }, mRestoreScreenOrientationDelay);
3039 }
3040
Winson Chung82f55532011-08-09 14:14:23 -07003041 /* Cling related */
3042 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3043 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3044 private void enableClingsIfNecessary() {
3045 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3046 if (LauncherApplication.isScreenLarge()) return;
3047
Brett Chabot2a9e2282011-08-23 15:03:13 -07003048 // disable clings when running in a test harness
3049 if(ActivityManager.isRunningInTestHarness()) return;
3050
Winson Chung82f55532011-08-09 14:14:23 -07003051 // Enable the clings only if they have not been dismissed before
3052 SharedPreferences prefs =
3053 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3054 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3055 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3056 cling.init(this);
3057 cling.setVisibility(View.VISIBLE);
3058 }
3059 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3060 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3061 cling.init(this);
3062 cling.setVisibility(View.VISIBLE);
3063 }
3064 }
3065 private void dismissCling(final Cling cling, final String flag) {
3066 if (cling != null) {
3067 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3068 anim.setDuration(DISMISS_CLING_DURATION);
3069 anim.addListener(new AnimatorListenerAdapter() {
3070 public void onAnimationEnd(Animator animation) {
3071 cling.setVisibility(View.GONE);
3072 cling.cleanup();
3073 SharedPreferences prefs =
3074 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3075 SharedPreferences.Editor editor = prefs.edit();
3076 editor.putBoolean(flag, true);
3077 editor.commit();
3078 };
3079 });
3080 anim.start();
3081 }
3082 }
3083 public void dismissWorkspaceCling(View v) {
3084 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3085 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3086 }
3087 public void dismissAllAppsCling(View v) {
3088 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3089 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3090 }
3091
Winson Chung80baf5a2010-08-09 16:03:15 -07003092 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003093 * Prints out out state for debugging.
3094 */
3095 public void dumpState() {
3096 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003097 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003098 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3099 Log.d(TAG, "mRestoring=" + mRestoring);
3100 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3101 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003102 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003103 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104
Winson Chung785d2eb2011-04-14 16:08:02 -07003105 if (mAppsCustomizeContent != null) {
3106 mAppsCustomizeContent.dumpState();
3107 }
Joe Onoratobe386092009-11-17 17:32:16 -08003108 Log.d(TAG, "END launcher2 dump state");
3109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110}
Michael Jurka2763be32011-02-24 11:19:57 -08003111
Michael Jurkaabded662011-03-04 12:06:57 -08003112interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003113 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3114 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003115}