blob: 795d5ccb94135d68dee7a5ff21205662cdab56a0 [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.pm.ResolveInfo;
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;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070081import android.view.ViewGroup;
Michael Jurkae0f5a612011-02-07 16:45:41 -080082import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080083import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Michael Jurkad74c9842011-07-10 12:44:21 -070097import com.android.launcher2.Workspace.State;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800120 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700123 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
124 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
126 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700127 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_PICK_APPLICATION = 6;
132 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700134 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800138 static final int SCREEN_COUNT = 5;
139 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Joe Onorato7c312c12009-08-13 21:36:53 -0700141 static final int DIALOG_CREATE_SHORTCUT = 1;
142 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Romain Guy98d01652009-06-30 16:21:04 -0700144 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 // Type: int
147 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700148 // Type: int
149 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
152 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700157 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: boolean
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
160 // Type: long
161 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
162
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700163 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
164
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700165 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700166 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700167 private State mState = State.WORKSPACE;
168 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700171 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
172 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800175 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800177 private final BroadcastReceiver mCloseSystemDialogsReceiver
178 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800179 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
180
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private LayoutInflater mInflater;
182
Joe Onorato00acb122009-08-04 16:04:30 -0400183 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700185
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700186 private AppWidgetManager mAppWidgetManager;
187 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700188
Winson Chung3d503fb2011-07-13 17:25:49 -0700189 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700190 private int[] mTmpAddItemCellCoordinates = new int[2];
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private FolderInfo mFolderInfo;
193
Winson Chung3d503fb2011-07-13 17:25:49 -0700194 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800195 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700196
Winson Chungf0ea4d32011-06-06 14:27:16 -0700197 private SearchDropTargetBar mSearchDeleteBar;
198 private AppsCustomizeTabHost mAppsCustomizeTabHost;
199 private AppsCustomizePagedView mAppsCustomizeContent;
200 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Bundle mSavedState;
203
204 private SpannableStringBuilder mDefaultKeySsb = null;
205
Joe Onorato9c1289c2009-08-17 11:03:03 -0400206 private boolean mWorkspaceLoading = true;
207
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800208 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private boolean mRestoring;
210 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700211 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
213 private Bundle mSavedInstanceState;
214
Joe Onorato9c1289c2009-08-17 11:03:03 -0400215 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800216 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800217 private boolean mUserPresent = true;
218 private boolean mVisible = false;
219 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700221 private static LocaleConfiguration sLocaleConfiguration = null;
222
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700223 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700224
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400225 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400226 private String[] mHotseatConfig = null;
227 private Intent[] mHotseats = null;
228 private Drawable[] mHotseatIcons = null;
229 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400230
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700231 private Intent mAppMarketIntent = null;
232
Adam Cohended9f8d2010-11-03 13:25:16 -0700233 // Related to the auto-advancing of widgets
234 private final int ADVANCE_MSG = 1;
235 private final int mAdvanceInterval = 20000;
236 private final int mAdvanceStagger = 250;
237 private long mAutoAdvanceSentTime;
238 private long mAutoAdvanceTimeLeft = -1;
239 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
240 new HashMap<View, AppWidgetProviderInfo>();
241
Winson Chung400438b2011-01-16 17:53:48 -0800242 // Determines how long to wait after a rotation before restoring the screen orientation to
243 // match the sensor state.
244 private final int mRestoreScreenOrientationDelay = 500;
245
Michael Jurka4ef207b2010-11-29 17:05:45 -0800246 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800247 private static Drawable.ConstantState sGlobalSearchIcon;
248 private static Drawable.ConstantState sVoiceSearchIcon;
249 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800250
Adam Cohen2801caf2011-05-13 20:57:39 -0700251 private DragLayer mDragLayer;
252
Michael Jurkaddd62e92011-02-16 17:49:14 -0800253 private BubbleTextView mWaitingForResume;
254
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800255 private static ArrayList<PendingAddArguments> sPendingAddList
256 = new ArrayList<PendingAddArguments>();
257
258 private static class PendingAddArguments {
259 int requestCode;
260 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700261 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800262 int screen;
263 int cellX;
264 int cellY;
265 }
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 LauncherApplication app = ((LauncherApplication)getApplication());
271 mModel = app.setLauncher(this);
272 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400273 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700275
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700276 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
278 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200281 android.os.Debug.startMethodTracing(
282 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
285 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 setContentView(R.layout.launcher);
287 setupViews();
288
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800289 registerContentObservers();
290
Joe Onorato7c312c12009-08-13 21:36:53 -0700291 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700292
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 mSavedState = savedInstanceState;
294 restoreState(mSavedState);
295
Winson Chunga12a2502010-12-20 14:41:35 -0800296 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700297 if (mAppsCustomizeContent != null) {
298 mAppsCustomizeContent.onPackagesUpdated();
299 }
Winson Chunga12a2502010-12-20 14:41:35 -0800300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 if (PROFILE_STARTUP) {
302 android.os.Debug.stopMethodTracing();
303 }
304
305 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 }
308
309 // For handling default keys
310 mDefaultKeySsb = new SpannableStringBuilder();
311 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800312
313 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
314 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800315
316 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700317 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
318 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100319 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700320 }
321 if (sGlobalSearchIcon != null) {
322 updateGlobalSearchIcon(sGlobalSearchIcon);
323 }
324 if (sVoiceSearchIcon != null) {
325 updateVoiceSearchIcon(sVoiceSearchIcon);
326 }
327 if (sAppMarketIcon != null) {
328 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
Romain Guycbb89e42009-06-08 15:52:54 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700333 if (sLocaleConfiguration == null) {
334 new AsyncTask<Void, Void, LocaleConfiguration>() {
335 @Override
336 protected LocaleConfiguration doInBackground(Void... unused) {
337 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
338 readConfiguration(Launcher.this, localeConfiguration);
339 return localeConfiguration;
340 }
341
342 @Override
343 protected void onPostExecute(LocaleConfiguration result) {
344 sLocaleConfiguration = result;
345 checkForLocaleChange(); // recursive, but now with a locale configuration
346 }
347 }.execute();
348 return;
349 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final Configuration configuration = getResources().getConfiguration();
352
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 final String locale = configuration.locale.toString();
355
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700356 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 final int mcc = configuration.mcc;
358
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 final int mnc = configuration.mnc;
361
Romain Guy84f296c2009-11-04 15:00:44 -0800362 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700365 sLocaleConfiguration.locale = locale;
366 sLocaleConfiguration.mcc = mcc;
367 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700368
Joe Onorato0589f0f2010-02-08 13:44:00 -0800369 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700370
371 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
372 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700373 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700374 public void run() {
375 writeConfiguration(Launcher.this, localeConfiguration);
376 }
377 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700378 }
379 }
380
381 private static class LocaleConfiguration {
382 public String locale;
383 public int mcc = -1;
384 public int mnc = -1;
385 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700386
Romain Guy98d01652009-06-30 16:21:04 -0700387 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
388 DataInputStream in = null;
389 try {
390 in = new DataInputStream(context.openFileInput(PREFERENCES));
391 configuration.locale = in.readUTF();
392 configuration.mcc = in.readInt();
393 configuration.mnc = in.readInt();
394 } catch (FileNotFoundException e) {
395 // Ignore
396 } catch (IOException e) {
397 // Ignore
398 } finally {
399 if (in != null) {
400 try {
401 in.close();
402 } catch (IOException e) {
403 // Ignore
404 }
405 }
406 }
407 }
408
409 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
410 DataOutputStream out = null;
411 try {
412 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
413 out.writeUTF(configuration.locale);
414 out.writeInt(configuration.mcc);
415 out.writeInt(configuration.mnc);
416 out.flush();
417 } catch (FileNotFoundException e) {
418 // Ignore
419 } catch (IOException e) {
420 //noinspection ResultOfMethodCallIgnored
421 context.getFileStreamPath(PREFERENCES).delete();
422 } finally {
423 if (out != null) {
424 try {
425 out.close();
426 } catch (IOException e) {
427 // Ignore
428 }
429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431 }
432
Adam Cohen716b51e2011-06-30 12:09:54 -0700433 public DragLayer getDragLayer() {
434 return mDragLayer;
435 }
436
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 static int getScreen() {
438 synchronized (sLock) {
439 return sScreen;
440 }
441 }
442
443 static void setScreen(int screen) {
444 synchronized (sLock) {
445 sScreen = screen;
446 }
447 }
448
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400449 // Note: This doesn't do all the client-id magic that BrowserProvider does
450 // in Browser. (http://b/2425179)
451 private Uri getDefaultBrowserUri() {
452 String url = getString(R.string.default_browser_url);
453 if (url.indexOf("{CID}") != -1) {
454 url = url.replace("{CID}", "android-google");
455 }
456 return Uri.parse(url);
457 }
458
Winson Chung557d6ed2011-07-08 15:34:52 -0700459 /**
460 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
461 * a configuration step, this allows the proper animations to run after other transitions.
462 */
463 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800464 switch (args.requestCode) {
465 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700466 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
467 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800468 break;
469 case REQUEST_PICK_SHORTCUT:
470 processShortcut(args.intent);
471 break;
472 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700473 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
474 args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700475 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800476 case REQUEST_PICK_APPWIDGET:
477 addAppWidgetFromPick(args.intent);
478 break;
479 case REQUEST_CREATE_APPWIDGET:
480 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700481 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700482 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 case REQUEST_PICK_WALLPAPER:
484 // We just wanted the activity result here so we can clear mWaitingForResult
485 break;
486 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700487 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488 }
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800491 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700492 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700493 mWaitingForResult = false;
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 // The pattern used here is that a user PICKs a specific application,
496 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
499 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700500 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
501 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800502 final PendingAddArguments args = new PendingAddArguments();
503 args.requestCode = requestCode;
504 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700505 args.container = mPendingAddInfo.container;
506 args.screen = mPendingAddInfo.screen;
507 args.cellX = mPendingAddInfo.cellX;
508 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800509
510 // If the loader is still running, defer the add until it is done.
511 if (isWorkspaceLocked()) {
512 sPendingAddList.add(args);
513 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700514 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 }
Romain Guy18042c82009-11-06 11:44:55 -0800516 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700517 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
518 if (data != null) {
519 // Clean up the appWidgetId if we canceled
520 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
521 if (appWidgetId != -1) {
522 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800524 }
525 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700526
527 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
528 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 }
530
531 @Override
532 protected void onResume() {
533 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800534 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700535 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400536 mWorkspaceLoading = true;
537 mModel.startLoader(this, true);
538 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700539 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800540 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800541 if (mWaitingForResume != null) {
542 mWaitingForResume.setStayPressed(false);
543 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700544 // When we resume Launcher, a different Activity might be responsible for the app
545 // market intent, so refresh the icon
546 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
548
549 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550 protected void onPause() {
551 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700552 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800553 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554 }
Romain Guycbb89e42009-06-08 15:52:54 -0700555
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700556 @Override
557 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400558 // Flag the loader to stop early before switching
559 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700560 if (mAppsCustomizeContent != null) {
561 mAppsCustomizeContent.surrender();
562 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800563 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700564 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800566 // We can't hide the IME if it was forced open. So don't bother
567 /*
568 @Override
569 public void onWindowFocusChanged(boolean hasFocus) {
570 super.onWindowFocusChanged(hasFocus);
571
572 if (hasFocus) {
573 final InputMethodManager inputManager = (InputMethodManager)
574 getSystemService(Context.INPUT_METHOD_SERVICE);
575 WindowManager.LayoutParams lp = getWindow().getAttributes();
576 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
577 android.os.Handler()) {
578 protected void onReceiveResult(int resultCode, Bundle resultData) {
579 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
580 }
581 });
582 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
583 }
584 }
585 */
586
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 private boolean acceptFilter() {
588 final InputMethodManager inputManager = (InputMethodManager)
589 getSystemService(Context.INPUT_METHOD_SERVICE);
590 return !inputManager.isFullscreenMode();
591 }
592
593 @Override
594 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700595 final int uniChar = event.getUnicodeChar();
596 final boolean handled = super.onKeyDown(keyCode, event);
597 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
598 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
600 keyCode, event);
601 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700602 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700603 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700604 // showSearchDialog()
605 // If there are multiple keystrokes before the search dialog takes focus,
606 // onSearchRequested() will be called for every keystroke,
607 // but it is idempotent, so it's fine.
608 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610 }
611
Joe Onorato8a9625e2010-01-28 15:55:35 -0800612 // Eat the long press event so the keyboard doesn't come up.
613 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
614 return true;
615 }
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 return handled;
618 }
619
Karl Rosaen138a0412009-04-23 19:00:21 -0700620 private String getTypedText() {
621 return mDefaultKeySsb.toString();
622 }
623
624 private void clearTypedText() {
625 mDefaultKeySsb.clear();
626 mDefaultKeySsb.clearSpans();
627 Selection.setSelection(mDefaultKeySsb, 0);
628 }
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700631 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
632 * State
633 */
634 private static State intToState(int stateOrdinal) {
635 State state = State.WORKSPACE;
636 final State[] stateValues = State.values();
637 for (int i = 0; i < stateValues.length; i++) {
638 if (stateValues[i].ordinal() == stateOrdinal) {
639 state = stateValues[i];
640 break;
641 }
642 }
643 return state;
644 }
645
646 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 * Restores the previous state, if it exists.
648 *
649 * @param savedState The previous state.
650 */
651 private void restoreState(Bundle savedState) {
652 if (savedState == null) {
653 return;
654 }
655
Michael Jurka883f55b2010-10-21 15:47:14 -0700656 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
657
Winson Chungf0ea4d32011-06-06 14:27:16 -0700658 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800659 showAllApps(false);
660 }
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
663 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700664 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
666
Winson Chung3d503fb2011-07-13 17:25:49 -0700667 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
668 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669
Winson Chung3d503fb2011-07-13 17:25:49 -0700670
671 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
672 mPendingAddInfo.container = pendingAddContainer;
673 mPendingAddInfo.screen = pendingAddScreen;
674 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
675 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 mRestoring = true;
677 }
678
679 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
680 if (renameFolder) {
681 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700682 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 mRestoring = true;
684 }
Winson Chunga12a2502010-12-20 14:41:35 -0800685
Winson Chung785d2eb2011-04-14 16:08:02 -0700686
687 // Restore the AppsCustomize tab
688 if (mAppsCustomizeTabHost != null) {
689 String curTab = savedState.getString("apps_customize_currentTab");
690 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700691 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700692 mAppsCustomizeContent.setContentType(
693 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
694 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
695 }
696
697 // Note: currently we do not restore the page for the AppsCustomize pane because the
698 // change in layout can drastically affect the saved page index
699 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 }
701
702 /**
703 * Finds all the views we need and configure them properly.
704 */
705 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700706 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400707
Winson Chung4c98d922011-05-31 16:50:48 -0700708 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
709 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710
Winson Chung4c98d922011-05-31 16:50:48 -0700711 // Setup the drag layer
712 mDragLayer.setup(this, dragController);
713
Winson Chung3d503fb2011-07-13 17:25:49 -0700714 // Setup the hotseat
715 mHotseat = (Hotseat) findViewById(R.id.hotseat);
716 if (mHotseat != null) {
717 mHotseat.setup(this);
718 }
719
Winson Chung4c98d922011-05-31 16:50:48 -0700720 // Setup the workspace
721 mWorkspace.setHapticFeedbackEnabled(false);
722 mWorkspace.setOnLongClickListener(this);
723 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700724 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700725
Winson Chungf0ea4d32011-06-06 14:27:16 -0700726 // Get the search/delete bar
727 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700728
Winson Chungf0ea4d32011-06-06 14:27:16 -0700729 // Setup AppsCustomize
730 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
731 findViewById(R.id.apps_customize_pane);
732 mAppsCustomizeContent = (AppsCustomizePagedView)
733 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
734 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400735
Romain Guy1fbc1c82009-11-09 20:43:08 -0800736
Winson Chung4c98d922011-05-31 16:50:48 -0700737
Winson Chung3d503fb2011-07-13 17:25:49 -0700738 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700739 dragController.setDragScoller(mWorkspace);
740 dragController.setScrollView(mDragLayer);
741 dragController.setMoveTarget(mWorkspace);
742 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700743 if (mSearchDeleteBar != null) {
744 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747
Romain Guy8a73c512009-11-09 19:19:59 -0800748
Winson Chungaafa03c2010-06-11 17:34:16 -0700749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 /**
751 * Creates a view representing a shortcut.
752 *
753 * @param info The data structure describing the shortcut.
754 *
755 * @return A View inflated from R.layout.application.
756 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800757 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700759 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 /**
763 * Creates a view representing a shortcut inflated from the specified resource.
764 *
765 * @param layoutResId The id of the XML layout used to create the shortcut.
766 * @param parent The group the shortcut belongs to.
767 * @param info The data structure describing the shortcut.
768 *
769 * @return A View inflated from layoutResId.
770 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800771 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800772 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
773 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 return favorite;
776 }
777
778 /**
779 * Add an application shortcut to the workspace.
780 *
781 * @param data The intent describing the application.
782 * @param cellInfo The position on screen where to create the shortcut.
783 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787
Adam Cohenfbba09b2011-07-18 21:43:05 -0700788 // First we check if we already know the exact location where we want to add this item.
789 if (cellX >= 0 && cellY >= 0) {
790 cellXY[0] = cellX;
791 cellXY[1] = cellY;
792 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793 showOutOfSpaceMessage();
794 return;
795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800797 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800798
Romain Guy73b979d2009-06-09 12:57:21 -0700799 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800800 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800802 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700804 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800805 } else {
806 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808 }
Romain Guycbb89e42009-06-08 15:52:54 -0700809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 /**
811 * Add a shortcut to the workspace.
812 *
813 * @param data The intent describing the shortcut.
814 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700816 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
817 int cellY) {
818 int[] cellXY = mTmpAddItemCellCoordinates;
819 int[] touchXY = mPendingAddInfo.dropPos;
820 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700821
Michael Jurkad3ef3062010-11-23 16:23:58 -0800822 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700823
824 // First we check if we already know the exact location where we want to add this item.
825 if (cellX >= 0 && cellY >= 0) {
826 cellXY[0] = cellX;
827 cellXY[1] = cellY;
828 foundCellSpan = true;
829 } 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
842 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
845 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700847 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
848 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 }
850 }
851
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700855 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700856 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700858 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700859 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700861 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700862 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700863
864 // We want to account for the extra amount of padding that we are adding to the widget
865 // to ensure that it gets the full amount of space that it has requested
866 Resources r = getResources();
867 int requiredWidth = appWidgetInfo.minWidth +
868 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
869 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
870 int requiredHeight = appWidgetInfo.minHeight +
871 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
872 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
873 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700874
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700876 // We have saved the position to which the widget was dragged-- this really only matters
877 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700878 int[] cellXY = mTmpAddItemCellCoordinates;
879 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700881 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
882 cellXY[0] = mPendingAddInfo.cellX;
883 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700884 foundCellSpan = true;
885 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700888 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800889 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700890 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700891 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700892 }
893
Michael Jurka0280c3b2010-09-17 15:00:07 -0700894 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800895 if (appWidgetId != -1) {
896 // Deleting an app widget ID is a void call but writes to disk before returning
897 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800898 new Thread("deleteAppWidgetId") {
899 public void run() {
900 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
901 }
902 }.start();
903 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700904 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800905 return;
906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700908 // Build Launcher-specific widget info and save to database
909 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700910 launcherInfo.spanX = spanXY[0];
911 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915
916 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700919
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700922
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400924 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700925
926 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 }
928 }
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Adam Cohended9f8d2010-11-03 13:25:16 -0700930 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
931 @Override
932 public void onReceive(Context context, Intent intent) {
933 final String action = intent.getAction();
934 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
935 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700936 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700937 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700938
939 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -0700940 if (mAppsCustomizeContent != null) {
941 mAppsCustomizeContent.reset();
942 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700943 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
944 mUserPresent = true;
945 updateRunning();
946 }
947 }
948 };
949
950 @Override
951 public void onAttachedToWindow() {
952 super.onAttachedToWindow();
953
954 // Listen for broadcasts related to user-presence
955 final IntentFilter filter = new IntentFilter();
956 filter.addAction(Intent.ACTION_SCREEN_OFF);
957 filter.addAction(Intent.ACTION_USER_PRESENT);
958 registerReceiver(mReceiver, filter);
959
Adam Cohend113e0c2010-11-11 10:48:05 -0800960 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -0700961 mVisible = true;
962 }
963
964 @Override
965 public void onDetachedFromWindow() {
966 super.onDetachedFromWindow();
967 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700968 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700969
Adam Cohend113e0c2010-11-11 10:48:05 -0800970 if (mAttached) {
971 unregisterReceiver(mReceiver);
972 mAttached = false;
973 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700974 updateRunning();
975 }
976
977 public void onWindowVisibilityChanged(int visibility) {
978 mVisible = visibility == View.VISIBLE;
979 updateRunning();
980 }
981
982 private void sendAdvanceMessage(long delay) {
983 mHandler.removeMessages(ADVANCE_MSG);
984 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
985 mHandler.sendMessageDelayed(msg, delay);
986 mAutoAdvanceSentTime = System.currentTimeMillis();
987 }
988
989 private void updateRunning() {
990 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
991 if (autoAdvanceRunning != mAutoAdvanceRunning) {
992 mAutoAdvanceRunning = autoAdvanceRunning;
993 if (autoAdvanceRunning) {
994 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
995 sendAdvanceMessage(delay);
996 } else {
997 if (!mWidgetsToAdvance.isEmpty()) {
998 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
999 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1000 }
1001 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001002 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001003 }
1004 }
1005 }
1006
1007 private final Handler mHandler = new Handler() {
1008 @Override
1009 public void handleMessage(Message msg) {
1010 if (msg.what == ADVANCE_MSG) {
1011 int i = 0;
1012 for (View key: mWidgetsToAdvance.keySet()) {
1013 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1014 final int delay = mAdvanceStagger * i;
1015 if (v instanceof Advanceable) {
1016 postDelayed(new Runnable() {
1017 public void run() {
1018 ((Advanceable) v).advance();
1019 }
1020 }, delay);
1021 }
1022 i++;
1023 }
1024 sendAdvanceMessage(mAdvanceInterval);
1025 }
1026 }
1027 };
1028
1029 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001030 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001031 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1032 if (v instanceof Advanceable) {
1033 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001034 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001035 updateRunning();
1036 }
1037 }
1038
1039 void removeWidgetToAutoAdvance(View hostView) {
1040 if (mWidgetsToAdvance.containsKey(hostView)) {
1041 mWidgetsToAdvance.remove(hostView);
1042 updateRunning();
1043 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 }
1045
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001047 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001048 launcherInfo.hostView = null;
1049 }
1050
Adam Cohended9f8d2010-11-03 13:25:16 -07001051 void showOutOfSpaceMessage() {
1052 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1053 }
1054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 public LauncherAppWidgetHost getAppWidgetHost() {
1056 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
Romain Guycbb89e42009-06-08 15:52:54 -07001058
Winson Chunga9abd0e2010-10-27 17:18:37 -07001059 public LauncherModel getModel() {
1060 return mModel;
1061 }
1062
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001063 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001064 getWindow().closeAllPanels();
1065
1066 try {
1067 dismissDialog(DIALOG_CREATE_SHORTCUT);
1068 // Unlock the workspace if the dialog was showing
1069 } catch (Exception e) {
1070 // An exception is thrown if the dialog is not visible, which is fine
1071 }
1072
1073 try {
1074 dismissDialog(DIALOG_RENAME_FOLDER);
1075 // Unlock the workspace if the dialog was showing
1076 } catch (Exception e) {
1077 // An exception is thrown if the dialog is not visible, which is fine
1078 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001079
1080 // Whatever we were doing is hereby canceled.
1081 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001082 }
1083
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 @Override
1085 protected void onNewIntent(Intent intent) {
1086 super.onNewIntent(intent);
1087
1088 // Close the menu
1089 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001090 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001091 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001092
Adam Cohen95bb8002011-07-03 23:40:28 -07001093 closeFolder();
1094
Joe Onorato14f122b2009-11-19 14:06:36 -08001095 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1096 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001097
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001098 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001099 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001100 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001101 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001102 }
Winson Chungde1af762011-07-21 16:44:07 -07001103 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001104 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001105
Joe Onorato3a8820b2009-11-10 15:06:42 -08001106 final View v = getWindow().peekDecorView();
1107 if (v != null && v.getWindowToken() != null) {
1108 InputMethodManager imm = (InputMethodManager)getSystemService(
1109 INPUT_METHOD_SERVICE);
1110 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001112
Michael Jurka35aa14d2011-07-07 17:01:08 -07001113 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001114 if (mAppsCustomizeContent != null) {
1115 mAppsCustomizeContent.reset();
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
1118 }
1119
1120 @Override
1121 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1122 // Do not call super here
1123 mSavedInstanceState = savedInstanceState;
1124 }
1125
1126 @Override
1127 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001128 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001129 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
Michael Jurka883f55b2010-10-21 15:47:14 -07001131 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001132 // We close any open folder since it will not be re-opened, and we need to make sure
1133 // this state is reflected.
1134 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1137 mWaitingForResult) {
1138 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1139 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1140 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1141 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142 }
1143
1144 if (mFolderInfo != null && mWaitingForResult) {
1145 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1146 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1147 }
Michael Jurka946ad472010-07-09 18:05:18 -07001148
Winson Chung785d2eb2011-04-14 16:08:02 -07001149 // Save the current AppsCustomize tab
1150 if (mAppsCustomizeTabHost != null) {
1151 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1152 if (currentTabTag != null) {
1153 outState.putString("apps_customize_currentTab", currentTabTag);
1154 }
1155 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
1158 @Override
1159 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001161
Winson Chungcd2b0142011-06-08 16:02:26 -07001162 // Stop callbacks from LauncherModel
1163 LauncherApplication app = ((LauncherApplication) getApplication());
1164 mModel.stopLoader();
1165 app.setLauncher(null);
1166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001168 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001170 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001172 mAppWidgetHost = null;
1173
1174 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175
1176 TextKeyListener.getInstance().release();
1177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Winson Chung3d503fb2011-07-13 17:25:49 -07001179 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001180
1181 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001182 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001183
1184 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1185 mWorkspace.removeAllViews();
1186 mWorkspace = null;
1187 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001188
1189 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
Adam Cohena9cf38f2011-05-02 15:36:58 -07001192 public DragController getDragController() {
1193 return mDragController;
1194 }
1195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 @Override
1197 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001198 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 super.startActivityForResult(intent, requestCode);
1200 }
1201
1202 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001203 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001205
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001206 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Karl Rosaen138a0412009-04-23 19:00:21 -07001208 if (initialQuery == null) {
1209 // Use any text typed in the launcher as the initial query
1210 initialQuery = getTypedText();
1211 clearTypedText();
1212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 if (appSearchData == null) {
1214 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001215 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Karl Rosaen138a0412009-04-23 19:00:21 -07001218 final SearchManager searchManager =
1219 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001220 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001221 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001222 }
1223
1224 @Override
1225 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001226 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001227 return false;
1228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
1230 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1233 .setIcon(android.R.drawable.ic_menu_add)
1234 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001235 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1236 .setIcon(android.R.drawable.ic_menu_manage)
1237 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001238 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 .setIcon(android.R.drawable.ic_menu_gallery)
1240 .setAlphabeticShortcut('W');
1241 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1242 .setIcon(android.R.drawable.ic_search_category_default)
1243 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1244 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1245 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1246 .setAlphabeticShortcut('N');
1247
1248 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001249 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1250 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251
1252 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1253 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1254 .setIntent(settings);
1255
1256 return true;
1257 }
1258
1259 @Override
1260 public boolean onPrepareOptionsMenu(Menu menu) {
1261 super.onPrepareOptionsMenu(menu);
1262
Winson Chung785d2eb2011-04-14 16:08:02 -07001263 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1264 // related code?
1265 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
1267 return true;
1268 }
1269
1270 @Override
1271 public boolean onOptionsItemSelected(MenuItem item) {
1272 switch (item.getItemId()) {
1273 case MENU_ADD:
1274 addItems();
1275 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001276 case MENU_MANAGE_APPS:
1277 manageApps();
1278 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 case MENU_WALLPAPER_SETTINGS:
1280 startWallpaper();
1281 return true;
1282 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001283 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 return true;
1285 case MENU_NOTIFICATIONS:
1286 showNotifications();
1287 return true;
1288 }
1289
1290 return super.onOptionsItemSelected(item);
1291 }
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Karl Rosaen138a0412009-04-23 19:00:21 -07001293 /**
1294 * Indicates that we want global search for this activity by setting the globalSearch
1295 * argument for {@link #startSearch} to true.
1296 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001298 @Override
1299 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001300 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001301 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001302 }
1303
Joe Onorato9c1289c2009-08-17 11:03:03 -04001304 public boolean isWorkspaceLocked() {
1305 return mWorkspaceLoading || mWaitingForResult;
1306 }
1307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001309 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001310 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 }
1312
Michael Jurka0280c3b2010-09-17 15:00:07 -07001313 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001314 mPendingAddInfo.container = ItemInfo.NO_ID;
1315 mPendingAddInfo.screen = -1;
1316 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1317 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1318 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001319 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001320
Winson Chung7ad01412010-09-27 11:33:03 -07001321 private void manageApps() {
1322 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1323 }
1324
Michael Jurkaaf442092010-06-10 17:01:57 -07001325 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001326 // TODO: catch bad widget exception when sent
1327 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001328 // TODO: Is this log message meaningful?
1329 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001330 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001331 }
1332
Winson Chung55cef262010-10-28 14:14:18 -07001333 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001334 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335
Bjorn Bringert7984c942009-12-09 15:38:25 +00001336 if (appWidget.configure != null) {
1337 // Launch over to configure widget, if needed
1338 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1339 intent.setComponent(appWidget.configure);
1340 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001341 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001342 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1343 intent.putExtra(
1344 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1345 info.mimeType);
1346
1347 final String mimeType = info.mimeType;
1348 final ClipData clipData = (ClipData) info.configurationData;
1349 final ClipDescription clipDesc = clipData.getDescription();
1350 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1351 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001352 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001353 final CharSequence stringData = item.getText();
1354 final Uri uriData = item.getUri();
1355 final Intent intentData = item.getIntent();
1356 final String key =
1357 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1358 if (uriData != null) {
1359 intent.putExtra(key, uriData);
1360 } else if (intentData != null) {
1361 intent.putExtra(key, intentData);
1362 } else if (stringData != null) {
1363 intent.putExtra(key, stringData);
1364 }
1365 break;
1366 }
1367 }
1368 }
Winson Chung55cef262010-10-28 14:14:18 -07001369 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001370
Romain Guy8e633c52010-03-04 12:51:36 -08001371 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001373 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001375
1376 // Exit spring loaded mode if necessary after adding the widget
1377 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379 }
Romain Guycbb89e42009-06-08 15:52:54 -07001380
Adam Cohenfbba09b2011-07-18 21:43:05 -07001381 /**
1382 * Process a shortcut drop.
1383 *
1384 * @param componentName The name of the component
1385 * @param screen The screen where it should be added
1386 * @param cell The cell it should be added to, optional
1387 * @param position The location on the screen where it was dropped, optional
1388 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001389 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1390 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 mPendingAddInfo.container = container;
1393 mPendingAddInfo.screen = screen;
1394 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001395
1396 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001397 mPendingAddInfo.cellX = cell[0];
1398 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001399 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001400
1401 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1402 createShortcutIntent.setComponent(componentName);
1403 processShortcut(createShortcutIntent);
1404 }
1405
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406 /**
1407 * Process a widget drop.
1408 *
1409 * @param info The PendingAppWidgetInfo of the widget being added.
1410 * @param screen The screen where it should be added
1411 * @param cell The cell it should be added to, optional
1412 * @param position The location on the screen where it was dropped, optional
1413 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1415 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001416 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001417 mPendingAddInfo.container = info.container = container;
1418 mPendingAddInfo.screen = info.screen = screen;
1419 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001421 mPendingAddInfo.cellX = cell[0];
1422 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001423 }
1424
1425 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1426 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1427 addAppWidgetImpl(appWidgetId, info);
1428 }
1429
Joe Onoratodeb98af2010-02-19 14:59:39 -08001430 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001431 // Handle case where user selected "Applications"
1432 String applicationName = getResources().getString(R.string.group_applications);
1433 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001434
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001435 if (applicationName != null && applicationName.equals(shortcutName)) {
1436 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1437 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001438
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001439 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1440 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001441 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001442 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001443 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001444 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001445 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447
Winson Chung24ab2f12010-09-16 14:10:47 -07001448 void processWallpaper(Intent intent) {
1449 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1450 }
1451
Winson Chung3d503fb2011-07-13 17:25:49 -07001452 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1453 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001454 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 folderInfo.title = getText(R.string.folder_name);
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1459 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001460 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461
1462 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 FolderIcon newFolder =
1464 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1465 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1466 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001467 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Joe Onorato9c1289c2009-08-17 11:03:03 -04001470 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001471 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001472 }
1473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 private void showNotifications() {
1475 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1476 if (statusBar != null) {
1477 statusBar.expand();
1478 }
1479 }
1480
1481 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001482 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001484 Intent chooser = Intent.createChooser(pickWallpaper,
1485 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001486 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1487 // Removed in Eclair MR1
1488// WallpaperManager wm = (WallpaperManager)
1489// getSystemService(Context.WALLPAPER_SERVICE);
1490// WallpaperInfo wi = wm.getWallpaperInfo();
1491// if (wi != null && wi.getSettingsActivity() != null) {
1492// LabeledIntent li = new LabeledIntent(getPackageName(),
1493// R.string.configure_wallpaper, 0);
1494// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1495// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1496// }
Mike Clerona0618e42009-10-22 13:55:21 -07001497 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001500 /**
1501 * Registers various content observers. The current implementation registers
1502 * only a favorites observer to keep track of the favorites applications.
1503 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001504 private void registerContentObservers() {
1505 ContentResolver resolver = getContentResolver();
1506 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1507 true, mWidgetObserver);
1508 }
1509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 @Override
1511 public boolean dispatchKeyEvent(KeyEvent event) {
1512 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1513 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001515 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001516 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001517 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001518 dumpState();
1519 return true;
1520 }
1521 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001522 }
1523 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1524 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001525 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 return true;
1527 }
1528 }
1529
1530 return super.dispatchKeyEvent(event);
1531 }
1532
Joe Onorato88ec0992009-11-19 13:16:06 -08001533 @Override
1534 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001535 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001536 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001537 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001538 Folder openFolder = mWorkspace.getOpenFolder();
1539 if (openFolder.isEditingName()) {
1540 openFolder.dismissEditingName();
1541 } else {
1542 closeFolder();
1543 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001544 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001545 mWorkspace.exitWidgetResizeMode();
1546
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001547 // Back button is a no-op here, but give at least some feedback for the button press
1548 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001549 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001550 }
1551
Adam Cohen2801caf2011-05-13 20:57:39 -07001552 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 Folder folder = mWorkspace.getOpenFolder();
1554 if (folder != null) {
1555 closeFolder(folder);
1556 }
1557 }
1558
1559 void closeFolder(Folder folder) {
1560 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001561
Michael Jurka8c920dd2011-01-20 14:16:56 -08001562 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 if (parent != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Adam Cohen2801caf2011-05-13 20:57:39 -07001565 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001566 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001568 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570
1571 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001572 * Re-listen when widgets are reset.
1573 */
1574 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001575 if (mAppWidgetHost != null) {
1576 mAppWidgetHost.startListening();
1577 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001578 }
1579
1580 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001581 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1582 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001584 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001585 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001587
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 /**
1589 * Launches the intent referred by the clicked shortcut.
1590 *
1591 * @param v The view representing the clicked shortcut.
1592 */
1593 public void onClick(View v) {
1594 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001595 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001597 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001598 int[] pos = new int[2];
1599 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001600 intent.setSourceBounds(new Rect(pos[0], pos[1],
1601 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001602 boolean success = startActivitySafely(intent, tag);
1603
1604 if (success && v instanceof BubbleTextView) {
1605 mWaitingForResume = (BubbleTextView) v;
1606 mWaitingForResume.setStayPressed(true);
1607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001609 if (v instanceof FolderIcon) {
1610 FolderIcon fi = (FolderIcon) v;
1611 handleFolderClick(fi);
1612 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001613 } else if (v == mAllAppsButton) {
1614 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001615 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001616 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001617 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001618 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
1620 }
1621
Michael Jurka0e260592010-06-30 17:07:39 -07001622 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001623 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001624 // clicking anywhere on the workspace causes the customization drawer to slide down
1625 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001626 return false;
1627 }
1628
Michael Jurkaaf442092010-06-10 17:01:57 -07001629 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001630 * Event handler for the search button
1631 *
1632 * @param v The view that was clicked.
1633 */
1634 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001635 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001636 // Use a custom animation for launching search
1637 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001638 }
1639
1640 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001641 * Event handler for the voice button
1642 *
1643 * @param v The view that was clicked.
1644 */
1645 public void onClickVoiceButton(View v) {
1646 startVoiceSearch();
1647 }
1648
1649 private void startVoiceSearch() {
1650 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1651 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1652 startActivity(intent);
1653 }
1654
1655 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001656 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001657 * enters home screen customization mode.
1658 *
1659 * @param v The view that was clicked.
1660 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001661 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001662 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001663 }
1664
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001665 /**
1666 * Event handler for the "grid" button that appears on the home screen, which
1667 * enters all apps mode.
1668 *
1669 * @param v The view that was clicked.
1670 */
1671 public void onClickAllAppsButton(View v) {
1672 showAllApps(true);
1673 }
1674
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001675 public void onClickAppMarketButton(View v) {
1676 if (mAppMarketIntent != null) {
1677 startActivitySafely(mAppMarketIntent, "app market");
1678 }
1679 }
1680
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001681 void startApplicationDetailsActivity(ComponentName componentName) {
1682 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001683 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1684 Uri.fromParts("package", packageName, null));
1685 startActivity(intent);
1686 }
1687
Patrick Dubroy5539af72010-09-07 15:22:01 -07001688 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1689 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1690 // System applications cannot be installed. For now, show a toast explaining that.
1691 // We may give them the option of disabling apps this way.
1692 int messageId = R.string.uninstall_system_app_text;
1693 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1694 } else {
1695 String packageName = appInfo.componentName.getPackageName();
1696 String className = appInfo.componentName.getClassName();
1697 Intent intent = new Intent(
1698 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1699 startActivity(intent);
1700 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001701 }
1702
Michael Jurkaddd62e92011-02-16 17:49:14 -08001703 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1705 try {
1706 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001707 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 } catch (ActivityNotFoundException e) {
1709 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001710 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 } catch (SecurityException e) {
1712 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001713 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001715 "or use the exported attribute for this activity. "
1716 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001717 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001718 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001720
Romain Guy8e633c52010-03-04 12:51:36 -08001721 void startActivityForResultSafely(Intent intent, int requestCode) {
1722 try {
1723 startActivityForResult(intent, requestCode);
1724 } catch (ActivityNotFoundException e) {
1725 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1726 } catch (SecurityException e) {
1727 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1728 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1729 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1730 "or use the exported attribute for this activity.", e);
1731 }
1732 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733
Adam Cohena9cf38f2011-05-02 15:36:58 -07001734 private void handleFolderClick(FolderIcon folderIcon) {
1735 final FolderInfo info = folderIcon.mInfo;
1736 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 // Close any open folder
1738 closeFolder();
1739 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001740 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 } else {
1742 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001743 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744 int folderScreen;
1745 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001746 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 // .. and close it
1748 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001749 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 // Close any folder open on the current screen
1751 closeFolder();
1752 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001753 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
1755 }
1756 }
1757 }
1758
Adam Cohen2801caf2011-05-13 20:57:39 -07001759 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1760 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1761 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1762 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1763
1764 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1765 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1766 oa.start();
1767 }
1768
1769 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1770 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1771 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1772 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1773
1774 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1775 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1776 oa.start();
1777 }
1778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001780 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 * is animated relative to the specified View. If the View is null, no animation
1782 * is played.
1783 *
1784 * @param folderInfo The FolderInfo describing the folder to open.
1785 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001786 public void openFolder(FolderIcon folderIcon) {
1787 Folder folder = folderIcon.mFolder;
1788 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789
Adam Cohen2801caf2011-05-13 20:57:39 -07001790 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001791 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792
Adam Cohen8e776a62011-06-28 18:10:06 -07001793 mDragLayer.addView(folder);
1794 mDragController.addDropTarget((DropTarget) folder);
1795
Adam Cohena9cf38f2011-05-02 15:36:58 -07001796 folder.animateOpen();
1797 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001801 if (mState != State.WORKSPACE) {
1802 return false;
1803 }
1804
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 return false;
1807 }
1808
1809 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001810 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
1812
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 resetAddInfo();
1814 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001816 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 return true;
1818 }
1819
Winson Chung3d503fb2011-07-13 17:25:49 -07001820 // The hotseat touch handling does not go through Workspace, and we always allow long press
1821 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001823 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1824 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 if (itemUnderLongClick == null) {
1826 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1828 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001829 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001831 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001833 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1834 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
1838 }
1839 return true;
1840 }
1841
Winson Chung3d503fb2011-07-13 17:25:49 -07001842 boolean isHotseatLayout(View layout) {
1843 return mHotseat != null && layout != null &&
1844 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1845 }
1846 Hotseat getHotseat() {
1847 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001848 }
1849
Winson Chung3d503fb2011-07-13 17:25:49 -07001850 /**
1851 * Returns the CellLayout of the specified container at the specified screen.
1852 */
1853 CellLayout getCellLayout(long container, int screen) {
1854 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1855 if (mHotseat != null) {
1856 return mHotseat.getLayout();
1857 } else {
1858 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001859 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001860 } else {
1861 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001862 }
1863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 Workspace getWorkspace() {
1866 return mWorkspace;
1867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 @Override
1870 protected Dialog onCreateDialog(int id) {
1871 switch (id) {
1872 case DIALOG_CREATE_SHORTCUT:
1873 return new CreateShortcut().createDialog();
1874 case DIALOG_RENAME_FOLDER:
1875 return new RenameFolder().createDialog();
1876 }
1877
1878 return super.onCreateDialog(id);
1879 }
1880
1881 @Override
1882 protected void onPrepareDialog(int id, Dialog dialog) {
1883 switch (id) {
1884 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 break;
1886 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001887 if (mFolderInfo != null) {
1888 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1889 final CharSequence text = mFolderInfo.title;
1890 input.setText(text);
1891 input.setSelection(0, text.length());
1892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 break;
1894 }
1895 }
1896
1897 void showRenameDialog(FolderInfo info) {
1898 mFolderInfo = info;
1899 mWaitingForResult = true;
1900 showDialog(DIALOG_RENAME_FOLDER);
1901 }
1902
Adam Cohenfbba09b2011-07-18 21:43:05 -07001903 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001904 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1906 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 mWaitingForResult = true;
1908 showDialog(DIALOG_CREATE_SHORTCUT);
1909 }
1910
Joe Onoratodeb98af2010-02-19 14:59:39 -08001911 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001912 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001913 Bundle bundle = new Bundle();
1914
1915 ArrayList<String> shortcutNames = new ArrayList<String>();
1916 shortcutNames.add(getString(R.string.group_applications));
1917 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1918
1919 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1920 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1921 R.drawable.ic_launcher_application));
1922 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1923
1924 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1925 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001926 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001927 pickIntent.putExtras(bundle);
1928
Joe Onoratodeb98af2010-02-19 14:59:39 -08001929 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001930 }
1931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 private class RenameFolder {
1933 private EditText mInput;
1934
1935 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1937 mInput = (EditText) layout.findViewById(R.id.folder_name);
1938
1939 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1940 builder.setIcon(0);
1941 builder.setTitle(getString(R.string.rename_folder_title));
1942 builder.setCancelable(true);
1943 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1944 public void onCancel(DialogInterface dialog) {
1945 cleanup();
1946 }
1947 });
1948 builder.setNegativeButton(getString(R.string.cancel_action),
1949 new Dialog.OnClickListener() {
1950 public void onClick(DialogInterface dialog, int which) {
1951 cleanup();
1952 }
1953 }
1954 );
1955 builder.setPositiveButton(getString(R.string.rename_action),
1956 new Dialog.OnClickListener() {
1957 public void onClick(DialogInterface dialog, int which) {
1958 changeFolderName();
1959 }
1960 }
1961 );
1962 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07001963
1964 final AlertDialog dialog = builder.create();
1965 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
1966 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07001967 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05001968 mInput.requestFocus();
1969 InputMethodManager inputManager = (InputMethodManager)
1970 getSystemService(Context.INPUT_METHOD_SERVICE);
1971 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07001972 }
1973 });
1974
1975 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
1978 private void changeFolderName() {
1979 final String name = mInput.getText().toString();
1980 if (!TextUtils.isEmpty(name)) {
1981 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001982 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 mFolderInfo.title = name;
1984 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
1985
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07001987 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001988 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 } else {
1990 final FolderIcon folderIcon = (FolderIcon)
1991 mWorkspace.getViewForTag(mFolderInfo);
1992 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001993 // TODO: At some point we'll probably want some version of setting
1994 // the text for a folder icon.
1995 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 getWorkspace().requestLayout();
1997 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07001998 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04001999 mWorkspaceLoading = true;
2000 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 }
2002 }
2003 }
2004 cleanup();
2005 }
2006
2007 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 dismissDialog(DIALOG_RENAME_FOLDER);
2009 mWaitingForResult = false;
2010 mFolderInfo = null;
2011 }
2012 }
2013
Daniel Sandler843e8602010-06-07 14:59:01 -04002014 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2015 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002016 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002017 }
2018
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002019 // AllAppsView.Watcher
2020 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002021 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002022 mWorkspace.setVisibility(View.GONE);
2023 }
2024 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002025
2026 /**
2027 * Helper method for the cameraZoomIn/cameraZoomOut animations
2028 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002029 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002030 * @param scaleFactor The scale factor used for the zoom
2031 */
2032 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2033 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002034
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002035 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002036 // Set pivotY so that at the starting zoom factor, the view is partially
2037 // visible. Modifying initialHeightFactor changes how much of the view is
2038 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002039 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002040 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002041 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002042 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002043 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002044 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002045 }
2046 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002047
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002048 /**
2049 * Zoom the camera out from the workspace to reveal 'toView'.
2050 * Assumes that the view to show is anchored at either the very top or very bottom
2051 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002052 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002053 */
Winson Chungc07918d2011-07-01 15:35:26 -07002054 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002055 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002056
Winson Chungf0ea4d32011-06-06 14:27:16 -07002057 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2058 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2059 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2060 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002061
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002062 setPivotsForZoom(toView, toState, scale);
2063
Michael Jurkad74c9842011-07-10 12:44:21 -07002064 // Shrink workspaces away if going to AppsCustomize from workspace
2065 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002066
2067 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002068 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002069 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002070 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2071 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002072 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002073 toView.setFastScaleX(a * scale + b * 1f);
2074 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002075 }
2076 });
Adam Cohen61033d32010-11-15 18:29:44 -08002077
Winson Chungf0ea4d32011-06-06 14:27:16 -07002078 toView.setVisibility(View.VISIBLE);
2079 toView.setFastAlpha(0f);
2080 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2081 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2082 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2083 public void onAnimationUpdate(float a, float b) {
2084 // don't need to invalidate because we do so above
2085 toView.setFastAlpha(a * 0f + b * 1f);
2086 }
2087 });
2088 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002089
Michael Jurkaabded662011-03-04 12:06:57 -08002090 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002091 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002092 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002093 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002094 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002095 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002096 // Prepare the position
2097 toView.setTranslationX(0.0f);
2098 toView.setTranslationY(0.0f);
2099 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002100 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002101 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002102 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002103 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002104 // If we don't set the final scale values here, if this animation is cancelled
2105 // it will have the wrong scale value and subsequent cameraPan animations will
2106 // not fix that
2107 toView.setScaleX(1.0f);
2108 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002109 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002110 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002111 }
Winson Chung32174c82011-07-19 15:47:55 -07002112
2113 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2114 // Hide the workspace scrollbar
2115 mWorkspace.hideScrollingIndicator(true);
Winson Chung32174c82011-07-19 15:47:55 -07002116 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002117 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002118 });
2119
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002120 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002121 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002122
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002123 if (mStateAnimation != null) mStateAnimation.cancel();
2124 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002125 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002126 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002127 } else {
2128 toView.setTranslationX(0.0f);
2129 toView.setTranslationY(0.0f);
2130 toView.setScaleX(1.0f);
2131 toView.setScaleY(1.0f);
2132 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002133 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002134 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002135 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2136 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002137
Winson Chungc07918d2011-07-01 15:35:26 -07002138 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2139 // Hide the workspace scrollbar
2140 mWorkspace.hideScrollingIndicator(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002141 }
Michael Jurkaabded662011-03-04 12:06:57 -08002142 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002143 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002144 }
2145
2146 /**
2147 * Zoom the camera back into the workspace, hiding 'fromView'.
2148 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002149 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002150 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002151 */
Winson Chungc07918d2011-07-01 15:35:26 -07002152 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002153 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002154
Winson Chungf0ea4d32011-06-06 14:27:16 -07002155 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2156 final float scaleFactor = (float)
2157 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2158 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002159
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002160 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002161
Michael Jurkad3ef3062010-11-23 16:23:58 -08002162 if (!springLoaded) {
2163 mWorkspace.unshrink(animated);
2164 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002165 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002166 if (mStateAnimation != null) mStateAnimation.cancel();
2167 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002168
Michael Jurka742574b2011-02-02 23:51:01 -08002169 final float oldScaleX = fromView.getScaleX();
2170 final float oldScaleY = fromView.getScaleY();
2171
2172 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2173 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002174 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2175 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002176 ((View)fromView.getParent()).fastInvalidate();
2177 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2178 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2179 }
2180 });
Michael Jurkaabded662011-03-04 12:06:57 -08002181 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002182 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002183 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002184 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2185 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002186 // don't need to invalidate because we do so above
2187 fromView.setFastAlpha(a * 1f + b * 0f);
2188 }
2189 });
Michael Jurkaabded662011-03-04 12:06:57 -08002190 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002191 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002192 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002193 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002194 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002195 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002196 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002197 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002198 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002199 }
Winson Chung5a808352011-06-27 19:08:49 -07002200 mWorkspace.flashScrollingIndicator();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002201 }
2202 });
2203
Winson Chungf0ea4d32011-06-06 14:27:16 -07002204 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002205 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002206 } else {
2207 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002208 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002209 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2210 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Winson Chung3ac74c52011-06-30 17:39:37 -07002211
Winson Chungc07918d2011-07-01 15:35:26 -07002212 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chungf5f8cef2011-07-22 11:16:13 -07002213 // Flash the workspace scrollbar
Winson Chungc07918d2011-07-01 15:35:26 -07002214 mWorkspace.flashScrollingIndicator();
2215 }
Michael Jurkaabded662011-03-04 12:06:57 -08002216 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002217 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002218 }
2219
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002220 void showWorkspace(boolean animated) {
2221 showWorkspace(animated, null);
2222 }
2223
2224 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002225 if (layout != null) {
2226 // always animated, but that's ok since we never specify a layout and
2227 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 mWorkspace.unshrink(layout);
2229 } else {
2230 mWorkspace.unshrink(animated);
2231 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002232 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002233 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002234 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002235
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002236 // Change the state *after* we've called all the transition code
2237 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002238
Winson Chung5fb63472011-02-02 17:03:37 -08002239 // Resume the auto-advance of widgets
2240 mUserPresent = true;
2241 updateRunning();
2242
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002243 // send an accessibility event to announce the context change
2244 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002245 }
2246
Michael Jurkad3ef3062010-11-23 16:23:58 -08002247 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002248 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002249 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002250 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002251 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002252 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002253 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002254 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002255 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2256 mWorkspace.postDelayed(new Runnable() {
2257 @Override
2258 public void run() {
2259 exitSpringLoadedDragMode();
2260 }
2261 }, (extendedDelay ?
2262 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2263 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2264 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002265 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002266 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002267 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002268 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2269 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002270 }
2271 // Otherwise, we are not in spring loaded mode, so don't do anything.
2272 }
2273
Adam Cohenfc53cd22011-07-20 15:45:11 -07002274 public boolean isAllAppsCustomizeOpen() {
2275 return mState == State.APPS_CUSTOMIZE;
2276 }
2277
Winson Chungf0ea4d32011-06-06 14:27:16 -07002278 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002280 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002282 if (!LauncherApplication.isScreenLarge()) {
2283 if (animated) {
2284 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002286 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 }
2289 }
2290 }
2291
2292 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002294 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002295 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002296 if (!LauncherApplication.isScreenLarge()) {
2297 if (animated) {
2298 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002299 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002300 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002301 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002302 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002303 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002304 }
2305
Winson Chung785d2eb2011-04-14 16:08:02 -07002306 void showAllApps(boolean animated) {
2307 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002308
Winson Chungf0ea4d32011-06-06 14:27:16 -07002309 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2310 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002311
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002313 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 hideHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002315
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 // Change the state *after* we've called all the transition code
2317 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002318
2319 // Pause the auto-advance of widgets until we are out of AllApps
2320 mUserPresent = false;
2321 updateRunning();
2322
2323 // Send an accessibility event to announce the context change
2324 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2325 }
2326
Joe Onoratob2061212009-11-24 16:13:54 -05002327 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002328 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002329 * - Home from workspace
2330 * - from center screen
2331 * - from other screens
2332 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002333 * - from center screen
2334 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002335 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002336 * - from center screen
2337 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002338 * - Launch app from workspace and quit
2339 * - with back
2340 * - with home
2341 * - Launch app from all apps and quit
2342 * - with back
2343 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002344 * - Go to a screen that's not the default, then all
2345 * apps, and launch and app, and go back
2346 * - with back
2347 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002348 * - On workspace, long press power and go back
2349 * - with back
2350 * - with home
2351 * - On all apps, long press power and go back
2352 * - with back
2353 * - with home
2354 * - On workspace, power off
2355 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002356 * - Launch an app and turn off the screen while in that app
2357 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002358 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002359 * - From all apps
2360 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002361 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2362 * - From all apps
2363 * - From the center workspace
2364 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002365 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002366 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002367 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2368 mWorkspace.setVisibility(View.VISIBLE);
2369 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002370
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002372 mSearchDeleteBar.showSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 showHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002374
Winson Chungf0ea4d32011-06-06 14:27:16 -07002375 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 if (mAllAppsButton != null) {
2377 mAllAppsButton.requestFocus();
2378 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002379 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002380 }
2381
Joe Onorato7c312c12009-08-13 21:36:53 -07002382 void lockAllApps() {
2383 // TODO
2384 }
2385
2386 void unlockAllApps() {
2387 // TODO
2388 }
2389
Patrick Dubroy5f445422011-02-18 14:35:21 -08002390 /**
2391 * Add an item from all apps or customize onto the given workspace screen.
2392 * If layout is null, add to the current screen.
2393 */
2394 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002395 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2396 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002397 } else {
2398 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002399 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002400 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002401
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002402 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002403 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002404 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002405 // Look for the toolbar icon specified in the activity meta-data
2406 Bundle metaData = packageManager.getActivityInfo(
2407 activityName, PackageManager.GET_META_DATA).metaData;
2408 if (metaData != null) {
2409 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2410 if (iconResId != 0) {
2411 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002412 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002413 }
2414 }
2415 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002416 // This can happen if the activity defines an invalid drawable
2417 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2418 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002419 } catch (Resources.NotFoundException nfe) {
2420 // This can happen if the activity defines an invalid drawable
2421 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2422 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002423 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002424 return null;
2425 }
2426
2427 // if successful in getting icon, return it; otherwise, set button to use default drawable
2428 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2429 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2430 TextView button = (TextView) findViewById(buttonId);
2431 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2432
2433 // If we were unable to find the icon via the meta-data, use a generic one
2434 if (toolbarIcon == null) {
2435 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2436 return null;
2437 } else {
2438 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2439 return toolbarIcon.getConstantState();
2440 }
2441 }
2442
2443 // if successful in getting icon, return it; otherwise, set button to use default drawable
2444 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2445 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2446 ImageView button = (ImageView) findViewById(buttonId);
2447 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2448
Michael Jurka19e0fc52011-07-22 18:00:21 -07002449 if (button != null) {
2450 // If we were unable to find the icon via the meta-data, use a
2451 // generic one
2452 if (toolbarIcon == null) {
2453 button.setImageResource(fallbackDrawableId);
2454 } else {
2455 button.setImageDrawable(toolbarIcon);
2456 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002457 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002458
2459 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2460
Michael Jurka0423dcf2010-10-05 14:56:18 -07002461 }
2462
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002463 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2464 TextView button = (TextView) findViewById(buttonId);
2465 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2466 }
2467
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002468 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002469 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002470 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002471 }
2472
Michael Jurka0423dcf2010-10-05 14:56:18 -07002473 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002474 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2475 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002476
Winson Chung1cad91e2011-05-25 17:41:01 -07002477 final SearchManager searchManager =
2478 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2479 ComponentName activityName = searchManager.getGlobalSearchActivity();
2480 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002481 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002482 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002483 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002484 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002485 } else {
2486 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002487 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002488 }
2489 }
2490
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002491 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002492 updateButtonWithDrawable(R.id.search_button, d);
2493 }
2494
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002495 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002496 final View searchDivider = findViewById(R.id.search_divider);
2497 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002498
Winson Chung1cad91e2011-05-25 17:41:01 -07002499 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2500 ComponentName activityName = intent.resolveActivity(getPackageManager());
2501 if (activityName != null) {
2502 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002503 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2504 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002505 voiceButton.setVisibility(View.VISIBLE);
2506 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002507 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002508 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002509 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002510 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002511
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002512 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002513 updateButtonWithDrawable(R.id.voice_button, d);
2514 }
2515
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002516 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002517 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002518 */
2519 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002520 final View marketButton = findViewById(R.id.market_button);
2521 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2522 // Find the app market activity by resolving an intent.
2523 // (If multiple app markets are installed, it will return the ResolverActivity.)
2524 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002525 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002526 mAppMarketIntent = intent;
2527 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002528 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002529 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002530
2531 // Remove the shop icon text in the Phone UI
2532 if (!LauncherApplication.isScreenLarge()) {
2533 ((TextView) marketButton).setText("");
2534 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002535 } else {
2536 // We should hide and disable the view so that we don't try and restore the visibility
2537 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2538 marketButton.setVisibility(View.GONE);
2539 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002540 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002541 }
2542
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002543 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002544 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002545 }
2546
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002547 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 * Displays the shortcut creation dialog and launches, if necessary, the
2549 * appropriate activity.
2550 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002551 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002552 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2553 DialogInterface.OnShowListener {
2554
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002555 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002558 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002559
Winson Chung55b65502011-05-26 12:03:43 -07002560 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2561 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002562 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002563
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 AlertDialog dialog = builder.create();
2565 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002566 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002567 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002569 return dialog;
2570 }
2571
2572 public void onCancel(DialogInterface dialog) {
2573 mWaitingForResult = false;
2574 cleanup();
2575 }
2576
Romain Guycbb89e42009-06-08 15:52:54 -07002577 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002578 mWaitingForResult = false;
2579 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002580 }
2581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002582 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002583 try {
2584 dismissDialog(DIALOG_CREATE_SHORTCUT);
2585 } catch (Exception e) {
2586 // An exception is thrown if the dialog is not visible, which is fine
2587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002588 }
2589
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002590 /**
2591 * Handle the action clicked in the "Add to home" dialog.
2592 */
2593 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002594 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002595
Winson Chung55b65502011-05-26 12:03:43 -07002596 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2597 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002598 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002599 if (mAppsCustomizeTabHost != null) {
Winson Chung5a808352011-06-27 19:08:49 -07002600 mAppsCustomizeTabHost.selectShortcutsTab();
Winson Chungd2945262011-06-24 15:22:14 -07002601 }
2602 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002603 break;
2604 }
Winson Chung55b65502011-05-26 12:03:43 -07002605 case AddAdapter.ITEM_APPLICATION: {
2606 if (mAppsCustomizeTabHost != null) {
2607 mAppsCustomizeTabHost.selectAppsTab();
2608 }
2609 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002610 break;
2611 }
Winson Chung55b65502011-05-26 12:03:43 -07002612 case AddAdapter.ITEM_APPWIDGET: {
2613 if (mAppsCustomizeTabHost != null) {
2614 mAppsCustomizeTabHost.selectWidgetsTab();
2615 }
2616 showAllApps(true);
2617 break;
2618 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002619 case AddAdapter.ITEM_WALLPAPER: {
2620 startWallpaper();
2621 break;
2622 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002623 }
2624 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002625
2626 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002627 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002629 }
2630
2631 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002632 * Receives notifications when applications are added/removed.
2633 */
2634 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2635 @Override
2636 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002637 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002638 String reason = intent.getStringExtra("reason");
2639 if (!"homekey".equals(reason)) {
2640 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002641 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002642 animate = false;
2643 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002644 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002645 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002646 }
2647 }
2648
2649 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002650 * Receives notifications whenever the appwidgets are reset.
2651 */
2652 private class AppWidgetResetObserver extends ContentObserver {
2653 public AppWidgetResetObserver() {
2654 super(new Handler());
2655 }
2656
2657 @Override
2658 public void onChange(boolean selfChange) {
2659 onAppWidgetReset();
2660 }
2661 }
2662
2663 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002664 * If the activity is currently paused, signal that we need to re-run the loader
2665 * in onResume.
2666 *
2667 * This needs to be called from incoming places where resources might have been loaded
2668 * while we are paused. That is becaues the Configuration might be wrong
2669 * when we're not running, and if it comes back to what it was when we
2670 * were paused, we are not restarted.
2671 *
2672 * Implementation of the method from LauncherModel.Callbacks.
2673 *
2674 * @return true if we are currently paused. The caller might be able to
2675 * skip some work in that case since we will come back again.
2676 */
2677 public boolean setLoadOnResume() {
2678 if (mPaused) {
2679 Log.i(TAG, "setLoadOnResume");
2680 mOnResumeNeedsLoad = true;
2681 return true;
2682 } else {
2683 return false;
2684 }
2685 }
2686
2687 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002688 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002690 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002691 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002692 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002693 } else {
2694 return SCREEN_COUNT / 2;
2695 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002696 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002697
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002698
Joe Onorato9c1289c2009-08-17 11:03:03 -04002699 /**
2700 * Refreshes the shortcuts shown on the workspace.
2701 *
2702 * Implementation of the method from LauncherModel.Callbacks.
2703 */
2704 public void startBinding() {
2705 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002706
2707 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002708 int count = workspace.getChildCount();
2709 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002710 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002711 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2712 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002713 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002714 if (mHotseat != null) {
2715 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002717
Adam Cohen4eac29a2011-07-11 17:53:37 -07002718 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002719 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 }
2721
2722 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002723 * Bind the items start-end from the list.
2724 *
2725 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002727 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002728 setLoadOnResume();
2729
Joe Onorato9c1289c2009-08-17 11:03:03 -04002730 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002731 for (int i=start; i<end; i++) {
2732 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002733
2734 // Short circuit if we are loading dock items for a configuration which has no dock
2735 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2736 mHotseat == null) {
2737 continue;
2738 }
2739
Joe Onorato9c1289c2009-08-17 11:03:03 -04002740 switch (item.itemType) {
2741 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2742 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002743 View shortcut = createShortcut((ShortcutInfo)item);
2744 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2745 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002746 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002747 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002748 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002749 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002750 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002751 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2752 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002753 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002754 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002755 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002756 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 }
2758
Joe Onorato9c1289c2009-08-17 11:03:03 -04002759 /**
2760 * Implementation of the method from LauncherModel.Callbacks.
2761 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002762 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002763 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002764 sFolders.clear();
2765 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002767
2768 /**
2769 * Add the views for a widget to the workspace.
2770 *
2771 * Implementation of the method from LauncherModel.Callbacks.
2772 */
2773 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002774 setLoadOnResume();
2775
Daniel Sandler843e8602010-06-07 14:59:01 -04002776 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2777 if (DEBUG_WIDGETS) {
2778 Log.d(TAG, "bindAppWidget: " + item);
2779 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002780 final Workspace workspace = mWorkspace;
2781
2782 final int appWidgetId = item.appWidgetId;
2783 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002784 if (DEBUG_WIDGETS) {
2785 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2786 }
2787
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2789
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2791 item.hostView.setTag(item);
2792
Winson Chung3d503fb2011-07-13 17:25:49 -07002793 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 item.cellY, item.spanX, item.spanY, false);
2795
Adam Cohended9f8d2010-11-03 13:25:16 -07002796 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2797
Joe Onorato9c1289c2009-08-17 11:03:03 -04002798 workspace.requestLayout();
2799
Daniel Sandler843e8602010-06-07 14:59:01 -04002800 if (DEBUG_WIDGETS) {
2801 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2802 + (SystemClock.uptimeMillis()-start) + "ms");
2803 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 }
2805
2806 /**
2807 * Callback saying that there aren't any more items to bind.
2808 *
2809 * Implementation of the method from LauncherModel.Callbacks.
2810 */
2811 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002812 setLoadOnResume();
2813
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 if (mSavedState != null) {
2815 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002816 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 mSavedState = null;
2819 }
2820
2821 if (mSavedInstanceState != null) {
2822 super.onRestoreInstanceState(mSavedInstanceState);
2823 mSavedInstanceState = null;
2824 }
2825
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002827
2828 // If we received the result of any pending adds while the loader was running (e.g. the
2829 // widget configuration forced an orientation change), process them now.
2830 for (int i = 0; i < sPendingAddList.size(); i++) {
2831 completeAdd(sPendingAddList.get(i));
2832 }
2833 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 }
2835
2836 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002837 * Updates the icons on the launcher that are affected by changes to the package list
2838 * on the device.
2839 */
2840 private void updateIconsAffectedByPackageManagerChanges() {
2841 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002842 updateVoiceSearchIcon();
2843 }
2844
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002845 @Override
2846 public void bindSearchablesChanged() {
2847 updateGlobalSearchIcon();
2848 }
2849
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 * Add the icons for all apps.
2852 *
2853 * Implementation of the method from LauncherModel.Callbacks.
2854 */
2855 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002856 if (mAppsCustomizeContent != null) {
2857 mAppsCustomizeContent.setApps(apps);
2858 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002859 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002860 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 }
2862
2863 /**
2864 * A package was installed.
2865 *
2866 * Implementation of the method from LauncherModel.Callbacks.
2867 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002868 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002869 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002871
Winson Chung785d2eb2011-04-14 16:08:02 -07002872 if (mAppsCustomizeContent != null) {
2873 mAppsCustomizeContent.addApps(apps);
2874 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002875 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 }
2877
2878 /**
2879 * A package was updated.
2880 *
2881 * Implementation of the method from LauncherModel.Callbacks.
2882 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002883 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002884 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002886 if (mWorkspace != null) {
2887 mWorkspace.updateShortcuts(apps);
2888 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889
Winson Chung785d2eb2011-04-14 16:08:02 -07002890 if (mAppsCustomizeContent != null) {
2891 mAppsCustomizeContent.updateApps(apps);
2892 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002893 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 }
2895
2896 /**
2897 * A package was uninstalled.
2898 *
2899 * Implementation of the method from LauncherModel.Callbacks.
2900 */
Joe Onorato36115782010-06-17 13:28:48 -04002901 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002902 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002903 if (permanent) {
2904 mWorkspace.removeItems(apps);
2905 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002906
Winson Chung785d2eb2011-04-14 16:08:02 -07002907 if (mAppsCustomizeContent != null) {
2908 mAppsCustomizeContent.removeApps(apps);
2909 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002910 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 }
Joe Onoratobe386092009-11-17 17:32:16 -08002912
2913 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002914 * A number of packages were updated.
2915 */
2916 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002917
Winson Chung785d2eb2011-04-14 16:08:02 -07002918 if (mAppsCustomizeContent != null) {
2919 mAppsCustomizeContent.onPackagesUpdated();
2920 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002921 }
2922
Winson Chung400438b2011-01-16 17:53:48 -08002923 private int mapConfigurationOriActivityInfoOri(int configOri) {
2924 final Display d = getWindowManager().getDefaultDisplay();
2925 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
2926 switch (d.getRotation()) {
2927 case Surface.ROTATION_0:
2928 case Surface.ROTATION_180:
2929 // We are currently in the same basic orientation as the natural orientation
2930 naturalOri = configOri;
2931 break;
2932 case Surface.ROTATION_90:
2933 case Surface.ROTATION_270:
2934 // We are currently in the other basic orientation to the natural orientation
2935 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
2936 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
2937 break;
2938 }
2939
2940 int[] oriMap = {
2941 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
2942 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
2943 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
2944 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
2945 };
2946 // Since the map starts at portrait, we need to offset if this device's natural orientation
2947 // is landscape.
2948 int indexOffset = 0;
2949 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
2950 indexOffset = 1;
2951 }
2952 return oriMap[(d.getRotation() + indexOffset) % 4];
2953 }
2954 public void lockScreenOrientation() {
2955 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
2956 .getConfiguration().orientation));
2957 }
2958 public void unlockScreenOrientation() {
2959 mHandler.postDelayed(new Runnable() {
2960 public void run() {
2961 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
2962 }
2963 }, mRestoreScreenOrientationDelay);
2964 }
2965
Winson Chung80baf5a2010-08-09 16:03:15 -07002966 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002967 * Prints out out state for debugging.
2968 */
2969 public void dumpState() {
2970 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002971 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002972 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2973 Log.d(TAG, "mRestoring=" + mRestoring);
2974 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2975 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002976 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002977 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002978
Winson Chung785d2eb2011-04-14 16:08:02 -07002979 if (mAppsCustomizeContent != null) {
2980 mAppsCustomizeContent.dumpState();
2981 }
Joe Onoratobe386092009-11-17 17:32:16 -08002982 Log.d(TAG, "END launcher2 dump state");
2983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002984}
Michael Jurka2763be32011-02-24 11:19:57 -08002985
Michael Jurkaabded662011-03-04 12:06:57 -08002986interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07002987 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
2988 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08002989}