blob: a2e4bd0c7c468d75abce8bc51445e2f7c052d5ce [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();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100320 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700321 }
322 if (sGlobalSearchIcon != null) {
323 updateGlobalSearchIcon(sGlobalSearchIcon);
324 }
325 if (sVoiceSearchIcon != null) {
326 updateVoiceSearchIcon(sVoiceSearchIcon);
327 }
328 if (sAppMarketIcon != null) {
329 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Romain Guycbb89e42009-06-08 15:52:54 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700334 if (sLocaleConfiguration == null) {
335 new AsyncTask<Void, Void, LocaleConfiguration>() {
336 @Override
337 protected LocaleConfiguration doInBackground(Void... unused) {
338 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
339 readConfiguration(Launcher.this, localeConfiguration);
340 return localeConfiguration;
341 }
342
343 @Override
344 protected void onPostExecute(LocaleConfiguration result) {
345 sLocaleConfiguration = result;
346 checkForLocaleChange(); // recursive, but now with a locale configuration
347 }
348 }.execute();
349 return;
350 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 final Configuration configuration = getResources().getConfiguration();
353
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 final String locale = configuration.locale.toString();
356
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700357 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final int mcc = configuration.mcc;
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final int mnc = configuration.mnc;
362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Romain Guy84f296c2009-11-04 15:00:44 -0800365 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 sLocaleConfiguration.locale = locale;
367 sLocaleConfiguration.mcc = mcc;
368 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700369
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400371 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372
373 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
374 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700375 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 public void run() {
377 writeConfiguration(Launcher.this, localeConfiguration);
378 }
379 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700380 }
381 }
382
383 private static class LocaleConfiguration {
384 public String locale;
385 public int mcc = -1;
386 public int mnc = -1;
387 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700388
Romain Guy98d01652009-06-30 16:21:04 -0700389 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
390 DataInputStream in = null;
391 try {
392 in = new DataInputStream(context.openFileInput(PREFERENCES));
393 configuration.locale = in.readUTF();
394 configuration.mcc = in.readInt();
395 configuration.mnc = in.readInt();
396 } catch (FileNotFoundException e) {
397 // Ignore
398 } catch (IOException e) {
399 // Ignore
400 } finally {
401 if (in != null) {
402 try {
403 in.close();
404 } catch (IOException e) {
405 // Ignore
406 }
407 }
408 }
409 }
410
411 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
412 DataOutputStream out = null;
413 try {
414 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
415 out.writeUTF(configuration.locale);
416 out.writeInt(configuration.mcc);
417 out.writeInt(configuration.mnc);
418 out.flush();
419 } catch (FileNotFoundException e) {
420 // Ignore
421 } catch (IOException e) {
422 //noinspection ResultOfMethodCallIgnored
423 context.getFileStreamPath(PREFERENCES).delete();
424 } finally {
425 if (out != null) {
426 try {
427 out.close();
428 } catch (IOException e) {
429 // Ignore
430 }
431 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433 }
434
Adam Cohen716b51e2011-06-30 12:09:54 -0700435 public DragLayer getDragLayer() {
436 return mDragLayer;
437 }
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 static int getScreen() {
440 synchronized (sLock) {
441 return sScreen;
442 }
443 }
444
445 static void setScreen(int screen) {
446 synchronized (sLock) {
447 sScreen = screen;
448 }
449 }
450
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 // Note: This doesn't do all the client-id magic that BrowserProvider does
452 // in Browser. (http://b/2425179)
453 private Uri getDefaultBrowserUri() {
454 String url = getString(R.string.default_browser_url);
455 if (url.indexOf("{CID}") != -1) {
456 url = url.replace("{CID}", "android-google");
457 }
458 return Uri.parse(url);
459 }
460
461 // Load the Intent templates from arrays.xml to populate the hotseats. For
462 // each Intent, if it resolves to a single app, use that as the launch
463 // intent & use that app's label as the contentDescription. Otherwise,
464 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400465 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400466 if (mHotseatConfig == null) {
467 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
468 if (mHotseatConfig.length > 0) {
469 mHotseats = new Intent[mHotseatConfig.length];
470 mHotseatLabels = new CharSequence[mHotseatConfig.length];
471 mHotseatIcons = new Drawable[mHotseatConfig.length];
472 } else {
473 mHotseats = null;
474 mHotseatIcons = null;
475 mHotseatLabels = null;
476 }
477
478 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
479 for (int i=0; i<mHotseatConfig.length; i++) {
480 // load icon for this slot; currently unrelated to the actual activity
481 try {
482 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
483 } catch (ArrayIndexOutOfBoundsException ex) {
484 Log.w(TAG, "Missing hotseat_icons array item #" + i);
485 mHotseatIcons[i] = null;
486 }
487 }
488 hotseatIconDrawables.recycle();
489 }
490
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400491 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400492 for (int i=0; i<mHotseatConfig.length; i++) {
493 Intent intent = null;
494 if (mHotseatConfig[i].equals("*BROWSER*")) {
495 // magic value meaning "launch user's default web browser"
496 // replace it with a generic web request so we can see if there is indeed a default
497 String defaultUri = getString(R.string.default_browser_url);
498 intent = new Intent(
499 Intent.ACTION_VIEW,
500 ((defaultUri != null)
501 ? Uri.parse(defaultUri)
502 : getDefaultBrowserUri())
503 ).addCategory(Intent.CATEGORY_BROWSABLE);
504 // note: if the user launches this without a default set, she
505 // will always be taken to the default URL above; this is
506 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700507 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 // URL is unavoidably sent to the chosen app.
509 } else {
510 try {
511 intent = Intent.parseUri(mHotseatConfig[i], 0);
512 } catch (java.net.URISyntaxException ex) {
513 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
514 // bogus; leave intent=null
515 }
516 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400518 if (intent == null) {
519 mHotseats[i] = null;
520 mHotseatLabels[i] = getText(R.string.activity_not_found);
521 continue;
522 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523
524 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700525 Log.d(TAG, "loadHotseats: hotseat " + i
526 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400527 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400528 + "]");
529 }
530
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400531 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
532 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700533 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 Log.d(TAG, "Best match for intent: " + bestMatch);
535 Log.d(TAG, "All matches: ");
536 for (ResolveInfo ri : allMatches) {
537 Log.d(TAG, " --> " + ri);
538 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400539 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // did this resolve to a single app, or the resolver?
541 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700542 // can't find any activity to handle this. let's leave the
543 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 // to launch.
545 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 // set accessibility text to "Not installed"
548 mHotseatLabels[i] = getText(R.string.activity_not_found);
549 } else {
550 boolean found = false;
551 for (ResolveInfo ri : allMatches) {
552 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
553 && bestMatch.activityInfo.applicationInfo.packageName
554 .equals(ri.activityInfo.applicationInfo.packageName)) {
555 found = true;
556 break;
557 }
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (!found) {
561 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
562 // the bestMatch is probably the ResolveActivity, meaning the
563 // user has not yet selected a default
564 // so: we'll keep the original intent for now
565 mHotseats[i] = intent;
566
567 // set the accessibility text to "Select shortcut"
568 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
569 } else {
570 // we have an app!
571 // now reconstruct the intent to launch it through the front
572 // door
573 ComponentName com = new ComponentName(
574 bestMatch.activityInfo.applicationInfo.packageName,
575 bestMatch.activityInfo.name);
576 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
577
578 // load the app label for accessibility
579 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
580 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 }
582
583 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 Log.d(TAG, "loadHotseats: hotseat " + i
585 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 + ((mHotseats[i] == null)
587 ? "null"
588 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400589 + "] label=[" + mHotseatLabels[i]
590 + "]"
591 );
592 }
593 }
594 }
595
Winson Chung557d6ed2011-07-08 15:34:52 -0700596 /**
597 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
598 * a configuration step, this allows the proper animations to run after other transitions.
599 */
600 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 switch (args.requestCode) {
602 case REQUEST_PICK_APPLICATION:
603 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
604 break;
605 case REQUEST_PICK_SHORTCUT:
606 processShortcut(args.intent);
607 break;
608 case REQUEST_CREATE_SHORTCUT:
609 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_APPWIDGET:
612 addAppWidgetFromPick(args.intent);
613 break;
614 case REQUEST_CREATE_APPWIDGET:
615 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
616 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700617 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 case REQUEST_PICK_WALLPAPER:
619 // We just wanted the activity result here so we can clear mWaitingForResult
620 break;
621 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700622 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800626 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700627 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700628 mWaitingForResult = false;
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // The pattern used here is that a user PICKs a specific application,
631 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
634 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700635
Michael Jurka0280c3b2010-09-17 15:00:07 -0700636 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800637 final PendingAddArguments args = new PendingAddArguments();
638 args.requestCode = requestCode;
639 args.intent = data;
640 args.screen = mAddScreen;
641 args.cellX = mAddIntersectCellX;
642 args.cellY = mAddIntersectCellY;
643
644 // If the loader is still running, defer the add until it is done.
645 if (isWorkspaceLocked()) {
646 sPendingAddList.add(args);
647 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700648 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Romain Guy18042c82009-11-06 11:44:55 -0800650 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700651 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
652 if (data != null) {
653 // Clean up the appWidgetId if we canceled
654 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
655 if (appWidgetId != -1) {
656 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700660
661 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
662 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 }
664
665 @Override
666 protected void onResume() {
667 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800668 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700669 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 mWorkspaceLoading = true;
671 mModel.startLoader(this, true);
672 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700673 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800675 if (mWaitingForResume != null) {
676 mWaitingForResume.setStayPressed(false);
677 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700678 // When we resume Launcher, a different Activity might be responsible for the app
679 // market intent, so refresh the icon
680 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 }
682
683 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 protected void onPause() {
685 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700686 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800687 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 }
Romain Guycbb89e42009-06-08 15:52:54 -0700689
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700690 @Override
691 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400692 // Flag the loader to stop early before switching
693 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700694 if (mAppsCustomizeContent != null) {
695 mAppsCustomizeContent.surrender();
696 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800697 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700698 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700699
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800700 // We can't hide the IME if it was forced open. So don't bother
701 /*
702 @Override
703 public void onWindowFocusChanged(boolean hasFocus) {
704 super.onWindowFocusChanged(hasFocus);
705
706 if (hasFocus) {
707 final InputMethodManager inputManager = (InputMethodManager)
708 getSystemService(Context.INPUT_METHOD_SERVICE);
709 WindowManager.LayoutParams lp = getWindow().getAttributes();
710 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
711 android.os.Handler()) {
712 protected void onReceiveResult(int resultCode, Bundle resultData) {
713 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
714 }
715 });
716 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
717 }
718 }
719 */
720
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 private boolean acceptFilter() {
722 final InputMethodManager inputManager = (InputMethodManager)
723 getSystemService(Context.INPUT_METHOD_SERVICE);
724 return !inputManager.isFullscreenMode();
725 }
726
727 @Override
728 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700729 final int uniChar = event.getUnicodeChar();
730 final boolean handled = super.onKeyDown(keyCode, event);
731 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
732 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
734 keyCode, event);
735 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700736 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700737 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700738 // showSearchDialog()
739 // If there are multiple keystrokes before the search dialog takes focus,
740 // onSearchRequested() will be called for every keystroke,
741 // but it is idempotent, so it's fine.
742 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 }
744 }
745
Joe Onorato8a9625e2010-01-28 15:55:35 -0800746 // Eat the long press event so the keyboard doesn't come up.
747 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
748 return true;
749 }
750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 return handled;
752 }
753
Karl Rosaen138a0412009-04-23 19:00:21 -0700754 private String getTypedText() {
755 return mDefaultKeySsb.toString();
756 }
757
758 private void clearTypedText() {
759 mDefaultKeySsb.clear();
760 mDefaultKeySsb.clearSpans();
761 Selection.setSelection(mDefaultKeySsb, 0);
762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700765 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
766 * State
767 */
768 private static State intToState(int stateOrdinal) {
769 State state = State.WORKSPACE;
770 final State[] stateValues = State.values();
771 for (int i = 0; i < stateValues.length; i++) {
772 if (stateValues[i].ordinal() == stateOrdinal) {
773 state = stateValues[i];
774 break;
775 }
776 }
777 return state;
778 }
779
780 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 * Restores the previous state, if it exists.
782 *
783 * @param savedState The previous state.
784 */
785 private void restoreState(Bundle savedState) {
786 if (savedState == null) {
787 return;
788 }
789
Michael Jurka883f55b2010-10-21 15:47:14 -0700790 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
791
Winson Chungf0ea4d32011-06-06 14:27:16 -0700792 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800793 showAllApps(false);
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
797 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700798 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
800
801 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700802
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700804 mAddScreen = addScreen;
805 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
806 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 mRestoring = true;
808 }
809
810 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
811 if (renameFolder) {
812 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700813 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 mRestoring = true;
815 }
Winson Chunga12a2502010-12-20 14:41:35 -0800816
Winson Chung785d2eb2011-04-14 16:08:02 -0700817
818 // Restore the AppsCustomize tab
819 if (mAppsCustomizeTabHost != null) {
820 String curTab = savedState.getString("apps_customize_currentTab");
821 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700822 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700823 mAppsCustomizeContent.setContentType(
824 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
825 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
826 }
827
828 // Note: currently we do not restore the page for the AppsCustomize pane because the
829 // change in layout can drastically affect the saved page index
830 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832
833 /**
834 * Finds all the views we need and configure them properly.
835 */
836 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700837 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400838
Winson Chung4c98d922011-05-31 16:50:48 -0700839 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
840 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841
Winson Chung4c98d922011-05-31 16:50:48 -0700842 // Setup the drag layer
843 mDragLayer.setup(this, dragController);
844
845 // Setup the workspace
846 mWorkspace.setHapticFeedbackEnabled(false);
847 mWorkspace.setOnLongClickListener(this);
848 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700849 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700850
Winson Chungf0ea4d32011-06-06 14:27:16 -0700851 // Get the search/delete bar
852 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700853
Winson Chungf0ea4d32011-06-06 14:27:16 -0700854 // Setup AppsCustomize
855 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
856 findViewById(R.id.apps_customize_pane);
857 mAppsCustomizeContent = (AppsCustomizePagedView)
858 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
859 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400860
Winson Chungf0ea4d32011-06-06 14:27:16 -0700861 // Setup AppsCustomize button
862 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
863 mAllAppsButton.setOnClickListener(new OnClickListener() {
864 public void onClick(View v) {
865 onClickAllAppsButton(v);
866 }
867 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800868
Winson Chungf0ea4d32011-06-06 14:27:16 -0700869 if (!LauncherApplication.isScreenLarge()) {
870 // Setup AppsCustomize button on the phone
871 HandleView handleView = (HandleView) mAllAppsButton;
872 handleView.setLauncher(this);
873 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700874
875 // Setup Hotseat
876 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
877 hotseatLeft.setContentDescription(mHotseatLabels[0]);
878 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
879 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
880 hotseatRight.setContentDescription(mHotseatLabels[1]);
881 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800882 }
883
Winson Chungf0ea4d32011-06-06 14:27:16 -0700884 if (!LauncherApplication.isScreenLarge()) {
885 // Setup keylistener for button cluster
886 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
887 View.OnKeyListener listener = null;
888 if (LauncherApplication.isScreenLarge()) {
889 // For tablets, AllApps lives in the button bar at the top
890 listener = new ButtonBarKeyEventListener();
891 } else {
892 // For phones, AppsCustomize lives in the "dock" at the bottom
893 listener = new DockKeyEventListener();
894 }
895 int buttonCount = mButtonCluster.getChildCount();
896 for (int i = 0; i < buttonCount; ++i) {
897 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
898 }
Winson Chung97d85d22011-04-13 11:27:36 -0700899 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800900
Winson Chung4c98d922011-05-31 16:50:48 -0700901 // Setup the drag controller (the drop targets have to be added in reverse order)
902 dragController.setDragScoller(mWorkspace);
903 dragController.setScrollView(mDragLayer);
904 dragController.setMoveTarget(mWorkspace);
905 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700906 if (mSearchDeleteBar != null) {
907 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
910
Romain Guy8a73c512009-11-09 19:19:59 -0800911 @SuppressWarnings({"UnusedDeclaration"})
912 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700913 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800914 mWorkspace.scrollLeft();
915 }
Romain Guy8a73c512009-11-09 19:19:59 -0800916 }
917
918 @SuppressWarnings({"UnusedDeclaration"})
919 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700920 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800921 mWorkspace.scrollRight();
922 }
Romain Guy8a73c512009-11-09 19:19:59 -0800923 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400924
925 @SuppressWarnings({"UnusedDeclaration"})
926 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700927 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400928
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400929 int index = -1;
930 if (v.getId() == R.id.hotseat_left) {
931 index = 0;
932 } else if (v.getId() == R.id.hotseat_right) {
933 index = 1;
934 }
935
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400936 // reload these every tap; you never know when they might change
937 loadHotseats();
938 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400939 startActivitySafely(
940 mHotseats[index],
941 "hotseat"
942 );
943 }
944 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 /**
947 * Creates a view representing a shortcut.
948 *
949 * @param info The data structure describing the shortcut.
950 *
951 * @return A View inflated from R.layout.application.
952 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800953 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700955 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957
958 /**
959 * Creates a view representing a shortcut inflated from the specified resource.
960 *
961 * @param layoutResId The id of the XML layout used to create the shortcut.
962 * @param parent The group the shortcut belongs to.
963 * @param info The data structure describing the shortcut.
964 *
965 * @return A View inflated from layoutResId.
966 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800967 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800968 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
969 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 return favorite;
972 }
973
974 /**
975 * Add an application shortcut to the workspace.
976 *
977 * @param data The intent describing the application.
978 * @param cellInfo The position on screen where to create the shortcut.
979 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800980 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700981 int intersectCellX, int intersectCellY) {
982 final int[] cellXY = mTmpAddItemCellCoordinates;
983 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
984
985 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
986 showOutOfSpaceMessage();
987 return;
988 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800990 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800991
Romain Guy73b979d2009-06-09 12:57:21 -0700992 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700996 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
997 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 } else {
999 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001 }
Romain Guycbb89e42009-06-08 15:52:54 -07001002
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 /**
1004 * Add a shortcut to the workspace.
1005 *
1006 * @param data The intent describing the shortcut.
1007 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001009 private void completeAddShortcut(Intent data, int screen,
1010 int intersectCellX, int intersectCellY) {
1011 final int[] cellXY = mTmpAddItemCellCoordinates;
1012 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001013
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001014 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001015 boolean foundCellSpan = false;
1016 if (touchXY != null) {
1017 // when dragging and dropping, just find the closest free spot
1018 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1019 int[] result = screenLayout.findNearestVacantArea(
1020 touchXY[0], touchXY[1], 1, 1, cellXY);
1021 foundCellSpan = (result != null);
1022 } else {
1023 foundCellSpan = layout.findCellForSpanThatIntersects(
1024 cellXY, 1, 1, intersectCellX, intersectCellY);
1025 }
1026
1027 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 showOutOfSpaceMessage();
1029 return;
1030 }
1031
1032 final ShortcutInfo info = mModel.addShortcut(
1033 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034
1035 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001037 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039 }
1040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001042 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001044 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001045 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 */
Winson Chungf7640c82011-02-28 13:47:29 -08001047 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001049
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Adam Cohen09353862011-07-14 16:10:03 -07001052
1053 // We want to account for the extra amount of padding that we are adding to the widget
1054 // to ensure that it gets the full amount of space that it has requested
1055 Resources r = getResources();
1056 int requiredWidth = appWidgetInfo.minWidth +
1057 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1058 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1059 int requiredHeight = appWidgetInfo.minHeight +
1060 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1061 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1062 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001065 // We have saved the position to which the widget was dragged-- this really only matters
1066 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001068
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001069 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 boolean foundCellSpan = false;
1071 if (touchXY != null) {
1072 // when dragging and dropping, just find the closest free spot
1073 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1074 int[] result = screenLayout.findNearestVacantArea(
1075 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001076 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001078 // if we long pressed on an empty cell to bring up a menu,
1079 // make sure we intersect the empty cell
1080 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1081 // findCellForSpanThatIntersects will just ignore them
1082 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1083 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001084 }
1085
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001087 if (appWidgetId != -1) {
1088 // Deleting an app widget ID is a void call but writes to disk before returning
1089 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001090 new Thread("deleteAppWidgetId") {
1091 public void run() {
1092 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1093 }
1094 }.start();
1095 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001097 return;
1098 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 // Build Launcher-specific widget info and save to database
1101 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001102 launcherInfo.spanX = spanXY[0];
1103 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 LauncherModel.addItemToDatabase(this, launcherInfo,
1106 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108
1109 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001113 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001117 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001118
1119 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120 }
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Adam Cohended9f8d2010-11-03 13:25:16 -07001123 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1124 @Override
1125 public void onReceive(Context context, Intent intent) {
1126 final String action = intent.getAction();
1127 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1128 mUserPresent = false;
1129 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001130
1131 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001132 if (mAppsCustomizeContent != null) {
1133 mAppsCustomizeContent.reset();
1134 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1136 mUserPresent = true;
1137 updateRunning();
1138 }
1139 }
1140 };
1141
1142 @Override
1143 public void onAttachedToWindow() {
1144 super.onAttachedToWindow();
1145
1146 // Listen for broadcasts related to user-presence
1147 final IntentFilter filter = new IntentFilter();
1148 filter.addAction(Intent.ACTION_SCREEN_OFF);
1149 filter.addAction(Intent.ACTION_USER_PRESENT);
1150 registerReceiver(mReceiver, filter);
1151
Adam Cohend113e0c2010-11-11 10:48:05 -08001152 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001153 mVisible = true;
1154 }
1155
1156 @Override
1157 public void onDetachedFromWindow() {
1158 super.onDetachedFromWindow();
1159 mVisible = false;
1160
Adam Cohend113e0c2010-11-11 10:48:05 -08001161 if (mAttached) {
1162 unregisterReceiver(mReceiver);
1163 mAttached = false;
1164 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001165 updateRunning();
1166 }
1167
1168 public void onWindowVisibilityChanged(int visibility) {
1169 mVisible = visibility == View.VISIBLE;
1170 updateRunning();
1171 }
1172
1173 private void sendAdvanceMessage(long delay) {
1174 mHandler.removeMessages(ADVANCE_MSG);
1175 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1176 mHandler.sendMessageDelayed(msg, delay);
1177 mAutoAdvanceSentTime = System.currentTimeMillis();
1178 }
1179
1180 private void updateRunning() {
1181 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1182 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1183 mAutoAdvanceRunning = autoAdvanceRunning;
1184 if (autoAdvanceRunning) {
1185 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1186 sendAdvanceMessage(delay);
1187 } else {
1188 if (!mWidgetsToAdvance.isEmpty()) {
1189 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1190 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1191 }
1192 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001193 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 }
1195 }
1196 }
1197
1198 private final Handler mHandler = new Handler() {
1199 @Override
1200 public void handleMessage(Message msg) {
1201 if (msg.what == ADVANCE_MSG) {
1202 int i = 0;
1203 for (View key: mWidgetsToAdvance.keySet()) {
1204 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1205 final int delay = mAdvanceStagger * i;
1206 if (v instanceof Advanceable) {
1207 postDelayed(new Runnable() {
1208 public void run() {
1209 ((Advanceable) v).advance();
1210 }
1211 }, delay);
1212 }
1213 i++;
1214 }
1215 sendAdvanceMessage(mAdvanceInterval);
1216 }
1217 }
1218 };
1219
1220 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1221 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1222 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1223 if (v instanceof Advanceable) {
1224 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001225 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001226 updateRunning();
1227 }
1228 }
1229
1230 void removeWidgetToAutoAdvance(View hostView) {
1231 if (mWidgetsToAdvance.containsKey(hostView)) {
1232 mWidgetsToAdvance.remove(hostView);
1233 updateRunning();
1234 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001235 }
1236
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 launcherInfo.hostView = null;
1240 }
1241
Adam Cohended9f8d2010-11-03 13:25:16 -07001242 void showOutOfSpaceMessage() {
1243 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1244 }
1245
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246 public LauncherAppWidgetHost getAppWidgetHost() {
1247 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Winson Chunga9abd0e2010-10-27 17:18:37 -07001250 public LauncherModel getModel() {
1251 return mModel;
1252 }
1253
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001254 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001255 getWindow().closeAllPanels();
1256
1257 try {
1258 dismissDialog(DIALOG_CREATE_SHORTCUT);
1259 // Unlock the workspace if the dialog was showing
1260 } catch (Exception e) {
1261 // An exception is thrown if the dialog is not visible, which is fine
1262 }
1263
1264 try {
1265 dismissDialog(DIALOG_RENAME_FOLDER);
1266 // Unlock the workspace if the dialog was showing
1267 } catch (Exception e) {
1268 // An exception is thrown if the dialog is not visible, which is fine
1269 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001270
1271 // Whatever we were doing is hereby canceled.
1272 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001273 }
1274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 @Override
1276 protected void onNewIntent(Intent intent) {
1277 super.onNewIntent(intent);
1278
1279 // Close the menu
1280 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001281 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001282 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001283
Adam Cohen95bb8002011-07-03 23:40:28 -07001284 closeFolder();
1285
Joe Onorato14f122b2009-11-19 14:06:36 -08001286 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1287 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001288
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001289 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001290 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001291 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001292 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001293 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001294 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001295 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001296
Joe Onorato3a8820b2009-11-10 15:06:42 -08001297 final View v = getWindow().peekDecorView();
1298 if (v != null && v.getWindowToken() != null) {
1299 InputMethodManager imm = (InputMethodManager)getSystemService(
1300 INPUT_METHOD_SERVICE);
1301 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001303
Michael Jurka35aa14d2011-07-07 17:01:08 -07001304 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 if (mAppsCustomizeContent != null) {
1306 mAppsCustomizeContent.reset();
1307 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
1309 }
1310
1311 @Override
1312 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1313 // Do not call super here
1314 mSavedInstanceState = savedInstanceState;
1315 }
1316
1317 @Override
1318 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001319 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001320 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321
Michael Jurka883f55b2010-10-21 15:47:14 -07001322 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001323 // We close any open folder since it will not be re-opened, and we need to make sure
1324 // this state is reflected.
1325 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
Michael Jurka0280c3b2010-09-17 15:00:07 -07001327 if (mAddScreen > -1 && mWaitingForResult) {
1328 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1329 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1330 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 if (mFolderInfo != null && mWaitingForResult) {
1334 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1335 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1336 }
Michael Jurka946ad472010-07-09 18:05:18 -07001337
Winson Chung785d2eb2011-04-14 16:08:02 -07001338 // Save the current AppsCustomize tab
1339 if (mAppsCustomizeTabHost != null) {
1340 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1341 if (currentTabTag != null) {
1342 outState.putString("apps_customize_currentTab", currentTabTag);
1343 }
1344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001345 }
1346
1347 @Override
1348 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001350
Winson Chungcd2b0142011-06-08 16:02:26 -07001351 // Stop callbacks from LauncherModel
1352 LauncherApplication app = ((LauncherApplication) getApplication());
1353 mModel.stopLoader();
1354 app.setLauncher(null);
1355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001357 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001359 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001361 mAppWidgetHost = null;
1362
1363 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364
1365 TextKeyListener.getInstance().release();
1366
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Adam Cohen4eac29a2011-07-11 17:53:37 -07001368 unbindWorkspaceItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001369
1370 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001371 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001372
1373 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1374 mWorkspace.removeAllViews();
1375 mWorkspace = null;
1376 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001377
1378 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380
Adam Cohena9cf38f2011-05-02 15:36:58 -07001381 public DragController getDragController() {
1382 return mDragController;
1383 }
1384
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 @Override
1386 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001387 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 super.startActivityForResult(intent, requestCode);
1389 }
1390
1391 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001392 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001394
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001395 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001396
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 if (initialQuery == null) {
1398 // Use any text typed in the launcher as the initial query
1399 initialQuery = getTypedText();
1400 clearTypedText();
1401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 if (appSearchData == null) {
1403 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001404 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
Romain Guycbb89e42009-06-08 15:52:54 -07001406
Karl Rosaen138a0412009-04-23 19:00:21 -07001407 final SearchManager searchManager =
1408 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001409 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001410 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 }
1412
1413 @Override
1414 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001415 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001416 return false;
1417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
1419 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001420
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1422 .setIcon(android.R.drawable.ic_menu_add)
1423 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001424 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1425 .setIcon(android.R.drawable.ic_menu_manage)
1426 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001427 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 .setIcon(android.R.drawable.ic_menu_gallery)
1429 .setAlphabeticShortcut('W');
1430 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1431 .setIcon(android.R.drawable.ic_search_category_default)
1432 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1433 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1434 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1435 .setAlphabeticShortcut('N');
1436
1437 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001438 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1439 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440
1441 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1442 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1443 .setIntent(settings);
1444
1445 return true;
1446 }
1447
1448 @Override
1449 public boolean onPrepareOptionsMenu(Menu menu) {
1450 super.onPrepareOptionsMenu(menu);
1451
Winson Chung785d2eb2011-04-14 16:08:02 -07001452 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1453 // related code?
1454 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455
1456 return true;
1457 }
1458
1459 @Override
1460 public boolean onOptionsItemSelected(MenuItem item) {
1461 switch (item.getItemId()) {
1462 case MENU_ADD:
1463 addItems();
1464 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001465 case MENU_MANAGE_APPS:
1466 manageApps();
1467 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 case MENU_WALLPAPER_SETTINGS:
1469 startWallpaper();
1470 return true;
1471 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001472 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 return true;
1474 case MENU_NOTIFICATIONS:
1475 showNotifications();
1476 return true;
1477 }
1478
1479 return super.onOptionsItemSelected(item);
1480 }
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Karl Rosaen138a0412009-04-23 19:00:21 -07001482 /**
1483 * Indicates that we want global search for this activity by setting the globalSearch
1484 * argument for {@link #startSearch} to true.
1485 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001487 @Override
1488 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001489 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001490 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001491 }
1492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 public boolean isWorkspaceLocked() {
1494 return mWorkspaceLoading || mWaitingForResult;
1495 }
1496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001498 showWorkspace(true);
1499 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 private void resetAddInfo() {
1503 mAddScreen = -1;
1504 mAddIntersectCellX = -1;
1505 mAddIntersectCellY = -1;
1506 mAddDropPosition = null;
1507 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001508
Winson Chung55cef262010-10-28 14:14:18 -07001509 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 resetAddInfo();
1511 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001512 mAddDropPosition = position;
1513
Michael Jurkaaf442092010-06-10 17:01:57 -07001514 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001515 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1516 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 }
1518
Winson Chung7ad01412010-09-27 11:33:03 -07001519 private void manageApps() {
1520 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1521 }
1522
Michael Jurkaaf442092010-06-10 17:01:57 -07001523 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001524 // TODO: catch bad widget exception when sent
1525 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001526 // TODO: Is this log message meaningful?
1527 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001528 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001529 }
1530
Winson Chung55cef262010-10-28 14:14:18 -07001531 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001532 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
Bjorn Bringert7984c942009-12-09 15:38:25 +00001534 if (appWidget.configure != null) {
1535 // Launch over to configure widget, if needed
1536 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1537 intent.setComponent(appWidget.configure);
1538 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001539 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001540 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1541 intent.putExtra(
1542 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1543 info.mimeType);
1544
1545 final String mimeType = info.mimeType;
1546 final ClipData clipData = (ClipData) info.configurationData;
1547 final ClipDescription clipDesc = clipData.getDescription();
1548 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1549 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001550 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001551 final CharSequence stringData = item.getText();
1552 final Uri uriData = item.getUri();
1553 final Intent intentData = item.getIntent();
1554 final String key =
1555 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1556 if (uriData != null) {
1557 intent.putExtra(key, uriData);
1558 } else if (intentData != null) {
1559 intent.putExtra(key, intentData);
1560 } else if (stringData != null) {
1561 intent.putExtra(key, stringData);
1562 }
1563 break;
1564 }
1565 }
1566 }
Winson Chung55cef262010-10-28 14:14:18 -07001567 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001568
Romain Guy8e633c52010-03-04 12:51:36 -08001569 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001571 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001573
1574 // Exit spring loaded mode if necessary after adding the widget
1575 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 }
1577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Michael Jurka0280c3b2010-09-17 15:00:07 -07001579 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1580 resetAddInfo();
1581 mAddScreen = screen;
1582 mAddDropPosition = position;
1583
1584 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1585 createShortcutIntent.setComponent(componentName);
1586 processShortcut(createShortcutIntent);
1587 }
1588
Joe Onoratodeb98af2010-02-19 14:59:39 -08001589 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001590 // Handle case where user selected "Applications"
1591 String applicationName = getResources().getString(R.string.group_applications);
1592 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001593
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001594 if (applicationName != null && applicationName.equals(shortcutName)) {
1595 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1596 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001597
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001598 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1599 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001600 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001601 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001602 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001603 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 }
1606
Winson Chung24ab2f12010-09-16 14:10:47 -07001607 void processWallpaper(Intent intent) {
1608 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1609 }
1610
Adam Cohend0445262011-07-04 23:53:22 -07001611 FolderIcon addFolder(final int screen, int intersectCellX, int intersectCellY) {
1612 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 folderInfo.title = getText(R.string.folder_name);
1614
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1616 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohend0445262011-07-04 23:53:22 -07001617 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1,
1618 intersectCellX, intersectCellY)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001619 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001620 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001621 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
1623 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001624 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001625 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001626 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001627 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628
1629 // Create the view
1630 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001631 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1632 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001633 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001634 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001638 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001639 }
1640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 private void showNotifications() {
1642 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1643 if (statusBar != null) {
1644 statusBar.expand();
1645 }
1646 }
1647
1648 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001649 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001651 Intent chooser = Intent.createChooser(pickWallpaper,
1652 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001653 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1654 // Removed in Eclair MR1
1655// WallpaperManager wm = (WallpaperManager)
1656// getSystemService(Context.WALLPAPER_SERVICE);
1657// WallpaperInfo wi = wm.getWallpaperInfo();
1658// if (wi != null && wi.getSettingsActivity() != null) {
1659// LabeledIntent li = new LabeledIntent(getPackageName(),
1660// R.string.configure_wallpaper, 0);
1661// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1662// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1663// }
Mike Clerona0618e42009-10-22 13:55:21 -07001664 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 }
1666
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001667 /**
1668 * Registers various content observers. The current implementation registers
1669 * only a favorites observer to keep track of the favorites applications.
1670 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001671 private void registerContentObservers() {
1672 ContentResolver resolver = getContentResolver();
1673 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1674 true, mWidgetObserver);
1675 }
1676
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 @Override
1678 public boolean dispatchKeyEvent(KeyEvent event) {
1679 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1680 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001682 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001683 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001684 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001685 dumpState();
1686 return true;
1687 }
1688 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001689 }
1690 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1691 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001692 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 return true;
1694 }
1695 }
1696
1697 return super.dispatchKeyEvent(event);
1698 }
1699
Joe Onorato88ec0992009-11-19 13:16:06 -08001700 @Override
1701 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001702 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001703 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001704 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001705 Folder openFolder = mWorkspace.getOpenFolder();
1706 if (openFolder.isEditingName()) {
1707 openFolder.dismissEditingName();
1708 } else {
1709 closeFolder();
1710 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001711 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001712 mWorkspace.exitWidgetResizeMode();
1713
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001714 // Back button is a no-op here, but give at least some feedback for the button press
1715 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001716 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001717 }
1718
Adam Cohen2801caf2011-05-13 20:57:39 -07001719 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 Folder folder = mWorkspace.getOpenFolder();
1721 if (folder != null) {
1722 closeFolder(folder);
1723 }
1724 }
1725
1726 void closeFolder(Folder folder) {
1727 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001728
Michael Jurka8c920dd2011-01-20 14:16:56 -08001729 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001731 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001732 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1733 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001734 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001736 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738
1739 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001740 * Re-listen when widgets are reset.
1741 */
1742 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001743 if (mAppWidgetHost != null) {
1744 mAppWidgetHost.startListening();
1745 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001746 }
1747
1748 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001749 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1750 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 */
Adam Cohen4eac29a2011-07-11 17:53:37 -07001752 private void unbindWorkspaceItems() {
1753 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 /**
1757 * Launches the intent referred by the clicked shortcut.
1758 *
1759 * @param v The view representing the clicked shortcut.
1760 */
1761 public void onClick(View v) {
1762 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001763 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001765 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001766 int[] pos = new int[2];
1767 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001768 intent.setSourceBounds(new Rect(pos[0], pos[1],
1769 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001770 boolean success = startActivitySafely(intent, tag);
1771
1772 if (success && v instanceof BubbleTextView) {
1773 mWaitingForResume = (BubbleTextView) v;
1774 mWaitingForResume.setStayPressed(true);
1775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001777 if (v instanceof FolderIcon) {
1778 FolderIcon fi = (FolderIcon) v;
1779 handleFolderClick(fi);
1780 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001781 } else if (v == mAllAppsButton) {
1782 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001783 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001784 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001785 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788 }
1789
Michael Jurka0e260592010-06-30 17:07:39 -07001790 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001791 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001792 // clicking anywhere on the workspace causes the customization drawer to slide down
1793 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001794 return false;
1795 }
1796
Michael Jurkaaf442092010-06-10 17:01:57 -07001797 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001798 * Event handler for the search button
1799 *
1800 * @param v The view that was clicked.
1801 */
1802 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001803 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001804 // Use a custom animation for launching search
1805 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001806 }
1807
1808 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001809 * Event handler for the voice button
1810 *
1811 * @param v The view that was clicked.
1812 */
1813 public void onClickVoiceButton(View v) {
1814 startVoiceSearch();
1815 }
1816
1817 private void startVoiceSearch() {
1818 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1819 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1820 startActivity(intent);
1821 }
1822
1823 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001824 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001825 * enters home screen customization mode.
1826 *
1827 * @param v The view that was clicked.
1828 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001829 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001830 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001831 }
1832
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001833 /**
1834 * Event handler for the "grid" button that appears on the home screen, which
1835 * enters all apps mode.
1836 *
1837 * @param v The view that was clicked.
1838 */
1839 public void onClickAllAppsButton(View v) {
1840 showAllApps(true);
1841 }
1842
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001843 public void onClickAppMarketButton(View v) {
1844 if (mAppMarketIntent != null) {
1845 startActivitySafely(mAppMarketIntent, "app market");
1846 }
1847 }
1848
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001849 void startApplicationDetailsActivity(ComponentName componentName) {
1850 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001851 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1852 Uri.fromParts("package", packageName, null));
1853 startActivity(intent);
1854 }
1855
Patrick Dubroy5539af72010-09-07 15:22:01 -07001856 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1857 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1858 // System applications cannot be installed. For now, show a toast explaining that.
1859 // We may give them the option of disabling apps this way.
1860 int messageId = R.string.uninstall_system_app_text;
1861 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1862 } else {
1863 String packageName = appInfo.componentName.getPackageName();
1864 String className = appInfo.componentName.getClassName();
1865 Intent intent = new Intent(
1866 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1867 startActivity(intent);
1868 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001869 }
1870
Michael Jurkaddd62e92011-02-16 17:49:14 -08001871 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1873 try {
1874 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001875 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 } catch (ActivityNotFoundException e) {
1877 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001878 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 } catch (SecurityException e) {
1880 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001881 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001883 "or use the exported attribute for this activity. "
1884 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001886 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001888
Romain Guy8e633c52010-03-04 12:51:36 -08001889 void startActivityForResultSafely(Intent intent, int requestCode) {
1890 try {
1891 startActivityForResult(intent, requestCode);
1892 } catch (ActivityNotFoundException e) {
1893 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1894 } catch (SecurityException e) {
1895 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1896 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1897 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1898 "or use the exported attribute for this activity.", e);
1899 }
1900 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901
Adam Cohena9cf38f2011-05-02 15:36:58 -07001902 private void handleFolderClick(FolderIcon folderIcon) {
1903 final FolderInfo info = folderIcon.mInfo;
1904 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 // Close any open folder
1906 closeFolder();
1907 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001908 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 } else {
1910 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001911 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 int folderScreen;
1913 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001914 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 // .. and close it
1916 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001917 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 // Close any folder open on the current screen
1919 closeFolder();
1920 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001921 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001922 }
1923 }
1924 }
1925 }
1926
Adam Cohen2801caf2011-05-13 20:57:39 -07001927 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1928 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1929 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1930 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1931
1932 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1933 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1934 oa.start();
1935 }
1936
1937 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1938 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1939 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1940 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1941
1942 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1943 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1944 oa.start();
1945 }
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001948 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 * is animated relative to the specified View. If the View is null, no animation
1950 * is played.
1951 *
1952 * @param folderInfo The FolderInfo describing the folder to open.
1953 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001954 public void openFolder(FolderIcon folderIcon) {
1955 Folder folder = folderIcon.mFolder;
1956 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957
Adam Cohen2801caf2011-05-13 20:57:39 -07001958 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001959 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohen8e776a62011-06-28 18:10:06 -07001961 mDragLayer.addView(folder);
1962 mDragController.addDropTarget((DropTarget) folder);
1963
Adam Cohena9cf38f2011-05-02 15:36:58 -07001964 folder.animateOpen();
1965 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
1967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001969 if (mState != State.WORKSPACE) {
1970 return false;
1971 }
1972
Romain Guy1fbc1c82009-11-09 20:43:08 -08001973 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001974 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001975 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001976 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1977 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1978 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001979 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001980 return true;
1981 }
1982
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 return false;
1985 }
1986
1987 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001988 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990
Michael Jurka0280c3b2010-09-17 15:00:07 -07001991 resetAddInfo();
1992 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001994 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 return true;
1996 }
1997
Michael Jurka0280c3b2010-09-17 15:00:07 -07001998 final View itemUnderLongClick = longClickCellInfo.cell;
1999
Winson Chung304dcde2011-01-07 11:17:23 -08002000 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002001 if (itemUnderLongClick == null) {
2002 // User long pressed on empty space
2003 mWorkspace.setAllowLongPress(false);
2004 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2005 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002006 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002008 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002010 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2011 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002012 mAddIntersectCellX = longClickCellInfo.cellX;
2013 mAddIntersectCellY = longClickCellInfo.cellY;
2014 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016 }
2017 }
2018 return true;
2019 }
2020
Romain Guye6b8e2f2009-11-10 11:56:55 -08002021 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002022 private void dismissPreview(final View v) {
2023 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002024 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002025 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2026 public void onDismiss() {
2027 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2028 int count = group.getChildCount();
2029 for (int i = 0; i < count; i++) {
2030 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2031 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002032 ArrayList<Bitmap> bitmaps =
2033 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002034 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2035
2036 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002037 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002038 window.setOnDismissListener(null);
2039 }
2040 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002041 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002042 }
2043 v.setTag(null);
2044 }
2045
Romain Guyf8e6a802009-12-07 17:48:02 -08002046 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002047 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002048 }
2049
Romain Guya6abce82009-11-10 02:54:41 -08002050 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002051 final Resources resources = getResources();
2052 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002053
Romain Guya6abce82009-11-10 02:54:41 -08002054 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002055
Romain Guy9d31e9f2009-11-11 18:54:28 -08002056 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002057
Romain Guyf8e6a802009-12-07 17:48:02 -08002058 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002060 int extraW = (int) ((r.left + r.right) * max);
2061 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002062
2063 int aW = cell.getWidth() - extraW;
2064 float w = aW / max;
2065
2066 int width = cell.getWidth();
2067 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002068 int x = cell.getPaddingLeft();
2069 int y = cell.getPaddingTop();
2070 width -= (x + cell.getPaddingRight());
2071 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002072
2073 float scale = w / width;
2074
2075 int count = end - start;
2076
2077 final float sWidth = width * scale;
2078 float sHeight = height * scale;
2079
Romain Guye6b8e2f2009-11-10 11:56:55 -08002080 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002081
Romain Guye6b8e2f2009-11-10 11:56:55 -08002082 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2083 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002084
2085 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002086 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002087 cell = (CellLayout) workspace.getChildAt(i);
2088
Romain Guyf8e6a802009-12-07 17:48:02 -08002089 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002090 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002091
2092 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002093 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002094 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002095 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002096
Romain Guy9d31e9f2009-11-11 18:54:28 -08002097 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002098 image.setImageBitmap(bitmap);
2099 image.setTag(i);
2100 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002101 image.setOnFocusChangeListener(handler);
2102 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002103 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002104
2105 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002106 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2107
Winson Chungaafa03c2010-06-11 17:34:16 -07002108 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002109 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002110
2111 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002112 p.setContentView(preview);
2113 p.setWidth((int) (sWidth * count + extraW));
2114 p.setHeight((int) (sHeight + extraH));
2115 p.setAnimationStyle(R.style.AnimationPreview);
2116 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002117 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002118 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002119 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002120
Romain Guye6b8e2f2009-11-10 11:56:55 -08002121 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2122 public void onDismiss() {
2123 dismissPreview(anchor);
2124 }
2125 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002126
2127 anchor.setTag(p);
2128 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002129 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002130 }
2131
Romain Guy9d31e9f2009-11-11 18:54:28 -08002132 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002133 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002134
Romain Guye6b8e2f2009-11-10 11:56:55 -08002135 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002136 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002137 }
2138
2139 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002140 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002141 v.post(this);
2142 }
2143
2144 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002145 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002146 }
2147
2148 public void onFocusChange(View v, boolean hasFocus) {
2149 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002150 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002151 }
Romain Guya6abce82009-11-10 02:54:41 -08002152 }
2153 }
2154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 Workspace getWorkspace() {
2156 return mWorkspace;
2157 }
2158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 @Override
2160 protected Dialog onCreateDialog(int id) {
2161 switch (id) {
2162 case DIALOG_CREATE_SHORTCUT:
2163 return new CreateShortcut().createDialog();
2164 case DIALOG_RENAME_FOLDER:
2165 return new RenameFolder().createDialog();
2166 }
2167
2168 return super.onCreateDialog(id);
2169 }
2170
2171 @Override
2172 protected void onPrepareDialog(int id, Dialog dialog) {
2173 switch (id) {
2174 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 break;
2176 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002177 if (mFolderInfo != null) {
2178 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2179 final CharSequence text = mFolderInfo.title;
2180 input.setText(text);
2181 input.setSelection(0, text.length());
2182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 break;
2184 }
2185 }
2186
2187 void showRenameDialog(FolderInfo info) {
2188 mFolderInfo = info;
2189 mWaitingForResult = true;
2190 showDialog(DIALOG_RENAME_FOLDER);
2191 }
2192
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 private void showAddDialog(int intersectX, int intersectY) {
2194 resetAddInfo();
2195 mAddIntersectCellX = intersectX;
2196 mAddIntersectCellY = intersectY;
2197 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 mWaitingForResult = true;
2199 showDialog(DIALOG_CREATE_SHORTCUT);
2200 }
2201
Joe Onoratodeb98af2010-02-19 14:59:39 -08002202 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002203 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002204 Bundle bundle = new Bundle();
2205
2206 ArrayList<String> shortcutNames = new ArrayList<String>();
2207 shortcutNames.add(getString(R.string.group_applications));
2208 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2209
2210 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2211 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2212 R.drawable.ic_launcher_application));
2213 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2214
2215 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2216 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002217 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002218 pickIntent.putExtras(bundle);
2219
Joe Onoratodeb98af2010-02-19 14:59:39 -08002220 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002221 }
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 private class RenameFolder {
2224 private EditText mInput;
2225
2226 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2228 mInput = (EditText) layout.findViewById(R.id.folder_name);
2229
2230 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2231 builder.setIcon(0);
2232 builder.setTitle(getString(R.string.rename_folder_title));
2233 builder.setCancelable(true);
2234 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2235 public void onCancel(DialogInterface dialog) {
2236 cleanup();
2237 }
2238 });
2239 builder.setNegativeButton(getString(R.string.cancel_action),
2240 new Dialog.OnClickListener() {
2241 public void onClick(DialogInterface dialog, int which) {
2242 cleanup();
2243 }
2244 }
2245 );
2246 builder.setPositiveButton(getString(R.string.rename_action),
2247 new Dialog.OnClickListener() {
2248 public void onClick(DialogInterface dialog, int which) {
2249 changeFolderName();
2250 }
2251 }
2252 );
2253 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002254
2255 final AlertDialog dialog = builder.create();
2256 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2257 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002258 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002259 mInput.requestFocus();
2260 InputMethodManager inputManager = (InputMethodManager)
2261 getSystemService(Context.INPUT_METHOD_SERVICE);
2262 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002263 }
2264 });
2265
2266 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 }
2268
2269 private void changeFolderName() {
2270 final String name = mInput.getText().toString();
2271 if (!TextUtils.isEmpty(name)) {
2272 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002273 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 mFolderInfo.title = name;
2275 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2276
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002278 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 } else {
2281 final FolderIcon folderIcon = (FolderIcon)
2282 mWorkspace.getViewForTag(mFolderInfo);
2283 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002284 // TODO: At some point we'll probably want some version of setting
2285 // the text for a folder icon.
2286 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 getWorkspace().requestLayout();
2288 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002289 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002290 mWorkspaceLoading = true;
2291 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 }
2293 }
2294 }
2295 cleanup();
2296 }
2297
2298 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 dismissDialog(DIALOG_RENAME_FOLDER);
2300 mWaitingForResult = false;
2301 mFolderInfo = null;
2302 }
2303 }
2304
Daniel Sandler843e8602010-06-07 14:59:01 -04002305 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2306 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002307 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002308 }
2309
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002310 // AllAppsView.Watcher
2311 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002312 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002313 mWorkspace.setVisibility(View.GONE);
2314 }
2315 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002316
2317 /**
2318 * Helper method for the cameraZoomIn/cameraZoomOut animations
2319 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002320 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002321 * @param scaleFactor The scale factor used for the zoom
2322 */
2323 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2324 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002325
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002326 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002327 // Set pivotY so that at the starting zoom factor, the view is partially
2328 // visible. Modifying initialHeightFactor changes how much of the view is
2329 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002331 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002332 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002333 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002334 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002335 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002336 }
2337 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002338
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 /**
2340 * Zoom the camera out from the workspace to reveal 'toView'.
2341 * Assumes that the view to show is anchored at either the very top or very bottom
2342 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002343 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002344 */
Winson Chungc07918d2011-07-01 15:35:26 -07002345 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002346 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002347
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2349 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2350 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2351 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002352
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 setPivotsForZoom(toView, toState, scale);
2354
Michael Jurkad74c9842011-07-10 12:44:21 -07002355 // Shrink workspaces away if going to AppsCustomize from workspace
2356 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002357
2358 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002359 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002360 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002361 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2362 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002363 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002364 toView.setFastScaleX(a * scale + b * 1f);
2365 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002366 }
2367 });
Adam Cohen61033d32010-11-15 18:29:44 -08002368
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 toView.setVisibility(View.VISIBLE);
2370 toView.setFastAlpha(0f);
2371 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2372 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2373 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2374 public void onAnimationUpdate(float a, float b) {
2375 // don't need to invalidate because we do so above
2376 toView.setFastAlpha(a * 0f + b * 1f);
2377 }
2378 });
2379 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002380
Michael Jurkaabded662011-03-04 12:06:57 -08002381 if (toView instanceof LauncherTransitionable) {
2382 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002383 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002384 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002385 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002386 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002387 // Prepare the position
2388 toView.setTranslationX(0.0f);
2389 toView.setTranslationY(0.0f);
2390 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002391 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002392
Winson Chungc07918d2011-07-01 15:35:26 -07002393 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2394 // Hide the workspace scrollbar
2395 mWorkspace.hideScrollingIndicator(true);
2396 mWorkspace.hideScrollIndicatorTrack();
2397 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002399 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002400 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002401 // If we don't set the final scale values here, if this animation is cancelled
2402 // it will have the wrong scale value and subsequent cameraPan animations will
2403 // not fix that
2404 toView.setScaleX(1.0f);
2405 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002406 if (toView instanceof LauncherTransitionable) {
2407 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002408 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002409 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410 });
2411
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002412 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002413 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002414
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002415 if (mStateAnimation != null) mStateAnimation.cancel();
2416 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002417 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002418 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419 } else {
2420 toView.setTranslationX(0.0f);
2421 toView.setTranslationY(0.0f);
2422 toView.setScaleX(1.0f);
2423 toView.setScaleY(1.0f);
2424 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002425 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002426 if (toView instanceof LauncherTransitionable) {
2427 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2428 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002429
Winson Chungc07918d2011-07-01 15:35:26 -07002430 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2431 // Hide the workspace scrollbar
2432 mWorkspace.hideScrollingIndicator(true);
2433 mWorkspace.hideScrollIndicatorTrack();
2434 }
Michael Jurkaabded662011-03-04 12:06:57 -08002435 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002436 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002437 }
2438
2439 /**
2440 * Zoom the camera back into the workspace, hiding 'fromView'.
2441 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002442 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002443 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002444 */
Winson Chungc07918d2011-07-01 15:35:26 -07002445 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002446 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002447
Winson Chungf0ea4d32011-06-06 14:27:16 -07002448 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2449 final float scaleFactor = (float)
2450 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2451 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002452
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002453 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002454
Michael Jurkad3ef3062010-11-23 16:23:58 -08002455 if (!springLoaded) {
2456 mWorkspace.unshrink(animated);
2457 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002458 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002459 if (mStateAnimation != null) mStateAnimation.cancel();
2460 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002461
Michael Jurka742574b2011-02-02 23:51:01 -08002462 final float oldScaleX = fromView.getScaleX();
2463 final float oldScaleY = fromView.getScaleY();
2464
2465 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2466 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002467 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2468 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002469 ((View)fromView.getParent()).fastInvalidate();
2470 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2471 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2472 }
2473 });
Michael Jurkaabded662011-03-04 12:06:57 -08002474 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002475 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002476 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002477 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2478 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002479 // don't need to invalidate because we do so above
2480 fromView.setFastAlpha(a * 1f + b * 0f);
2481 }
2482 });
Michael Jurkaabded662011-03-04 12:06:57 -08002483 if (fromView instanceof LauncherTransitionable) {
2484 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002485 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002486 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002487 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002488 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002489 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002490 if (fromView instanceof LauncherTransitionable) {
2491 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002492
Winson Chungc07918d2011-07-01 15:35:26 -07002493 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2494 // Show the workspace scrollbar
2495 mWorkspace.showScrollIndicatorTrack();
2496 mWorkspace.flashScrollingIndicator();
2497 }
Michael Jurka2763be32011-02-24 11:19:57 -08002498 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 }
2500 });
2501
Winson Chungf0ea4d32011-06-06 14:27:16 -07002502 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002503 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504 } else {
2505 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002506 if (fromView instanceof LauncherTransitionable) {
2507 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2508 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002509
Winson Chungc07918d2011-07-01 15:35:26 -07002510 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2511 // Show the workspace scrollbar
2512 mWorkspace.showScrollIndicatorTrack();
2513 mWorkspace.flashScrollingIndicator();
2514 }
Michael Jurkaabded662011-03-04 12:06:57 -08002515 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002516 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002517 }
2518
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002519 void showWorkspace(boolean animated) {
2520 showWorkspace(animated, null);
2521 }
2522
2523 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002524 if (layout != null) {
2525 // always animated, but that's ok since we never specify a layout and
2526 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002527 mWorkspace.unshrink(layout);
2528 } else {
2529 mWorkspace.unshrink(animated);
2530 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002531 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002533 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002534
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002535 // Change the state *after* we've called all the transition code
2536 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002537
Winson Chung5fb63472011-02-02 17:03:37 -08002538 // Resume the auto-advance of widgets
2539 mUserPresent = true;
2540 updateRunning();
2541
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002542 // send an accessibility event to announce the context change
2543 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002544 }
2545
Michael Jurkad3ef3062010-11-23 16:23:58 -08002546 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002547 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002548 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002549 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002550 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002551 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002553 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002554 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2555 mWorkspace.postDelayed(new Runnable() {
2556 @Override
2557 public void run() {
2558 exitSpringLoadedDragMode();
2559 }
2560 }, (extendedDelay ?
2561 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2562 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2563 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002564 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002565 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002566 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002567 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2568 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002569 }
2570 // Otherwise, we are not in spring loaded mode, so don't do anything.
2571 }
2572
2573 /**
2574 * Shows the dock/hotseat area.
2575 */
2576 void showDock(boolean animated) {
2577 if (!LauncherApplication.isScreenLarge()) {
2578 if (animated) {
2579 int duration = mSearchDeleteBar.getTransitionInDuration();
2580 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002581 } else {
2582 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002583 }
2584 }
2585 }
2586
2587 /**
2588 * Hides the dock/hotseat area.
2589 */
2590 void hideDock(boolean animated) {
2591 if (!LauncherApplication.isScreenLarge()) {
2592 if (animated) {
2593 int duration = mSearchDeleteBar.getTransitionOutDuration();
2594 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002595 } else {
2596 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002597 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002598 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002599 }
2600
Winson Chung785d2eb2011-04-14 16:08:02 -07002601 void showAllApps(boolean animated) {
2602 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002603
Winson Chungf0ea4d32011-06-06 14:27:16 -07002604 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2605 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002606
Winson Chungf0ea4d32011-06-06 14:27:16 -07002607 // Hide the search bar and dock
2608 mSearchDeleteBar.hideSearchBar(animated);
2609 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002610
Winson Chungf0ea4d32011-06-06 14:27:16 -07002611 // Change the state *after* we've called all the transition code
2612 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002613
2614 // Pause the auto-advance of widgets until we are out of AllApps
2615 mUserPresent = false;
2616 updateRunning();
2617
2618 // Send an accessibility event to announce the context change
2619 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2620 }
2621
Joe Onoratob2061212009-11-24 16:13:54 -05002622 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002623 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002624 * - Home from workspace
2625 * - from center screen
2626 * - from other screens
2627 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002628 * - from center screen
2629 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002630 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002631 * - from center screen
2632 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002633 * - Launch app from workspace and quit
2634 * - with back
2635 * - with home
2636 * - Launch app from all apps and quit
2637 * - with back
2638 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002639 * - Go to a screen that's not the default, then all
2640 * apps, and launch and app, and go back
2641 * - with back
2642 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002643 * - On workspace, long press power and go back
2644 * - with back
2645 * - with home
2646 * - On all apps, long press power and go back
2647 * - with back
2648 * - with home
2649 * - On workspace, power off
2650 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002651 * - Launch an app and turn off the screen while in that app
2652 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002653 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002654 * - From all apps
2655 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002656 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2657 * - From all apps
2658 * - From the center workspace
2659 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002660 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002661 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002662 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2663 mWorkspace.setVisibility(View.VISIBLE);
2664 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002665
Winson Chungf0ea4d32011-06-06 14:27:16 -07002666 // Show the search bar and dock
2667 mSearchDeleteBar.showSearchBar(animated);
2668 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002669
Winson Chungf0ea4d32011-06-06 14:27:16 -07002670 // Set focus to the AppsCustomize button
2671 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002672 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002673 }
2674
Joe Onorato7c312c12009-08-13 21:36:53 -07002675 void lockAllApps() {
2676 // TODO
2677 }
2678
2679 void unlockAllApps() {
2680 // TODO
2681 }
2682
Patrick Dubroy5f445422011-02-18 14:35:21 -08002683 /**
2684 * Add an item from all apps or customize onto the given workspace screen.
2685 * If layout is null, add to the current screen.
2686 */
2687 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002688 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2689 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002690 } else {
2691 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002692 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002693 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002694
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002695 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002696 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002697 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002698 // Look for the toolbar icon specified in the activity meta-data
2699 Bundle metaData = packageManager.getActivityInfo(
2700 activityName, PackageManager.GET_META_DATA).metaData;
2701 if (metaData != null) {
2702 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2703 if (iconResId != 0) {
2704 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002705 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002706 }
2707 }
2708 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002709 // This can happen if the activity defines an invalid drawable
2710 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2711 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002712 } catch (Resources.NotFoundException nfe) {
2713 // This can happen if the activity defines an invalid drawable
2714 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2715 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002716 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002717 return null;
2718 }
2719
2720 // if successful in getting icon, return it; otherwise, set button to use default drawable
2721 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2722 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2723 TextView button = (TextView) findViewById(buttonId);
2724 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2725
2726 // If we were unable to find the icon via the meta-data, use a generic one
2727 if (toolbarIcon == null) {
2728 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2729 return null;
2730 } else {
2731 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2732 return toolbarIcon.getConstantState();
2733 }
2734 }
2735
2736 // if successful in getting icon, return it; otherwise, set button to use default drawable
2737 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2738 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2739 ImageView button = (ImageView) findViewById(buttonId);
2740 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2741
Michael Jurka0423dcf2010-10-05 14:56:18 -07002742 // If we were unable to find the icon via the meta-data, use a generic one
2743 if (toolbarIcon == null) {
2744 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002745 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002746 } else {
2747 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002748 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002749 }
2750 }
2751
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002752 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2753 TextView button = (TextView) findViewById(buttonId);
2754 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2755 }
2756
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002757 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002758 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002759 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002760 }
2761
Michael Jurka0423dcf2010-10-05 14:56:18 -07002762 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002763 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2764 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002765
Winson Chung1cad91e2011-05-25 17:41:01 -07002766 final SearchManager searchManager =
2767 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2768 ComponentName activityName = searchManager.getGlobalSearchActivity();
2769 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002770 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002771 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002772 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002773 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002774 } else {
2775 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002776 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002777 }
2778 }
2779
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002780 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002781 updateButtonWithDrawable(R.id.search_button, d);
2782 }
2783
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002784 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002785 final View searchDivider = findViewById(R.id.search_divider);
2786 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002787
Winson Chung1cad91e2011-05-25 17:41:01 -07002788 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2789 ComponentName activityName = intent.resolveActivity(getPackageManager());
2790 if (activityName != null) {
2791 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002792 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2793 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002794 voiceButton.setVisibility(View.VISIBLE);
2795 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002796 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002797 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002798 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002799 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002800
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002801 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002802 updateButtonWithDrawable(R.id.voice_button, d);
2803 }
2804
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002805 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002806 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002807 */
2808 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002809 final View marketButton = findViewById(R.id.market_button);
2810 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2811 // Find the app market activity by resolving an intent.
2812 // (If multiple app markets are installed, it will return the ResolverActivity.)
2813 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002814 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002815 mAppMarketIntent = intent;
2816 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002817 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002818 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002819
2820 // Remove the shop icon text in the Phone UI
2821 if (!LauncherApplication.isScreenLarge()) {
2822 ((TextView) marketButton).setText("");
2823 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002824 } else {
2825 // We should hide and disable the view so that we don't try and restore the visibility
2826 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2827 marketButton.setVisibility(View.GONE);
2828 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002829 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002830 }
2831
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002832 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002833 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002834 }
2835
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002836 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837 * Displays the shortcut creation dialog and launches, if necessary, the
2838 * appropriate activity.
2839 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002840 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002841 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2842 DialogInterface.OnShowListener {
2843
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002847 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002848
Winson Chung55b65502011-05-26 12:03:43 -07002849 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2850 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002852 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002853
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002854 AlertDialog dialog = builder.create();
2855 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002856 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002857 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002859 return dialog;
2860 }
2861
2862 public void onCancel(DialogInterface dialog) {
2863 mWaitingForResult = false;
2864 cleanup();
2865 }
2866
Romain Guycbb89e42009-06-08 15:52:54 -07002867 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002868 mWaitingForResult = false;
2869 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002870 }
2871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002873 try {
2874 dismissDialog(DIALOG_CREATE_SHORTCUT);
2875 } catch (Exception e) {
2876 // An exception is thrown if the dialog is not visible, which is fine
2877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002878 }
2879
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002880 /**
2881 * Handle the action clicked in the "Add to home" dialog.
2882 */
2883 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002884 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002885
Winson Chung55b65502011-05-26 12:03:43 -07002886 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2887 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002888 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002889 if (mAppsCustomizeTabHost != null) {
2890 mAppsCustomizeTabHost.selectWidgetsTab();
2891 }
2892 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002893 break;
2894 }
Winson Chung55b65502011-05-26 12:03:43 -07002895 case AddAdapter.ITEM_APPLICATION: {
2896 if (mAppsCustomizeTabHost != null) {
2897 mAppsCustomizeTabHost.selectAppsTab();
2898 }
2899 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002900 break;
2901 }
Winson Chung55b65502011-05-26 12:03:43 -07002902 case AddAdapter.ITEM_APPWIDGET: {
2903 if (mAppsCustomizeTabHost != null) {
2904 mAppsCustomizeTabHost.selectWidgetsTab();
2905 }
2906 showAllApps(true);
2907 break;
2908 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002909 case AddAdapter.ITEM_WALLPAPER: {
2910 startWallpaper();
2911 break;
2912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
2914 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002915
2916 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002917 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 }
2920
2921 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002922 * Receives notifications when applications are added/removed.
2923 */
2924 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2925 @Override
2926 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002927 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002928 String reason = intent.getStringExtra("reason");
2929 if (!"homekey".equals(reason)) {
2930 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002931 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002932 animate = false;
2933 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002934 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002935 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002936 }
2937 }
2938
2939 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002940 * Receives notifications whenever the appwidgets are reset.
2941 */
2942 private class AppWidgetResetObserver extends ContentObserver {
2943 public AppWidgetResetObserver() {
2944 super(new Handler());
2945 }
2946
2947 @Override
2948 public void onChange(boolean selfChange) {
2949 onAppWidgetReset();
2950 }
2951 }
2952
2953 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002954 * If the activity is currently paused, signal that we need to re-run the loader
2955 * in onResume.
2956 *
2957 * This needs to be called from incoming places where resources might have been loaded
2958 * while we are paused. That is becaues the Configuration might be wrong
2959 * when we're not running, and if it comes back to what it was when we
2960 * were paused, we are not restarted.
2961 *
2962 * Implementation of the method from LauncherModel.Callbacks.
2963 *
2964 * @return true if we are currently paused. The caller might be able to
2965 * skip some work in that case since we will come back again.
2966 */
2967 public boolean setLoadOnResume() {
2968 if (mPaused) {
2969 Log.i(TAG, "setLoadOnResume");
2970 mOnResumeNeedsLoad = true;
2971 return true;
2972 } else {
2973 return false;
2974 }
2975 }
2976
2977 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002981 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002982 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002983 } else {
2984 return SCREEN_COUNT / 2;
2985 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002986 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002987
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002988
Joe Onorato9c1289c2009-08-17 11:03:03 -04002989 /**
2990 * Refreshes the shortcuts shown on the workspace.
2991 *
2992 * Implementation of the method from LauncherModel.Callbacks.
2993 */
2994 public void startBinding() {
2995 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002996
2997 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 int count = workspace.getChildCount();
2999 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003000 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003001 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3002 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003004
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 if (DEBUG_USER_INTERFACE) {
3006 android.widget.Button finishButton = new android.widget.Button(this);
3007 finishButton.setText("Finish");
3008 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3009
3010 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3011 public void onClick(View v) {
3012 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003013 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003015 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003016
Adam Cohen4eac29a2011-07-11 17:53:37 -07003017 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3018 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 }
3020
3021 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 * Bind the items start-end from the list.
3023 *
3024 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3027
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003028 setLoadOnResume();
3029
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 final Workspace workspace = mWorkspace;
3031
3032 for (int i=start; i<end; i++) {
3033 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 switch (item.itemType) {
3035 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3036 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003037 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3039 false);
3040 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003041 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003043 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003044 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3046 false);
3047 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
3050
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 }
3053
Joe Onorato9c1289c2009-08-17 11:03:03 -04003054 /**
3055 * Implementation of the method from LauncherModel.Callbacks.
3056 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003057 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003058 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003059 sFolders.clear();
3060 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062
3063 /**
3064 * Add the views for a widget to the workspace.
3065 *
3066 * Implementation of the method from LauncherModel.Callbacks.
3067 */
3068 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003069 setLoadOnResume();
3070
Daniel Sandler843e8602010-06-07 14:59:01 -04003071 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3072 if (DEBUG_WIDGETS) {
3073 Log.d(TAG, "bindAppWidget: " + item);
3074 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 final Workspace workspace = mWorkspace;
3076
3077 final int appWidgetId = item.appWidgetId;
3078 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003079 if (DEBUG_WIDGETS) {
3080 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3081 }
3082
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3086 item.hostView.setTag(item);
3087
3088 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3089 item.cellY, item.spanX, item.spanY, false);
3090
Adam Cohended9f8d2010-11-03 13:25:16 -07003091 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3092
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 workspace.requestLayout();
3094
Daniel Sandler843e8602010-06-07 14:59:01 -04003095 if (DEBUG_WIDGETS) {
3096 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3097 + (SystemClock.uptimeMillis()-start) + "ms");
3098 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 }
3100
3101 /**
3102 * Callback saying that there aren't any more items to bind.
3103 *
3104 * Implementation of the method from LauncherModel.Callbacks.
3105 */
3106 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003107 setLoadOnResume();
3108
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 if (mSavedState != null) {
3110 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003111 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 mSavedState = null;
3114 }
3115
3116 if (mSavedInstanceState != null) {
3117 super.onRestoreInstanceState(mSavedInstanceState);
3118 mSavedInstanceState = null;
3119 }
3120
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003122
3123 // If we received the result of any pending adds while the loader was running (e.g. the
3124 // widget configuration forced an orientation change), process them now.
3125 for (int i = 0; i < sPendingAddList.size(); i++) {
3126 completeAdd(sPendingAddList.get(i));
3127 }
3128 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 }
3130
3131 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003132 * Updates the icons on the launcher that are affected by changes to the package list
3133 * on the device.
3134 */
3135 private void updateIconsAffectedByPackageManagerChanges() {
3136 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003137 updateVoiceSearchIcon();
3138 }
3139
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003140 @Override
3141 public void bindSearchablesChanged() {
3142 updateGlobalSearchIcon();
3143 }
3144
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003145 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003146 * Add the icons for all apps.
3147 *
3148 * Implementation of the method from LauncherModel.Callbacks.
3149 */
3150 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003151 if (mAppsCustomizeContent != null) {
3152 mAppsCustomizeContent.setApps(apps);
3153 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003154 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003155 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 }
3157
3158 /**
3159 * A package was installed.
3160 *
3161 * Implementation of the method from LauncherModel.Callbacks.
3162 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003163 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003164 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166
Winson Chung785d2eb2011-04-14 16:08:02 -07003167 if (mAppsCustomizeContent != null) {
3168 mAppsCustomizeContent.addApps(apps);
3169 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003170 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 }
3172
3173 /**
3174 * A package was updated.
3175 *
3176 * Implementation of the method from LauncherModel.Callbacks.
3177 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003178 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003179 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003181 if (mWorkspace != null) {
3182 mWorkspace.updateShortcuts(apps);
3183 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003184
Winson Chung785d2eb2011-04-14 16:08:02 -07003185 if (mAppsCustomizeContent != null) {
3186 mAppsCustomizeContent.updateApps(apps);
3187 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003188 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003189 }
3190
3191 /**
3192 * A package was uninstalled.
3193 *
3194 * Implementation of the method from LauncherModel.Callbacks.
3195 */
Joe Onorato36115782010-06-17 13:28:48 -04003196 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003198 if (permanent) {
3199 mWorkspace.removeItems(apps);
3200 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003201
Winson Chung785d2eb2011-04-14 16:08:02 -07003202 if (mAppsCustomizeContent != null) {
3203 mAppsCustomizeContent.removeApps(apps);
3204 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003205 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003206 }
Joe Onoratobe386092009-11-17 17:32:16 -08003207
3208 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003209 * A number of packages were updated.
3210 */
3211 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003212
Winson Chung785d2eb2011-04-14 16:08:02 -07003213 if (mAppsCustomizeContent != null) {
3214 mAppsCustomizeContent.onPackagesUpdated();
3215 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003216 }
3217
Winson Chung400438b2011-01-16 17:53:48 -08003218 private int mapConfigurationOriActivityInfoOri(int configOri) {
3219 final Display d = getWindowManager().getDefaultDisplay();
3220 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3221 switch (d.getRotation()) {
3222 case Surface.ROTATION_0:
3223 case Surface.ROTATION_180:
3224 // We are currently in the same basic orientation as the natural orientation
3225 naturalOri = configOri;
3226 break;
3227 case Surface.ROTATION_90:
3228 case Surface.ROTATION_270:
3229 // We are currently in the other basic orientation to the natural orientation
3230 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3231 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3232 break;
3233 }
3234
3235 int[] oriMap = {
3236 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3237 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3238 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3239 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3240 };
3241 // Since the map starts at portrait, we need to offset if this device's natural orientation
3242 // is landscape.
3243 int indexOffset = 0;
3244 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3245 indexOffset = 1;
3246 }
3247 return oriMap[(d.getRotation() + indexOffset) % 4];
3248 }
3249 public void lockScreenOrientation() {
3250 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3251 .getConfiguration().orientation));
3252 }
3253 public void unlockScreenOrientation() {
3254 mHandler.postDelayed(new Runnable() {
3255 public void run() {
3256 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3257 }
3258 }, mRestoreScreenOrientationDelay);
3259 }
3260
Winson Chung80baf5a2010-08-09 16:03:15 -07003261 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003262 * Prints out out state for debugging.
3263 */
3264 public void dumpState() {
3265 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003266 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003267 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3268 Log.d(TAG, "mRestoring=" + mRestoring);
3269 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3270 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003271 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003272 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003273
Winson Chung785d2eb2011-04-14 16:08:02 -07003274 if (mAppsCustomizeContent != null) {
3275 mAppsCustomizeContent.dumpState();
3276 }
Joe Onoratobe386092009-11-17 17:32:16 -08003277 Log.d(TAG, "END launcher2 dump state");
3278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279}
Michael Jurka2763be32011-02-24 11:19:57 -08003280
Michael Jurkaabded662011-03-04 12:06:57 -08003281interface LauncherTransitionable {
3282 void onLauncherTransitionStart(Animator animation);
3283 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003284}