blob: 7341e713302de1463f7409dc6b5e35e10417e0ca [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();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002391 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002392 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002393 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002394 // If we don't set the final scale values here, if this animation is cancelled
2395 // it will have the wrong scale value and subsequent cameraPan animations will
2396 // not fix that
2397 toView.setScaleX(1.0f);
2398 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002399 if (toView instanceof LauncherTransitionable) {
2400 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002401 }
Winson Chung32174c82011-07-19 15:47:55 -07002402
2403 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2404 // Hide the workspace scrollbar
2405 mWorkspace.hideScrollingIndicator(true);
2406 mWorkspace.hideScrollIndicatorTrack();
2407 }
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
Winson Chung32174c82011-07-19 15:47:55 -07002487 public void onAnimationStart(android.animation.Animator animation) {
2488 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2489 // Show the workspace scrollbar
2490 mWorkspace.showScrollIndicatorTrack();
2491 mWorkspace.flashScrollingIndicator();
2492 }
2493 }
2494 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002495 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002496 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002497 if (fromView instanceof LauncherTransitionable) {
2498 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002499 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 }
2501 });
2502
Winson Chungf0ea4d32011-06-06 14:27:16 -07002503 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002504 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002505 } else {
2506 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002507 if (fromView instanceof LauncherTransitionable) {
2508 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2509 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002510
Winson Chungc07918d2011-07-01 15:35:26 -07002511 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2512 // Show the workspace scrollbar
2513 mWorkspace.showScrollIndicatorTrack();
2514 mWorkspace.flashScrollingIndicator();
2515 }
Michael Jurkaabded662011-03-04 12:06:57 -08002516 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002518 }
2519
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002520 void showWorkspace(boolean animated) {
2521 showWorkspace(animated, null);
2522 }
2523
2524 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002525 if (layout != null) {
2526 // always animated, but that's ok since we never specify a layout and
2527 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002528 mWorkspace.unshrink(layout);
2529 } else {
2530 mWorkspace.unshrink(animated);
2531 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002532 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002533 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002534 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002535
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002536 // Change the state *after* we've called all the transition code
2537 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002538
Winson Chung5fb63472011-02-02 17:03:37 -08002539 // Resume the auto-advance of widgets
2540 mUserPresent = true;
2541 updateRunning();
2542
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002543 // send an accessibility event to announce the context change
2544 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002545 }
2546
Michael Jurkad3ef3062010-11-23 16:23:58 -08002547 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002548 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002549 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002550 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002551 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002552 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002553 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002554 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002555 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2556 mWorkspace.postDelayed(new Runnable() {
2557 @Override
2558 public void run() {
2559 exitSpringLoadedDragMode();
2560 }
2561 }, (extendedDelay ?
2562 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2563 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2564 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002565 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002566 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002567 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002568 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2569 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002570 }
2571 // Otherwise, we are not in spring loaded mode, so don't do anything.
2572 }
2573
2574 /**
2575 * Shows the dock/hotseat area.
2576 */
2577 void showDock(boolean animated) {
2578 if (!LauncherApplication.isScreenLarge()) {
2579 if (animated) {
2580 int duration = mSearchDeleteBar.getTransitionInDuration();
2581 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002582 } else {
2583 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002584 }
2585 }
2586 }
2587
2588 /**
2589 * Hides the dock/hotseat area.
2590 */
2591 void hideDock(boolean animated) {
2592 if (!LauncherApplication.isScreenLarge()) {
2593 if (animated) {
2594 int duration = mSearchDeleteBar.getTransitionOutDuration();
2595 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002596 } else {
2597 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002598 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002599 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002600 }
2601
Winson Chung785d2eb2011-04-14 16:08:02 -07002602 void showAllApps(boolean animated) {
2603 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002604
Winson Chungf0ea4d32011-06-06 14:27:16 -07002605 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2606 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002607
Winson Chungf0ea4d32011-06-06 14:27:16 -07002608 // Hide the search bar and dock
2609 mSearchDeleteBar.hideSearchBar(animated);
2610 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002611
Winson Chungf0ea4d32011-06-06 14:27:16 -07002612 // Change the state *after* we've called all the transition code
2613 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002614
2615 // Pause the auto-advance of widgets until we are out of AllApps
2616 mUserPresent = false;
2617 updateRunning();
2618
2619 // Send an accessibility event to announce the context change
2620 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2621 }
2622
Joe Onoratob2061212009-11-24 16:13:54 -05002623 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002624 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002625 * - Home from workspace
2626 * - from center screen
2627 * - from other screens
2628 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002629 * - from center screen
2630 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002631 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002632 * - from center screen
2633 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002634 * - Launch app from workspace and quit
2635 * - with back
2636 * - with home
2637 * - Launch app from all apps and quit
2638 * - with back
2639 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002640 * - Go to a screen that's not the default, then all
2641 * apps, and launch and app, and go back
2642 * - with back
2643 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002644 * - On workspace, long press power and go back
2645 * - with back
2646 * - with home
2647 * - On all apps, long press power and go back
2648 * - with back
2649 * - with home
2650 * - On workspace, power off
2651 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002652 * - Launch an app and turn off the screen while in that app
2653 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002654 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002655 * - From all apps
2656 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002657 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2658 * - From all apps
2659 * - From the center workspace
2660 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002661 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002662 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002663 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2664 mWorkspace.setVisibility(View.VISIBLE);
2665 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002666
Winson Chungf0ea4d32011-06-06 14:27:16 -07002667 // Show the search bar and dock
2668 mSearchDeleteBar.showSearchBar(animated);
2669 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002670
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 // Set focus to the AppsCustomize button
2672 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002673 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002674 }
2675
Joe Onorato7c312c12009-08-13 21:36:53 -07002676 void lockAllApps() {
2677 // TODO
2678 }
2679
2680 void unlockAllApps() {
2681 // TODO
2682 }
2683
Patrick Dubroy5f445422011-02-18 14:35:21 -08002684 /**
2685 * Add an item from all apps or customize onto the given workspace screen.
2686 * If layout is null, add to the current screen.
2687 */
2688 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002689 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2690 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002691 } else {
2692 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002693 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002694 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002695
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002696 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002697 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002698 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002699 // Look for the toolbar icon specified in the activity meta-data
2700 Bundle metaData = packageManager.getActivityInfo(
2701 activityName, PackageManager.GET_META_DATA).metaData;
2702 if (metaData != null) {
2703 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2704 if (iconResId != 0) {
2705 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002706 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002707 }
2708 }
2709 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002710 // This can happen if the activity defines an invalid drawable
2711 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2712 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002713 } catch (Resources.NotFoundException nfe) {
2714 // This can happen if the activity defines an invalid drawable
2715 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2716 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002718 return null;
2719 }
2720
2721 // if successful in getting icon, return it; otherwise, set button to use default drawable
2722 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2723 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2724 TextView button = (TextView) findViewById(buttonId);
2725 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2726
2727 // If we were unable to find the icon via the meta-data, use a generic one
2728 if (toolbarIcon == null) {
2729 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2730 return null;
2731 } else {
2732 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2733 return toolbarIcon.getConstantState();
2734 }
2735 }
2736
2737 // if successful in getting icon, return it; otherwise, set button to use default drawable
2738 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2739 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2740 ImageView button = (ImageView) findViewById(buttonId);
2741 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2742
Michael Jurka0423dcf2010-10-05 14:56:18 -07002743 // If we were unable to find the icon via the meta-data, use a generic one
2744 if (toolbarIcon == null) {
2745 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002746 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002747 } else {
2748 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002749 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002750 }
2751 }
2752
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002753 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2754 TextView button = (TextView) findViewById(buttonId);
2755 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2756 }
2757
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002758 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002759 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002760 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002761 }
2762
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002764 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2765 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002766
Winson Chung1cad91e2011-05-25 17:41:01 -07002767 final SearchManager searchManager =
2768 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2769 ComponentName activityName = searchManager.getGlobalSearchActivity();
2770 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002771 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002772 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002773 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002774 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002775 } else {
2776 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002777 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002778 }
2779 }
2780
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002781 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002782 updateButtonWithDrawable(R.id.search_button, d);
2783 }
2784
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002785 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002786 final View searchDivider = findViewById(R.id.search_divider);
2787 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002788
Winson Chung1cad91e2011-05-25 17:41:01 -07002789 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2790 ComponentName activityName = intent.resolveActivity(getPackageManager());
2791 if (activityName != null) {
2792 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002793 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2794 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002795 voiceButton.setVisibility(View.VISIBLE);
2796 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002797 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002798 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002799 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002800 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002801
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002802 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002803 updateButtonWithDrawable(R.id.voice_button, d);
2804 }
2805
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002806 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002807 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002808 */
2809 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002810 final View marketButton = findViewById(R.id.market_button);
2811 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2812 // Find the app market activity by resolving an intent.
2813 // (If multiple app markets are installed, it will return the ResolverActivity.)
2814 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002815 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002816 mAppMarketIntent = intent;
2817 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002818 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002819 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002820
2821 // Remove the shop icon text in the Phone UI
2822 if (!LauncherApplication.isScreenLarge()) {
2823 ((TextView) marketButton).setText("");
2824 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002825 } else {
2826 // We should hide and disable the view so that we don't try and restore the visibility
2827 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2828 marketButton.setVisibility(View.GONE);
2829 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002830 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002831 }
2832
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002833 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002834 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002835 }
2836
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002837 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 * Displays the shortcut creation dialog and launches, if necessary, the
2839 * appropriate activity.
2840 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002841 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002842 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2843 DialogInterface.OnShowListener {
2844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002849
Winson Chung55b65502011-05-26 12:03:43 -07002850 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2851 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002853 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 AlertDialog dialog = builder.create();
2856 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002857 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002858 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 return dialog;
2861 }
2862
2863 public void onCancel(DialogInterface dialog) {
2864 mWaitingForResult = false;
2865 cleanup();
2866 }
2867
Romain Guycbb89e42009-06-08 15:52:54 -07002868 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002869 mWaitingForResult = false;
2870 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002871 }
2872
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002874 try {
2875 dismissDialog(DIALOG_CREATE_SHORTCUT);
2876 } catch (Exception e) {
2877 // An exception is thrown if the dialog is not visible, which is fine
2878 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879 }
2880
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002881 /**
2882 * Handle the action clicked in the "Add to home" dialog.
2883 */
2884 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002885 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002886
Winson Chung55b65502011-05-26 12:03:43 -07002887 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2888 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002889 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002890 if (mAppsCustomizeTabHost != null) {
2891 mAppsCustomizeTabHost.selectWidgetsTab();
2892 }
2893 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002894 break;
2895 }
Winson Chung55b65502011-05-26 12:03:43 -07002896 case AddAdapter.ITEM_APPLICATION: {
2897 if (mAppsCustomizeTabHost != null) {
2898 mAppsCustomizeTabHost.selectAppsTab();
2899 }
2900 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002901 break;
2902 }
Winson Chung55b65502011-05-26 12:03:43 -07002903 case AddAdapter.ITEM_APPWIDGET: {
2904 if (mAppsCustomizeTabHost != null) {
2905 mAppsCustomizeTabHost.selectWidgetsTab();
2906 }
2907 showAllApps(true);
2908 break;
2909 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002910 case AddAdapter.ITEM_WALLPAPER: {
2911 startWallpaper();
2912 break;
2913 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 }
2915 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002916
2917 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002918 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002919 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 }
2921
2922 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002923 * Receives notifications when applications are added/removed.
2924 */
2925 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2926 @Override
2927 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002928 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002929 String reason = intent.getStringExtra("reason");
2930 if (!"homekey".equals(reason)) {
2931 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002932 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002933 animate = false;
2934 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002935 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002936 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002937 }
2938 }
2939
2940 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002941 * Receives notifications whenever the appwidgets are reset.
2942 */
2943 private class AppWidgetResetObserver extends ContentObserver {
2944 public AppWidgetResetObserver() {
2945 super(new Handler());
2946 }
2947
2948 @Override
2949 public void onChange(boolean selfChange) {
2950 onAppWidgetReset();
2951 }
2952 }
2953
2954 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002955 * If the activity is currently paused, signal that we need to re-run the loader
2956 * in onResume.
2957 *
2958 * This needs to be called from incoming places where resources might have been loaded
2959 * while we are paused. That is becaues the Configuration might be wrong
2960 * when we're not running, and if it comes back to what it was when we
2961 * were paused, we are not restarted.
2962 *
2963 * Implementation of the method from LauncherModel.Callbacks.
2964 *
2965 * @return true if we are currently paused. The caller might be able to
2966 * skip some work in that case since we will come back again.
2967 */
2968 public boolean setLoadOnResume() {
2969 if (mPaused) {
2970 Log.i(TAG, "setLoadOnResume");
2971 mOnResumeNeedsLoad = true;
2972 return true;
2973 } else {
2974 return false;
2975 }
2976 }
2977
2978 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002982 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002983 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002984 } else {
2985 return SCREEN_COUNT / 2;
2986 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002988
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002989
Joe Onorato9c1289c2009-08-17 11:03:03 -04002990 /**
2991 * Refreshes the shortcuts shown on the workspace.
2992 *
2993 * Implementation of the method from LauncherModel.Callbacks.
2994 */
2995 public void startBinding() {
2996 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002997
2998 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 int count = workspace.getChildCount();
3000 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003001 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003002 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3003 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003005
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 if (DEBUG_USER_INTERFACE) {
3007 android.widget.Button finishButton = new android.widget.Button(this);
3008 finishButton.setText("Finish");
3009 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3010
3011 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3012 public void onClick(View v) {
3013 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003017
Adam Cohen4eac29a2011-07-11 17:53:37 -07003018 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3019 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 }
3021
3022 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 * Bind the items start-end from the list.
3024 *
3025 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3028
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003029 setLoadOnResume();
3030
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 final Workspace workspace = mWorkspace;
3032
3033 for (int i=start; i<end; i++) {
3034 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003035 switch (item.itemType) {
3036 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3037 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003038 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3040 false);
3041 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003042 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003044 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003045 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3047 false);
3048 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050 }
3051
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 }
3054
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 /**
3056 * Implementation of the method from LauncherModel.Callbacks.
3057 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003058 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003059 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003060 sFolders.clear();
3061 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063
3064 /**
3065 * Add the views for a widget to the workspace.
3066 *
3067 * Implementation of the method from LauncherModel.Callbacks.
3068 */
3069 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003070 setLoadOnResume();
3071
Daniel Sandler843e8602010-06-07 14:59:01 -04003072 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3073 if (DEBUG_WIDGETS) {
3074 Log.d(TAG, "bindAppWidget: " + item);
3075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 final Workspace workspace = mWorkspace;
3077
3078 final int appWidgetId = item.appWidgetId;
3079 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003080 if (DEBUG_WIDGETS) {
3081 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3082 }
3083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3085
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3087 item.hostView.setTag(item);
3088
3089 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3090 item.cellY, item.spanX, item.spanY, false);
3091
Adam Cohended9f8d2010-11-03 13:25:16 -07003092 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 workspace.requestLayout();
3095
Daniel Sandler843e8602010-06-07 14:59:01 -04003096 if (DEBUG_WIDGETS) {
3097 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3098 + (SystemClock.uptimeMillis()-start) + "ms");
3099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 }
3101
3102 /**
3103 * Callback saying that there aren't any more items to bind.
3104 *
3105 * Implementation of the method from LauncherModel.Callbacks.
3106 */
3107 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003108 setLoadOnResume();
3109
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 if (mSavedState != null) {
3111 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003112 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 mSavedState = null;
3115 }
3116
3117 if (mSavedInstanceState != null) {
3118 super.onRestoreInstanceState(mSavedInstanceState);
3119 mSavedInstanceState = null;
3120 }
3121
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003123
3124 // If we received the result of any pending adds while the loader was running (e.g. the
3125 // widget configuration forced an orientation change), process them now.
3126 for (int i = 0; i < sPendingAddList.size(); i++) {
3127 completeAdd(sPendingAddList.get(i));
3128 }
3129 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 }
3131
3132 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 * Updates the icons on the launcher that are affected by changes to the package list
3134 * on the device.
3135 */
3136 private void updateIconsAffectedByPackageManagerChanges() {
3137 updateAppMarketIcon();
3138 updateGlobalSearchIcon();
3139 updateVoiceSearchIcon();
3140 }
3141
3142 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 * Add the icons for all apps.
3144 *
3145 * Implementation of the method from LauncherModel.Callbacks.
3146 */
3147 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 if (mAppsCustomizeContent != null) {
3149 mAppsCustomizeContent.setApps(apps);
3150 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 }
3153
3154 /**
3155 * A package was installed.
3156 *
3157 * Implementation of the method from LauncherModel.Callbacks.
3158 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003159 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003160 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003162
Winson Chung785d2eb2011-04-14 16:08:02 -07003163 if (mAppsCustomizeContent != null) {
3164 mAppsCustomizeContent.addApps(apps);
3165 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 }
3168
3169 /**
3170 * A package was updated.
3171 *
3172 * Implementation of the method from LauncherModel.Callbacks.
3173 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003174 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003175 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003177 if (mWorkspace != null) {
3178 mWorkspace.updateShortcuts(apps);
3179 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180
Winson Chung785d2eb2011-04-14 16:08:02 -07003181 if (mAppsCustomizeContent != null) {
3182 mAppsCustomizeContent.updateApps(apps);
3183 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003184 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 }
3186
3187 /**
3188 * A package was uninstalled.
3189 *
3190 * Implementation of the method from LauncherModel.Callbacks.
3191 */
Joe Onorato36115782010-06-17 13:28:48 -04003192 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003194 if (permanent) {
3195 mWorkspace.removeItems(apps);
3196 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003197
Winson Chung785d2eb2011-04-14 16:08:02 -07003198 if (mAppsCustomizeContent != null) {
3199 mAppsCustomizeContent.removeApps(apps);
3200 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003201 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202 }
Joe Onoratobe386092009-11-17 17:32:16 -08003203
3204 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003205 * A number of packages were updated.
3206 */
3207 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003208
Winson Chung785d2eb2011-04-14 16:08:02 -07003209 if (mAppsCustomizeContent != null) {
3210 mAppsCustomizeContent.onPackagesUpdated();
3211 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003212 }
3213
Winson Chung400438b2011-01-16 17:53:48 -08003214 private int mapConfigurationOriActivityInfoOri(int configOri) {
3215 final Display d = getWindowManager().getDefaultDisplay();
3216 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3217 switch (d.getRotation()) {
3218 case Surface.ROTATION_0:
3219 case Surface.ROTATION_180:
3220 // We are currently in the same basic orientation as the natural orientation
3221 naturalOri = configOri;
3222 break;
3223 case Surface.ROTATION_90:
3224 case Surface.ROTATION_270:
3225 // We are currently in the other basic orientation to the natural orientation
3226 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3227 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3228 break;
3229 }
3230
3231 int[] oriMap = {
3232 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3233 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3234 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3235 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3236 };
3237 // Since the map starts at portrait, we need to offset if this device's natural orientation
3238 // is landscape.
3239 int indexOffset = 0;
3240 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3241 indexOffset = 1;
3242 }
3243 return oriMap[(d.getRotation() + indexOffset) % 4];
3244 }
3245 public void lockScreenOrientation() {
3246 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3247 .getConfiguration().orientation));
3248 }
3249 public void unlockScreenOrientation() {
3250 mHandler.postDelayed(new Runnable() {
3251 public void run() {
3252 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3253 }
3254 }, mRestoreScreenOrientationDelay);
3255 }
3256
Winson Chung80baf5a2010-08-09 16:03:15 -07003257 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003258 * Prints out out state for debugging.
3259 */
3260 public void dumpState() {
3261 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003262 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003263 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3264 Log.d(TAG, "mRestoring=" + mRestoring);
3265 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3266 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003267 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003268 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003269
Winson Chung785d2eb2011-04-14 16:08:02 -07003270 if (mAppsCustomizeContent != null) {
3271 mAppsCustomizeContent.dumpState();
3272 }
Joe Onoratobe386092009-11-17 17:32:16 -08003273 Log.d(TAG, "END launcher2 dump state");
3274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003275}
Michael Jurka2763be32011-02-24 11:19:57 -08003276
Michael Jurkaabded662011-03-04 12:06:57 -08003277interface LauncherTransitionable {
3278 void onLauncherTransitionStart(Animator animation);
3279 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003280}