blob: bb8e497d40e7b47cd3a8b8a976cdc31b852945c4 [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;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
153 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700162 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
163
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700166 private State mState = State.WORKSPACE;
167 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800176 private final BroadcastReceiver mCloseSystemDialogsReceiver
177 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800178 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private LayoutInflater mInflater;
181
Joe Onorato00acb122009-08-04 16:04:30 -0400182 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private AppWidgetManager mAppWidgetManager;
186 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700187
Michael Jurka0280c3b2010-09-17 15:00:07 -0700188 private int mAddScreen = -1;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private int mAddCellX = -1;
190 private int mAddCellY = -1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700191 private int[] mAddDropPosition;
192 private int[] mTmpAddItemCellCoordinates = new int[2];
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private FolderInfo mFolderInfo;
195
Winson Chung97d85d22011-04-13 11:27:36 -0700196 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800197 private View mAllAppsButton;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700198 private SearchDropTargetBar mSearchDeleteBar;
199 private AppsCustomizeTabHost mAppsCustomizeTabHost;
200 private AppsCustomizePagedView mAppsCustomizeContent;
201 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private Bundle mSavedState;
204
205 private SpannableStringBuilder mDefaultKeySsb = null;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 private boolean mWorkspaceLoading = true;
208
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800209 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private boolean mRestoring;
211 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700212 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 private Bundle mSavedInstanceState;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800218 private boolean mUserPresent = true;
219 private boolean mVisible = false;
220 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700222 private static LocaleConfiguration sLocaleConfiguration = null;
223
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700224 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400226 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400227 private String[] mHotseatConfig = null;
228 private Intent[] mHotseats = null;
229 private Drawable[] mHotseatIcons = null;
230 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700232 private Intent mAppMarketIntent = null;
233
Adam Cohended9f8d2010-11-03 13:25:16 -0700234 // Related to the auto-advancing of widgets
235 private final int ADVANCE_MSG = 1;
236 private final int mAdvanceInterval = 20000;
237 private final int mAdvanceStagger = 250;
238 private long mAutoAdvanceSentTime;
239 private long mAutoAdvanceTimeLeft = -1;
240 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
241 new HashMap<View, AppWidgetProviderInfo>();
242
Winson Chung400438b2011-01-16 17:53:48 -0800243 // Determines how long to wait after a rotation before restoring the screen orientation to
244 // match the sensor state.
245 private final int mRestoreScreenOrientationDelay = 500;
246
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800248 private static Drawable.ConstantState sGlobalSearchIcon;
249 private static Drawable.ConstantState sVoiceSearchIcon;
250 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251
Adam Cohen2801caf2011-05-13 20:57:39 -0700252 private DragLayer mDragLayer;
253
Michael Jurkaddd62e92011-02-16 17:49:14 -0800254 private BubbleTextView mWaitingForResume;
255
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256 private static ArrayList<PendingAddArguments> sPendingAddList
257 = new ArrayList<PendingAddArguments>();
258
259 private static class PendingAddArguments {
260 int requestCode;
261 Intent intent;
262 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:
603 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
604 break;
605 case REQUEST_PICK_SHORTCUT:
606 processShortcut(args.intent);
607 break;
608 case REQUEST_CREATE_SHORTCUT:
609 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_APPWIDGET:
612 addAppWidgetFromPick(args.intent);
613 break;
614 case REQUEST_CREATE_APPWIDGET:
615 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
616 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700617 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 case REQUEST_PICK_WALLPAPER:
619 // We just wanted the activity result here so we can clear mWaitingForResult
620 break;
621 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700622 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800626 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700627 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700628 mWaitingForResult = false;
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // The pattern used here is that a user PICKs a specific application,
631 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
634 // launch over to the Music app to actually CREATE_SHORTCUT.
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800636 final PendingAddArguments args = new PendingAddArguments();
637 args.requestCode = requestCode;
638 args.intent = data;
639 args.screen = mAddScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700640 args.cellX = mAddCellX;
641 args.cellY = mAddCellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800642
643 // If the loader is still running, defer the add until it is done.
644 if (isWorkspaceLocked()) {
645 sPendingAddList.add(args);
646 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Romain Guy18042c82009-11-06 11:44:55 -0800649 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700650 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
651 if (data != null) {
652 // Clean up the appWidgetId if we canceled
653 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
654 if (appWidgetId != -1) {
655 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
658 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700659
660 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
661 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 @Override
665 protected void onResume() {
666 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800667 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700668 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 mWorkspaceLoading = true;
670 mModel.startLoader(this, true);
671 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700672 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800674 if (mWaitingForResume != null) {
675 mWaitingForResume.setStayPressed(false);
676 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700677 // When we resume Launcher, a different Activity might be responsible for the app
678 // market intent, so refresh the icon
679 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681
682 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 protected void onPause() {
684 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700685 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800686 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 }
Romain Guycbb89e42009-06-08 15:52:54 -0700688
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700689 @Override
690 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 // Flag the loader to stop early before switching
692 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 if (mAppsCustomizeContent != null) {
694 mAppsCustomizeContent.surrender();
695 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800696 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700697 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800699 // We can't hide the IME if it was forced open. So don't bother
700 /*
701 @Override
702 public void onWindowFocusChanged(boolean hasFocus) {
703 super.onWindowFocusChanged(hasFocus);
704
705 if (hasFocus) {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 WindowManager.LayoutParams lp = getWindow().getAttributes();
709 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
710 android.os.Handler()) {
711 protected void onReceiveResult(int resultCode, Bundle resultData) {
712 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
713 }
714 });
715 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
716 }
717 }
718 */
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 private boolean acceptFilter() {
721 final InputMethodManager inputManager = (InputMethodManager)
722 getSystemService(Context.INPUT_METHOD_SERVICE);
723 return !inputManager.isFullscreenMode();
724 }
725
726 @Override
727 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700728 final int uniChar = event.getUnicodeChar();
729 final boolean handled = super.onKeyDown(keyCode, event);
730 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
731 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
733 keyCode, event);
734 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700735 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700736 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700737 // showSearchDialog()
738 // If there are multiple keystrokes before the search dialog takes focus,
739 // onSearchRequested() will be called for every keystroke,
740 // but it is idempotent, so it's fine.
741 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743 }
744
Joe Onorato8a9625e2010-01-28 15:55:35 -0800745 // Eat the long press event so the keyboard doesn't come up.
746 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
747 return true;
748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return handled;
751 }
752
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 private String getTypedText() {
754 return mDefaultKeySsb.toString();
755 }
756
757 private void clearTypedText() {
758 mDefaultKeySsb.clear();
759 mDefaultKeySsb.clearSpans();
760 Selection.setSelection(mDefaultKeySsb, 0);
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700764 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
765 * State
766 */
767 private static State intToState(int stateOrdinal) {
768 State state = State.WORKSPACE;
769 final State[] stateValues = State.values();
770 for (int i = 0; i < stateValues.length; i++) {
771 if (stateValues[i].ordinal() == stateOrdinal) {
772 state = stateValues[i];
773 break;
774 }
775 }
776 return state;
777 }
778
779 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 * Restores the previous state, if it exists.
781 *
782 * @param savedState The previous state.
783 */
784 private void restoreState(Bundle savedState) {
785 if (savedState == null) {
786 return;
787 }
788
Michael Jurka883f55b2010-10-21 15:47:14 -0700789 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
790
Winson Chungf0ea4d32011-06-06 14:27:16 -0700791 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800792 showAllApps(false);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
796 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700797 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
800 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803 mAddScreen = addScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700804 mAddCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
805 mAddCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 mRestoring = true;
807 }
808
809 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
810 if (renameFolder) {
811 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700812 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
Winson Chunga12a2502010-12-20 14:41:35 -0800815
Winson Chung785d2eb2011-04-14 16:08:02 -0700816
817 // Restore the AppsCustomize tab
818 if (mAppsCustomizeTabHost != null) {
819 String curTab = savedState.getString("apps_customize_currentTab");
820 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700821 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 mAppsCustomizeContent.setContentType(
823 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
824 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
825 }
826
827 // Note: currently we do not restore the page for the AppsCustomize pane because the
828 // change in layout can drastically affect the saved page index
829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
832 /**
833 * Finds all the views we need and configure them properly.
834 */
835 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700836 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400837
Winson Chung4c98d922011-05-31 16:50:48 -0700838 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
839 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
Winson Chung4c98d922011-05-31 16:50:48 -0700841 // Setup the drag layer
842 mDragLayer.setup(this, dragController);
843
844 // Setup the workspace
845 mWorkspace.setHapticFeedbackEnabled(false);
846 mWorkspace.setOnLongClickListener(this);
847 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700848 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Get the search/delete bar
851 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700852
Winson Chungf0ea4d32011-06-06 14:27:16 -0700853 // Setup AppsCustomize
854 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
855 findViewById(R.id.apps_customize_pane);
856 mAppsCustomizeContent = (AppsCustomizePagedView)
857 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
858 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400859
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 // Setup AppsCustomize button
861 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
862 mAllAppsButton.setOnClickListener(new OnClickListener() {
863 public void onClick(View v) {
864 onClickAllAppsButton(v);
865 }
866 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800867
Winson Chungf0ea4d32011-06-06 14:27:16 -0700868 if (!LauncherApplication.isScreenLarge()) {
869 // Setup AppsCustomize button on the phone
870 HandleView handleView = (HandleView) mAllAppsButton;
871 handleView.setLauncher(this);
872 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700873
874 // Setup Hotseat
875 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
876 hotseatLeft.setContentDescription(mHotseatLabels[0]);
877 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
878 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
879 hotseatRight.setContentDescription(mHotseatLabels[1]);
880 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800881 }
882
Winson Chungf0ea4d32011-06-06 14:27:16 -0700883 if (!LauncherApplication.isScreenLarge()) {
884 // Setup keylistener for button cluster
885 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
886 View.OnKeyListener listener = null;
887 if (LauncherApplication.isScreenLarge()) {
888 // For tablets, AllApps lives in the button bar at the top
889 listener = new ButtonBarKeyEventListener();
890 } else {
891 // For phones, AppsCustomize lives in the "dock" at the bottom
892 listener = new DockKeyEventListener();
893 }
894 int buttonCount = mButtonCluster.getChildCount();
895 for (int i = 0; i < buttonCount; ++i) {
896 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
897 }
Winson Chung97d85d22011-04-13 11:27:36 -0700898 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800899
Winson Chung4c98d922011-05-31 16:50:48 -0700900 // Setup the drag controller (the drop targets have to be added in reverse order)
901 dragController.setDragScoller(mWorkspace);
902 dragController.setScrollView(mDragLayer);
903 dragController.setMoveTarget(mWorkspace);
904 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700905 if (mSearchDeleteBar != null) {
906 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
909
Romain Guy8a73c512009-11-09 19:19:59 -0800910 @SuppressWarnings({"UnusedDeclaration"})
911 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700912 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800913 mWorkspace.scrollLeft();
914 }
Romain Guy8a73c512009-11-09 19:19:59 -0800915 }
916
917 @SuppressWarnings({"UnusedDeclaration"})
918 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700919 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800920 mWorkspace.scrollRight();
921 }
Romain Guy8a73c512009-11-09 19:19:59 -0800922 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400923
924 @SuppressWarnings({"UnusedDeclaration"})
925 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700926 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400927
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400928 int index = -1;
929 if (v.getId() == R.id.hotseat_left) {
930 index = 0;
931 } else if (v.getId() == R.id.hotseat_right) {
932 index = 1;
933 }
934
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400935 // reload these every tap; you never know when they might change
936 loadHotseats();
937 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400938 startActivitySafely(
939 mHotseats[index],
940 "hotseat"
941 );
942 }
943 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
946 * Creates a view representing a shortcut.
947 *
948 * @param info The data structure describing the shortcut.
949 *
950 * @return A View inflated from R.layout.application.
951 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800952 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700954 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 /**
958 * Creates a view representing a shortcut inflated from the specified resource.
959 *
960 * @param layoutResId The id of the XML layout used to create the shortcut.
961 * @param parent The group the shortcut belongs to.
962 * @param info The data structure describing the shortcut.
963 *
964 * @return A View inflated from layoutResId.
965 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800967 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
968 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 return favorite;
971 }
972
973 /**
974 * Add an application shortcut to the workspace.
975 *
976 * @param data The intent describing the application.
977 * @param cellInfo The position on screen where to create the shortcut.
978 */
Adam Cohenfbba09b2011-07-18 21:43:05 -0700979 void completeAddApplication(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700980 final int[] cellXY = mTmpAddItemCellCoordinates;
981 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
982
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983 // First we check if we already know the exact location where we want to add this item.
984 if (cellX >= 0 && cellY >= 0) {
985 cellXY[0] = cellX;
986 cellXY[1] = cellY;
987 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 showOutOfSpaceMessage();
989 return;
990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800992 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993
Romain Guy73b979d2009-06-09 12:57:21 -0700994 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700999 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 } else {
1001 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003 }
Romain Guycbb89e42009-06-08 15:52:54 -07001004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 /**
1006 * Add a shortcut to the workspace.
1007 *
1008 * @param data The intent describing the shortcut.
1009 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 */
Adam Cohenfbba09b2011-07-18 21:43:05 -07001011 private void completeAddShortcut(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 final int[] cellXY = mTmpAddItemCellCoordinates;
1013 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001014
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001015 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001016 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001017
1018 // First we check if we already know the exact location where we want to add this item.
1019 if (cellX >= 0 && cellY >= 0) {
1020 cellXY[0] = cellX;
1021 cellXY[1] = cellY;
1022 foundCellSpan = true;
1023 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001024 // when dragging and dropping, just find the closest free spot
1025 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1026 int[] result = screenLayout.findNearestVacantArea(
1027 touchXY[0], touchXY[1], 1, 1, cellXY);
1028 foundCellSpan = (result != null);
1029 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001030 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001031 }
1032
1033 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 showOutOfSpaceMessage();
1035 return;
1036 }
1037
1038 final ShortcutInfo info = mModel.addShortcut(
1039 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
1041 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001043 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 }
1045 }
1046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001050 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 */
Winson Chungf7640c82011-02-28 13:47:29 -08001053 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001055
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Adam Cohen09353862011-07-14 16:10:03 -07001058
1059 // We want to account for the extra amount of padding that we are adding to the widget
1060 // to ensure that it gets the full amount of space that it has requested
1061 Resources r = getResources();
1062 int requiredWidth = appWidgetInfo.minWidth +
1063 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1064 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1065 int requiredHeight = appWidgetInfo.minHeight +
1066 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1067 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1068 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 // We have saved the position to which the widget was dragged-- this really only matters
1072 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001075 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001077 if (mAddCellX >= 0 && mAddCellY >= 0) {
1078 cellXY[0] = mAddCellX;
1079 cellXY[1] = mAddCellY;
1080 foundCellSpan = true;
1081 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 // when dragging and dropping, just find the closest free spot
1083 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1084 int[] result = screenLayout.findNearestVacantArea(
1085 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001086 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001088 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001089 }
1090
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001092 if (appWidgetId != -1) {
1093 // Deleting an app widget ID is a void call but writes to disk before returning
1094 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001095 new Thread("deleteAppWidgetId") {
1096 public void run() {
1097 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1098 }
1099 }.start();
1100 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001102 return;
1103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 // Build Launcher-specific widget info and save to database
1106 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 launcherInfo.spanX = spanXY[0];
1108 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 LauncherModel.addItemToDatabase(this, launcherInfo,
1111 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
1114 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001123
1124 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1129 @Override
1130 public void onReceive(Context context, Intent intent) {
1131 final String action = intent.getAction();
1132 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1133 mUserPresent = false;
1134 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001135
1136 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001137 if (mAppsCustomizeContent != null) {
1138 mAppsCustomizeContent.reset();
1139 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1141 mUserPresent = true;
1142 updateRunning();
1143 }
1144 }
1145 };
1146
1147 @Override
1148 public void onAttachedToWindow() {
1149 super.onAttachedToWindow();
1150
1151 // Listen for broadcasts related to user-presence
1152 final IntentFilter filter = new IntentFilter();
1153 filter.addAction(Intent.ACTION_SCREEN_OFF);
1154 filter.addAction(Intent.ACTION_USER_PRESENT);
1155 registerReceiver(mReceiver, filter);
1156
Adam Cohend113e0c2010-11-11 10:48:05 -08001157 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 mVisible = true;
1159 }
1160
1161 @Override
1162 public void onDetachedFromWindow() {
1163 super.onDetachedFromWindow();
1164 mVisible = false;
1165
Adam Cohend113e0c2010-11-11 10:48:05 -08001166 if (mAttached) {
1167 unregisterReceiver(mReceiver);
1168 mAttached = false;
1169 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001170 updateRunning();
1171 }
1172
1173 public void onWindowVisibilityChanged(int visibility) {
1174 mVisible = visibility == View.VISIBLE;
1175 updateRunning();
1176 }
1177
1178 private void sendAdvanceMessage(long delay) {
1179 mHandler.removeMessages(ADVANCE_MSG);
1180 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1181 mHandler.sendMessageDelayed(msg, delay);
1182 mAutoAdvanceSentTime = System.currentTimeMillis();
1183 }
1184
1185 private void updateRunning() {
1186 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1187 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1188 mAutoAdvanceRunning = autoAdvanceRunning;
1189 if (autoAdvanceRunning) {
1190 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1191 sendAdvanceMessage(delay);
1192 } else {
1193 if (!mWidgetsToAdvance.isEmpty()) {
1194 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1195 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1196 }
1197 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001198 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001199 }
1200 }
1201 }
1202
1203 private final Handler mHandler = new Handler() {
1204 @Override
1205 public void handleMessage(Message msg) {
1206 if (msg.what == ADVANCE_MSG) {
1207 int i = 0;
1208 for (View key: mWidgetsToAdvance.keySet()) {
1209 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1210 final int delay = mAdvanceStagger * i;
1211 if (v instanceof Advanceable) {
1212 postDelayed(new Runnable() {
1213 public void run() {
1214 ((Advanceable) v).advance();
1215 }
1216 }, delay);
1217 }
1218 i++;
1219 }
1220 sendAdvanceMessage(mAdvanceInterval);
1221 }
1222 }
1223 };
1224
1225 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001226 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001227 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1228 if (v instanceof Advanceable) {
1229 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001230 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 updateRunning();
1232 }
1233 }
1234
1235 void removeWidgetToAutoAdvance(View hostView) {
1236 if (mWidgetsToAdvance.containsKey(hostView)) {
1237 mWidgetsToAdvance.remove(hostView);
1238 updateRunning();
1239 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 }
1241
Joe Onorato9c1289c2009-08-17 11:03:03 -04001242 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 launcherInfo.hostView = null;
1245 }
1246
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 void showOutOfSpaceMessage() {
1248 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1249 }
1250
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 public LauncherAppWidgetHost getAppWidgetHost() {
1252 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
Romain Guycbb89e42009-06-08 15:52:54 -07001254
Winson Chunga9abd0e2010-10-27 17:18:37 -07001255 public LauncherModel getModel() {
1256 return mModel;
1257 }
1258
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001259 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001260 getWindow().closeAllPanels();
1261
1262 try {
1263 dismissDialog(DIALOG_CREATE_SHORTCUT);
1264 // Unlock the workspace if the dialog was showing
1265 } catch (Exception e) {
1266 // An exception is thrown if the dialog is not visible, which is fine
1267 }
1268
1269 try {
1270 dismissDialog(DIALOG_RENAME_FOLDER);
1271 // Unlock the workspace if the dialog was showing
1272 } catch (Exception e) {
1273 // An exception is thrown if the dialog is not visible, which is fine
1274 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001275
1276 // Whatever we were doing is hereby canceled.
1277 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001278 }
1279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 @Override
1281 protected void onNewIntent(Intent intent) {
1282 super.onNewIntent(intent);
1283
1284 // Close the menu
1285 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001286 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001287 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001288
Adam Cohen95bb8002011-07-03 23:40:28 -07001289 closeFolder();
1290
Joe Onorato14f122b2009-11-19 14:06:36 -08001291 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1292 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001293
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001294 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001295 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001296 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001297 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001298 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001299 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001300 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001301
Joe Onorato3a8820b2009-11-10 15:06:42 -08001302 final View v = getWindow().peekDecorView();
1303 if (v != null && v.getWindowToken() != null) {
1304 InputMethodManager imm = (InputMethodManager)getSystemService(
1305 INPUT_METHOD_SERVICE);
1306 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001308
Michael Jurka35aa14d2011-07-07 17:01:08 -07001309 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001310 if (mAppsCustomizeContent != null) {
1311 mAppsCustomizeContent.reset();
1312 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 }
1315
1316 @Override
1317 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1318 // Do not call super here
1319 mSavedInstanceState = savedInstanceState;
1320 }
1321
1322 @Override
1323 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001324 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001325 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
Michael Jurka883f55b2010-10-21 15:47:14 -07001327 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001328 // We close any open folder since it will not be re-opened, and we need to make sure
1329 // this state is reflected.
1330 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331
Michael Jurka0280c3b2010-09-17 15:00:07 -07001332 if (mAddScreen > -1 && mWaitingForResult) {
1333 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001334 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddCellX);
1335 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
1338 if (mFolderInfo != null && mWaitingForResult) {
1339 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1340 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1341 }
Michael Jurka946ad472010-07-09 18:05:18 -07001342
Winson Chung785d2eb2011-04-14 16:08:02 -07001343 // Save the current AppsCustomize tab
1344 if (mAppsCustomizeTabHost != null) {
1345 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1346 if (currentTabTag != null) {
1347 outState.putString("apps_customize_currentTab", currentTabTag);
1348 }
1349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 }
1351
1352 @Override
1353 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001355
Winson Chungcd2b0142011-06-08 16:02:26 -07001356 // Stop callbacks from LauncherModel
1357 LauncherApplication app = ((LauncherApplication) getApplication());
1358 mModel.stopLoader();
1359 app.setLauncher(null);
1360
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001362 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001364 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001366 mAppWidgetHost = null;
1367
1368 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
1370 TextKeyListener.getInstance().release();
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372
Adam Cohen4eac29a2011-07-11 17:53:37 -07001373 unbindWorkspaceItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001374
1375 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001376 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001377
1378 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1379 mWorkspace.removeAllViews();
1380 mWorkspace = null;
1381 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001382
1383 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
Adam Cohena9cf38f2011-05-02 15:36:58 -07001386 public DragController getDragController() {
1387 return mDragController;
1388 }
1389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 @Override
1391 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001392 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 super.startActivityForResult(intent, requestCode);
1394 }
1395
1396 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001397 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001399
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001400 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001401
Karl Rosaen138a0412009-04-23 19:00:21 -07001402 if (initialQuery == null) {
1403 // Use any text typed in the launcher as the initial query
1404 initialQuery = getTypedText();
1405 clearTypedText();
1406 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 if (appSearchData == null) {
1408 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001409 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
Romain Guycbb89e42009-06-08 15:52:54 -07001411
Karl Rosaen138a0412009-04-23 19:00:21 -07001412 final SearchManager searchManager =
1413 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001415 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
1418 @Override
1419 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001420 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001421 return false;
1422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423
1424 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001425
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1427 .setIcon(android.R.drawable.ic_menu_add)
1428 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001429 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1430 .setIcon(android.R.drawable.ic_menu_manage)
1431 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001432 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 .setIcon(android.R.drawable.ic_menu_gallery)
1434 .setAlphabeticShortcut('W');
1435 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1436 .setIcon(android.R.drawable.ic_search_category_default)
1437 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1438 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1439 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1440 .setAlphabeticShortcut('N');
1441
1442 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001443 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1444 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
1446 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1447 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1448 .setIntent(settings);
1449
1450 return true;
1451 }
1452
1453 @Override
1454 public boolean onPrepareOptionsMenu(Menu menu) {
1455 super.onPrepareOptionsMenu(menu);
1456
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1458 // related code?
1459 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
1461 return true;
1462 }
1463
1464 @Override
1465 public boolean onOptionsItemSelected(MenuItem item) {
1466 switch (item.getItemId()) {
1467 case MENU_ADD:
1468 addItems();
1469 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001470 case MENU_MANAGE_APPS:
1471 manageApps();
1472 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 case MENU_WALLPAPER_SETTINGS:
1474 startWallpaper();
1475 return true;
1476 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001477 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 return true;
1479 case MENU_NOTIFICATIONS:
1480 showNotifications();
1481 return true;
1482 }
1483
1484 return super.onOptionsItemSelected(item);
1485 }
Romain Guycbb89e42009-06-08 15:52:54 -07001486
Karl Rosaen138a0412009-04-23 19:00:21 -07001487 /**
1488 * Indicates that we want global search for this activity by setting the globalSearch
1489 * argument for {@link #startSearch} to true.
1490 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001492 @Override
1493 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001494 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001495 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001496 }
1497
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 public boolean isWorkspaceLocked() {
1499 return mWorkspaceLoading || mWaitingForResult;
1500 }
1501
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001503 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 private void resetAddInfo() {
1508 mAddScreen = -1;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001509 mAddCellX = -1;
1510 mAddCellY = -1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 mAddDropPosition = null;
1512 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001513
Winson Chung7ad01412010-09-27 11:33:03 -07001514 private void manageApps() {
1515 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1516 }
1517
Michael Jurkaaf442092010-06-10 17:01:57 -07001518 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001519 // TODO: catch bad widget exception when sent
1520 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001521 // TODO: Is this log message meaningful?
1522 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001523 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001524 }
1525
Winson Chung55cef262010-10-28 14:14:18 -07001526 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001527 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
Bjorn Bringert7984c942009-12-09 15:38:25 +00001529 if (appWidget.configure != null) {
1530 // Launch over to configure widget, if needed
1531 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1532 intent.setComponent(appWidget.configure);
1533 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001534 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001535 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1536 intent.putExtra(
1537 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1538 info.mimeType);
1539
1540 final String mimeType = info.mimeType;
1541 final ClipData clipData = (ClipData) info.configurationData;
1542 final ClipDescription clipDesc = clipData.getDescription();
1543 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1544 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001545 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001546 final CharSequence stringData = item.getText();
1547 final Uri uriData = item.getUri();
1548 final Intent intentData = item.getIntent();
1549 final String key =
1550 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1551 if (uriData != null) {
1552 intent.putExtra(key, uriData);
1553 } else if (intentData != null) {
1554 intent.putExtra(key, intentData);
1555 } else if (stringData != null) {
1556 intent.putExtra(key, stringData);
1557 }
1558 break;
1559 }
1560 }
1561 }
Winson Chung55cef262010-10-28 14:14:18 -07001562 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001563
Romain Guy8e633c52010-03-04 12:51:36 -08001564 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001566 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001567 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001568
1569 // Exit spring loaded mode if necessary after adding the widget
1570 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 }
1572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
Adam Cohenfbba09b2011-07-18 21:43:05 -07001574 /**
1575 * Process a shortcut drop.
1576 *
1577 * @param componentName The name of the component
1578 * @param screen The screen where it should be added
1579 * @param cell The cell it should be added to, optional
1580 * @param position The location on the screen where it was dropped, optional
1581 */
1582 void processShortcutFromDrop(ComponentName componentName, int screen, int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001583 resetAddInfo();
1584 mAddScreen = screen;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001585 mAddDropPosition = loc;
1586
1587 if (cell != null) {
1588 mAddCellX = cell[0];
1589 mAddCellY = cell[1];
1590 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001591
1592 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1593 createShortcutIntent.setComponent(componentName);
1594 processShortcut(createShortcutIntent);
1595 }
1596
Adam Cohenfbba09b2011-07-18 21:43:05 -07001597 /**
1598 * Process a widget drop.
1599 *
1600 * @param info The PendingAppWidgetInfo of the widget being added.
1601 * @param screen The screen where it should be added
1602 * @param cell The cell it should be added to, optional
1603 * @param position The location on the screen where it was dropped, optional
1604 */
1605 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] cell, int[] loc) {
1606 resetAddInfo();
1607 mAddScreen = screen;
1608 mAddDropPosition = loc;
1609
1610 if (cell != null) {
1611 mAddCellX = cell[0];
1612 mAddCellY = cell[1];
1613 }
1614
1615 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1616 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1617 addAppWidgetImpl(appWidgetId, info);
1618 }
1619
Joe Onoratodeb98af2010-02-19 14:59:39 -08001620 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001621 // Handle case where user selected "Applications"
1622 String applicationName = getResources().getString(R.string.group_applications);
1623 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001624
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001625 if (applicationName != null && applicationName.equals(shortcutName)) {
1626 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1627 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001628
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001629 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1630 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001631 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001632 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001633 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001634 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001635 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637
Winson Chung24ab2f12010-09-16 14:10:47 -07001638 void processWallpaper(Intent intent) {
1639 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1640 }
1641
Adam Cohenfbba09b2011-07-18 21:43:05 -07001642 FolderIcon addFolder(final int screen, int cellX, int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001643 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 folderInfo.title = getText(R.string.folder_name);
1645
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001647 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001648 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Adam Cohenfbba09b2011-07-18 21:43:05 -07001649 screen, cellX, cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001650 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
1652 // Create the view
1653 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001654 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1655 folderInfo, mIconCache);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001656 mWorkspace.addInScreen(newFolder, screen, cellX, cellY, 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001657 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 }
Romain Guycbb89e42009-06-08 15:52:54 -07001659
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001661 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662 }
1663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 private void showNotifications() {
1665 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1666 if (statusBar != null) {
1667 statusBar.expand();
1668 }
1669 }
1670
1671 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001672 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001674 Intent chooser = Intent.createChooser(pickWallpaper,
1675 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001676 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1677 // Removed in Eclair MR1
1678// WallpaperManager wm = (WallpaperManager)
1679// getSystemService(Context.WALLPAPER_SERVICE);
1680// WallpaperInfo wi = wm.getWallpaperInfo();
1681// if (wi != null && wi.getSettingsActivity() != null) {
1682// LabeledIntent li = new LabeledIntent(getPackageName(),
1683// R.string.configure_wallpaper, 0);
1684// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1685// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1686// }
Mike Clerona0618e42009-10-22 13:55:21 -07001687 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001690 /**
1691 * Registers various content observers. The current implementation registers
1692 * only a favorites observer to keep track of the favorites applications.
1693 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001694 private void registerContentObservers() {
1695 ContentResolver resolver = getContentResolver();
1696 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1697 true, mWidgetObserver);
1698 }
1699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 @Override
1701 public boolean dispatchKeyEvent(KeyEvent event) {
1702 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1703 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001705 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001706 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001707 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001708 dumpState();
1709 return true;
1710 }
1711 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001712 }
1713 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1714 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001715 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 return true;
1717 }
1718 }
1719
1720 return super.dispatchKeyEvent(event);
1721 }
1722
Joe Onorato88ec0992009-11-19 13:16:06 -08001723 @Override
1724 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001725 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001726 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001727 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001728 Folder openFolder = mWorkspace.getOpenFolder();
1729 if (openFolder.isEditingName()) {
1730 openFolder.dismissEditingName();
1731 } else {
1732 closeFolder();
1733 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001734 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001735 mWorkspace.exitWidgetResizeMode();
1736
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001737 // Back button is a no-op here, but give at least some feedback for the button press
1738 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001739 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001740 }
1741
Adam Cohen2801caf2011-05-13 20:57:39 -07001742 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 Folder folder = mWorkspace.getOpenFolder();
1744 if (folder != null) {
1745 closeFolder(folder);
1746 }
1747 }
1748
1749 void closeFolder(Folder folder) {
1750 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001751
Michael Jurka8c920dd2011-01-20 14:16:56 -08001752 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001754 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001755 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1756 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001757 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001759 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
1761
1762 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001763 * Re-listen when widgets are reset.
1764 */
1765 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001766 if (mAppWidgetHost != null) {
1767 mAppWidgetHost.startListening();
1768 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001769 }
1770
1771 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001772 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1773 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 */
Adam Cohen4eac29a2011-07-11 17:53:37 -07001775 private void unbindWorkspaceItems() {
1776 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001778
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 /**
1780 * Launches the intent referred by the clicked shortcut.
1781 *
1782 * @param v The view representing the clicked shortcut.
1783 */
1784 public void onClick(View v) {
1785 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001786 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001788 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001789 int[] pos = new int[2];
1790 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001791 intent.setSourceBounds(new Rect(pos[0], pos[1],
1792 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001793 boolean success = startActivitySafely(intent, tag);
1794
1795 if (success && v instanceof BubbleTextView) {
1796 mWaitingForResume = (BubbleTextView) v;
1797 mWaitingForResume.setStayPressed(true);
1798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001800 if (v instanceof FolderIcon) {
1801 FolderIcon fi = (FolderIcon) v;
1802 handleFolderClick(fi);
1803 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001804 } else if (v == mAllAppsButton) {
1805 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001806 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001807 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001808 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 }
1811 }
1812
Michael Jurka0e260592010-06-30 17:07:39 -07001813 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001814 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001815 // clicking anywhere on the workspace causes the customization drawer to slide down
1816 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001817 return false;
1818 }
1819
Michael Jurkaaf442092010-06-10 17:01:57 -07001820 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001821 * Event handler for the search button
1822 *
1823 * @param v The view that was clicked.
1824 */
1825 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001826 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001827 // Use a custom animation for launching search
1828 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001829 }
1830
1831 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001832 * Event handler for the voice button
1833 *
1834 * @param v The view that was clicked.
1835 */
1836 public void onClickVoiceButton(View v) {
1837 startVoiceSearch();
1838 }
1839
1840 private void startVoiceSearch() {
1841 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1842 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1843 startActivity(intent);
1844 }
1845
1846 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001847 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001848 * enters home screen customization mode.
1849 *
1850 * @param v The view that was clicked.
1851 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001852 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001853 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001854 }
1855
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001856 /**
1857 * Event handler for the "grid" button that appears on the home screen, which
1858 * enters all apps mode.
1859 *
1860 * @param v The view that was clicked.
1861 */
1862 public void onClickAllAppsButton(View v) {
1863 showAllApps(true);
1864 }
1865
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001866 public void onClickAppMarketButton(View v) {
1867 if (mAppMarketIntent != null) {
1868 startActivitySafely(mAppMarketIntent, "app market");
1869 }
1870 }
1871
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001872 void startApplicationDetailsActivity(ComponentName componentName) {
1873 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001874 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1875 Uri.fromParts("package", packageName, null));
1876 startActivity(intent);
1877 }
1878
Patrick Dubroy5539af72010-09-07 15:22:01 -07001879 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1880 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1881 // System applications cannot be installed. For now, show a toast explaining that.
1882 // We may give them the option of disabling apps this way.
1883 int messageId = R.string.uninstall_system_app_text;
1884 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1885 } else {
1886 String packageName = appInfo.componentName.getPackageName();
1887 String className = appInfo.componentName.getClassName();
1888 Intent intent = new Intent(
1889 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1890 startActivity(intent);
1891 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001892 }
1893
Michael Jurkaddd62e92011-02-16 17:49:14 -08001894 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1896 try {
1897 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001898 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } catch (ActivityNotFoundException e) {
1900 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001901 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 } catch (SecurityException e) {
1903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001904 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001906 "or use the exported attribute for this activity. "
1907 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001909 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001911
Romain Guy8e633c52010-03-04 12:51:36 -08001912 void startActivityForResultSafely(Intent intent, int requestCode) {
1913 try {
1914 startActivityForResult(intent, requestCode);
1915 } catch (ActivityNotFoundException e) {
1916 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1917 } catch (SecurityException e) {
1918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1919 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1920 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1921 "or use the exported attribute for this activity.", e);
1922 }
1923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924
Adam Cohena9cf38f2011-05-02 15:36:58 -07001925 private void handleFolderClick(FolderIcon folderIcon) {
1926 final FolderInfo info = folderIcon.mInfo;
1927 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 // Close any open folder
1929 closeFolder();
1930 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001931 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 } else {
1933 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001934 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 int folderScreen;
1936 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001937 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 // .. and close it
1939 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001940 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 // Close any folder open on the current screen
1942 closeFolder();
1943 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001944 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
1946 }
1947 }
1948 }
1949
Adam Cohen2801caf2011-05-13 20:57:39 -07001950 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1951 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1952 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1953 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1954
1955 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1956 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1957 oa.start();
1958 }
1959
1960 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1961 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1962 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1963 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1964
1965 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1966 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1967 oa.start();
1968 }
1969
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001971 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 * is animated relative to the specified View. If the View is null, no animation
1973 * is played.
1974 *
1975 * @param folderInfo The FolderInfo describing the folder to open.
1976 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001977 public void openFolder(FolderIcon folderIcon) {
1978 Folder folder = folderIcon.mFolder;
1979 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980
Adam Cohen2801caf2011-05-13 20:57:39 -07001981 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001982 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983
Adam Cohen8e776a62011-06-28 18:10:06 -07001984 mDragLayer.addView(folder);
1985 mDragController.addDropTarget((DropTarget) folder);
1986
Adam Cohena9cf38f2011-05-02 15:36:58 -07001987 folder.animateOpen();
1988 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001992 if (mState != State.WORKSPACE) {
1993 return false;
1994 }
1995
Romain Guy1fbc1c82009-11-09 20:43:08 -08001996 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001997 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001998 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001999 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2000 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2001 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002002 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002003 return true;
2004 }
2005
Joe Onorato9c1289c2009-08-17 11:03:03 -04002006 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 return false;
2008 }
2009
2010 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002011 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 }
2013
Michael Jurka0280c3b2010-09-17 15:00:07 -07002014 resetAddInfo();
2015 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002017 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 return true;
2019 }
2020
Michael Jurka0280c3b2010-09-17 15:00:07 -07002021 final View itemUnderLongClick = longClickCellInfo.cell;
2022
Winson Chung304dcde2011-01-07 11:17:23 -08002023 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002024 if (itemUnderLongClick == null) {
2025 // User long pressed on empty space
2026 mWorkspace.setAllowLongPress(false);
2027 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2028 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002029 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002031 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002033 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2034 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002035 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037 }
2038 }
2039 return true;
2040 }
2041
Romain Guye6b8e2f2009-11-10 11:56:55 -08002042 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002043 private void dismissPreview(final View v) {
2044 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002045 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2047 public void onDismiss() {
2048 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2049 int count = group.getChildCount();
2050 for (int i = 0; i < count; i++) {
2051 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2052 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002053 ArrayList<Bitmap> bitmaps =
2054 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2056
2057 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002058 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 window.setOnDismissListener(null);
2060 }
2061 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002062 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002063 }
2064 v.setTag(null);
2065 }
2066
Romain Guyf8e6a802009-12-07 17:48:02 -08002067 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002068 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002069 }
2070
Romain Guya6abce82009-11-10 02:54:41 -08002071 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002072 final Resources resources = getResources();
2073 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002074
Romain Guya6abce82009-11-10 02:54:41 -08002075 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002076
Romain Guy9d31e9f2009-11-11 18:54:28 -08002077 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002078
Romain Guyf8e6a802009-12-07 17:48:02 -08002079 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002080 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002081 int extraW = (int) ((r.left + r.right) * max);
2082 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002083
2084 int aW = cell.getWidth() - extraW;
2085 float w = aW / max;
2086
2087 int width = cell.getWidth();
2088 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002089 int x = cell.getPaddingLeft();
2090 int y = cell.getPaddingTop();
2091 width -= (x + cell.getPaddingRight());
2092 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002093
2094 float scale = w / width;
2095
2096 int count = end - start;
2097
2098 final float sWidth = width * scale;
2099 float sHeight = height * scale;
2100
Romain Guye6b8e2f2009-11-10 11:56:55 -08002101 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002102
Romain Guye6b8e2f2009-11-10 11:56:55 -08002103 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2104 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002105
2106 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002107 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002108 cell = (CellLayout) workspace.getChildAt(i);
2109
Romain Guyf8e6a802009-12-07 17:48:02 -08002110 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002111 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002112
2113 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002114 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002115 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002116 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002117
Romain Guy9d31e9f2009-11-11 18:54:28 -08002118 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002119 image.setImageBitmap(bitmap);
2120 image.setTag(i);
2121 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002122 image.setOnFocusChangeListener(handler);
2123 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002124 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002125
2126 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002127 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2128
Winson Chungaafa03c2010-06-11 17:34:16 -07002129 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002130 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002131
2132 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002133 p.setContentView(preview);
2134 p.setWidth((int) (sWidth * count + extraW));
2135 p.setHeight((int) (sHeight + extraH));
2136 p.setAnimationStyle(R.style.AnimationPreview);
2137 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002138 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002139 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002140 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002141
Romain Guye6b8e2f2009-11-10 11:56:55 -08002142 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2143 public void onDismiss() {
2144 dismissPreview(anchor);
2145 }
2146 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002147
2148 anchor.setTag(p);
2149 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002150 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002151 }
2152
Romain Guy9d31e9f2009-11-11 18:54:28 -08002153 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002154 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002155
Romain Guye6b8e2f2009-11-10 11:56:55 -08002156 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002157 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002158 }
2159
2160 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002161 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002162 v.post(this);
2163 }
2164
2165 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002166 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002167 }
2168
2169 public void onFocusChange(View v, boolean hasFocus) {
2170 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002171 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002172 }
Romain Guya6abce82009-11-10 02:54:41 -08002173 }
2174 }
2175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 Workspace getWorkspace() {
2177 return mWorkspace;
2178 }
2179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 @Override
2181 protected Dialog onCreateDialog(int id) {
2182 switch (id) {
2183 case DIALOG_CREATE_SHORTCUT:
2184 return new CreateShortcut().createDialog();
2185 case DIALOG_RENAME_FOLDER:
2186 return new RenameFolder().createDialog();
2187 }
2188
2189 return super.onCreateDialog(id);
2190 }
2191
2192 @Override
2193 protected void onPrepareDialog(int id, Dialog dialog) {
2194 switch (id) {
2195 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196 break;
2197 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002198 if (mFolderInfo != null) {
2199 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2200 final CharSequence text = mFolderInfo.title;
2201 input.setText(text);
2202 input.setSelection(0, text.length());
2203 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 break;
2205 }
2206 }
2207
2208 void showRenameDialog(FolderInfo info) {
2209 mFolderInfo = info;
2210 mWaitingForResult = true;
2211 showDialog(DIALOG_RENAME_FOLDER);
2212 }
2213
Adam Cohenfbba09b2011-07-18 21:43:05 -07002214 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 resetAddInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 mWaitingForResult = true;
2218 showDialog(DIALOG_CREATE_SHORTCUT);
2219 }
2220
Joe Onoratodeb98af2010-02-19 14:59:39 -08002221 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002222 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002223 Bundle bundle = new Bundle();
2224
2225 ArrayList<String> shortcutNames = new ArrayList<String>();
2226 shortcutNames.add(getString(R.string.group_applications));
2227 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2228
2229 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2230 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2231 R.drawable.ic_launcher_application));
2232 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2233
2234 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2235 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002236 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002237 pickIntent.putExtras(bundle);
2238
Joe Onoratodeb98af2010-02-19 14:59:39 -08002239 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002240 }
2241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 private class RenameFolder {
2243 private EditText mInput;
2244
2245 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2247 mInput = (EditText) layout.findViewById(R.id.folder_name);
2248
2249 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2250 builder.setIcon(0);
2251 builder.setTitle(getString(R.string.rename_folder_title));
2252 builder.setCancelable(true);
2253 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2254 public void onCancel(DialogInterface dialog) {
2255 cleanup();
2256 }
2257 });
2258 builder.setNegativeButton(getString(R.string.cancel_action),
2259 new Dialog.OnClickListener() {
2260 public void onClick(DialogInterface dialog, int which) {
2261 cleanup();
2262 }
2263 }
2264 );
2265 builder.setPositiveButton(getString(R.string.rename_action),
2266 new Dialog.OnClickListener() {
2267 public void onClick(DialogInterface dialog, int which) {
2268 changeFolderName();
2269 }
2270 }
2271 );
2272 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002273
2274 final AlertDialog dialog = builder.create();
2275 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2276 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002277 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002278 mInput.requestFocus();
2279 InputMethodManager inputManager = (InputMethodManager)
2280 getSystemService(Context.INPUT_METHOD_SERVICE);
2281 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002282 }
2283 });
2284
2285 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 }
2287
2288 private void changeFolderName() {
2289 final String name = mInput.getText().toString();
2290 if (!TextUtils.isEmpty(name)) {
2291 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002292 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 mFolderInfo.title = name;
2294 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2295
Joe Onorato9c1289c2009-08-17 11:03:03 -04002296 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002297 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002298 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 } else {
2300 final FolderIcon folderIcon = (FolderIcon)
2301 mWorkspace.getViewForTag(mFolderInfo);
2302 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002303 // TODO: At some point we'll probably want some version of setting
2304 // the text for a folder icon.
2305 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 getWorkspace().requestLayout();
2307 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002308 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002309 mWorkspaceLoading = true;
2310 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
2312 }
2313 }
2314 cleanup();
2315 }
2316
2317 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 dismissDialog(DIALOG_RENAME_FOLDER);
2319 mWaitingForResult = false;
2320 mFolderInfo = null;
2321 }
2322 }
2323
Daniel Sandler843e8602010-06-07 14:59:01 -04002324 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2325 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002326 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002327 }
2328
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002329 // AllAppsView.Watcher
2330 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002331 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002332 mWorkspace.setVisibility(View.GONE);
2333 }
2334 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002335
2336 /**
2337 * Helper method for the cameraZoomIn/cameraZoomOut animations
2338 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002339 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002340 * @param scaleFactor The scale factor used for the zoom
2341 */
2342 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2343 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002344
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002345 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002346 // Set pivotY so that at the starting zoom factor, the view is partially
2347 // visible. Modifying initialHeightFactor changes how much of the view is
2348 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002349 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002350 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002351 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002352 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002353 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002354 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355 }
2356 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002357
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002358 /**
2359 * Zoom the camera out from the workspace to reveal 'toView'.
2360 * Assumes that the view to show is anchored at either the very top or very bottom
2361 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002362 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002363 */
Winson Chungc07918d2011-07-01 15:35:26 -07002364 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002365 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002366
Winson Chungf0ea4d32011-06-06 14:27:16 -07002367 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2368 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2369 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2370 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002371
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002372 setPivotsForZoom(toView, toState, scale);
2373
Michael Jurkad74c9842011-07-10 12:44:21 -07002374 // Shrink workspaces away if going to AppsCustomize from workspace
2375 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002376
2377 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002378 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002379 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002380 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2381 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002382 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002383 toView.setFastScaleX(a * scale + b * 1f);
2384 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002385 }
2386 });
Adam Cohen61033d32010-11-15 18:29:44 -08002387
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 toView.setVisibility(View.VISIBLE);
2389 toView.setFastAlpha(0f);
2390 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2391 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2392 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2393 public void onAnimationUpdate(float a, float b) {
2394 // don't need to invalidate because we do so above
2395 toView.setFastAlpha(a * 0f + b * 1f);
2396 }
2397 });
2398 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002399
Michael Jurkaabded662011-03-04 12:06:57 -08002400 if (toView instanceof LauncherTransitionable) {
2401 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002402 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002403 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002404 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002405 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406 // Prepare the position
2407 toView.setTranslationX(0.0f);
2408 toView.setTranslationY(0.0f);
2409 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002410 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002411
Winson Chungc07918d2011-07-01 15:35:26 -07002412 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2413 // Hide the workspace scrollbar
2414 mWorkspace.hideScrollingIndicator(true);
2415 mWorkspace.hideScrollIndicatorTrack();
2416 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002417 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002418 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002419 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002420 // If we don't set the final scale values here, if this animation is cancelled
2421 // it will have the wrong scale value and subsequent cameraPan animations will
2422 // not fix that
2423 toView.setScaleX(1.0f);
2424 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002425 if (toView instanceof LauncherTransitionable) {
2426 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002427 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002428 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002429 });
2430
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002432 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002434 if (mStateAnimation != null) mStateAnimation.cancel();
2435 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002436 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002437 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002438 } else {
2439 toView.setTranslationX(0.0f);
2440 toView.setTranslationY(0.0f);
2441 toView.setScaleX(1.0f);
2442 toView.setScaleY(1.0f);
2443 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002444 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002445 if (toView instanceof LauncherTransitionable) {
2446 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2447 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002448
Winson Chungc07918d2011-07-01 15:35:26 -07002449 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2450 // Hide the workspace scrollbar
2451 mWorkspace.hideScrollingIndicator(true);
2452 mWorkspace.hideScrollIndicatorTrack();
2453 }
Michael Jurkaabded662011-03-04 12:06:57 -08002454 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002455 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456 }
2457
2458 /**
2459 * Zoom the camera back into the workspace, hiding 'fromView'.
2460 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002461 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002462 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002463 */
Winson Chungc07918d2011-07-01 15:35:26 -07002464 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002465 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002466
Winson Chungf0ea4d32011-06-06 14:27:16 -07002467 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2468 final float scaleFactor = (float)
2469 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2470 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002471
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002472 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002473
Michael Jurkad3ef3062010-11-23 16:23:58 -08002474 if (!springLoaded) {
2475 mWorkspace.unshrink(animated);
2476 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002478 if (mStateAnimation != null) mStateAnimation.cancel();
2479 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002480
Michael Jurka742574b2011-02-02 23:51:01 -08002481 final float oldScaleX = fromView.getScaleX();
2482 final float oldScaleY = fromView.getScaleY();
2483
2484 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2485 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002486 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2487 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002488 ((View)fromView.getParent()).fastInvalidate();
2489 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2490 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2491 }
2492 });
Michael Jurkaabded662011-03-04 12:06:57 -08002493 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002494 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002495 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002496 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2497 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002498 // don't need to invalidate because we do so above
2499 fromView.setFastAlpha(a * 1f + b * 0f);
2500 }
2501 });
Michael Jurkaabded662011-03-04 12:06:57 -08002502 if (fromView instanceof LauncherTransitionable) {
2503 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002504 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002505 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002506 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002507 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002508 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002509 if (fromView instanceof LauncherTransitionable) {
2510 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002511
Winson Chungc07918d2011-07-01 15:35:26 -07002512 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2513 // Show the workspace scrollbar
2514 mWorkspace.showScrollIndicatorTrack();
2515 mWorkspace.flashScrollingIndicator();
2516 }
Michael Jurka2763be32011-02-24 11:19:57 -08002517 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002518 }
2519 });
2520
Winson Chungf0ea4d32011-06-06 14:27:16 -07002521 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002522 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 } else {
2524 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002525 if (fromView instanceof LauncherTransitionable) {
2526 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2527 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002528
Winson Chungc07918d2011-07-01 15:35:26 -07002529 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2530 // Show the workspace scrollbar
2531 mWorkspace.showScrollIndicatorTrack();
2532 mWorkspace.flashScrollingIndicator();
2533 }
Michael Jurkaabded662011-03-04 12:06:57 -08002534 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002536 }
2537
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002538 void showWorkspace(boolean animated) {
2539 showWorkspace(animated, null);
2540 }
2541
2542 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002543 if (layout != null) {
2544 // always animated, but that's ok since we never specify a layout and
2545 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002546 mWorkspace.unshrink(layout);
2547 } else {
2548 mWorkspace.unshrink(animated);
2549 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002551 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002552 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002553
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002554 // Change the state *after* we've called all the transition code
2555 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002556
Winson Chung5fb63472011-02-02 17:03:37 -08002557 // Resume the auto-advance of widgets
2558 mUserPresent = true;
2559 updateRunning();
2560
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002561 // send an accessibility event to announce the context change
2562 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002563 }
2564
Michael Jurkad3ef3062010-11-23 16:23:58 -08002565 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002566 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002567 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002568 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002569 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002570 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002571 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002572 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002573 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2574 mWorkspace.postDelayed(new Runnable() {
2575 @Override
2576 public void run() {
2577 exitSpringLoadedDragMode();
2578 }
2579 }, (extendedDelay ?
2580 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2581 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2582 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002583 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002584 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002585 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002586 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2587 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002588 }
2589 // Otherwise, we are not in spring loaded mode, so don't do anything.
2590 }
2591
2592 /**
2593 * Shows the dock/hotseat area.
2594 */
2595 void showDock(boolean animated) {
2596 if (!LauncherApplication.isScreenLarge()) {
2597 if (animated) {
2598 int duration = mSearchDeleteBar.getTransitionInDuration();
2599 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002600 } else {
2601 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002602 }
2603 }
2604 }
2605
2606 /**
2607 * Hides the dock/hotseat area.
2608 */
2609 void hideDock(boolean animated) {
2610 if (!LauncherApplication.isScreenLarge()) {
2611 if (animated) {
2612 int duration = mSearchDeleteBar.getTransitionOutDuration();
2613 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002614 } else {
2615 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002616 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002617 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002618 }
2619
Winson Chung785d2eb2011-04-14 16:08:02 -07002620 void showAllApps(boolean animated) {
2621 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002622
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2624 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002625
Winson Chungf0ea4d32011-06-06 14:27:16 -07002626 // Hide the search bar and dock
2627 mSearchDeleteBar.hideSearchBar(animated);
2628 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002629
Winson Chungf0ea4d32011-06-06 14:27:16 -07002630 // Change the state *after* we've called all the transition code
2631 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002632
2633 // Pause the auto-advance of widgets until we are out of AllApps
2634 mUserPresent = false;
2635 updateRunning();
2636
2637 // Send an accessibility event to announce the context change
2638 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2639 }
2640
Joe Onoratob2061212009-11-24 16:13:54 -05002641 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002642 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002643 * - Home from workspace
2644 * - from center screen
2645 * - from other screens
2646 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002647 * - from center screen
2648 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002649 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002650 * - from center screen
2651 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002652 * - Launch app from workspace and quit
2653 * - with back
2654 * - with home
2655 * - Launch app from all apps and quit
2656 * - with back
2657 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002658 * - Go to a screen that's not the default, then all
2659 * apps, and launch and app, and go back
2660 * - with back
2661 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002662 * - On workspace, long press power and go back
2663 * - with back
2664 * - with home
2665 * - On all apps, long press power and go back
2666 * - with back
2667 * - with home
2668 * - On workspace, power off
2669 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002670 * - Launch an app and turn off the screen while in that app
2671 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002672 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002673 * - From all apps
2674 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002675 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2676 * - From all apps
2677 * - From the center workspace
2678 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002679 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002680 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002681 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2682 mWorkspace.setVisibility(View.VISIBLE);
2683 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002684
Winson Chungf0ea4d32011-06-06 14:27:16 -07002685 // Show the search bar and dock
2686 mSearchDeleteBar.showSearchBar(animated);
2687 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002688
Winson Chungf0ea4d32011-06-06 14:27:16 -07002689 // Set focus to the AppsCustomize button
2690 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002691 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002692 }
2693
Joe Onorato7c312c12009-08-13 21:36:53 -07002694 void lockAllApps() {
2695 // TODO
2696 }
2697
2698 void unlockAllApps() {
2699 // TODO
2700 }
2701
Patrick Dubroy5f445422011-02-18 14:35:21 -08002702 /**
2703 * Add an item from all apps or customize onto the given workspace screen.
2704 * If layout is null, add to the current screen.
2705 */
2706 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002707 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2708 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002709 } else {
2710 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002711 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002712 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002713
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002714 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002715 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002716 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 // Look for the toolbar icon specified in the activity meta-data
2718 Bundle metaData = packageManager.getActivityInfo(
2719 activityName, PackageManager.GET_META_DATA).metaData;
2720 if (metaData != null) {
2721 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2722 if (iconResId != 0) {
2723 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002724 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002725 }
2726 }
2727 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002728 // This can happen if the activity defines an invalid drawable
2729 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2730 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002731 } catch (Resources.NotFoundException nfe) {
2732 // This can happen if the activity defines an invalid drawable
2733 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2734 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002735 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002736 return null;
2737 }
2738
2739 // if successful in getting icon, return it; otherwise, set button to use default drawable
2740 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2741 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2742 TextView button = (TextView) findViewById(buttonId);
2743 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2744
2745 // If we were unable to find the icon via the meta-data, use a generic one
2746 if (toolbarIcon == null) {
2747 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2748 return null;
2749 } else {
2750 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2751 return toolbarIcon.getConstantState();
2752 }
2753 }
2754
2755 // if successful in getting icon, return it; otherwise, set button to use default drawable
2756 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2757 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2758 ImageView button = (ImageView) findViewById(buttonId);
2759 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2760
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 // If we were unable to find the icon via the meta-data, use a generic one
2762 if (toolbarIcon == null) {
2763 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002764 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002765 } else {
2766 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002767 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002768 }
2769 }
2770
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002771 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2772 TextView button = (TextView) findViewById(buttonId);
2773 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2774 }
2775
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002776 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002777 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002778 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002779 }
2780
Michael Jurka0423dcf2010-10-05 14:56:18 -07002781 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002782 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2783 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002784
Winson Chung1cad91e2011-05-25 17:41:01 -07002785 final SearchManager searchManager =
2786 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2787 ComponentName activityName = searchManager.getGlobalSearchActivity();
2788 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002789 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002790 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002791 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002792 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 } else {
2794 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002795 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002796 }
2797 }
2798
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002799 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002800 updateButtonWithDrawable(R.id.search_button, d);
2801 }
2802
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002803 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002804 final View searchDivider = findViewById(R.id.search_divider);
2805 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002806
Winson Chung1cad91e2011-05-25 17:41:01 -07002807 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2808 ComponentName activityName = intent.resolveActivity(getPackageManager());
2809 if (activityName != null) {
2810 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002811 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2812 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002813 voiceButton.setVisibility(View.VISIBLE);
2814 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002815 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002816 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002817 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002818 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002819
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002820 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002821 updateButtonWithDrawable(R.id.voice_button, d);
2822 }
2823
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002824 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002825 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002826 */
2827 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002828 final View marketButton = findViewById(R.id.market_button);
2829 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2830 // Find the app market activity by resolving an intent.
2831 // (If multiple app markets are installed, it will return the ResolverActivity.)
2832 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002833 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002834 mAppMarketIntent = intent;
2835 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002836 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002837 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002838
2839 // Remove the shop icon text in the Phone UI
2840 if (!LauncherApplication.isScreenLarge()) {
2841 ((TextView) marketButton).setText("");
2842 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002843 } else {
2844 // We should hide and disable the view so that we don't try and restore the visibility
2845 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2846 marketButton.setVisibility(View.GONE);
2847 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002848 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002849 }
2850
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002851 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002852 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002853 }
2854
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002855 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 * Displays the shortcut creation dialog and launches, if necessary, the
2857 * appropriate activity.
2858 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002859 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002860 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2861 DialogInterface.OnShowListener {
2862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002866 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002867
Winson Chung55b65502011-05-26 12:03:43 -07002868 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2869 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002870 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002871 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 AlertDialog dialog = builder.create();
2874 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002875 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002876 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 return dialog;
2879 }
2880
2881 public void onCancel(DialogInterface dialog) {
2882 mWaitingForResult = false;
2883 cleanup();
2884 }
2885
Romain Guycbb89e42009-06-08 15:52:54 -07002886 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002887 mWaitingForResult = false;
2888 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002889 }
2890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002892 try {
2893 dismissDialog(DIALOG_CREATE_SHORTCUT);
2894 } catch (Exception e) {
2895 // An exception is thrown if the dialog is not visible, which is fine
2896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 }
2898
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002899 /**
2900 * Handle the action clicked in the "Add to home" dialog.
2901 */
2902 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002903 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002904
Winson Chung55b65502011-05-26 12:03:43 -07002905 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2906 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002907 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002908 if (mAppsCustomizeTabHost != null) {
2909 mAppsCustomizeTabHost.selectWidgetsTab();
2910 }
2911 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002912 break;
2913 }
Winson Chung55b65502011-05-26 12:03:43 -07002914 case AddAdapter.ITEM_APPLICATION: {
2915 if (mAppsCustomizeTabHost != null) {
2916 mAppsCustomizeTabHost.selectAppsTab();
2917 }
2918 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002919 break;
2920 }
Winson Chung55b65502011-05-26 12:03:43 -07002921 case AddAdapter.ITEM_APPWIDGET: {
2922 if (mAppsCustomizeTabHost != null) {
2923 mAppsCustomizeTabHost.selectWidgetsTab();
2924 }
2925 showAllApps(true);
2926 break;
2927 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002928 case AddAdapter.ITEM_WALLPAPER: {
2929 startWallpaper();
2930 break;
2931 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 }
2933 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002934
2935 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002936 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 }
2939
2940 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002941 * Receives notifications when applications are added/removed.
2942 */
2943 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2944 @Override
2945 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002946 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002947 String reason = intent.getStringExtra("reason");
2948 if (!"homekey".equals(reason)) {
2949 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002950 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002951 animate = false;
2952 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002953 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002954 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002955 }
2956 }
2957
2958 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002959 * Receives notifications whenever the appwidgets are reset.
2960 */
2961 private class AppWidgetResetObserver extends ContentObserver {
2962 public AppWidgetResetObserver() {
2963 super(new Handler());
2964 }
2965
2966 @Override
2967 public void onChange(boolean selfChange) {
2968 onAppWidgetReset();
2969 }
2970 }
2971
2972 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002973 * If the activity is currently paused, signal that we need to re-run the loader
2974 * in onResume.
2975 *
2976 * This needs to be called from incoming places where resources might have been loaded
2977 * while we are paused. That is becaues the Configuration might be wrong
2978 * when we're not running, and if it comes back to what it was when we
2979 * were paused, we are not restarted.
2980 *
2981 * Implementation of the method from LauncherModel.Callbacks.
2982 *
2983 * @return true if we are currently paused. The caller might be able to
2984 * skip some work in that case since we will come back again.
2985 */
2986 public boolean setLoadOnResume() {
2987 if (mPaused) {
2988 Log.i(TAG, "setLoadOnResume");
2989 mOnResumeNeedsLoad = true;
2990 return true;
2991 } else {
2992 return false;
2993 }
2994 }
2995
2996 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002998 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003000 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003001 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003002 } else {
3003 return SCREEN_COUNT / 2;
3004 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003006
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003007
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 /**
3009 * Refreshes the shortcuts shown on the workspace.
3010 *
3011 * Implementation of the method from LauncherModel.Callbacks.
3012 */
3013 public void startBinding() {
3014 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003015
3016 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003017 int count = workspace.getChildCount();
3018 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003019 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003020 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3021 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003023
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 if (DEBUG_USER_INTERFACE) {
3025 android.widget.Button finishButton = new android.widget.Button(this);
3026 finishButton.setText("Finish");
3027 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3028
3029 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3030 public void onClick(View v) {
3031 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003032 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003033 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003035
Adam Cohen4eac29a2011-07-11 17:53:37 -07003036 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3037 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 }
3039
3040 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 * Bind the items start-end from the list.
3042 *
3043 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3046
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003047 setLoadOnResume();
3048
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 final Workspace workspace = mWorkspace;
3050
3051 for (int i=start; i<end; i++) {
3052 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 switch (item.itemType) {
3054 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3055 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003056 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3058 false);
3059 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003060 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003062 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003063 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3065 false);
3066 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068 }
3069
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003071 }
3072
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 /**
3074 * Implementation of the method from LauncherModel.Callbacks.
3075 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003076 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003077 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003078 sFolders.clear();
3079 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081
3082 /**
3083 * Add the views for a widget to the workspace.
3084 *
3085 * Implementation of the method from LauncherModel.Callbacks.
3086 */
3087 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003088 setLoadOnResume();
3089
Daniel Sandler843e8602010-06-07 14:59:01 -04003090 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3091 if (DEBUG_WIDGETS) {
3092 Log.d(TAG, "bindAppWidget: " + item);
3093 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 final Workspace workspace = mWorkspace;
3095
3096 final int appWidgetId = item.appWidgetId;
3097 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003098 if (DEBUG_WIDGETS) {
3099 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3100 }
3101
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3103
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3105 item.hostView.setTag(item);
3106
3107 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3108 item.cellY, item.spanX, item.spanY, false);
3109
Adam Cohended9f8d2010-11-03 13:25:16 -07003110 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3111
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 workspace.requestLayout();
3113
Daniel Sandler843e8602010-06-07 14:59:01 -04003114 if (DEBUG_WIDGETS) {
3115 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3116 + (SystemClock.uptimeMillis()-start) + "ms");
3117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 }
3119
3120 /**
3121 * Callback saying that there aren't any more items to bind.
3122 *
3123 * Implementation of the method from LauncherModel.Callbacks.
3124 */
3125 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003126 setLoadOnResume();
3127
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 if (mSavedState != null) {
3129 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003130 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 mSavedState = null;
3133 }
3134
3135 if (mSavedInstanceState != null) {
3136 super.onRestoreInstanceState(mSavedInstanceState);
3137 mSavedInstanceState = null;
3138 }
3139
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003141
3142 // If we received the result of any pending adds while the loader was running (e.g. the
3143 // widget configuration forced an orientation change), process them now.
3144 for (int i = 0; i < sPendingAddList.size(); i++) {
3145 completeAdd(sPendingAddList.get(i));
3146 }
3147 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 }
3149
3150 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 * Updates the icons on the launcher that are affected by changes to the package list
3152 * on the device.
3153 */
3154 private void updateIconsAffectedByPackageManagerChanges() {
3155 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003156 updateVoiceSearchIcon();
3157 }
3158
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003159 @Override
3160 public void bindSearchablesChanged() {
3161 updateGlobalSearchIcon();
3162 }
3163
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003164 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 * Add the icons for all apps.
3166 *
3167 * Implementation of the method from LauncherModel.Callbacks.
3168 */
3169 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003170 if (mAppsCustomizeContent != null) {
3171 mAppsCustomizeContent.setApps(apps);
3172 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003173 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003174 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175 }
3176
3177 /**
3178 * A package was installed.
3179 *
3180 * Implementation of the method from LauncherModel.Callbacks.
3181 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003182 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003183 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003185
Winson Chung785d2eb2011-04-14 16:08:02 -07003186 if (mAppsCustomizeContent != null) {
3187 mAppsCustomizeContent.addApps(apps);
3188 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003189 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 }
3191
3192 /**
3193 * A package was updated.
3194 *
3195 * Implementation of the method from LauncherModel.Callbacks.
3196 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003197 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003198 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003199 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003200 if (mWorkspace != null) {
3201 mWorkspace.updateShortcuts(apps);
3202 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003203
Winson Chung785d2eb2011-04-14 16:08:02 -07003204 if (mAppsCustomizeContent != null) {
3205 mAppsCustomizeContent.updateApps(apps);
3206 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003207 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 }
3209
3210 /**
3211 * A package was uninstalled.
3212 *
3213 * Implementation of the method from LauncherModel.Callbacks.
3214 */
Joe Onorato36115782010-06-17 13:28:48 -04003215 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003217 if (permanent) {
3218 mWorkspace.removeItems(apps);
3219 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003220
Winson Chung785d2eb2011-04-14 16:08:02 -07003221 if (mAppsCustomizeContent != null) {
3222 mAppsCustomizeContent.removeApps(apps);
3223 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003224 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003225 }
Joe Onoratobe386092009-11-17 17:32:16 -08003226
3227 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003228 * A number of packages were updated.
3229 */
3230 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003231
Winson Chung785d2eb2011-04-14 16:08:02 -07003232 if (mAppsCustomizeContent != null) {
3233 mAppsCustomizeContent.onPackagesUpdated();
3234 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003235 }
3236
Winson Chung400438b2011-01-16 17:53:48 -08003237 private int mapConfigurationOriActivityInfoOri(int configOri) {
3238 final Display d = getWindowManager().getDefaultDisplay();
3239 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3240 switch (d.getRotation()) {
3241 case Surface.ROTATION_0:
3242 case Surface.ROTATION_180:
3243 // We are currently in the same basic orientation as the natural orientation
3244 naturalOri = configOri;
3245 break;
3246 case Surface.ROTATION_90:
3247 case Surface.ROTATION_270:
3248 // We are currently in the other basic orientation to the natural orientation
3249 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3250 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3251 break;
3252 }
3253
3254 int[] oriMap = {
3255 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3256 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3257 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3258 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3259 };
3260 // Since the map starts at portrait, we need to offset if this device's natural orientation
3261 // is landscape.
3262 int indexOffset = 0;
3263 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3264 indexOffset = 1;
3265 }
3266 return oriMap[(d.getRotation() + indexOffset) % 4];
3267 }
3268 public void lockScreenOrientation() {
3269 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3270 .getConfiguration().orientation));
3271 }
3272 public void unlockScreenOrientation() {
3273 mHandler.postDelayed(new Runnable() {
3274 public void run() {
3275 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3276 }
3277 }, mRestoreScreenOrientationDelay);
3278 }
3279
Winson Chung80baf5a2010-08-09 16:03:15 -07003280 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003281 * Prints out out state for debugging.
3282 */
3283 public void dumpState() {
3284 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003285 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003286 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3287 Log.d(TAG, "mRestoring=" + mRestoring);
3288 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3289 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003290 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003291 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003292
Winson Chung785d2eb2011-04-14 16:08:02 -07003293 if (mAppsCustomizeContent != null) {
3294 mAppsCustomizeContent.dumpState();
3295 }
Joe Onoratobe386092009-11-17 17:32:16 -08003296 Log.d(TAG, "END launcher2 dump state");
3297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003298}
Michael Jurka2763be32011-02-24 11:19:57 -08003299
Michael Jurkaabded662011-03-04 12:06:57 -08003300interface LauncherTransitionable {
3301 void onLauncherTransitionStart(Animator animation);
3302 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003303}