blob: 489f3fbb806e4bbc8d45d7637497cabe3f2c6d05 [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
154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
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;
189 private int mAddIntersectCellX = -1;
190 private int mAddIntersectCellY = -1;
191 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();
320 }
321 if (sGlobalSearchIcon != null) {
322 updateGlobalSearchIcon(sGlobalSearchIcon);
323 }
324 if (sVoiceSearchIcon != null) {
325 updateVoiceSearchIcon(sVoiceSearchIcon);
326 }
327 if (sAppMarketIcon != null) {
328 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
Romain Guycbb89e42009-06-08 15:52:54 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700333 if (sLocaleConfiguration == null) {
334 new AsyncTask<Void, Void, LocaleConfiguration>() {
335 @Override
336 protected LocaleConfiguration doInBackground(Void... unused) {
337 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
338 readConfiguration(Launcher.this, localeConfiguration);
339 return localeConfiguration;
340 }
341
342 @Override
343 protected void onPostExecute(LocaleConfiguration result) {
344 sLocaleConfiguration = result;
345 checkForLocaleChange(); // recursive, but now with a locale configuration
346 }
347 }.execute();
348 return;
349 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final Configuration configuration = getResources().getConfiguration();
352
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 final String locale = configuration.locale.toString();
355
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700356 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 final int mcc = configuration.mcc;
358
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 final int mnc = configuration.mnc;
361
Romain Guy84f296c2009-11-04 15:00:44 -0800362 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700365 sLocaleConfiguration.locale = locale;
366 sLocaleConfiguration.mcc = mcc;
367 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700368
Joe Onorato0589f0f2010-02-08 13:44:00 -0800369 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400370 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371
372 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
373 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700374 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700375 public void run() {
376 writeConfiguration(Launcher.this, localeConfiguration);
377 }
378 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700379 }
380 }
381
382 private static class LocaleConfiguration {
383 public String locale;
384 public int mcc = -1;
385 public int mnc = -1;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
Romain Guy98d01652009-06-30 16:21:04 -0700388 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
389 DataInputStream in = null;
390 try {
391 in = new DataInputStream(context.openFileInput(PREFERENCES));
392 configuration.locale = in.readUTF();
393 configuration.mcc = in.readInt();
394 configuration.mnc = in.readInt();
395 } catch (FileNotFoundException e) {
396 // Ignore
397 } catch (IOException e) {
398 // Ignore
399 } finally {
400 if (in != null) {
401 try {
402 in.close();
403 } catch (IOException e) {
404 // Ignore
405 }
406 }
407 }
408 }
409
410 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
411 DataOutputStream out = null;
412 try {
413 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
414 out.writeUTF(configuration.locale);
415 out.writeInt(configuration.mcc);
416 out.writeInt(configuration.mnc);
417 out.flush();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 //noinspection ResultOfMethodCallIgnored
422 context.getFileStreamPath(PREFERENCES).delete();
423 } finally {
424 if (out != null) {
425 try {
426 out.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432 }
433
Adam Cohen716b51e2011-06-30 12:09:54 -0700434 public DragLayer getDragLayer() {
435 return mDragLayer;
436 }
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 static int getScreen() {
439 synchronized (sLock) {
440 return sScreen;
441 }
442 }
443
444 static void setScreen(int screen) {
445 synchronized (sLock) {
446 sScreen = screen;
447 }
448 }
449
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400450 // Note: This doesn't do all the client-id magic that BrowserProvider does
451 // in Browser. (http://b/2425179)
452 private Uri getDefaultBrowserUri() {
453 String url = getString(R.string.default_browser_url);
454 if (url.indexOf("{CID}") != -1) {
455 url = url.replace("{CID}", "android-google");
456 }
457 return Uri.parse(url);
458 }
459
460 // Load the Intent templates from arrays.xml to populate the hotseats. For
461 // each Intent, if it resolves to a single app, use that as the launch
462 // intent & use that app's label as the contentDescription. Otherwise,
463 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400464 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 if (mHotseatConfig == null) {
466 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
467 if (mHotseatConfig.length > 0) {
468 mHotseats = new Intent[mHotseatConfig.length];
469 mHotseatLabels = new CharSequence[mHotseatConfig.length];
470 mHotseatIcons = new Drawable[mHotseatConfig.length];
471 } else {
472 mHotseats = null;
473 mHotseatIcons = null;
474 mHotseatLabels = null;
475 }
476
477 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
478 for (int i=0; i<mHotseatConfig.length; i++) {
479 // load icon for this slot; currently unrelated to the actual activity
480 try {
481 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
482 } catch (ArrayIndexOutOfBoundsException ex) {
483 Log.w(TAG, "Missing hotseat_icons array item #" + i);
484 mHotseatIcons[i] = null;
485 }
486 }
487 hotseatIconDrawables.recycle();
488 }
489
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400490 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 for (int i=0; i<mHotseatConfig.length; i++) {
492 Intent intent = null;
493 if (mHotseatConfig[i].equals("*BROWSER*")) {
494 // magic value meaning "launch user's default web browser"
495 // replace it with a generic web request so we can see if there is indeed a default
496 String defaultUri = getString(R.string.default_browser_url);
497 intent = new Intent(
498 Intent.ACTION_VIEW,
499 ((defaultUri != null)
500 ? Uri.parse(defaultUri)
501 : getDefaultBrowserUri())
502 ).addCategory(Intent.CATEGORY_BROWSABLE);
503 // note: if the user launches this without a default set, she
504 // will always be taken to the default URL above; this is
505 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700506 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400507 // URL is unavoidably sent to the chosen app.
508 } else {
509 try {
510 intent = Intent.parseUri(mHotseatConfig[i], 0);
511 } catch (java.net.URISyntaxException ex) {
512 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
513 // bogus; leave intent=null
514 }
515 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700516
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (intent == null) {
518 mHotseats[i] = null;
519 mHotseatLabels[i] = getText(R.string.activity_not_found);
520 continue;
521 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522
523 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700524 Log.d(TAG, "loadHotseats: hotseat " + i
525 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400526 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400527 + "]");
528 }
529
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400530 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
531 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700532 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 Log.d(TAG, "Best match for intent: " + bestMatch);
534 Log.d(TAG, "All matches: ");
535 for (ResolveInfo ri : allMatches) {
536 Log.d(TAG, " --> " + ri);
537 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400538 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400539 // did this resolve to a single app, or the resolver?
540 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 // can't find any activity to handle this. let's leave the
542 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 // to launch.
544 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 // set accessibility text to "Not installed"
547 mHotseatLabels[i] = getText(R.string.activity_not_found);
548 } else {
549 boolean found = false;
550 for (ResolveInfo ri : allMatches) {
551 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
552 && bestMatch.activityInfo.applicationInfo.packageName
553 .equals(ri.activityInfo.applicationInfo.packageName)) {
554 found = true;
555 break;
556 }
557 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 if (!found) {
560 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
561 // the bestMatch is probably the ResolveActivity, meaning the
562 // user has not yet selected a default
563 // so: we'll keep the original intent for now
564 mHotseats[i] = intent;
565
566 // set the accessibility text to "Select shortcut"
567 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
568 } else {
569 // we have an app!
570 // now reconstruct the intent to launch it through the front
571 // door
572 ComponentName com = new ComponentName(
573 bestMatch.activityInfo.applicationInfo.packageName,
574 bestMatch.activityInfo.name);
575 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
576
577 // load the app label for accessibility
578 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 }
581
582 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 Log.d(TAG, "loadHotseats: hotseat " + i
584 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 + ((mHotseats[i] == null)
586 ? "null"
587 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588 + "] label=[" + mHotseatLabels[i]
589 + "]"
590 );
591 }
592 }
593 }
594
Winson Chung557d6ed2011-07-08 15:34:52 -0700595 /**
596 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
597 * a configuration step, this allows the proper animations to run after other transitions.
598 */
599 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800600 switch (args.requestCode) {
601 case REQUEST_PICK_APPLICATION:
602 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
603 break;
604 case REQUEST_PICK_SHORTCUT:
605 processShortcut(args.intent);
606 break;
607 case REQUEST_CREATE_SHORTCUT:
608 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700609 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_APPWIDGET:
611 addAppWidgetFromPick(args.intent);
612 break;
613 case REQUEST_CREATE_APPWIDGET:
614 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
615 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700616 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_PICK_WALLPAPER:
618 // We just wanted the activity result here so we can clear mWaitingForResult
619 break;
620 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700621 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700626 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700627 mWaitingForResult = false;
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 // The pattern used here is that a user PICKs a specific application,
630 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
633 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700634
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;
640 args.cellX = mAddIntersectCellX;
641 args.cellY = mAddIntersectCellY;
642
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;
804 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
805 mAddIntersectCellY = 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 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800979 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700980 int intersectCellX, int intersectCellY) {
981 final int[] cellXY = mTmpAddItemCellCoordinates;
982 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
983
984 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
985 showOutOfSpaceMessage();
986 return;
987 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800989 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800990
Romain Guy73b979d2009-06-09 12:57:21 -0700991 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700995 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
996 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 } else {
998 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000 }
Romain Guycbb89e42009-06-08 15:52:54 -07001001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 /**
1003 * Add a shortcut to the workspace.
1004 *
1005 * @param data The intent describing the shortcut.
1006 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008 private void completeAddShortcut(Intent data, int screen,
1009 int intersectCellX, int intersectCellY) {
1010 final int[] cellXY = mTmpAddItemCellCoordinates;
1011 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001012
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001013 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001014 boolean foundCellSpan = false;
1015 if (touchXY != null) {
1016 // when dragging and dropping, just find the closest free spot
1017 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1018 int[] result = screenLayout.findNearestVacantArea(
1019 touchXY[0], touchXY[1], 1, 1, cellXY);
1020 foundCellSpan = (result != null);
1021 } else {
1022 foundCellSpan = layout.findCellForSpanThatIntersects(
1023 cellXY, 1, 1, intersectCellX, intersectCellY);
1024 }
1025
1026 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001027 showOutOfSpaceMessage();
1028 return;
1029 }
1030
1031 final ShortcutInfo info = mModel.addShortcut(
1032 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033
1034 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001036 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038 }
1039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001041 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001043 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001044 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 */
Winson Chungf7640c82011-02-28 13:47:29 -08001046 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Adam Cohen09353862011-07-14 16:10:03 -07001051
1052 // We want to account for the extra amount of padding that we are adding to the widget
1053 // to ensure that it gets the full amount of space that it has requested
1054 Resources r = getResources();
1055 int requiredWidth = appWidgetInfo.minWidth +
1056 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1057 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1058 int requiredHeight = appWidgetInfo.minHeight +
1059 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1060 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1061 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001064 // We have saved the position to which the widget was dragged-- this really only matters
1065 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001066 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001067
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001068 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001069 boolean foundCellSpan = false;
1070 if (touchXY != null) {
1071 // when dragging and dropping, just find the closest free spot
1072 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1073 int[] result = screenLayout.findNearestVacantArea(
1074 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001075 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001077 // if we long pressed on an empty cell to bring up a menu,
1078 // make sure we intersect the empty cell
1079 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1080 // findCellForSpanThatIntersects will just ignore them
1081 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1082 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001083 }
1084
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001086 if (appWidgetId != -1) {
1087 // Deleting an app widget ID is a void call but writes to disk before returning
1088 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001089 new Thread("deleteAppWidgetId") {
1090 public void run() {
1091 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1092 }
1093 }.start();
1094 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001096 return;
1097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 // Build Launcher-specific widget info and save to database
1100 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 launcherInfo.spanX = spanXY[0];
1102 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 LauncherModel.addItemToDatabase(this, launcherInfo,
1105 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001106 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107
1108 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001111
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001114
Michael Jurka0280c3b2010-09-17 15:00:07 -07001115 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001117
1118 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 }
1120 }
Romain Guycbb89e42009-06-08 15:52:54 -07001121
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1123 @Override
1124 public void onReceive(Context context, Intent intent) {
1125 final String action = intent.getAction();
1126 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1127 mUserPresent = false;
1128 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001129
1130 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001131 if (mAppsCustomizeContent != null) {
1132 mAppsCustomizeContent.reset();
1133 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001134 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1135 mUserPresent = true;
1136 updateRunning();
1137 }
1138 }
1139 };
1140
1141 @Override
1142 public void onAttachedToWindow() {
1143 super.onAttachedToWindow();
1144
1145 // Listen for broadcasts related to user-presence
1146 final IntentFilter filter = new IntentFilter();
1147 filter.addAction(Intent.ACTION_SCREEN_OFF);
1148 filter.addAction(Intent.ACTION_USER_PRESENT);
1149 registerReceiver(mReceiver, filter);
1150
Adam Cohend113e0c2010-11-11 10:48:05 -08001151 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001152 mVisible = true;
1153 }
1154
1155 @Override
1156 public void onDetachedFromWindow() {
1157 super.onDetachedFromWindow();
1158 mVisible = false;
1159
Adam Cohend113e0c2010-11-11 10:48:05 -08001160 if (mAttached) {
1161 unregisterReceiver(mReceiver);
1162 mAttached = false;
1163 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001164 updateRunning();
1165 }
1166
1167 public void onWindowVisibilityChanged(int visibility) {
1168 mVisible = visibility == View.VISIBLE;
1169 updateRunning();
1170 }
1171
1172 private void sendAdvanceMessage(long delay) {
1173 mHandler.removeMessages(ADVANCE_MSG);
1174 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1175 mHandler.sendMessageDelayed(msg, delay);
1176 mAutoAdvanceSentTime = System.currentTimeMillis();
1177 }
1178
1179 private void updateRunning() {
1180 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1181 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1182 mAutoAdvanceRunning = autoAdvanceRunning;
1183 if (autoAdvanceRunning) {
1184 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1185 sendAdvanceMessage(delay);
1186 } else {
1187 if (!mWidgetsToAdvance.isEmpty()) {
1188 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1189 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1190 }
1191 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001192 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 }
1194 }
1195 }
1196
1197 private final Handler mHandler = new Handler() {
1198 @Override
1199 public void handleMessage(Message msg) {
1200 if (msg.what == ADVANCE_MSG) {
1201 int i = 0;
1202 for (View key: mWidgetsToAdvance.keySet()) {
1203 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1204 final int delay = mAdvanceStagger * i;
1205 if (v instanceof Advanceable) {
1206 postDelayed(new Runnable() {
1207 public void run() {
1208 ((Advanceable) v).advance();
1209 }
1210 }, delay);
1211 }
1212 i++;
1213 }
1214 sendAdvanceMessage(mAdvanceInterval);
1215 }
1216 }
1217 };
1218
1219 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1220 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1221 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1222 if (v instanceof Advanceable) {
1223 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001224 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001225 updateRunning();
1226 }
1227 }
1228
1229 void removeWidgetToAutoAdvance(View hostView) {
1230 if (mWidgetsToAdvance.containsKey(hostView)) {
1231 mWidgetsToAdvance.remove(hostView);
1232 updateRunning();
1233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 }
1235
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 launcherInfo.hostView = null;
1239 }
1240
Adam Cohended9f8d2010-11-03 13:25:16 -07001241 void showOutOfSpaceMessage() {
1242 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1243 }
1244
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 public LauncherAppWidgetHost getAppWidgetHost() {
1246 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Winson Chunga9abd0e2010-10-27 17:18:37 -07001249 public LauncherModel getModel() {
1250 return mModel;
1251 }
1252
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001253 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001254 getWindow().closeAllPanels();
1255
1256 try {
1257 dismissDialog(DIALOG_CREATE_SHORTCUT);
1258 // Unlock the workspace if the dialog was showing
1259 } catch (Exception e) {
1260 // An exception is thrown if the dialog is not visible, which is fine
1261 }
1262
1263 try {
1264 dismissDialog(DIALOG_RENAME_FOLDER);
1265 // Unlock the workspace if the dialog was showing
1266 } catch (Exception e) {
1267 // An exception is thrown if the dialog is not visible, which is fine
1268 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001269
1270 // Whatever we were doing is hereby canceled.
1271 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001272 }
1273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 @Override
1275 protected void onNewIntent(Intent intent) {
1276 super.onNewIntent(intent);
1277
1278 // Close the menu
1279 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001280 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001281 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001282
Adam Cohen95bb8002011-07-03 23:40:28 -07001283 closeFolder();
1284
Joe Onorato14f122b2009-11-19 14:06:36 -08001285 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1286 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001287
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001288 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001289 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001290 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001291 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001292 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001293 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001294 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001295
Joe Onorato3a8820b2009-11-10 15:06:42 -08001296 final View v = getWindow().peekDecorView();
1297 if (v != null && v.getWindowToken() != null) {
1298 InputMethodManager imm = (InputMethodManager)getSystemService(
1299 INPUT_METHOD_SERVICE);
1300 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001302
Michael Jurka35aa14d2011-07-07 17:01:08 -07001303 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 if (mAppsCustomizeContent != null) {
1305 mAppsCustomizeContent.reset();
1306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308 }
1309
1310 @Override
1311 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1312 // Do not call super here
1313 mSavedInstanceState = savedInstanceState;
1314 }
1315
1316 @Override
1317 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001318 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001319 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001320
Michael Jurka883f55b2010-10-21 15:47:14 -07001321 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001322 // We close any open folder since it will not be re-opened, and we need to make sure
1323 // this state is reflected.
1324 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Michael Jurka0280c3b2010-09-17 15:00:07 -07001326 if (mAddScreen > -1 && mWaitingForResult) {
1327 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1328 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1329 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 if (mFolderInfo != null && mWaitingForResult) {
1333 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1334 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1335 }
Michael Jurka946ad472010-07-09 18:05:18 -07001336
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 // Save the current AppsCustomize tab
1338 if (mAppsCustomizeTabHost != null) {
1339 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1340 if (currentTabTag != null) {
1341 outState.putString("apps_customize_currentTab", currentTabTag);
1342 }
1343 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
1346 @Override
1347 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001349
Winson Chungcd2b0142011-06-08 16:02:26 -07001350 // Stop callbacks from LauncherModel
1351 LauncherApplication app = ((LauncherApplication) getApplication());
1352 mModel.stopLoader();
1353 app.setLauncher(null);
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001356 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001358 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001360 mAppWidgetHost = null;
1361
1362 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
1364 TextKeyListener.getInstance().release();
1365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
Adam Cohen4eac29a2011-07-11 17:53:37 -07001367 unbindWorkspaceItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001368
1369 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001370 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001371
1372 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1373 mWorkspace.removeAllViews();
1374 mWorkspace = null;
1375 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001376
1377 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379
Adam Cohena9cf38f2011-05-02 15:36:58 -07001380 public DragController getDragController() {
1381 return mDragController;
1382 }
1383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 @Override
1385 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001386 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 super.startActivityForResult(intent, requestCode);
1388 }
1389
1390 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001391 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001393
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001394 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001395
Karl Rosaen138a0412009-04-23 19:00:21 -07001396 if (initialQuery == null) {
1397 // Use any text typed in the launcher as the initial query
1398 initialQuery = getTypedText();
1399 clearTypedText();
1400 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401 if (appSearchData == null) {
1402 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001403 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
Romain Guycbb89e42009-06-08 15:52:54 -07001405
Karl Rosaen138a0412009-04-23 19:00:21 -07001406 final SearchManager searchManager =
1407 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001408 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001409 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411
1412 @Override
1413 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001414 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001415 return false;
1416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417
1418 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001419
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1421 .setIcon(android.R.drawable.ic_menu_add)
1422 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001423 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1424 .setIcon(android.R.drawable.ic_menu_manage)
1425 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001426 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427 .setIcon(android.R.drawable.ic_menu_gallery)
1428 .setAlphabeticShortcut('W');
1429 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1430 .setIcon(android.R.drawable.ic_search_category_default)
1431 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1432 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1433 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1434 .setAlphabeticShortcut('N');
1435
1436 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001437 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1438 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
1440 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1441 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1442 .setIntent(settings);
1443
1444 return true;
1445 }
1446
1447 @Override
1448 public boolean onPrepareOptionsMenu(Menu menu) {
1449 super.onPrepareOptionsMenu(menu);
1450
Winson Chung785d2eb2011-04-14 16:08:02 -07001451 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1452 // related code?
1453 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454
1455 return true;
1456 }
1457
1458 @Override
1459 public boolean onOptionsItemSelected(MenuItem item) {
1460 switch (item.getItemId()) {
1461 case MENU_ADD:
1462 addItems();
1463 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001464 case MENU_MANAGE_APPS:
1465 manageApps();
1466 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 case MENU_WALLPAPER_SETTINGS:
1468 startWallpaper();
1469 return true;
1470 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001471 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 return true;
1473 case MENU_NOTIFICATIONS:
1474 showNotifications();
1475 return true;
1476 }
1477
1478 return super.onOptionsItemSelected(item);
1479 }
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Karl Rosaen138a0412009-04-23 19:00:21 -07001481 /**
1482 * Indicates that we want global search for this activity by setting the globalSearch
1483 * argument for {@link #startSearch} to true.
1484 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001486 @Override
1487 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001488 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001489 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001490 }
1491
Joe Onorato9c1289c2009-08-17 11:03:03 -04001492 public boolean isWorkspaceLocked() {
1493 return mWorkspaceLoading || mWaitingForResult;
1494 }
1495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001497 showWorkspace(true);
1498 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
1500
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 private void resetAddInfo() {
1502 mAddScreen = -1;
1503 mAddIntersectCellX = -1;
1504 mAddIntersectCellY = -1;
1505 mAddDropPosition = null;
1506 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001507
Winson Chung55cef262010-10-28 14:14:18 -07001508 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 resetAddInfo();
1510 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001511 mAddDropPosition = position;
1512
Michael Jurkaaf442092010-06-10 17:01:57 -07001513 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001514 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1515 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001516 }
1517
Winson Chung7ad01412010-09-27 11:33:03 -07001518 private void manageApps() {
1519 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1520 }
1521
Michael Jurkaaf442092010-06-10 17:01:57 -07001522 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 // TODO: catch bad widget exception when sent
1524 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001525 // TODO: Is this log message meaningful?
1526 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001527 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001528 }
1529
Winson Chung55cef262010-10-28 14:14:18 -07001530 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001531 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532
Bjorn Bringert7984c942009-12-09 15:38:25 +00001533 if (appWidget.configure != null) {
1534 // Launch over to configure widget, if needed
1535 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1536 intent.setComponent(appWidget.configure);
1537 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001538 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001539 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1540 intent.putExtra(
1541 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1542 info.mimeType);
1543
1544 final String mimeType = info.mimeType;
1545 final ClipData clipData = (ClipData) info.configurationData;
1546 final ClipDescription clipDesc = clipData.getDescription();
1547 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1548 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001549 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001550 final CharSequence stringData = item.getText();
1551 final Uri uriData = item.getUri();
1552 final Intent intentData = item.getIntent();
1553 final String key =
1554 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1555 if (uriData != null) {
1556 intent.putExtra(key, uriData);
1557 } else if (intentData != null) {
1558 intent.putExtra(key, intentData);
1559 } else if (stringData != null) {
1560 intent.putExtra(key, stringData);
1561 }
1562 break;
1563 }
1564 }
1565 }
Winson Chung55cef262010-10-28 14:14:18 -07001566 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001567
Romain Guy8e633c52010-03-04 12:51:36 -08001568 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001570 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001571 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001572
1573 // Exit spring loaded mode if necessary after adding the widget
1574 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 }
1576 }
Romain Guycbb89e42009-06-08 15:52:54 -07001577
Michael Jurka0280c3b2010-09-17 15:00:07 -07001578 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1579 resetAddInfo();
1580 mAddScreen = screen;
1581 mAddDropPosition = position;
1582
1583 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1584 createShortcutIntent.setComponent(componentName);
1585 processShortcut(createShortcutIntent);
1586 }
1587
Joe Onoratodeb98af2010-02-19 14:59:39 -08001588 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001589 // Handle case where user selected "Applications"
1590 String applicationName = getResources().getString(R.string.group_applications);
1591 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001593 if (applicationName != null && applicationName.equals(shortcutName)) {
1594 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1595 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001596
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001597 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1598 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001599 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001600 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001601 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001602 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 }
1605
Winson Chung24ab2f12010-09-16 14:10:47 -07001606 void processWallpaper(Intent intent) {
1607 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1608 }
1609
Adam Cohend0445262011-07-04 23:53:22 -07001610 FolderIcon addFolder(final int screen, int intersectCellX, int intersectCellY) {
1611 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 folderInfo.title = getText(R.string.folder_name);
1613
Michael Jurka0280c3b2010-09-17 15:00:07 -07001614 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1615 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohend0445262011-07-04 23:53:22 -07001616 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1,
1617 intersectCellX, intersectCellY)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001618 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001619 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001620 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621
1622 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001623 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001626 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
1628 // Create the view
1629 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001630 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1631 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001632 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001633 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 }
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Joe Onorato9c1289c2009-08-17 11:03:03 -04001636 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001637 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001638 }
1639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 private void showNotifications() {
1641 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1642 if (statusBar != null) {
1643 statusBar.expand();
1644 }
1645 }
1646
1647 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001650 Intent chooser = Intent.createChooser(pickWallpaper,
1651 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001652 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1653 // Removed in Eclair MR1
1654// WallpaperManager wm = (WallpaperManager)
1655// getSystemService(Context.WALLPAPER_SERVICE);
1656// WallpaperInfo wi = wm.getWallpaperInfo();
1657// if (wi != null && wi.getSettingsActivity() != null) {
1658// LabeledIntent li = new LabeledIntent(getPackageName(),
1659// R.string.configure_wallpaper, 0);
1660// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1661// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1662// }
Mike Clerona0618e42009-10-22 13:55:21 -07001663 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001666 /**
1667 * Registers various content observers. The current implementation registers
1668 * only a favorites observer to keep track of the favorites applications.
1669 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001670 private void registerContentObservers() {
1671 ContentResolver resolver = getContentResolver();
1672 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1673 true, mWidgetObserver);
1674 }
1675
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 @Override
1677 public boolean dispatchKeyEvent(KeyEvent event) {
1678 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1679 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001681 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001682 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001683 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001684 dumpState();
1685 return true;
1686 }
1687 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001688 }
1689 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1690 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001691 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 return true;
1693 }
1694 }
1695
1696 return super.dispatchKeyEvent(event);
1697 }
1698
Joe Onorato88ec0992009-11-19 13:16:06 -08001699 @Override
1700 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001701 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001702 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001703 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001704 Folder openFolder = mWorkspace.getOpenFolder();
1705 if (openFolder.isEditingName()) {
1706 openFolder.dismissEditingName();
1707 } else {
1708 closeFolder();
1709 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001710 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001711 mWorkspace.exitWidgetResizeMode();
1712
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001713 // Back button is a no-op here, but give at least some feedback for the button press
1714 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001716 }
1717
Adam Cohen2801caf2011-05-13 20:57:39 -07001718 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 Folder folder = mWorkspace.getOpenFolder();
1720 if (folder != null) {
1721 closeFolder(folder);
1722 }
1723 }
1724
1725 void closeFolder(Folder folder) {
1726 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001727
Michael Jurka8c920dd2011-01-20 14:16:56 -08001728 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001730 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001731 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1732 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001733 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001735 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 }
1737
1738 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001739 * Re-listen when widgets are reset.
1740 */
1741 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001742 if (mAppWidgetHost != null) {
1743 mAppWidgetHost.startListening();
1744 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001745 }
1746
1747 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001748 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1749 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 */
Adam Cohen4eac29a2011-07-11 17:53:37 -07001751 private void unbindWorkspaceItems() {
1752 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 /**
1756 * Launches the intent referred by the clicked shortcut.
1757 *
1758 * @param v The view representing the clicked shortcut.
1759 */
1760 public void onClick(View v) {
1761 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001762 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001764 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001765 int[] pos = new int[2];
1766 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001767 intent.setSourceBounds(new Rect(pos[0], pos[1],
1768 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001769 boolean success = startActivitySafely(intent, tag);
1770
1771 if (success && v instanceof BubbleTextView) {
1772 mWaitingForResume = (BubbleTextView) v;
1773 mWaitingForResume.setStayPressed(true);
1774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001776 if (v instanceof FolderIcon) {
1777 FolderIcon fi = (FolderIcon) v;
1778 handleFolderClick(fi);
1779 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001780 } else if (v == mAllAppsButton) {
1781 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001782 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001783 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001784 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
1787 }
1788
Michael Jurka0e260592010-06-30 17:07:39 -07001789 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001790 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001791 // clicking anywhere on the workspace causes the customization drawer to slide down
1792 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001793 return false;
1794 }
1795
Michael Jurkaaf442092010-06-10 17:01:57 -07001796 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001797 * Event handler for the search button
1798 *
1799 * @param v The view that was clicked.
1800 */
1801 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001802 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001803 // Use a custom animation for launching search
1804 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001805 }
1806
1807 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001808 * Event handler for the voice button
1809 *
1810 * @param v The view that was clicked.
1811 */
1812 public void onClickVoiceButton(View v) {
1813 startVoiceSearch();
1814 }
1815
1816 private void startVoiceSearch() {
1817 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1818 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1819 startActivity(intent);
1820 }
1821
1822 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001823 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001824 * enters home screen customization mode.
1825 *
1826 * @param v The view that was clicked.
1827 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001828 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001829 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001830 }
1831
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001832 /**
1833 * Event handler for the "grid" button that appears on the home screen, which
1834 * enters all apps mode.
1835 *
1836 * @param v The view that was clicked.
1837 */
1838 public void onClickAllAppsButton(View v) {
1839 showAllApps(true);
1840 }
1841
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001842 public void onClickAppMarketButton(View v) {
1843 if (mAppMarketIntent != null) {
1844 startActivitySafely(mAppMarketIntent, "app market");
1845 }
1846 }
1847
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001848 void startApplicationDetailsActivity(ComponentName componentName) {
1849 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001850 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1851 Uri.fromParts("package", packageName, null));
1852 startActivity(intent);
1853 }
1854
Patrick Dubroy5539af72010-09-07 15:22:01 -07001855 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1856 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1857 // System applications cannot be installed. For now, show a toast explaining that.
1858 // We may give them the option of disabling apps this way.
1859 int messageId = R.string.uninstall_system_app_text;
1860 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1861 } else {
1862 String packageName = appInfo.componentName.getPackageName();
1863 String className = appInfo.componentName.getClassName();
1864 Intent intent = new Intent(
1865 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1866 startActivity(intent);
1867 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001868 }
1869
Michael Jurkaddd62e92011-02-16 17:49:14 -08001870 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1872 try {
1873 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001874 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 } catch (ActivityNotFoundException e) {
1876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001877 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } catch (SecurityException e) {
1879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001880 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001882 "or use the exported attribute for this activity. "
1883 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001885 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001887
Romain Guy8e633c52010-03-04 12:51:36 -08001888 void startActivityForResultSafely(Intent intent, int requestCode) {
1889 try {
1890 startActivityForResult(intent, requestCode);
1891 } catch (ActivityNotFoundException e) {
1892 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1893 } catch (SecurityException e) {
1894 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1895 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1896 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1897 "or use the exported attribute for this activity.", e);
1898 }
1899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900
Adam Cohena9cf38f2011-05-02 15:36:58 -07001901 private void handleFolderClick(FolderIcon folderIcon) {
1902 final FolderInfo info = folderIcon.mInfo;
1903 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 // Close any open folder
1905 closeFolder();
1906 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001907 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 } else {
1909 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001910 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 int folderScreen;
1912 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001913 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // .. and close it
1915 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001916 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 // Close any folder open on the current screen
1918 closeFolder();
1919 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001920 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922 }
1923 }
1924 }
1925
Adam Cohen2801caf2011-05-13 20:57:39 -07001926 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1927 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1928 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1929 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1930
1931 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1932 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1933 oa.start();
1934 }
1935
1936 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1937 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1938 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1939 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1940
1941 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1942 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1943 oa.start();
1944 }
1945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001947 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 * is animated relative to the specified View. If the View is null, no animation
1949 * is played.
1950 *
1951 * @param folderInfo The FolderInfo describing the folder to open.
1952 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001953 public void openFolder(FolderIcon folderIcon) {
1954 Folder folder = folderIcon.mFolder;
1955 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956
Adam Cohen2801caf2011-05-13 20:57:39 -07001957 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001958 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959
Adam Cohen8e776a62011-06-28 18:10:06 -07001960 mDragLayer.addView(folder);
1961 mDragController.addDropTarget((DropTarget) folder);
1962
Adam Cohena9cf38f2011-05-02 15:36:58 -07001963 folder.animateOpen();
1964 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001968 if (mState != State.WORKSPACE) {
1969 return false;
1970 }
1971
Romain Guy1fbc1c82009-11-09 20:43:08 -08001972 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001973 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001974 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001975 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1976 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1977 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001978 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001979 return true;
1980 }
1981
Joe Onorato9c1289c2009-08-17 11:03:03 -04001982 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 return false;
1984 }
1985
1986 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001987 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
Michael Jurka0280c3b2010-09-17 15:00:07 -07001990 resetAddInfo();
1991 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001993 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 return true;
1995 }
1996
Michael Jurka0280c3b2010-09-17 15:00:07 -07001997 final View itemUnderLongClick = longClickCellInfo.cell;
1998
Winson Chung304dcde2011-01-07 11:17:23 -08001999 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002000 if (itemUnderLongClick == null) {
2001 // User long pressed on empty space
2002 mWorkspace.setAllowLongPress(false);
2003 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2004 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002005 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002007 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002009 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2010 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002011 mAddIntersectCellX = longClickCellInfo.cellX;
2012 mAddIntersectCellY = longClickCellInfo.cellY;
2013 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015 }
2016 }
2017 return true;
2018 }
2019
Romain Guye6b8e2f2009-11-10 11:56:55 -08002020 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002021 private void dismissPreview(final View v) {
2022 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002023 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002024 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2025 public void onDismiss() {
2026 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2027 int count = group.getChildCount();
2028 for (int i = 0; i < count; i++) {
2029 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2030 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002031 ArrayList<Bitmap> bitmaps =
2032 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002033 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2034
2035 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002036 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002037 window.setOnDismissListener(null);
2038 }
2039 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002040 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002041 }
2042 v.setTag(null);
2043 }
2044
Romain Guyf8e6a802009-12-07 17:48:02 -08002045 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002047 }
2048
Romain Guya6abce82009-11-10 02:54:41 -08002049 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002050 final Resources resources = getResources();
2051 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002052
Romain Guya6abce82009-11-10 02:54:41 -08002053 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002054
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002056
Romain Guyf8e6a802009-12-07 17:48:02 -08002057 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002058 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002059 int extraW = (int) ((r.left + r.right) * max);
2060 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002061
2062 int aW = cell.getWidth() - extraW;
2063 float w = aW / max;
2064
2065 int width = cell.getWidth();
2066 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002067 int x = cell.getPaddingLeft();
2068 int y = cell.getPaddingTop();
2069 width -= (x + cell.getPaddingRight());
2070 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002071
2072 float scale = w / width;
2073
2074 int count = end - start;
2075
2076 final float sWidth = width * scale;
2077 float sHeight = height * scale;
2078
Romain Guye6b8e2f2009-11-10 11:56:55 -08002079 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002080
Romain Guye6b8e2f2009-11-10 11:56:55 -08002081 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2082 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002083
2084 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002085 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002086 cell = (CellLayout) workspace.getChildAt(i);
2087
Romain Guyf8e6a802009-12-07 17:48:02 -08002088 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002089 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002090
2091 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002092 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002093 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002094 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002095
Romain Guy9d31e9f2009-11-11 18:54:28 -08002096 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002097 image.setImageBitmap(bitmap);
2098 image.setTag(i);
2099 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002100 image.setOnFocusChangeListener(handler);
2101 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002102 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002103
2104 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002105 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2106
Winson Chungaafa03c2010-06-11 17:34:16 -07002107 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002108 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002109
2110 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002111 p.setContentView(preview);
2112 p.setWidth((int) (sWidth * count + extraW));
2113 p.setHeight((int) (sHeight + extraH));
2114 p.setAnimationStyle(R.style.AnimationPreview);
2115 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002116 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002117 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002118 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002119
Romain Guye6b8e2f2009-11-10 11:56:55 -08002120 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2121 public void onDismiss() {
2122 dismissPreview(anchor);
2123 }
2124 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002125
2126 anchor.setTag(p);
2127 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002128 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002129 }
2130
Romain Guy9d31e9f2009-11-11 18:54:28 -08002131 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002132 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002133
Romain Guye6b8e2f2009-11-10 11:56:55 -08002134 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002135 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002136 }
2137
2138 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002139 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002140 v.post(this);
2141 }
2142
2143 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002144 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002145 }
2146
2147 public void onFocusChange(View v, boolean hasFocus) {
2148 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002149 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002150 }
Romain Guya6abce82009-11-10 02:54:41 -08002151 }
2152 }
2153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 Workspace getWorkspace() {
2155 return mWorkspace;
2156 }
2157
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002158 @Override
2159 protected Dialog onCreateDialog(int id) {
2160 switch (id) {
2161 case DIALOG_CREATE_SHORTCUT:
2162 return new CreateShortcut().createDialog();
2163 case DIALOG_RENAME_FOLDER:
2164 return new RenameFolder().createDialog();
2165 }
2166
2167 return super.onCreateDialog(id);
2168 }
2169
2170 @Override
2171 protected void onPrepareDialog(int id, Dialog dialog) {
2172 switch (id) {
2173 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 break;
2175 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002176 if (mFolderInfo != null) {
2177 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2178 final CharSequence text = mFolderInfo.title;
2179 input.setText(text);
2180 input.setSelection(0, text.length());
2181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 break;
2183 }
2184 }
2185
2186 void showRenameDialog(FolderInfo info) {
2187 mFolderInfo = info;
2188 mWaitingForResult = true;
2189 showDialog(DIALOG_RENAME_FOLDER);
2190 }
2191
Michael Jurka0280c3b2010-09-17 15:00:07 -07002192 private void showAddDialog(int intersectX, int intersectY) {
2193 resetAddInfo();
2194 mAddIntersectCellX = intersectX;
2195 mAddIntersectCellY = intersectY;
2196 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 mWaitingForResult = true;
2198 showDialog(DIALOG_CREATE_SHORTCUT);
2199 }
2200
Joe Onoratodeb98af2010-02-19 14:59:39 -08002201 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002202 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002203 Bundle bundle = new Bundle();
2204
2205 ArrayList<String> shortcutNames = new ArrayList<String>();
2206 shortcutNames.add(getString(R.string.group_applications));
2207 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2208
2209 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2210 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2211 R.drawable.ic_launcher_application));
2212 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2213
2214 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2215 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002216 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002217 pickIntent.putExtras(bundle);
2218
Joe Onoratodeb98af2010-02-19 14:59:39 -08002219 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002220 }
2221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 private class RenameFolder {
2223 private EditText mInput;
2224
2225 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2227 mInput = (EditText) layout.findViewById(R.id.folder_name);
2228
2229 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2230 builder.setIcon(0);
2231 builder.setTitle(getString(R.string.rename_folder_title));
2232 builder.setCancelable(true);
2233 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2234 public void onCancel(DialogInterface dialog) {
2235 cleanup();
2236 }
2237 });
2238 builder.setNegativeButton(getString(R.string.cancel_action),
2239 new Dialog.OnClickListener() {
2240 public void onClick(DialogInterface dialog, int which) {
2241 cleanup();
2242 }
2243 }
2244 );
2245 builder.setPositiveButton(getString(R.string.rename_action),
2246 new Dialog.OnClickListener() {
2247 public void onClick(DialogInterface dialog, int which) {
2248 changeFolderName();
2249 }
2250 }
2251 );
2252 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002253
2254 final AlertDialog dialog = builder.create();
2255 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2256 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002257 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002258 mInput.requestFocus();
2259 InputMethodManager inputManager = (InputMethodManager)
2260 getSystemService(Context.INPUT_METHOD_SERVICE);
2261 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002262 }
2263 });
2264
2265 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 }
2267
2268 private void changeFolderName() {
2269 final String name = mInput.getText().toString();
2270 if (!TextUtils.isEmpty(name)) {
2271 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002272 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 mFolderInfo.title = name;
2274 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2275
Joe Onorato9c1289c2009-08-17 11:03:03 -04002276 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002277 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002278 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 } else {
2280 final FolderIcon folderIcon = (FolderIcon)
2281 mWorkspace.getViewForTag(mFolderInfo);
2282 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002283 // TODO: At some point we'll probably want some version of setting
2284 // the text for a folder icon.
2285 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 getWorkspace().requestLayout();
2287 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002288 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002289 mWorkspaceLoading = true;
2290 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
2292 }
2293 }
2294 cleanup();
2295 }
2296
2297 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 dismissDialog(DIALOG_RENAME_FOLDER);
2299 mWaitingForResult = false;
2300 mFolderInfo = null;
2301 }
2302 }
2303
Daniel Sandler843e8602010-06-07 14:59:01 -04002304 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2305 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002306 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002307 }
2308
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002309 // AllAppsView.Watcher
2310 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002311 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002312 mWorkspace.setVisibility(View.GONE);
2313 }
2314 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002315
2316 /**
2317 * Helper method for the cameraZoomIn/cameraZoomOut animations
2318 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002319 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002320 * @param scaleFactor The scale factor used for the zoom
2321 */
2322 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2323 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002324
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002325 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002326 // Set pivotY so that at the starting zoom factor, the view is partially
2327 // visible. Modifying initialHeightFactor changes how much of the view is
2328 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002329 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002330 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002331 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002332 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002333 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002334 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002335 }
2336 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002337
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002338 /**
2339 * Zoom the camera out from the workspace to reveal 'toView'.
2340 * Assumes that the view to show is anchored at either the very top or very bottom
2341 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002343 */
Winson Chungc07918d2011-07-01 15:35:26 -07002344 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002345 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002346
Winson Chungf0ea4d32011-06-06 14:27:16 -07002347 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2348 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2349 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2350 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002351
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002352 setPivotsForZoom(toView, toState, scale);
2353
Michael Jurkad74c9842011-07-10 12:44:21 -07002354 // Shrink workspaces away if going to AppsCustomize from workspace
2355 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356
2357 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002358 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002359 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002360 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2361 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002362 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002363 toView.setFastScaleX(a * scale + b * 1f);
2364 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002365 }
2366 });
Adam Cohen61033d32010-11-15 18:29:44 -08002367
Winson Chungf0ea4d32011-06-06 14:27:16 -07002368 toView.setVisibility(View.VISIBLE);
2369 toView.setFastAlpha(0f);
2370 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2371 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2372 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2373 public void onAnimationUpdate(float a, float b) {
2374 // don't need to invalidate because we do so above
2375 toView.setFastAlpha(a * 0f + b * 1f);
2376 }
2377 });
2378 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002379
Michael Jurkaabded662011-03-04 12:06:57 -08002380 if (toView instanceof LauncherTransitionable) {
2381 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002382 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002383 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002384 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002385 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002386 // Prepare the position
2387 toView.setTranslationX(0.0f);
2388 toView.setTranslationY(0.0f);
2389 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002390 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002391
Winson Chungc07918d2011-07-01 15:35:26 -07002392 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2393 // Hide the workspace scrollbar
2394 mWorkspace.hideScrollingIndicator(true);
2395 mWorkspace.hideScrollIndicatorTrack();
2396 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002397 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002398 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002399 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002400 // If we don't set the final scale values here, if this animation is cancelled
2401 // it will have the wrong scale value and subsequent cameraPan animations will
2402 // not fix that
2403 toView.setScaleX(1.0f);
2404 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002405 if (toView instanceof LauncherTransitionable) {
2406 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002407 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002408 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002409 });
2410
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002411 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002412 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002413
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002414 if (mStateAnimation != null) mStateAnimation.cancel();
2415 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002416 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002417 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002418 } else {
2419 toView.setTranslationX(0.0f);
2420 toView.setTranslationY(0.0f);
2421 toView.setScaleX(1.0f);
2422 toView.setScaleY(1.0f);
2423 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002424 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002425 if (toView instanceof LauncherTransitionable) {
2426 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2427 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002428
Winson Chungc07918d2011-07-01 15:35:26 -07002429 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2430 // Hide the workspace scrollbar
2431 mWorkspace.hideScrollingIndicator(true);
2432 mWorkspace.hideScrollIndicatorTrack();
2433 }
Michael Jurkaabded662011-03-04 12:06:57 -08002434 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002436 }
2437
2438 /**
2439 * Zoom the camera back into the workspace, hiding 'fromView'.
2440 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002441 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002442 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002443 */
Winson Chungc07918d2011-07-01 15:35:26 -07002444 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002445 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002446
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2448 final float scaleFactor = (float)
2449 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2450 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002451
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453
Michael Jurkad3ef3062010-11-23 16:23:58 -08002454 if (!springLoaded) {
2455 mWorkspace.unshrink(animated);
2456 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002457 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002458 if (mStateAnimation != null) mStateAnimation.cancel();
2459 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002460
Michael Jurka742574b2011-02-02 23:51:01 -08002461 final float oldScaleX = fromView.getScaleX();
2462 final float oldScaleY = fromView.getScaleY();
2463
2464 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2465 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002466 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2467 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002468 ((View)fromView.getParent()).fastInvalidate();
2469 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2470 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2471 }
2472 });
Michael Jurkaabded662011-03-04 12:06:57 -08002473 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002474 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002475 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002476 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2477 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002478 // don't need to invalidate because we do so above
2479 fromView.setFastAlpha(a * 1f + b * 0f);
2480 }
2481 });
Michael Jurkaabded662011-03-04 12:06:57 -08002482 if (fromView instanceof LauncherTransitionable) {
2483 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002484 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002485 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002486 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002487 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002488 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002489 if (fromView instanceof LauncherTransitionable) {
2490 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002491
Winson Chungc07918d2011-07-01 15:35:26 -07002492 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2493 // Show the workspace scrollbar
2494 mWorkspace.showScrollIndicatorTrack();
2495 mWorkspace.flashScrollingIndicator();
2496 }
Michael Jurka2763be32011-02-24 11:19:57 -08002497 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498 }
2499 });
2500
Winson Chungf0ea4d32011-06-06 14:27:16 -07002501 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002502 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002503 } else {
2504 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002505 if (fromView instanceof LauncherTransitionable) {
2506 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2507 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002508
Winson Chungc07918d2011-07-01 15:35:26 -07002509 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2510 // Show the workspace scrollbar
2511 mWorkspace.showScrollIndicatorTrack();
2512 mWorkspace.flashScrollingIndicator();
2513 }
Michael Jurkaabded662011-03-04 12:06:57 -08002514 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002515 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002516 }
2517
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002518 void showWorkspace(boolean animated) {
2519 showWorkspace(animated, null);
2520 }
2521
2522 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002523 if (layout != null) {
2524 // always animated, but that's ok since we never specify a layout and
2525 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002526 mWorkspace.unshrink(layout);
2527 } else {
2528 mWorkspace.unshrink(animated);
2529 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002530 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002531 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002533
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002534 // Change the state *after* we've called all the transition code
2535 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002536
Winson Chung5fb63472011-02-02 17:03:37 -08002537 // Resume the auto-advance of widgets
2538 mUserPresent = true;
2539 updateRunning();
2540
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002541 // send an accessibility event to announce the context change
2542 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002543 }
2544
Michael Jurkad3ef3062010-11-23 16:23:58 -08002545 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002546 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002547 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002548 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002549 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002550 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002551 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002552 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002553 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2554 mWorkspace.postDelayed(new Runnable() {
2555 @Override
2556 public void run() {
2557 exitSpringLoadedDragMode();
2558 }
2559 }, (extendedDelay ?
2560 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2561 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2562 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002563 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002564 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002565 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002566 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2567 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002568 }
2569 // Otherwise, we are not in spring loaded mode, so don't do anything.
2570 }
2571
2572 /**
2573 * Shows the dock/hotseat area.
2574 */
2575 void showDock(boolean animated) {
2576 if (!LauncherApplication.isScreenLarge()) {
2577 if (animated) {
2578 int duration = mSearchDeleteBar.getTransitionInDuration();
2579 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002580 } else {
2581 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002582 }
2583 }
2584 }
2585
2586 /**
2587 * Hides the dock/hotseat area.
2588 */
2589 void hideDock(boolean animated) {
2590 if (!LauncherApplication.isScreenLarge()) {
2591 if (animated) {
2592 int duration = mSearchDeleteBar.getTransitionOutDuration();
2593 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002594 } else {
2595 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002596 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002597 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002598 }
2599
Winson Chung785d2eb2011-04-14 16:08:02 -07002600 void showAllApps(boolean animated) {
2601 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002602
Winson Chungf0ea4d32011-06-06 14:27:16 -07002603 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2604 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002605
Winson Chungf0ea4d32011-06-06 14:27:16 -07002606 // Hide the search bar and dock
2607 mSearchDeleteBar.hideSearchBar(animated);
2608 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002609
Winson Chungf0ea4d32011-06-06 14:27:16 -07002610 // Change the state *after* we've called all the transition code
2611 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002612
2613 // Pause the auto-advance of widgets until we are out of AllApps
2614 mUserPresent = false;
2615 updateRunning();
2616
2617 // Send an accessibility event to announce the context change
2618 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2619 }
2620
Joe Onoratob2061212009-11-24 16:13:54 -05002621 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002622 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002623 * - Home from workspace
2624 * - from center screen
2625 * - from other screens
2626 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002627 * - from center screen
2628 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002629 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002630 * - from center screen
2631 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002632 * - Launch app from workspace and quit
2633 * - with back
2634 * - with home
2635 * - Launch app from all apps and quit
2636 * - with back
2637 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002638 * - Go to a screen that's not the default, then all
2639 * apps, and launch and app, and go back
2640 * - with back
2641 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002642 * - On workspace, long press power and go back
2643 * - with back
2644 * - with home
2645 * - On all apps, long press power and go back
2646 * - with back
2647 * - with home
2648 * - On workspace, power off
2649 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002650 * - Launch an app and turn off the screen while in that app
2651 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002652 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002653 * - From all apps
2654 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002655 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2656 * - From all apps
2657 * - From the center workspace
2658 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002659 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002660 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002661 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2662 mWorkspace.setVisibility(View.VISIBLE);
2663 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002664
Winson Chungf0ea4d32011-06-06 14:27:16 -07002665 // Show the search bar and dock
2666 mSearchDeleteBar.showSearchBar(animated);
2667 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002668
Winson Chungf0ea4d32011-06-06 14:27:16 -07002669 // Set focus to the AppsCustomize button
2670 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002671 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002672 }
2673
Joe Onorato7c312c12009-08-13 21:36:53 -07002674 void lockAllApps() {
2675 // TODO
2676 }
2677
2678 void unlockAllApps() {
2679 // TODO
2680 }
2681
Patrick Dubroy5f445422011-02-18 14:35:21 -08002682 /**
2683 * Add an item from all apps or customize onto the given workspace screen.
2684 * If layout is null, add to the current screen.
2685 */
2686 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002687 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2688 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002689 } else {
2690 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002691 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002692 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002693
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002694 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002695 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002696 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002697 // Look for the toolbar icon specified in the activity meta-data
2698 Bundle metaData = packageManager.getActivityInfo(
2699 activityName, PackageManager.GET_META_DATA).metaData;
2700 if (metaData != null) {
2701 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2702 if (iconResId != 0) {
2703 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002704 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002705 }
2706 }
2707 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002708 // This can happen if the activity defines an invalid drawable
2709 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2710 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002711 } catch (Resources.NotFoundException nfe) {
2712 // This can happen if the activity defines an invalid drawable
2713 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2714 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002715 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002716 return null;
2717 }
2718
2719 // if successful in getting icon, return it; otherwise, set button to use default drawable
2720 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2721 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2722 TextView button = (TextView) findViewById(buttonId);
2723 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2724
2725 // If we were unable to find the icon via the meta-data, use a generic one
2726 if (toolbarIcon == null) {
2727 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2728 return null;
2729 } else {
2730 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2731 return toolbarIcon.getConstantState();
2732 }
2733 }
2734
2735 // if successful in getting icon, return it; otherwise, set button to use default drawable
2736 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2737 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2738 ImageView button = (ImageView) findViewById(buttonId);
2739 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2740
Michael Jurka0423dcf2010-10-05 14:56:18 -07002741 // If we were unable to find the icon via the meta-data, use a generic one
2742 if (toolbarIcon == null) {
2743 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002744 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002745 } else {
2746 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002747 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002748 }
2749 }
2750
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002751 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2752 TextView button = (TextView) findViewById(buttonId);
2753 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2754 }
2755
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002756 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002757 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002758 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002759 }
2760
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002762 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2763 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002764
Winson Chung1cad91e2011-05-25 17:41:01 -07002765 final SearchManager searchManager =
2766 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2767 ComponentName activityName = searchManager.getGlobalSearchActivity();
2768 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002769 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002770 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002771 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002772 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002773 } else {
2774 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002775 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002776 }
2777 }
2778
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002779 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002780 updateButtonWithDrawable(R.id.search_button, d);
2781 }
2782
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002783 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002784 final View searchDivider = findViewById(R.id.search_divider);
2785 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002786
Winson Chung1cad91e2011-05-25 17:41:01 -07002787 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2788 ComponentName activityName = intent.resolveActivity(getPackageManager());
2789 if (activityName != null) {
2790 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002791 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2792 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 voiceButton.setVisibility(View.VISIBLE);
2794 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002795 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002796 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002797 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002798 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002799
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002800 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002801 updateButtonWithDrawable(R.id.voice_button, d);
2802 }
2803
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002804 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002805 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002806 */
2807 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002808 final View marketButton = findViewById(R.id.market_button);
2809 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2810 // Find the app market activity by resolving an intent.
2811 // (If multiple app markets are installed, it will return the ResolverActivity.)
2812 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002813 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002814 mAppMarketIntent = intent;
2815 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002816 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002817 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002818
2819 // Remove the shop icon text in the Phone UI
2820 if (!LauncherApplication.isScreenLarge()) {
2821 ((TextView) marketButton).setText("");
2822 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002823 } else {
2824 // We should hide and disable the view so that we don't try and restore the visibility
2825 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2826 marketButton.setVisibility(View.GONE);
2827 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002828 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002829 }
2830
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002831 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002832 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002833 }
2834
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002835 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 * Displays the shortcut creation dialog and launches, if necessary, the
2837 * appropriate activity.
2838 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002839 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002840 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2841 DialogInterface.OnShowListener {
2842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002847
Winson Chung55b65502011-05-26 12:03:43 -07002848 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2849 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002851 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 AlertDialog dialog = builder.create();
2854 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002855 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002856 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 return dialog;
2859 }
2860
2861 public void onCancel(DialogInterface dialog) {
2862 mWaitingForResult = false;
2863 cleanup();
2864 }
2865
Romain Guycbb89e42009-06-08 15:52:54 -07002866 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002867 mWaitingForResult = false;
2868 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002869 }
2870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002872 try {
2873 dismissDialog(DIALOG_CREATE_SHORTCUT);
2874 } catch (Exception e) {
2875 // An exception is thrown if the dialog is not visible, which is fine
2876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 }
2878
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002879 /**
2880 * Handle the action clicked in the "Add to home" dialog.
2881 */
2882 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002883 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002884
Winson Chung55b65502011-05-26 12:03:43 -07002885 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2886 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002887 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002888 if (mAppsCustomizeTabHost != null) {
2889 mAppsCustomizeTabHost.selectWidgetsTab();
2890 }
2891 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002892 break;
2893 }
Winson Chung55b65502011-05-26 12:03:43 -07002894 case AddAdapter.ITEM_APPLICATION: {
2895 if (mAppsCustomizeTabHost != null) {
2896 mAppsCustomizeTabHost.selectAppsTab();
2897 }
2898 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002899 break;
2900 }
Winson Chung55b65502011-05-26 12:03:43 -07002901 case AddAdapter.ITEM_APPWIDGET: {
2902 if (mAppsCustomizeTabHost != null) {
2903 mAppsCustomizeTabHost.selectWidgetsTab();
2904 }
2905 showAllApps(true);
2906 break;
2907 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002908 case AddAdapter.ITEM_WALLPAPER: {
2909 startWallpaper();
2910 break;
2911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 }
2913 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002914
2915 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002916 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 }
2919
2920 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002921 * Receives notifications when applications are added/removed.
2922 */
2923 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2924 @Override
2925 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002926 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002927 String reason = intent.getStringExtra("reason");
2928 if (!"homekey".equals(reason)) {
2929 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002930 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002931 animate = false;
2932 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002933 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002934 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002935 }
2936 }
2937
2938 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002939 * Receives notifications whenever the appwidgets are reset.
2940 */
2941 private class AppWidgetResetObserver extends ContentObserver {
2942 public AppWidgetResetObserver() {
2943 super(new Handler());
2944 }
2945
2946 @Override
2947 public void onChange(boolean selfChange) {
2948 onAppWidgetReset();
2949 }
2950 }
2951
2952 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002953 * If the activity is currently paused, signal that we need to re-run the loader
2954 * in onResume.
2955 *
2956 * This needs to be called from incoming places where resources might have been loaded
2957 * while we are paused. That is becaues the Configuration might be wrong
2958 * when we're not running, and if it comes back to what it was when we
2959 * were paused, we are not restarted.
2960 *
2961 * Implementation of the method from LauncherModel.Callbacks.
2962 *
2963 * @return true if we are currently paused. The caller might be able to
2964 * skip some work in that case since we will come back again.
2965 */
2966 public boolean setLoadOnResume() {
2967 if (mPaused) {
2968 Log.i(TAG, "setLoadOnResume");
2969 mOnResumeNeedsLoad = true;
2970 return true;
2971 } else {
2972 return false;
2973 }
2974 }
2975
2976 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002980 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002981 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002982 } else {
2983 return SCREEN_COUNT / 2;
2984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002986
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002987
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 /**
2989 * Refreshes the shortcuts shown on the workspace.
2990 *
2991 * Implementation of the method from LauncherModel.Callbacks.
2992 */
2993 public void startBinding() {
2994 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002995
2996 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 int count = workspace.getChildCount();
2998 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002999 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003000 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3001 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003003
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 if (DEBUG_USER_INTERFACE) {
3005 android.widget.Button finishButton = new android.widget.Button(this);
3006 finishButton.setText("Finish");
3007 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3008
3009 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3010 public void onClick(View v) {
3011 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003015
Adam Cohen4eac29a2011-07-11 17:53:37 -07003016 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3017 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003018 }
3019
3020 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 * Bind the items start-end from the list.
3022 *
3023 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003024 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3026
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003027 setLoadOnResume();
3028
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 final Workspace workspace = mWorkspace;
3030
3031 for (int i=start; i<end; i++) {
3032 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003033 switch (item.itemType) {
3034 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3035 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003036 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3038 false);
3039 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003040 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003042 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003043 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3045 false);
3046 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003048 }
3049
Joe Onorato9c1289c2009-08-17 11:03:03 -04003050 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003051 }
3052
Joe Onorato9c1289c2009-08-17 11:03:03 -04003053 /**
3054 * Implementation of the method from LauncherModel.Callbacks.
3055 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003056 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003057 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003058 sFolders.clear();
3059 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003061
3062 /**
3063 * Add the views for a widget to the workspace.
3064 *
3065 * Implementation of the method from LauncherModel.Callbacks.
3066 */
3067 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003068 setLoadOnResume();
3069
Daniel Sandler843e8602010-06-07 14:59:01 -04003070 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3071 if (DEBUG_WIDGETS) {
3072 Log.d(TAG, "bindAppWidget: " + item);
3073 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003074 final Workspace workspace = mWorkspace;
3075
3076 final int appWidgetId = item.appWidgetId;
3077 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003078 if (DEBUG_WIDGETS) {
3079 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3080 }
3081
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3085 item.hostView.setTag(item);
3086
3087 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3088 item.cellY, item.spanX, item.spanY, false);
3089
Adam Cohended9f8d2010-11-03 13:25:16 -07003090 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3091
Joe Onorato9c1289c2009-08-17 11:03:03 -04003092 workspace.requestLayout();
3093
Daniel Sandler843e8602010-06-07 14:59:01 -04003094 if (DEBUG_WIDGETS) {
3095 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3096 + (SystemClock.uptimeMillis()-start) + "ms");
3097 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 }
3099
3100 /**
3101 * Callback saying that there aren't any more items to bind.
3102 *
3103 * Implementation of the method from LauncherModel.Callbacks.
3104 */
3105 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003106 setLoadOnResume();
3107
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 if (mSavedState != null) {
3109 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003110 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 mSavedState = null;
3113 }
3114
3115 if (mSavedInstanceState != null) {
3116 super.onRestoreInstanceState(mSavedInstanceState);
3117 mSavedInstanceState = null;
3118 }
3119
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003121
3122 // If we received the result of any pending adds while the loader was running (e.g. the
3123 // widget configuration forced an orientation change), process them now.
3124 for (int i = 0; i < sPendingAddList.size(); i++) {
3125 completeAdd(sPendingAddList.get(i));
3126 }
3127 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 }
3129
3130 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003131 * Updates the icons on the launcher that are affected by changes to the package list
3132 * on the device.
3133 */
3134 private void updateIconsAffectedByPackageManagerChanges() {
3135 updateAppMarketIcon();
3136 updateGlobalSearchIcon();
3137 updateVoiceSearchIcon();
3138 }
3139
3140 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 * Add the icons for all apps.
3142 *
3143 * Implementation of the method from LauncherModel.Callbacks.
3144 */
3145 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 if (mAppsCustomizeContent != null) {
3147 mAppsCustomizeContent.setApps(apps);
3148 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003149 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 }
3151
3152 /**
3153 * A package was installed.
3154 *
3155 * Implementation of the method from LauncherModel.Callbacks.
3156 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003157 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003158 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160
Winson Chung785d2eb2011-04-14 16:08:02 -07003161 if (mAppsCustomizeContent != null) {
3162 mAppsCustomizeContent.addApps(apps);
3163 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003164 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 }
3166
3167 /**
3168 * A package was updated.
3169 *
3170 * Implementation of the method from LauncherModel.Callbacks.
3171 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003172 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003173 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003175 if (mWorkspace != null) {
3176 mWorkspace.updateShortcuts(apps);
3177 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178
Winson Chung785d2eb2011-04-14 16:08:02 -07003179 if (mAppsCustomizeContent != null) {
3180 mAppsCustomizeContent.updateApps(apps);
3181 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003182 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 }
3184
3185 /**
3186 * A package was uninstalled.
3187 *
3188 * Implementation of the method from LauncherModel.Callbacks.
3189 */
Joe Onorato36115782010-06-17 13:28:48 -04003190 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003192 if (permanent) {
3193 mWorkspace.removeItems(apps);
3194 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003195
Winson Chung785d2eb2011-04-14 16:08:02 -07003196 if (mAppsCustomizeContent != null) {
3197 mAppsCustomizeContent.removeApps(apps);
3198 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003199 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 }
Joe Onoratobe386092009-11-17 17:32:16 -08003201
3202 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003203 * A number of packages were updated.
3204 */
3205 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206
Winson Chung785d2eb2011-04-14 16:08:02 -07003207 if (mAppsCustomizeContent != null) {
3208 mAppsCustomizeContent.onPackagesUpdated();
3209 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003210 }
3211
Winson Chung400438b2011-01-16 17:53:48 -08003212 private int mapConfigurationOriActivityInfoOri(int configOri) {
3213 final Display d = getWindowManager().getDefaultDisplay();
3214 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3215 switch (d.getRotation()) {
3216 case Surface.ROTATION_0:
3217 case Surface.ROTATION_180:
3218 // We are currently in the same basic orientation as the natural orientation
3219 naturalOri = configOri;
3220 break;
3221 case Surface.ROTATION_90:
3222 case Surface.ROTATION_270:
3223 // We are currently in the other basic orientation to the natural orientation
3224 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3225 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3226 break;
3227 }
3228
3229 int[] oriMap = {
3230 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3231 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3232 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3233 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3234 };
3235 // Since the map starts at portrait, we need to offset if this device's natural orientation
3236 // is landscape.
3237 int indexOffset = 0;
3238 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3239 indexOffset = 1;
3240 }
3241 return oriMap[(d.getRotation() + indexOffset) % 4];
3242 }
3243 public void lockScreenOrientation() {
3244 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3245 .getConfiguration().orientation));
3246 }
3247 public void unlockScreenOrientation() {
3248 mHandler.postDelayed(new Runnable() {
3249 public void run() {
3250 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3251 }
3252 }, mRestoreScreenOrientationDelay);
3253 }
3254
Winson Chung80baf5a2010-08-09 16:03:15 -07003255 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003256 * Prints out out state for debugging.
3257 */
3258 public void dumpState() {
3259 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003260 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003261 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3262 Log.d(TAG, "mRestoring=" + mRestoring);
3263 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3264 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003265 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003266 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003267
Winson Chung785d2eb2011-04-14 16:08:02 -07003268 if (mAppsCustomizeContent != null) {
3269 mAppsCustomizeContent.dumpState();
3270 }
Joe Onoratobe386092009-11-17 17:32:16 -08003271 Log.d(TAG, "END launcher2 dump state");
3272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273}
Michael Jurka2763be32011-02-24 11:19:57 -08003274
Michael Jurkaabded662011-03-04 12:06:57 -08003275interface LauncherTransitionable {
3276 void onLauncherTransitionStart(Animator animation);
3277 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003278}