blob: 3050be4813ec8aa10fbaea8e2644d8035493d0ef [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
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400285 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 setContentView(R.layout.launcher);
288 setupViews();
289
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800290 registerContentObservers();
291
Joe Onorato7c312c12009-08-13 21:36:53 -0700292 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 mSavedState = savedInstanceState;
295 restoreState(mSavedState);
296
Winson Chunga12a2502010-12-20 14:41:35 -0800297 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700298 if (mAppsCustomizeContent != null) {
299 mAppsCustomizeContent.onPackagesUpdated();
300 }
Winson Chunga12a2502010-12-20 14:41:35 -0800301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 if (PROFILE_STARTUP) {
303 android.os.Debug.stopMethodTracing();
304 }
305
306 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
310 // For handling default keys
311 mDefaultKeySsb = new SpannableStringBuilder();
312 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800313
314 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
315 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316
317 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700318 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
319 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100320 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700321 }
322 if (sGlobalSearchIcon != null) {
323 updateGlobalSearchIcon(sGlobalSearchIcon);
324 }
325 if (sVoiceSearchIcon != null) {
326 updateVoiceSearchIcon(sVoiceSearchIcon);
327 }
328 if (sAppMarketIcon != null) {
329 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Romain Guycbb89e42009-06-08 15:52:54 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700334 if (sLocaleConfiguration == null) {
335 new AsyncTask<Void, Void, LocaleConfiguration>() {
336 @Override
337 protected LocaleConfiguration doInBackground(Void... unused) {
338 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
339 readConfiguration(Launcher.this, localeConfiguration);
340 return localeConfiguration;
341 }
342
343 @Override
344 protected void onPostExecute(LocaleConfiguration result) {
345 sLocaleConfiguration = result;
346 checkForLocaleChange(); // recursive, but now with a locale configuration
347 }
348 }.execute();
349 return;
350 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 final Configuration configuration = getResources().getConfiguration();
353
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 final String locale = configuration.locale.toString();
356
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700357 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final int mcc = configuration.mcc;
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final int mnc = configuration.mnc;
362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Romain Guy84f296c2009-11-04 15:00:44 -0800365 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 sLocaleConfiguration.locale = locale;
367 sLocaleConfiguration.mcc = mcc;
368 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700369
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400371 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372
373 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
374 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700375 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 public void run() {
377 writeConfiguration(Launcher.this, localeConfiguration);
378 }
379 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700380 }
381 }
382
383 private static class LocaleConfiguration {
384 public String locale;
385 public int mcc = -1;
386 public int mnc = -1;
387 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700388
Romain Guy98d01652009-06-30 16:21:04 -0700389 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
390 DataInputStream in = null;
391 try {
392 in = new DataInputStream(context.openFileInput(PREFERENCES));
393 configuration.locale = in.readUTF();
394 configuration.mcc = in.readInt();
395 configuration.mnc = in.readInt();
396 } catch (FileNotFoundException e) {
397 // Ignore
398 } catch (IOException e) {
399 // Ignore
400 } finally {
401 if (in != null) {
402 try {
403 in.close();
404 } catch (IOException e) {
405 // Ignore
406 }
407 }
408 }
409 }
410
411 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
412 DataOutputStream out = null;
413 try {
414 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
415 out.writeUTF(configuration.locale);
416 out.writeInt(configuration.mcc);
417 out.writeInt(configuration.mnc);
418 out.flush();
419 } catch (FileNotFoundException e) {
420 // Ignore
421 } catch (IOException e) {
422 //noinspection ResultOfMethodCallIgnored
423 context.getFileStreamPath(PREFERENCES).delete();
424 } finally {
425 if (out != null) {
426 try {
427 out.close();
428 } catch (IOException e) {
429 // Ignore
430 }
431 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433 }
434
Adam Cohen716b51e2011-06-30 12:09:54 -0700435 public DragLayer getDragLayer() {
436 return mDragLayer;
437 }
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 static int getScreen() {
440 synchronized (sLock) {
441 return sScreen;
442 }
443 }
444
445 static void setScreen(int screen) {
446 synchronized (sLock) {
447 sScreen = screen;
448 }
449 }
450
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 // Note: This doesn't do all the client-id magic that BrowserProvider does
452 // in Browser. (http://b/2425179)
453 private Uri getDefaultBrowserUri() {
454 String url = getString(R.string.default_browser_url);
455 if (url.indexOf("{CID}") != -1) {
456 url = url.replace("{CID}", "android-google");
457 }
458 return Uri.parse(url);
459 }
460
461 // Load the Intent templates from arrays.xml to populate the hotseats. For
462 // each Intent, if it resolves to a single app, use that as the launch
463 // intent & use that app's label as the contentDescription. Otherwise,
464 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400465 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400466 if (mHotseatConfig == null) {
467 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
468 if (mHotseatConfig.length > 0) {
469 mHotseats = new Intent[mHotseatConfig.length];
470 mHotseatLabels = new CharSequence[mHotseatConfig.length];
471 mHotseatIcons = new Drawable[mHotseatConfig.length];
472 } else {
473 mHotseats = null;
474 mHotseatIcons = null;
475 mHotseatLabels = null;
476 }
477
478 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
479 for (int i=0; i<mHotseatConfig.length; i++) {
480 // load icon for this slot; currently unrelated to the actual activity
481 try {
482 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
483 } catch (ArrayIndexOutOfBoundsException ex) {
484 Log.w(TAG, "Missing hotseat_icons array item #" + i);
485 mHotseatIcons[i] = null;
486 }
487 }
488 hotseatIconDrawables.recycle();
489 }
490
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400491 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400492 for (int i=0; i<mHotseatConfig.length; i++) {
493 Intent intent = null;
494 if (mHotseatConfig[i].equals("*BROWSER*")) {
495 // magic value meaning "launch user's default web browser"
496 // replace it with a generic web request so we can see if there is indeed a default
497 String defaultUri = getString(R.string.default_browser_url);
498 intent = new Intent(
499 Intent.ACTION_VIEW,
500 ((defaultUri != null)
501 ? Uri.parse(defaultUri)
502 : getDefaultBrowserUri())
503 ).addCategory(Intent.CATEGORY_BROWSABLE);
504 // note: if the user launches this without a default set, she
505 // will always be taken to the default URL above; this is
506 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700507 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 // URL is unavoidably sent to the chosen app.
509 } else {
510 try {
511 intent = Intent.parseUri(mHotseatConfig[i], 0);
512 } catch (java.net.URISyntaxException ex) {
513 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
514 // bogus; leave intent=null
515 }
516 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400518 if (intent == null) {
519 mHotseats[i] = null;
520 mHotseatLabels[i] = getText(R.string.activity_not_found);
521 continue;
522 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523
524 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700525 Log.d(TAG, "loadHotseats: hotseat " + i
526 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400527 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400528 + "]");
529 }
530
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400531 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
532 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700533 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 Log.d(TAG, "Best match for intent: " + bestMatch);
535 Log.d(TAG, "All matches: ");
536 for (ResolveInfo ri : allMatches) {
537 Log.d(TAG, " --> " + ri);
538 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400539 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // did this resolve to a single app, or the resolver?
541 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700542 // can't find any activity to handle this. let's leave the
543 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 // to launch.
545 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 // set accessibility text to "Not installed"
548 mHotseatLabels[i] = getText(R.string.activity_not_found);
549 } else {
550 boolean found = false;
551 for (ResolveInfo ri : allMatches) {
552 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
553 && bestMatch.activityInfo.applicationInfo.packageName
554 .equals(ri.activityInfo.applicationInfo.packageName)) {
555 found = true;
556 break;
557 }
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (!found) {
561 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
562 // the bestMatch is probably the ResolveActivity, meaning the
563 // user has not yet selected a default
564 // so: we'll keep the original intent for now
565 mHotseats[i] = intent;
566
567 // set the accessibility text to "Select shortcut"
568 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
569 } else {
570 // we have an app!
571 // now reconstruct the intent to launch it through the front
572 // door
573 ComponentName com = new ComponentName(
574 bestMatch.activityInfo.applicationInfo.packageName,
575 bestMatch.activityInfo.name);
576 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
577
578 // load the app label for accessibility
579 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
580 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 }
582
583 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 Log.d(TAG, "loadHotseats: hotseat " + i
585 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 + ((mHotseats[i] == null)
587 ? "null"
588 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400589 + "] label=[" + mHotseatLabels[i]
590 + "]"
591 );
592 }
593 }
594 }
595
Winson Chung557d6ed2011-07-08 15:34:52 -0700596 /**
597 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
598 * a configuration step, this allows the proper animations to run after other transitions.
599 */
600 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 switch (args.requestCode) {
602 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700603 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
604 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 break;
606 case REQUEST_PICK_SHORTCUT:
607 processShortcut(args.intent);
608 break;
609 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700610 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
611 args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700612 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800613 case REQUEST_PICK_APPWIDGET:
614 addAppWidgetFromPick(args.intent);
615 break;
616 case REQUEST_CREATE_APPWIDGET:
617 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700618 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700619 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800620 case REQUEST_PICK_WALLPAPER:
621 // We just wanted the activity result here so we can clear mWaitingForResult
622 break;
623 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700624 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 }
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800628 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700629 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700630 mWaitingForResult = false;
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 // The pattern used here is that a user PICKs a specific application,
633 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
636 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chung3d503fb2011-07-13 17:25:49 -0700637 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID &&
638 mPendingAddInfo.screen > -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800639 final PendingAddArguments args = new PendingAddArguments();
640 args.requestCode = requestCode;
641 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700642 args.container = mPendingAddInfo.container;
643 args.screen = mPendingAddInfo.screen;
644 args.cellX = mPendingAddInfo.cellX;
645 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800646
647 // If the loader is still running, defer the add until it is done.
648 if (isWorkspaceLocked()) {
649 sPendingAddList.add(args);
650 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700651 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
Romain Guy18042c82009-11-06 11:44:55 -0800653 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700654 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
655 if (data != null) {
656 // Clean up the appWidgetId if we canceled
657 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
658 if (appWidgetId != -1) {
659 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
662 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700663
664 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
665 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
667
668 @Override
669 protected void onResume() {
670 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800671 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700672 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400673 mWorkspaceLoading = true;
674 mModel.startLoader(this, true);
675 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700676 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800678 if (mWaitingForResume != null) {
679 mWaitingForResume.setStayPressed(false);
680 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700681 // When we resume Launcher, a different Activity might be responsible for the app
682 // market intent, so refresh the icon
683 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 protected void onPause() {
688 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700689 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800690 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 }
Romain Guycbb89e42009-06-08 15:52:54 -0700692
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700693 @Override
694 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 // Flag the loader to stop early before switching
696 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 if (mAppsCustomizeContent != null) {
698 mAppsCustomizeContent.surrender();
699 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800700 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700701 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700702
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800703 // We can't hide the IME if it was forced open. So don't bother
704 /*
705 @Override
706 public void onWindowFocusChanged(boolean hasFocus) {
707 super.onWindowFocusChanged(hasFocus);
708
709 if (hasFocus) {
710 final InputMethodManager inputManager = (InputMethodManager)
711 getSystemService(Context.INPUT_METHOD_SERVICE);
712 WindowManager.LayoutParams lp = getWindow().getAttributes();
713 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
714 android.os.Handler()) {
715 protected void onReceiveResult(int resultCode, Bundle resultData) {
716 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
717 }
718 });
719 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
720 }
721 }
722 */
723
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 private boolean acceptFilter() {
725 final InputMethodManager inputManager = (InputMethodManager)
726 getSystemService(Context.INPUT_METHOD_SERVICE);
727 return !inputManager.isFullscreenMode();
728 }
729
730 @Override
731 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700732 final int uniChar = event.getUnicodeChar();
733 final boolean handled = super.onKeyDown(keyCode, event);
734 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
735 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
737 keyCode, event);
738 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700739 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700740 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700741 // showSearchDialog()
742 // If there are multiple keystrokes before the search dialog takes focus,
743 // onSearchRequested() will be called for every keystroke,
744 // but it is idempotent, so it's fine.
745 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800746 }
747 }
748
Joe Onorato8a9625e2010-01-28 15:55:35 -0800749 // Eat the long press event so the keyboard doesn't come up.
750 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
751 return true;
752 }
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 return handled;
755 }
756
Karl Rosaen138a0412009-04-23 19:00:21 -0700757 private String getTypedText() {
758 return mDefaultKeySsb.toString();
759 }
760
761 private void clearTypedText() {
762 mDefaultKeySsb.clear();
763 mDefaultKeySsb.clearSpans();
764 Selection.setSelection(mDefaultKeySsb, 0);
765 }
766
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700768 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
769 * State
770 */
771 private static State intToState(int stateOrdinal) {
772 State state = State.WORKSPACE;
773 final State[] stateValues = State.values();
774 for (int i = 0; i < stateValues.length; i++) {
775 if (stateValues[i].ordinal() == stateOrdinal) {
776 state = stateValues[i];
777 break;
778 }
779 }
780 return state;
781 }
782
783 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 * Restores the previous state, if it exists.
785 *
786 * @param savedState The previous state.
787 */
788 private void restoreState(Bundle savedState) {
789 if (savedState == null) {
790 return;
791 }
792
Michael Jurka883f55b2010-10-21 15:47:14 -0700793 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
794
Winson Chungf0ea4d32011-06-06 14:27:16 -0700795 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800796 showAllApps(false);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
800 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700801 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 }
803
Winson Chung3d503fb2011-07-13 17:25:49 -0700804 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
805 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700806
Winson Chung3d503fb2011-07-13 17:25:49 -0700807
808 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
809 mPendingAddInfo.container = pendingAddContainer;
810 mPendingAddInfo.screen = pendingAddScreen;
811 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
812 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
815
816 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
817 if (renameFolder) {
818 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700819 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 mRestoring = true;
821 }
Winson Chunga12a2502010-12-20 14:41:35 -0800822
Winson Chung785d2eb2011-04-14 16:08:02 -0700823
824 // Restore the AppsCustomize tab
825 if (mAppsCustomizeTabHost != null) {
826 String curTab = savedState.getString("apps_customize_currentTab");
827 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700828 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700829 mAppsCustomizeContent.setContentType(
830 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
831 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
832 }
833
834 // Note: currently we do not restore the page for the AppsCustomize pane because the
835 // change in layout can drastically affect the saved page index
836 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 }
838
839 /**
840 * Finds all the views we need and configure them properly.
841 */
842 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700843 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400844
Winson Chung4c98d922011-05-31 16:50:48 -0700845 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
846 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
Winson Chung4c98d922011-05-31 16:50:48 -0700848 // Setup the drag layer
849 mDragLayer.setup(this, dragController);
850
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 // Setup the hotseat
852 mHotseat = (Hotseat) findViewById(R.id.hotseat);
853 if (mHotseat != null) {
854 mHotseat.setup(this);
855 }
856
Winson Chung4c98d922011-05-31 16:50:48 -0700857 // Setup the workspace
858 mWorkspace.setHapticFeedbackEnabled(false);
859 mWorkspace.setOnLongClickListener(this);
860 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700861 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700862
Winson Chungf0ea4d32011-06-06 14:27:16 -0700863 // Get the search/delete bar
864 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700865
Winson Chungf0ea4d32011-06-06 14:27:16 -0700866 // Setup AppsCustomize
867 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
868 findViewById(R.id.apps_customize_pane);
869 mAppsCustomizeContent = (AppsCustomizePagedView)
870 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
871 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400872
Romain Guy1fbc1c82009-11-09 20:43:08 -0800873
Winson Chung4c98d922011-05-31 16:50:48 -0700874
Winson Chung3d503fb2011-07-13 17:25:49 -0700875 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700876 dragController.setDragScoller(mWorkspace);
877 dragController.setScrollView(mDragLayer);
878 dragController.setMoveTarget(mWorkspace);
879 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700880 if (mSearchDeleteBar != null) {
881 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800882 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 }
884
Romain Guy8a73c512009-11-09 19:19:59 -0800885
Winson Chungaafa03c2010-06-11 17:34:16 -0700886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 /**
888 * Creates a view representing a shortcut.
889 *
890 * @param info The data structure describing the shortcut.
891 *
892 * @return A View inflated from R.layout.application.
893 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800894 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700896 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 }
898
899 /**
900 * Creates a view representing a shortcut inflated from the specified resource.
901 *
902 * @param layoutResId The id of the XML layout used to create the shortcut.
903 * @param parent The group the shortcut belongs to.
904 * @param info The data structure describing the shortcut.
905 *
906 * @return A View inflated from layoutResId.
907 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800908 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800909 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
910 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 return favorite;
913 }
914
915 /**
916 * Add an application shortcut to the workspace.
917 *
918 * @param data The intent describing the application.
919 * @param cellInfo The position on screen where to create the shortcut.
920 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700922 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700924
Adam Cohenfbba09b2011-07-18 21:43:05 -0700925 // First we check if we already know the exact location where we want to add this item.
926 if (cellX >= 0 && cellY >= 0) {
927 cellXY[0] = cellX;
928 cellXY[1] = cellY;
929 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700930 showOutOfSpaceMessage();
931 return;
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800934 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800935
Romain Guy73b979d2009-06-09 12:57:21 -0700936 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800937 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800939 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700941 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800942 } else {
943 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 }
945 }
Romain Guycbb89e42009-06-08 15:52:54 -0700946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 /**
948 * Add a shortcut to the workspace.
949 *
950 * @param data The intent describing the shortcut.
951 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700953 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
954 int cellY) {
955 int[] cellXY = mTmpAddItemCellCoordinates;
956 int[] touchXY = mPendingAddInfo.dropPos;
957 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700958
Michael Jurkad3ef3062010-11-23 16:23:58 -0800959 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700960
961 // First we check if we already know the exact location where we want to add this item.
962 if (cellX >= 0 && cellY >= 0) {
963 cellXY[0] = cellX;
964 cellXY[1] = cellY;
965 foundCellSpan = true;
966 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800967 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700968 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800969 foundCellSpan = (result != null);
970 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700971 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800972 }
973
974 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700975 showOutOfSpaceMessage();
976 return;
977 }
978
979 final ShortcutInfo info = mModel.addShortcut(
Winson Chung3d503fb2011-07-13 17:25:49 -0700980 this, data, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981
982 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 final View view = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
985 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987 }
988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700992 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700993 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700995 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700996 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700997
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700998 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001000
1001 // We want to account for the extra amount of padding that we are adding to the widget
1002 // to ensure that it gets the full amount of space that it has requested
1003 Resources r = getResources();
1004 int requiredWidth = appWidgetInfo.minWidth +
1005 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1006 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1007 int requiredHeight = appWidgetInfo.minHeight +
1008 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1009 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1010 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001011
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001013 // We have saved the position to which the widget was dragged-- this really only matters
1014 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 int[] cellXY = mTmpAddItemCellCoordinates;
1016 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001017 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001018 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1019 cellXY[0] = mPendingAddInfo.cellX;
1020 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001021 foundCellSpan = true;
1022 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001023 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001024 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -07001025 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001026 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001027 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001028 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001029 }
1030
Michael Jurka0280c3b2010-09-17 15:00:07 -07001031 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001032 if (appWidgetId != -1) {
1033 // Deleting an app widget ID is a void call but writes to disk before returning
1034 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001035 new Thread("deleteAppWidgetId") {
1036 public void run() {
1037 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1038 }
1039 }.start();
1040 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001041 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001042 return;
1043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 // Build Launcher-specific widget info and save to database
1046 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 launcherInfo.spanX = spanXY[0];
1048 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001051 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052
1053 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001057 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001059
Winson Chung3d503fb2011-07-13 17:25:49 -07001060 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001061 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001062
1063 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 }
1065 }
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Adam Cohended9f8d2010-11-03 13:25:16 -07001067 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1068 @Override
1069 public void onReceive(Context context, Intent intent) {
1070 final String action = intent.getAction();
1071 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1072 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001073 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001074 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001075
1076 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001077 if (mAppsCustomizeContent != null) {
1078 mAppsCustomizeContent.reset();
1079 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001080 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1081 mUserPresent = true;
1082 updateRunning();
1083 }
1084 }
1085 };
1086
1087 @Override
1088 public void onAttachedToWindow() {
1089 super.onAttachedToWindow();
1090
1091 // Listen for broadcasts related to user-presence
1092 final IntentFilter filter = new IntentFilter();
1093 filter.addAction(Intent.ACTION_SCREEN_OFF);
1094 filter.addAction(Intent.ACTION_USER_PRESENT);
1095 registerReceiver(mReceiver, filter);
1096
Adam Cohend113e0c2010-11-11 10:48:05 -08001097 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001098 mVisible = true;
1099 }
1100
1101 @Override
1102 public void onDetachedFromWindow() {
1103 super.onDetachedFromWindow();
1104 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001105 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001106
Adam Cohend113e0c2010-11-11 10:48:05 -08001107 if (mAttached) {
1108 unregisterReceiver(mReceiver);
1109 mAttached = false;
1110 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 updateRunning();
1112 }
1113
1114 public void onWindowVisibilityChanged(int visibility) {
1115 mVisible = visibility == View.VISIBLE;
1116 updateRunning();
1117 }
1118
1119 private void sendAdvanceMessage(long delay) {
1120 mHandler.removeMessages(ADVANCE_MSG);
1121 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1122 mHandler.sendMessageDelayed(msg, delay);
1123 mAutoAdvanceSentTime = System.currentTimeMillis();
1124 }
1125
1126 private void updateRunning() {
1127 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1128 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1129 mAutoAdvanceRunning = autoAdvanceRunning;
1130 if (autoAdvanceRunning) {
1131 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1132 sendAdvanceMessage(delay);
1133 } else {
1134 if (!mWidgetsToAdvance.isEmpty()) {
1135 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1136 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1137 }
1138 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001139 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 }
1141 }
1142 }
1143
1144 private final Handler mHandler = new Handler() {
1145 @Override
1146 public void handleMessage(Message msg) {
1147 if (msg.what == ADVANCE_MSG) {
1148 int i = 0;
1149 for (View key: mWidgetsToAdvance.keySet()) {
1150 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1151 final int delay = mAdvanceStagger * i;
1152 if (v instanceof Advanceable) {
1153 postDelayed(new Runnable() {
1154 public void run() {
1155 ((Advanceable) v).advance();
1156 }
1157 }, delay);
1158 }
1159 i++;
1160 }
1161 sendAdvanceMessage(mAdvanceInterval);
1162 }
1163 }
1164 };
1165
1166 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001167 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001168 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1169 if (v instanceof Advanceable) {
1170 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001171 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 updateRunning();
1173 }
1174 }
1175
1176 void removeWidgetToAutoAdvance(View hostView) {
1177 if (mWidgetsToAdvance.containsKey(hostView)) {
1178 mWidgetsToAdvance.remove(hostView);
1179 updateRunning();
1180 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 }
1182
Joe Onorato9c1289c2009-08-17 11:03:03 -04001183 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 launcherInfo.hostView = null;
1186 }
1187
Adam Cohended9f8d2010-11-03 13:25:16 -07001188 void showOutOfSpaceMessage() {
1189 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1190 }
1191
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 public LauncherAppWidgetHost getAppWidgetHost() {
1193 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
Romain Guycbb89e42009-06-08 15:52:54 -07001195
Winson Chunga9abd0e2010-10-27 17:18:37 -07001196 public LauncherModel getModel() {
1197 return mModel;
1198 }
1199
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001200 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001201 getWindow().closeAllPanels();
1202
1203 try {
1204 dismissDialog(DIALOG_CREATE_SHORTCUT);
1205 // Unlock the workspace if the dialog was showing
1206 } catch (Exception e) {
1207 // An exception is thrown if the dialog is not visible, which is fine
1208 }
1209
1210 try {
1211 dismissDialog(DIALOG_RENAME_FOLDER);
1212 // Unlock the workspace if the dialog was showing
1213 } catch (Exception e) {
1214 // An exception is thrown if the dialog is not visible, which is fine
1215 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001216
1217 // Whatever we were doing is hereby canceled.
1218 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001219 }
1220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 @Override
1222 protected void onNewIntent(Intent intent) {
1223 super.onNewIntent(intent);
1224
1225 // Close the menu
1226 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001227 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001228 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001229
Adam Cohen95bb8002011-07-03 23:40:28 -07001230 closeFolder();
1231
Joe Onorato14f122b2009-11-19 14:06:36 -08001232 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1233 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001234
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001235 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001236 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001237 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001238 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001239 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001240 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001241 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001242
Joe Onorato3a8820b2009-11-10 15:06:42 -08001243 final View v = getWindow().peekDecorView();
1244 if (v != null && v.getWindowToken() != null) {
1245 InputMethodManager imm = (InputMethodManager)getSystemService(
1246 INPUT_METHOD_SERVICE);
1247 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001249
Michael Jurka35aa14d2011-07-07 17:01:08 -07001250 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001251 if (mAppsCustomizeContent != null) {
1252 mAppsCustomizeContent.reset();
1253 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 }
1255 }
1256
1257 @Override
1258 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1259 // Do not call super here
1260 mSavedInstanceState = savedInstanceState;
1261 }
1262
1263 @Override
1264 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001265 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001266 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267
Michael Jurka883f55b2010-10-21 15:47:14 -07001268 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001269 // We close any open folder since it will not be re-opened, and we need to make sure
1270 // this state is reflected.
1271 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
Winson Chung3d503fb2011-07-13 17:25:49 -07001273 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1274 mWaitingForResult) {
1275 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1276 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1277 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1278 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
1280
1281 if (mFolderInfo != null && mWaitingForResult) {
1282 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1283 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1284 }
Michael Jurka946ad472010-07-09 18:05:18 -07001285
Winson Chung785d2eb2011-04-14 16:08:02 -07001286 // Save the current AppsCustomize tab
1287 if (mAppsCustomizeTabHost != null) {
1288 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1289 if (currentTabTag != null) {
1290 outState.putString("apps_customize_currentTab", currentTabTag);
1291 }
1292 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 }
1294
1295 @Override
1296 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001298
Winson Chungcd2b0142011-06-08 16:02:26 -07001299 // Stop callbacks from LauncherModel
1300 LauncherApplication app = ((LauncherApplication) getApplication());
1301 mModel.stopLoader();
1302 app.setLauncher(null);
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001305 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001307 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001309 mAppWidgetHost = null;
1310
1311 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312
1313 TextKeyListener.getInstance().release();
1314
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001317
1318 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001319 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001320
1321 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1322 mWorkspace.removeAllViews();
1323 mWorkspace = null;
1324 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001325
1326 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
Adam Cohena9cf38f2011-05-02 15:36:58 -07001329 public DragController getDragController() {
1330 return mDragController;
1331 }
1332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 @Override
1334 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001335 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 super.startActivityForResult(intent, requestCode);
1337 }
1338
1339 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001340 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001342
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001343 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001344
Karl Rosaen138a0412009-04-23 19:00:21 -07001345 if (initialQuery == null) {
1346 // Use any text typed in the launcher as the initial query
1347 initialQuery = getTypedText();
1348 clearTypedText();
1349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 if (appSearchData == null) {
1351 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001352 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
Romain Guycbb89e42009-06-08 15:52:54 -07001354
Karl Rosaen138a0412009-04-23 19:00:21 -07001355 final SearchManager searchManager =
1356 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001357 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001358 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
1360
1361 @Override
1362 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001363 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 return false;
1365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
1367 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1370 .setIcon(android.R.drawable.ic_menu_add)
1371 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001372 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1373 .setIcon(android.R.drawable.ic_menu_manage)
1374 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001375 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 .setIcon(android.R.drawable.ic_menu_gallery)
1377 .setAlphabeticShortcut('W');
1378 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1379 .setIcon(android.R.drawable.ic_search_category_default)
1380 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1381 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1382 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1383 .setAlphabeticShortcut('N');
1384
1385 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001386 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1387 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388
1389 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1390 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1391 .setIntent(settings);
1392
1393 return true;
1394 }
1395
1396 @Override
1397 public boolean onPrepareOptionsMenu(Menu menu) {
1398 super.onPrepareOptionsMenu(menu);
1399
Winson Chung785d2eb2011-04-14 16:08:02 -07001400 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1401 // related code?
1402 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
1404 return true;
1405 }
1406
1407 @Override
1408 public boolean onOptionsItemSelected(MenuItem item) {
1409 switch (item.getItemId()) {
1410 case MENU_ADD:
1411 addItems();
1412 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001413 case MENU_MANAGE_APPS:
1414 manageApps();
1415 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 case MENU_WALLPAPER_SETTINGS:
1417 startWallpaper();
1418 return true;
1419 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001420 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 return true;
1422 case MENU_NOTIFICATIONS:
1423 showNotifications();
1424 return true;
1425 }
1426
1427 return super.onOptionsItemSelected(item);
1428 }
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Karl Rosaen138a0412009-04-23 19:00:21 -07001430 /**
1431 * Indicates that we want global search for this activity by setting the globalSearch
1432 * argument for {@link #startSearch} to true.
1433 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001435 @Override
1436 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001437 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001438 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001439 }
1440
Joe Onorato9c1289c2009-08-17 11:03:03 -04001441 public boolean isWorkspaceLocked() {
1442 return mWorkspaceLoading || mWaitingForResult;
1443 }
1444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001446 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001447 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449
Michael Jurka0280c3b2010-09-17 15:00:07 -07001450 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001451 mPendingAddInfo.container = ItemInfo.NO_ID;
1452 mPendingAddInfo.screen = -1;
1453 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1454 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1455 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001456 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001457
Winson Chung7ad01412010-09-27 11:33:03 -07001458 private void manageApps() {
1459 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1460 }
1461
Michael Jurkaaf442092010-06-10 17:01:57 -07001462 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001463 // TODO: catch bad widget exception when sent
1464 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001465 // TODO: Is this log message meaningful?
1466 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001467 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001468 }
1469
Winson Chung55cef262010-10-28 14:14:18 -07001470 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001471 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
Bjorn Bringert7984c942009-12-09 15:38:25 +00001473 if (appWidget.configure != null) {
1474 // Launch over to configure widget, if needed
1475 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1476 intent.setComponent(appWidget.configure);
1477 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001478 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001479 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1480 intent.putExtra(
1481 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1482 info.mimeType);
1483
1484 final String mimeType = info.mimeType;
1485 final ClipData clipData = (ClipData) info.configurationData;
1486 final ClipDescription clipDesc = clipData.getDescription();
1487 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1488 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001489 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001490 final CharSequence stringData = item.getText();
1491 final Uri uriData = item.getUri();
1492 final Intent intentData = item.getIntent();
1493 final String key =
1494 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1495 if (uriData != null) {
1496 intent.putExtra(key, uriData);
1497 } else if (intentData != null) {
1498 intent.putExtra(key, intentData);
1499 } else if (stringData != null) {
1500 intent.putExtra(key, stringData);
1501 }
1502 break;
1503 }
1504 }
1505 }
Winson Chung55cef262010-10-28 14:14:18 -07001506 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001507
Romain Guy8e633c52010-03-04 12:51:36 -08001508 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001510 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001512
1513 // Exit spring loaded mode if necessary after adding the widget
1514 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516 }
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 /**
1519 * Process a shortcut drop.
1520 *
1521 * @param componentName The name of the component
1522 * @param screen The screen where it should be added
1523 * @param cell The cell it should be added to, optional
1524 * @param position The location on the screen where it was dropped, optional
1525 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001526 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1527 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001528 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001529 mPendingAddInfo.container = container;
1530 mPendingAddInfo.screen = screen;
1531 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001532
1533 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 mPendingAddInfo.cellX = cell[0];
1535 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001536 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537
1538 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1539 createShortcutIntent.setComponent(componentName);
1540 processShortcut(createShortcutIntent);
1541 }
1542
Adam Cohenfbba09b2011-07-18 21:43:05 -07001543 /**
1544 * Process a widget drop.
1545 *
1546 * @param info The PendingAppWidgetInfo of the widget being added.
1547 * @param screen The screen where it should be added
1548 * @param cell The cell it should be added to, optional
1549 * @param position The location on the screen where it was dropped, optional
1550 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001551 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1552 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001554 mPendingAddInfo.container = info.container = container;
1555 mPendingAddInfo.screen = info.screen = screen;
1556 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001557 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 mPendingAddInfo.cellX = cell[0];
1559 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001560 }
1561
1562 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1563 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1564 addAppWidgetImpl(appWidgetId, info);
1565 }
1566
Joe Onoratodeb98af2010-02-19 14:59:39 -08001567 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001568 // Handle case where user selected "Applications"
1569 String applicationName = getResources().getString(R.string.group_applications);
1570 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001571
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001572 if (applicationName != null && applicationName.equals(shortcutName)) {
1573 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1574 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001576 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1577 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001578 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001579 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001580 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001581 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
Winson Chung24ab2f12010-09-16 14:10:47 -07001585 void processWallpaper(Intent intent) {
1586 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1587 }
1588
Winson Chung3d503fb2011-07-13 17:25:49 -07001589 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1590 int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001591 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 folderInfo.title = getText(R.string.folder_name);
1593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001595 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1596 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001597 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598
1599 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 FolderIcon newFolder =
1601 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1602 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1603 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001604 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
Romain Guycbb89e42009-06-08 15:52:54 -07001606
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001608 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 }
1610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 private void showNotifications() {
1612 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1613 if (statusBar != null) {
1614 statusBar.expand();
1615 }
1616 }
1617
1618 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001619 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001621 Intent chooser = Intent.createChooser(pickWallpaper,
1622 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001623 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1624 // Removed in Eclair MR1
1625// WallpaperManager wm = (WallpaperManager)
1626// getSystemService(Context.WALLPAPER_SERVICE);
1627// WallpaperInfo wi = wm.getWallpaperInfo();
1628// if (wi != null && wi.getSettingsActivity() != null) {
1629// LabeledIntent li = new LabeledIntent(getPackageName(),
1630// R.string.configure_wallpaper, 0);
1631// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1632// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1633// }
Mike Clerona0618e42009-10-22 13:55:21 -07001634 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001637 /**
1638 * Registers various content observers. The current implementation registers
1639 * only a favorites observer to keep track of the favorites applications.
1640 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001641 private void registerContentObservers() {
1642 ContentResolver resolver = getContentResolver();
1643 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1644 true, mWidgetObserver);
1645 }
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 @Override
1648 public boolean dispatchKeyEvent(KeyEvent event) {
1649 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1650 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001652 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001653 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001654 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001655 dumpState();
1656 return true;
1657 }
1658 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001659 }
1660 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1661 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001662 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 return true;
1664 }
1665 }
1666
1667 return super.dispatchKeyEvent(event);
1668 }
1669
Joe Onorato88ec0992009-11-19 13:16:06 -08001670 @Override
1671 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001672 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001673 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001674 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001675 Folder openFolder = mWorkspace.getOpenFolder();
1676 if (openFolder.isEditingName()) {
1677 openFolder.dismissEditingName();
1678 } else {
1679 closeFolder();
1680 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001681 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001682 mWorkspace.exitWidgetResizeMode();
1683
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001684 // Back button is a no-op here, but give at least some feedback for the button press
1685 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001686 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001687 }
1688
Adam Cohen2801caf2011-05-13 20:57:39 -07001689 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 Folder folder = mWorkspace.getOpenFolder();
1691 if (folder != null) {
1692 closeFolder(folder);
1693 }
1694 }
1695
1696 void closeFolder(Folder folder) {
1697 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001698
Michael Jurka8c920dd2011-01-20 14:16:56 -08001699 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 if (parent != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001701 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
Adam Cohen2801caf2011-05-13 20:57:39 -07001702 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001703 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001705 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
1707
1708 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001709 * Re-listen when widgets are reset.
1710 */
1711 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001712 if (mAppWidgetHost != null) {
1713 mAppWidgetHost.startListening();
1714 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001715 }
1716
1717 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001718 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1719 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001721 private void unbindWorkspaceAndHotseatItems() {
Adam Cohen4eac29a2011-07-11 17:53:37 -07001722 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 /**
1726 * Launches the intent referred by the clicked shortcut.
1727 *
1728 * @param v The view representing the clicked shortcut.
1729 */
1730 public void onClick(View v) {
1731 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001732 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001734 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001735 int[] pos = new int[2];
1736 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001737 intent.setSourceBounds(new Rect(pos[0], pos[1],
1738 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001739 boolean success = startActivitySafely(intent, tag);
1740
1741 if (success && v instanceof BubbleTextView) {
1742 mWaitingForResume = (BubbleTextView) v;
1743 mWaitingForResume.setStayPressed(true);
1744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001746 if (v instanceof FolderIcon) {
1747 FolderIcon fi = (FolderIcon) v;
1748 handleFolderClick(fi);
1749 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001750 } else if (v == mAllAppsButton) {
1751 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001752 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001753 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001754 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001755 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 }
1757 }
1758
Michael Jurka0e260592010-06-30 17:07:39 -07001759 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001760 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001761 // clicking anywhere on the workspace causes the customization drawer to slide down
1762 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001763 return false;
1764 }
1765
Michael Jurkaaf442092010-06-10 17:01:57 -07001766 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001767 * Event handler for the search button
1768 *
1769 * @param v The view that was clicked.
1770 */
1771 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001772 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001773 // Use a custom animation for launching search
1774 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001775 }
1776
1777 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001778 * Event handler for the voice button
1779 *
1780 * @param v The view that was clicked.
1781 */
1782 public void onClickVoiceButton(View v) {
1783 startVoiceSearch();
1784 }
1785
1786 private void startVoiceSearch() {
1787 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1788 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1789 startActivity(intent);
1790 }
1791
1792 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001793 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001794 * enters home screen customization mode.
1795 *
1796 * @param v The view that was clicked.
1797 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001798 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001799 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001800 }
1801
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001802 /**
1803 * Event handler for the "grid" button that appears on the home screen, which
1804 * enters all apps mode.
1805 *
1806 * @param v The view that was clicked.
1807 */
1808 public void onClickAllAppsButton(View v) {
1809 showAllApps(true);
1810 }
1811
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001812 public void onClickAppMarketButton(View v) {
1813 if (mAppMarketIntent != null) {
1814 startActivitySafely(mAppMarketIntent, "app market");
1815 }
1816 }
1817
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001818 void startApplicationDetailsActivity(ComponentName componentName) {
1819 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001820 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1821 Uri.fromParts("package", packageName, null));
1822 startActivity(intent);
1823 }
1824
Patrick Dubroy5539af72010-09-07 15:22:01 -07001825 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1826 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1827 // System applications cannot be installed. For now, show a toast explaining that.
1828 // We may give them the option of disabling apps this way.
1829 int messageId = R.string.uninstall_system_app_text;
1830 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1831 } else {
1832 String packageName = appInfo.componentName.getPackageName();
1833 String className = appInfo.componentName.getClassName();
1834 Intent intent = new Intent(
1835 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1836 startActivity(intent);
1837 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001838 }
1839
Michael Jurkaddd62e92011-02-16 17:49:14 -08001840 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1842 try {
1843 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001844 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 } catch (ActivityNotFoundException e) {
1846 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001847 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 } catch (SecurityException e) {
1849 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001850 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001852 "or use the exported attribute for this activity. "
1853 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001855 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001857
Romain Guy8e633c52010-03-04 12:51:36 -08001858 void startActivityForResultSafely(Intent intent, int requestCode) {
1859 try {
1860 startActivityForResult(intent, requestCode);
1861 } catch (ActivityNotFoundException e) {
1862 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1863 } catch (SecurityException e) {
1864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1865 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1866 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1867 "or use the exported attribute for this activity.", e);
1868 }
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870
Adam Cohena9cf38f2011-05-02 15:36:58 -07001871 private void handleFolderClick(FolderIcon folderIcon) {
1872 final FolderInfo info = folderIcon.mInfo;
1873 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 // Close any open folder
1875 closeFolder();
1876 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001877 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } else {
1879 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001880 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 int folderScreen;
1882 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001883 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 // .. and close it
1885 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001886 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 // Close any folder open on the current screen
1888 closeFolder();
1889 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001890 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 }
1892 }
1893 }
1894 }
1895
Adam Cohen2801caf2011-05-13 20:57:39 -07001896 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1897 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1898 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1899 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1900
1901 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1902 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1903 oa.start();
1904 }
1905
1906 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1907 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1908 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1909 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1910
1911 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1912 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1913 oa.start();
1914 }
1915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001917 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 * is animated relative to the specified View. If the View is null, no animation
1919 * is played.
1920 *
1921 * @param folderInfo The FolderInfo describing the folder to open.
1922 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001923 public void openFolder(FolderIcon folderIcon) {
1924 Folder folder = folderIcon.mFolder;
1925 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
Adam Cohen2801caf2011-05-13 20:57:39 -07001927 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001928 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929
Adam Cohen8e776a62011-06-28 18:10:06 -07001930 mDragLayer.addView(folder);
1931 mDragController.addDropTarget((DropTarget) folder);
1932
Adam Cohena9cf38f2011-05-02 15:36:58 -07001933 folder.animateOpen();
1934 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 }
1936
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001938 if (mState != State.WORKSPACE) {
1939 return false;
1940 }
1941
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 return false;
1944 }
1945
1946 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001947 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949
Michael Jurka0280c3b2010-09-17 15:00:07 -07001950 resetAddInfo();
1951 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 return true;
1955 }
1956
Winson Chung3d503fb2011-07-13 17:25:49 -07001957 // The hotseat touch handling does not go through Workspace, and we always allow long press
1958 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001959 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1961 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001962 if (itemUnderLongClick == null) {
1963 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1965 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001966 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001970 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1971 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001972 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
1975 }
1976 return true;
1977 }
1978
Winson Chung3d503fb2011-07-13 17:25:49 -07001979 boolean isHotseatLayout(View layout) {
1980 return mHotseat != null && layout != null &&
1981 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1982 }
1983 Hotseat getHotseat() {
1984 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001985 }
1986
Winson Chung3d503fb2011-07-13 17:25:49 -07001987 /**
1988 * Returns the CellLayout of the specified container at the specified screen.
1989 */
1990 CellLayout getCellLayout(long container, int screen) {
1991 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1992 if (mHotseat != null) {
1993 return mHotseat.getLayout();
1994 } else {
1995 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001996 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001997 } else {
1998 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001999 }
2000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 Workspace getWorkspace() {
2003 return mWorkspace;
2004 }
2005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 @Override
2007 protected Dialog onCreateDialog(int id) {
2008 switch (id) {
2009 case DIALOG_CREATE_SHORTCUT:
2010 return new CreateShortcut().createDialog();
2011 case DIALOG_RENAME_FOLDER:
2012 return new RenameFolder().createDialog();
2013 }
2014
2015 return super.onCreateDialog(id);
2016 }
2017
2018 @Override
2019 protected void onPrepareDialog(int id, Dialog dialog) {
2020 switch (id) {
2021 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 break;
2023 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002024 if (mFolderInfo != null) {
2025 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2026 final CharSequence text = mFolderInfo.title;
2027 input.setText(text);
2028 input.setSelection(0, text.length());
2029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 break;
2031 }
2032 }
2033
2034 void showRenameDialog(FolderInfo info) {
2035 mFolderInfo = info;
2036 mWaitingForResult = true;
2037 showDialog(DIALOG_RENAME_FOLDER);
2038 }
2039
Adam Cohenfbba09b2011-07-18 21:43:05 -07002040 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002041 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002042 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2043 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 mWaitingForResult = true;
2045 showDialog(DIALOG_CREATE_SHORTCUT);
2046 }
2047
Joe Onoratodeb98af2010-02-19 14:59:39 -08002048 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002049 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002050 Bundle bundle = new Bundle();
2051
2052 ArrayList<String> shortcutNames = new ArrayList<String>();
2053 shortcutNames.add(getString(R.string.group_applications));
2054 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2055
2056 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2057 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2058 R.drawable.ic_launcher_application));
2059 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2060
2061 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2062 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002063 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002064 pickIntent.putExtras(bundle);
2065
Joe Onoratodeb98af2010-02-19 14:59:39 -08002066 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002067 }
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 private class RenameFolder {
2070 private EditText mInput;
2071
2072 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2074 mInput = (EditText) layout.findViewById(R.id.folder_name);
2075
2076 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2077 builder.setIcon(0);
2078 builder.setTitle(getString(R.string.rename_folder_title));
2079 builder.setCancelable(true);
2080 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2081 public void onCancel(DialogInterface dialog) {
2082 cleanup();
2083 }
2084 });
2085 builder.setNegativeButton(getString(R.string.cancel_action),
2086 new Dialog.OnClickListener() {
2087 public void onClick(DialogInterface dialog, int which) {
2088 cleanup();
2089 }
2090 }
2091 );
2092 builder.setPositiveButton(getString(R.string.rename_action),
2093 new Dialog.OnClickListener() {
2094 public void onClick(DialogInterface dialog, int which) {
2095 changeFolderName();
2096 }
2097 }
2098 );
2099 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002100
2101 final AlertDialog dialog = builder.create();
2102 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2103 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002104 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002105 mInput.requestFocus();
2106 InputMethodManager inputManager = (InputMethodManager)
2107 getSystemService(Context.INPUT_METHOD_SERVICE);
2108 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002109 }
2110 });
2111
2112 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114
2115 private void changeFolderName() {
2116 final String name = mInput.getText().toString();
2117 if (!TextUtils.isEmpty(name)) {
2118 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002119 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 mFolderInfo.title = name;
2121 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2122
Joe Onorato9c1289c2009-08-17 11:03:03 -04002123 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002124 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002125 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 } else {
2127 final FolderIcon folderIcon = (FolderIcon)
2128 mWorkspace.getViewForTag(mFolderInfo);
2129 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002130 // TODO: At some point we'll probably want some version of setting
2131 // the text for a folder icon.
2132 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 getWorkspace().requestLayout();
2134 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002135 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002136 mWorkspaceLoading = true;
2137 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 }
2139 }
2140 }
2141 cleanup();
2142 }
2143
2144 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 dismissDialog(DIALOG_RENAME_FOLDER);
2146 mWaitingForResult = false;
2147 mFolderInfo = null;
2148 }
2149 }
2150
Daniel Sandler843e8602010-06-07 14:59:01 -04002151 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2152 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002153 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002154 }
2155
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002156 // AllAppsView.Watcher
2157 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002158 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002159 mWorkspace.setVisibility(View.GONE);
2160 }
2161 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002162
2163 /**
2164 * Helper method for the cameraZoomIn/cameraZoomOut animations
2165 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002166 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002167 * @param scaleFactor The scale factor used for the zoom
2168 */
2169 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2170 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002171
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002172 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002173 // Set pivotY so that at the starting zoom factor, the view is partially
2174 // visible. Modifying initialHeightFactor changes how much of the view is
2175 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002176 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002177 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002178 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002179 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002180 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002181 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002182 }
2183 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002184
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002185 /**
2186 * Zoom the camera out from the workspace to reveal 'toView'.
2187 * Assumes that the view to show is anchored at either the very top or very bottom
2188 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002189 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002190 */
Winson Chungc07918d2011-07-01 15:35:26 -07002191 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002192 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002193
Winson Chungf0ea4d32011-06-06 14:27:16 -07002194 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2195 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2196 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2197 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002198
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002199 setPivotsForZoom(toView, toState, scale);
2200
Michael Jurkad74c9842011-07-10 12:44:21 -07002201 // Shrink workspaces away if going to AppsCustomize from workspace
2202 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002203
2204 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002205 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002206 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002207 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2208 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002209 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002210 toView.setFastScaleX(a * scale + b * 1f);
2211 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002212 }
2213 });
Adam Cohen61033d32010-11-15 18:29:44 -08002214
Winson Chungf0ea4d32011-06-06 14:27:16 -07002215 toView.setVisibility(View.VISIBLE);
2216 toView.setFastAlpha(0f);
2217 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2218 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2219 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2220 public void onAnimationUpdate(float a, float b) {
2221 // don't need to invalidate because we do so above
2222 toView.setFastAlpha(a * 0f + b * 1f);
2223 }
2224 });
2225 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002226
Michael Jurkaabded662011-03-04 12:06:57 -08002227 if (toView instanceof LauncherTransitionable) {
2228 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002229 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002230 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002231 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002232 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002233 // Prepare the position
2234 toView.setTranslationX(0.0f);
2235 toView.setTranslationY(0.0f);
2236 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002237 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002238 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002239 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002240 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002241 // If we don't set the final scale values here, if this animation is cancelled
2242 // it will have the wrong scale value and subsequent cameraPan animations will
2243 // not fix that
2244 toView.setScaleX(1.0f);
2245 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002246 if (toView instanceof LauncherTransitionable) {
2247 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002248 }
Winson Chung32174c82011-07-19 15:47:55 -07002249
2250 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2251 // Hide the workspace scrollbar
2252 mWorkspace.hideScrollingIndicator(true);
2253 mWorkspace.hideScrollIndicatorTrack();
2254 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002255 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002256 });
2257
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002259 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002260
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002261 if (mStateAnimation != null) mStateAnimation.cancel();
2262 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002263 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002264 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002265 } else {
2266 toView.setTranslationX(0.0f);
2267 toView.setTranslationY(0.0f);
2268 toView.setScaleX(1.0f);
2269 toView.setScaleY(1.0f);
2270 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002271 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002272 if (toView instanceof LauncherTransitionable) {
2273 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2274 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002275
Winson Chungc07918d2011-07-01 15:35:26 -07002276 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2277 // Hide the workspace scrollbar
2278 mWorkspace.hideScrollingIndicator(true);
2279 mWorkspace.hideScrollIndicatorTrack();
2280 }
Michael Jurkaabded662011-03-04 12:06:57 -08002281 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002282 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002283 }
2284
2285 /**
2286 * Zoom the camera back into the workspace, hiding 'fromView'.
2287 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002289 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002290 */
Winson Chungc07918d2011-07-01 15:35:26 -07002291 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002293
Winson Chungf0ea4d32011-06-06 14:27:16 -07002294 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2295 final float scaleFactor = (float)
2296 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2297 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002298
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002299 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002300
Michael Jurkad3ef3062010-11-23 16:23:58 -08002301 if (!springLoaded) {
2302 mWorkspace.unshrink(animated);
2303 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002304 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002305 if (mStateAnimation != null) mStateAnimation.cancel();
2306 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002307
Michael Jurka742574b2011-02-02 23:51:01 -08002308 final float oldScaleX = fromView.getScaleX();
2309 final float oldScaleY = fromView.getScaleY();
2310
2311 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2312 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002313 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2314 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002315 ((View)fromView.getParent()).fastInvalidate();
2316 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2317 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2318 }
2319 });
Michael Jurkaabded662011-03-04 12:06:57 -08002320 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002321 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002322 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002323 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2324 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002325 // don't need to invalidate because we do so above
2326 fromView.setFastAlpha(a * 1f + b * 0f);
2327 }
2328 });
Michael Jurkaabded662011-03-04 12:06:57 -08002329 if (fromView instanceof LauncherTransitionable) {
2330 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002331 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002332 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002333 @Override
Winson Chung32174c82011-07-19 15:47:55 -07002334 public void onAnimationStart(android.animation.Animator animation) {
2335 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2336 // Show the workspace scrollbar
2337 mWorkspace.showScrollIndicatorTrack();
2338 mWorkspace.flashScrollingIndicator();
2339 }
2340 }
2341 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002342 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002343 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002344 if (fromView instanceof LauncherTransitionable) {
2345 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002346 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347 }
2348 });
2349
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002351 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002352 } else {
2353 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002354 if (fromView instanceof LauncherTransitionable) {
2355 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2356 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002357
Winson Chungc07918d2011-07-01 15:35:26 -07002358 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2359 // Show the workspace scrollbar
2360 mWorkspace.showScrollIndicatorTrack();
2361 mWorkspace.flashScrollingIndicator();
2362 }
Michael Jurkaabded662011-03-04 12:06:57 -08002363 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002364 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002365 }
2366
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002367 void showWorkspace(boolean animated) {
2368 showWorkspace(animated, null);
2369 }
2370
2371 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002372 if (layout != null) {
2373 // always animated, but that's ok since we never specify a layout and
2374 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002375 mWorkspace.unshrink(layout);
2376 } else {
2377 mWorkspace.unshrink(animated);
2378 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002379 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002380 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002381 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002382
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002383 // Change the state *after* we've called all the transition code
2384 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002385
Winson Chung5fb63472011-02-02 17:03:37 -08002386 // Resume the auto-advance of widgets
2387 mUserPresent = true;
2388 updateRunning();
2389
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002390 // send an accessibility event to announce the context change
2391 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002392 }
2393
Michael Jurkad3ef3062010-11-23 16:23:58 -08002394 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002395 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002396 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002397 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002398 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002399 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002400 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002401 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002402 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2403 mWorkspace.postDelayed(new Runnable() {
2404 @Override
2405 public void run() {
2406 exitSpringLoadedDragMode();
2407 }
2408 }, (extendedDelay ?
2409 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2410 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2411 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002412 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002413 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002414 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002415 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2416 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002417 }
2418 // Otherwise, we are not in spring loaded mode, so don't do anything.
2419 }
2420
Adam Cohenfc53cd22011-07-20 15:45:11 -07002421 public boolean isAllAppsCustomizeOpen() {
2422 return mState == State.APPS_CUSTOMIZE;
2423 }
2424
Winson Chungf0ea4d32011-06-06 14:27:16 -07002425 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002427 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002429 if (!LauncherApplication.isScreenLarge()) {
2430 if (animated) {
2431 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002432 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002433 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002434 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002435 }
2436 }
2437 }
2438
2439 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002440 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002441 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002442 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002443 if (!LauncherApplication.isScreenLarge()) {
2444 if (animated) {
2445 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002446 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002449 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002450 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002451 }
2452
Winson Chung785d2eb2011-04-14 16:08:02 -07002453 void showAllApps(boolean animated) {
2454 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002455
Winson Chungf0ea4d32011-06-06 14:27:16 -07002456 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2457 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002458
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002460 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002461 hideHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002462
Winson Chungf0ea4d32011-06-06 14:27:16 -07002463 // Change the state *after* we've called all the transition code
2464 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002465
2466 // Pause the auto-advance of widgets until we are out of AllApps
2467 mUserPresent = false;
2468 updateRunning();
2469
2470 // Send an accessibility event to announce the context change
2471 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2472 }
2473
Joe Onoratob2061212009-11-24 16:13:54 -05002474 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002475 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002476 * - Home from workspace
2477 * - from center screen
2478 * - from other screens
2479 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002480 * - from center screen
2481 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002482 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002483 * - from center screen
2484 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002485 * - Launch app from workspace and quit
2486 * - with back
2487 * - with home
2488 * - Launch app from all apps and quit
2489 * - with back
2490 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002491 * - Go to a screen that's not the default, then all
2492 * apps, and launch and app, and go back
2493 * - with back
2494 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002495 * - On workspace, long press power and go back
2496 * - with back
2497 * - with home
2498 * - On all apps, long press power and go back
2499 * - with back
2500 * - with home
2501 * - On workspace, power off
2502 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002503 * - Launch an app and turn off the screen while in that app
2504 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002505 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002506 * - From all apps
2507 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002508 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2509 * - From all apps
2510 * - From the center workspace
2511 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002512 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002513 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002514 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2515 mWorkspace.setVisibility(View.VISIBLE);
2516 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002517
Winson Chung3d503fb2011-07-13 17:25:49 -07002518 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002519 mSearchDeleteBar.showSearchBar(animated);
Winson Chung3d503fb2011-07-13 17:25:49 -07002520 showHotseat(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002521
Winson Chungf0ea4d32011-06-06 14:27:16 -07002522 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002523 if (mAllAppsButton != null) {
2524 mAllAppsButton.requestFocus();
2525 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002526 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002527 }
2528
Joe Onorato7c312c12009-08-13 21:36:53 -07002529 void lockAllApps() {
2530 // TODO
2531 }
2532
2533 void unlockAllApps() {
2534 // TODO
2535 }
2536
Patrick Dubroy5f445422011-02-18 14:35:21 -08002537 /**
2538 * Add an item from all apps or customize onto the given workspace screen.
2539 * If layout is null, add to the current screen.
2540 */
2541 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002542 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2543 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002544 } else {
2545 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002546 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002547 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002548
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002549 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002550 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002551 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002552 // Look for the toolbar icon specified in the activity meta-data
2553 Bundle metaData = packageManager.getActivityInfo(
2554 activityName, PackageManager.GET_META_DATA).metaData;
2555 if (metaData != null) {
2556 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2557 if (iconResId != 0) {
2558 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002559 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002560 }
2561 }
2562 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002563 // This can happen if the activity defines an invalid drawable
2564 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2565 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002566 } catch (Resources.NotFoundException nfe) {
2567 // This can happen if the activity defines an invalid drawable
2568 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2569 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002570 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002571 return null;
2572 }
2573
2574 // if successful in getting icon, return it; otherwise, set button to use default drawable
2575 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2576 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2577 TextView button = (TextView) findViewById(buttonId);
2578 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2579
2580 // If we were unable to find the icon via the meta-data, use a generic one
2581 if (toolbarIcon == null) {
2582 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2583 return null;
2584 } else {
2585 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2586 return toolbarIcon.getConstantState();
2587 }
2588 }
2589
2590 // if successful in getting icon, return it; otherwise, set button to use default drawable
2591 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2592 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2593 ImageView button = (ImageView) findViewById(buttonId);
2594 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2595
Michael Jurka0423dcf2010-10-05 14:56:18 -07002596 // If we were unable to find the icon via the meta-data, use a generic one
2597 if (toolbarIcon == null) {
2598 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002599 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002600 } else {
2601 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002602 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002603 }
2604 }
2605
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002606 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2607 TextView button = (TextView) findViewById(buttonId);
2608 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2609 }
2610
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002611 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002612 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002613 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002614 }
2615
Michael Jurka0423dcf2010-10-05 14:56:18 -07002616 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002617 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2618 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002619
Winson Chung1cad91e2011-05-25 17:41:01 -07002620 final SearchManager searchManager =
2621 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2622 ComponentName activityName = searchManager.getGlobalSearchActivity();
2623 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002624 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002625 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002626 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002627 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002628 } else {
2629 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002630 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002631 }
2632 }
2633
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002634 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002635 updateButtonWithDrawable(R.id.search_button, d);
2636 }
2637
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002638 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002639 final View searchDivider = findViewById(R.id.search_divider);
2640 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002641
Winson Chung1cad91e2011-05-25 17:41:01 -07002642 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2643 ComponentName activityName = intent.resolveActivity(getPackageManager());
2644 if (activityName != null) {
2645 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002646 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2647 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002648 voiceButton.setVisibility(View.VISIBLE);
2649 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002650 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002651 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002652 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002653 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002654
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002655 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002656 updateButtonWithDrawable(R.id.voice_button, d);
2657 }
2658
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002659 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002660 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002661 */
2662 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002663 final View marketButton = findViewById(R.id.market_button);
2664 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2665 // Find the app market activity by resolving an intent.
2666 // (If multiple app markets are installed, it will return the ResolverActivity.)
2667 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002668 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002669 mAppMarketIntent = intent;
2670 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002671 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002672 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002673
2674 // Remove the shop icon text in the Phone UI
2675 if (!LauncherApplication.isScreenLarge()) {
2676 ((TextView) marketButton).setText("");
2677 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002678 } else {
2679 // We should hide and disable the view so that we don't try and restore the visibility
2680 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2681 marketButton.setVisibility(View.GONE);
2682 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002683 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002684 }
2685
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002686 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002687 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002688 }
2689
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002690 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 * Displays the shortcut creation dialog and launches, if necessary, the
2692 * appropriate activity.
2693 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002694 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002695 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2696 DialogInterface.OnShowListener {
2697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002702
Winson Chung55b65502011-05-26 12:03:43 -07002703 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2704 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002706 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 AlertDialog dialog = builder.create();
2709 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002710 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002711 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 return dialog;
2714 }
2715
2716 public void onCancel(DialogInterface dialog) {
2717 mWaitingForResult = false;
2718 cleanup();
2719 }
2720
Romain Guycbb89e42009-06-08 15:52:54 -07002721 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002722 mWaitingForResult = false;
2723 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002724 }
2725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002727 try {
2728 dismissDialog(DIALOG_CREATE_SHORTCUT);
2729 } catch (Exception e) {
2730 // An exception is thrown if the dialog is not visible, which is fine
2731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
2733
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002734 /**
2735 * Handle the action clicked in the "Add to home" dialog.
2736 */
2737 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002738 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002739
Winson Chung55b65502011-05-26 12:03:43 -07002740 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2741 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002742 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002743 if (mAppsCustomizeTabHost != null) {
2744 mAppsCustomizeTabHost.selectWidgetsTab();
2745 }
2746 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002747 break;
2748 }
Winson Chung55b65502011-05-26 12:03:43 -07002749 case AddAdapter.ITEM_APPLICATION: {
2750 if (mAppsCustomizeTabHost != null) {
2751 mAppsCustomizeTabHost.selectAppsTab();
2752 }
2753 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002754 break;
2755 }
Winson Chung55b65502011-05-26 12:03:43 -07002756 case AddAdapter.ITEM_APPWIDGET: {
2757 if (mAppsCustomizeTabHost != null) {
2758 mAppsCustomizeTabHost.selectWidgetsTab();
2759 }
2760 showAllApps(true);
2761 break;
2762 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002763 case AddAdapter.ITEM_WALLPAPER: {
2764 startWallpaper();
2765 break;
2766 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 }
2768 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002769
2770 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002771 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 }
2774
2775 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002776 * Receives notifications when applications are added/removed.
2777 */
2778 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2779 @Override
2780 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002781 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002782 String reason = intent.getStringExtra("reason");
2783 if (!"homekey".equals(reason)) {
2784 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002785 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002786 animate = false;
2787 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002788 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002789 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002790 }
2791 }
2792
2793 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002794 * Receives notifications whenever the appwidgets are reset.
2795 */
2796 private class AppWidgetResetObserver extends ContentObserver {
2797 public AppWidgetResetObserver() {
2798 super(new Handler());
2799 }
2800
2801 @Override
2802 public void onChange(boolean selfChange) {
2803 onAppWidgetReset();
2804 }
2805 }
2806
2807 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002808 * If the activity is currently paused, signal that we need to re-run the loader
2809 * in onResume.
2810 *
2811 * This needs to be called from incoming places where resources might have been loaded
2812 * while we are paused. That is becaues the Configuration might be wrong
2813 * when we're not running, and if it comes back to what it was when we
2814 * were paused, we are not restarted.
2815 *
2816 * Implementation of the method from LauncherModel.Callbacks.
2817 *
2818 * @return true if we are currently paused. The caller might be able to
2819 * skip some work in that case since we will come back again.
2820 */
2821 public boolean setLoadOnResume() {
2822 if (mPaused) {
2823 Log.i(TAG, "setLoadOnResume");
2824 mOnResumeNeedsLoad = true;
2825 return true;
2826 } else {
2827 return false;
2828 }
2829 }
2830
2831 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002835 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002836 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002837 } else {
2838 return SCREEN_COUNT / 2;
2839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002841
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002842
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 /**
2844 * Refreshes the shortcuts shown on the workspace.
2845 *
2846 * Implementation of the method from LauncherModel.Callbacks.
2847 */
2848 public void startBinding() {
2849 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002850
2851 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 int count = workspace.getChildCount();
2853 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002854 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002855 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2856 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002858 if (mHotseat != null) {
2859 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002861
Adam Cohen4eac29a2011-07-11 17:53:37 -07002862 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
Winson Chung3d503fb2011-07-13 17:25:49 -07002863 unbindWorkspaceAndHotseatItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 }
2865
2866 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 * Bind the items start-end from the list.
2868 *
2869 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002872 setLoadOnResume();
2873
Joe Onorato9c1289c2009-08-17 11:03:03 -04002874 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 for (int i=start; i<end; i++) {
2876 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002877
2878 // Short circuit if we are loading dock items for a configuration which has no dock
2879 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2880 mHotseat == null) {
2881 continue;
2882 }
2883
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 switch (item.itemType) {
2885 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2886 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002887 View shortcut = createShortcut((ShortcutInfo)item);
2888 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2889 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002891 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002892 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002893 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002894 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002895 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2896 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002901 }
2902
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903 /**
2904 * Implementation of the method from LauncherModel.Callbacks.
2905 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002906 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002907 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002908 sFolders.clear();
2909 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911
2912 /**
2913 * Add the views for a widget to the workspace.
2914 *
2915 * Implementation of the method from LauncherModel.Callbacks.
2916 */
2917 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002918 setLoadOnResume();
2919
Daniel Sandler843e8602010-06-07 14:59:01 -04002920 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2921 if (DEBUG_WIDGETS) {
2922 Log.d(TAG, "bindAppWidget: " + item);
2923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 final Workspace workspace = mWorkspace;
2925
2926 final int appWidgetId = item.appWidgetId;
2927 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002928 if (DEBUG_WIDGETS) {
2929 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2930 }
2931
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2933
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2935 item.hostView.setTag(item);
2936
Winson Chung3d503fb2011-07-13 17:25:49 -07002937 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 item.cellY, item.spanX, item.spanY, false);
2939
Adam Cohended9f8d2010-11-03 13:25:16 -07002940 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2941
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 workspace.requestLayout();
2943
Daniel Sandler843e8602010-06-07 14:59:01 -04002944 if (DEBUG_WIDGETS) {
2945 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2946 + (SystemClock.uptimeMillis()-start) + "ms");
2947 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 }
2949
2950 /**
2951 * Callback saying that there aren't any more items to bind.
2952 *
2953 * Implementation of the method from LauncherModel.Callbacks.
2954 */
2955 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002956 setLoadOnResume();
2957
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 if (mSavedState != null) {
2959 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002960 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 mSavedState = null;
2963 }
2964
2965 if (mSavedInstanceState != null) {
2966 super.onRestoreInstanceState(mSavedInstanceState);
2967 mSavedInstanceState = null;
2968 }
2969
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002971
2972 // If we received the result of any pending adds while the loader was running (e.g. the
2973 // widget configuration forced an orientation change), process them now.
2974 for (int i = 0; i < sPendingAddList.size(); i++) {
2975 completeAdd(sPendingAddList.get(i));
2976 }
2977 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 }
2979
2980 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002981 * Updates the icons on the launcher that are affected by changes to the package list
2982 * on the device.
2983 */
2984 private void updateIconsAffectedByPackageManagerChanges() {
2985 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002986 updateVoiceSearchIcon();
2987 }
2988
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002989 @Override
2990 public void bindSearchablesChanged() {
2991 updateGlobalSearchIcon();
2992 }
2993
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002994 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 * Add the icons for all apps.
2996 *
2997 * Implementation of the method from LauncherModel.Callbacks.
2998 */
2999 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003000 if (mAppsCustomizeContent != null) {
3001 mAppsCustomizeContent.setApps(apps);
3002 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003003 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003004 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 }
3006
3007 /**
3008 * A package was installed.
3009 *
3010 * Implementation of the method from LauncherModel.Callbacks.
3011 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003012 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003013 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003015
Winson Chung785d2eb2011-04-14 16:08:02 -07003016 if (mAppsCustomizeContent != null) {
3017 mAppsCustomizeContent.addApps(apps);
3018 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003019 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 }
3021
3022 /**
3023 * A package was updated.
3024 *
3025 * Implementation of the method from LauncherModel.Callbacks.
3026 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003027 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003028 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003030 if (mWorkspace != null) {
3031 mWorkspace.updateShortcuts(apps);
3032 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003033
Winson Chung785d2eb2011-04-14 16:08:02 -07003034 if (mAppsCustomizeContent != null) {
3035 mAppsCustomizeContent.updateApps(apps);
3036 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003037 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 }
3039
3040 /**
3041 * A package was uninstalled.
3042 *
3043 * Implementation of the method from LauncherModel.Callbacks.
3044 */
Joe Onorato36115782010-06-17 13:28:48 -04003045 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003047 if (permanent) {
3048 mWorkspace.removeItems(apps);
3049 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003050
Winson Chung785d2eb2011-04-14 16:08:02 -07003051 if (mAppsCustomizeContent != null) {
3052 mAppsCustomizeContent.removeApps(apps);
3053 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003054 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 }
Joe Onoratobe386092009-11-17 17:32:16 -08003056
3057 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003058 * A number of packages were updated.
3059 */
3060 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003061
Winson Chung785d2eb2011-04-14 16:08:02 -07003062 if (mAppsCustomizeContent != null) {
3063 mAppsCustomizeContent.onPackagesUpdated();
3064 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003065 }
3066
Winson Chung400438b2011-01-16 17:53:48 -08003067 private int mapConfigurationOriActivityInfoOri(int configOri) {
3068 final Display d = getWindowManager().getDefaultDisplay();
3069 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3070 switch (d.getRotation()) {
3071 case Surface.ROTATION_0:
3072 case Surface.ROTATION_180:
3073 // We are currently in the same basic orientation as the natural orientation
3074 naturalOri = configOri;
3075 break;
3076 case Surface.ROTATION_90:
3077 case Surface.ROTATION_270:
3078 // We are currently in the other basic orientation to the natural orientation
3079 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3080 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3081 break;
3082 }
3083
3084 int[] oriMap = {
3085 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3086 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3087 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3088 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3089 };
3090 // Since the map starts at portrait, we need to offset if this device's natural orientation
3091 // is landscape.
3092 int indexOffset = 0;
3093 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3094 indexOffset = 1;
3095 }
3096 return oriMap[(d.getRotation() + indexOffset) % 4];
3097 }
3098 public void lockScreenOrientation() {
3099 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3100 .getConfiguration().orientation));
3101 }
3102 public void unlockScreenOrientation() {
3103 mHandler.postDelayed(new Runnable() {
3104 public void run() {
3105 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3106 }
3107 }, mRestoreScreenOrientationDelay);
3108 }
3109
Winson Chung80baf5a2010-08-09 16:03:15 -07003110 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003111 * Prints out out state for debugging.
3112 */
3113 public void dumpState() {
3114 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003115 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003116 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3117 Log.d(TAG, "mRestoring=" + mRestoring);
3118 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3119 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003120 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003121 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003122
Winson Chung785d2eb2011-04-14 16:08:02 -07003123 if (mAppsCustomizeContent != null) {
3124 mAppsCustomizeContent.dumpState();
3125 }
Joe Onoratobe386092009-11-17 17:32:16 -08003126 Log.d(TAG, "END launcher2 dump state");
3127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128}
Michael Jurka2763be32011-02-24 11:19:57 -08003129
Michael Jurkaabded662011-03-04 12:06:57 -08003130interface LauncherTransitionable {
3131 void onLauncherTransitionStart(Animator animation);
3132 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003133}