blob: 3f4a47c7a849ef27a778dddb35616b759a1f0d78 [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;
Adam Cohen860f4c52011-01-27 20:16:13 -080042import android.content.Intent.ShortcutIconResource;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
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;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.pm.PackageManager.NameNotFoundException;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047import android.content.pm.ResolveInfo;
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;
Michael Jurkae0f5a612011-02-07 16:45:41 -080081import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080082import android.view.View.OnLongClickListener;
Winson Chungf0ea4d32011-06-06 14:27:16 -070083import android.view.ViewGroup;
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;
97import com.android.launcher2.Workspace.ShrinkState;
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: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700152 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700164 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
165
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700166 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700168 private State mState = State.WORKSPACE;
169 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 static final int APPWIDGET_HOST_ID = 1024;
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
Romain Guy84f296c2009-11-04 15:00:44 -0800224 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700225
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700226 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400228 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400229 private String[] mHotseatConfig = null;
230 private Intent[] mHotseats = null;
231 private Drawable[] mHotseatIcons = null;
232 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400233
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700234 private Intent mAppMarketIntent = null;
235
Adam Cohended9f8d2010-11-03 13:25:16 -0700236 // Related to the auto-advancing of widgets
237 private final int ADVANCE_MSG = 1;
238 private final int mAdvanceInterval = 20000;
239 private final int mAdvanceStagger = 250;
240 private long mAutoAdvanceSentTime;
241 private long mAutoAdvanceTimeLeft = -1;
242 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
243 new HashMap<View, AppWidgetProviderInfo>();
244
Winson Chung400438b2011-01-16 17:53:48 -0800245 // Determines how long to wait after a rotation before restoring the screen orientation to
246 // match the sensor state.
247 private final int mRestoreScreenOrientationDelay = 500;
248
Michael Jurka4ef207b2010-11-29 17:05:45 -0800249 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800250 private static Drawable.ConstantState sGlobalSearchIcon;
251 private static Drawable.ConstantState sVoiceSearchIcon;
252 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800253
Adam Cohen2801caf2011-05-13 20:57:39 -0700254 private DragLayer mDragLayer;
255
Michael Jurkaddd62e92011-02-16 17:49:14 -0800256 private BubbleTextView mWaitingForResume;
257
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800258 private static ArrayList<PendingAddArguments> sPendingAddList
259 = new ArrayList<PendingAddArguments>();
260
261 private static class PendingAddArguments {
262 int requestCode;
263 Intent intent;
264 int screen;
265 int cellX;
266 int cellY;
267 }
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 @Override
270 protected void onCreate(Bundle savedInstanceState) {
271 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800272 LauncherApplication app = ((LauncherApplication)getApplication());
273 mModel = app.setLauncher(this);
274 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400275 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700277
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700279 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
280 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200283 android.os.Debug.startMethodTracing(
284 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
286
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400287 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 setContentView(R.layout.launcher);
290 setupViews();
291
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800292 registerContentObservers();
293
Joe Onorato7c312c12009-08-13 21:36:53 -0700294 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 mSavedState = savedInstanceState;
297 restoreState(mSavedState);
298
Winson Chunga12a2502010-12-20 14:41:35 -0800299 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700300 if (mAppsCustomizeContent != null) {
301 mAppsCustomizeContent.onPackagesUpdated();
302 }
Winson Chunga12a2502010-12-20 14:41:35 -0800303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 if (PROFILE_STARTUP) {
305 android.os.Debug.stopMethodTracing();
306 }
307
308 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
311
312 // For handling default keys
313 mDefaultKeySsb = new SpannableStringBuilder();
314 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800315
316 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
317 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800318
319 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700320 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
321 updateIconsAffectedByPackageManagerChanges();
322 }
323 if (sGlobalSearchIcon != null) {
324 updateGlobalSearchIcon(sGlobalSearchIcon);
325 }
326 if (sVoiceSearchIcon != null) {
327 updateVoiceSearchIcon(sVoiceSearchIcon);
328 }
329 if (sAppMarketIcon != null) {
330 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
Romain Guycbb89e42009-06-08 15:52:54 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700335 if (sLocaleConfiguration == null) {
336 new AsyncTask<Void, Void, LocaleConfiguration>() {
337 @Override
338 protected LocaleConfiguration doInBackground(Void... unused) {
339 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
340 readConfiguration(Launcher.this, localeConfiguration);
341 return localeConfiguration;
342 }
343
344 @Override
345 protected void onPostExecute(LocaleConfiguration result) {
346 sLocaleConfiguration = result;
347 checkForLocaleChange(); // recursive, but now with a locale configuration
348 }
349 }.execute();
350 return;
351 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 final Configuration configuration = getResources().getConfiguration();
354
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 final String locale = configuration.locale.toString();
357
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 final int mcc = configuration.mcc;
360
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700361 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 final int mnc = configuration.mnc;
363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365
Romain Guy84f296c2009-11-04 15:00:44 -0800366 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700367 sLocaleConfiguration.locale = locale;
368 sLocaleConfiguration.mcc = mcc;
369 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700370
Joe Onorato0589f0f2010-02-08 13:44:00 -0800371 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400372 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373
374 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
375 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700376 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700377 public void run() {
378 writeConfiguration(Launcher.this, localeConfiguration);
379 }
380 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700381 }
382 }
383
384 private static class LocaleConfiguration {
385 public String locale;
386 public int mcc = -1;
387 public int mnc = -1;
388 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700389
Romain Guy98d01652009-06-30 16:21:04 -0700390 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
391 DataInputStream in = null;
392 try {
393 in = new DataInputStream(context.openFileInput(PREFERENCES));
394 configuration.locale = in.readUTF();
395 configuration.mcc = in.readInt();
396 configuration.mnc = in.readInt();
397 } catch (FileNotFoundException e) {
398 // Ignore
399 } catch (IOException e) {
400 // Ignore
401 } finally {
402 if (in != null) {
403 try {
404 in.close();
405 } catch (IOException e) {
406 // Ignore
407 }
408 }
409 }
410 }
411
412 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
413 DataOutputStream out = null;
414 try {
415 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
416 out.writeUTF(configuration.locale);
417 out.writeInt(configuration.mcc);
418 out.writeInt(configuration.mnc);
419 out.flush();
420 } catch (FileNotFoundException e) {
421 // Ignore
422 } catch (IOException e) {
423 //noinspection ResultOfMethodCallIgnored
424 context.getFileStreamPath(PREFERENCES).delete();
425 } finally {
426 if (out != null) {
427 try {
428 out.close();
429 } catch (IOException e) {
430 // Ignore
431 }
432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434 }
435
436 static int getScreen() {
437 synchronized (sLock) {
438 return sScreen;
439 }
440 }
441
442 static void setScreen(int screen) {
443 synchronized (sLock) {
444 sScreen = screen;
445 }
446 }
447
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400448 // Note: This doesn't do all the client-id magic that BrowserProvider does
449 // in Browser. (http://b/2425179)
450 private Uri getDefaultBrowserUri() {
451 String url = getString(R.string.default_browser_url);
452 if (url.indexOf("{CID}") != -1) {
453 url = url.replace("{CID}", "android-google");
454 }
455 return Uri.parse(url);
456 }
457
458 // Load the Intent templates from arrays.xml to populate the hotseats. For
459 // each Intent, if it resolves to a single app, use that as the launch
460 // intent & use that app's label as the contentDescription. Otherwise,
461 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400462 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400463 if (mHotseatConfig == null) {
464 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
465 if (mHotseatConfig.length > 0) {
466 mHotseats = new Intent[mHotseatConfig.length];
467 mHotseatLabels = new CharSequence[mHotseatConfig.length];
468 mHotseatIcons = new Drawable[mHotseatConfig.length];
469 } else {
470 mHotseats = null;
471 mHotseatIcons = null;
472 mHotseatLabels = null;
473 }
474
475 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
476 for (int i=0; i<mHotseatConfig.length; i++) {
477 // load icon for this slot; currently unrelated to the actual activity
478 try {
479 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
480 } catch (ArrayIndexOutOfBoundsException ex) {
481 Log.w(TAG, "Missing hotseat_icons array item #" + i);
482 mHotseatIcons[i] = null;
483 }
484 }
485 hotseatIconDrawables.recycle();
486 }
487
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400488 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400489 for (int i=0; i<mHotseatConfig.length; i++) {
490 Intent intent = null;
491 if (mHotseatConfig[i].equals("*BROWSER*")) {
492 // magic value meaning "launch user's default web browser"
493 // replace it with a generic web request so we can see if there is indeed a default
494 String defaultUri = getString(R.string.default_browser_url);
495 intent = new Intent(
496 Intent.ACTION_VIEW,
497 ((defaultUri != null)
498 ? Uri.parse(defaultUri)
499 : getDefaultBrowserUri())
500 ).addCategory(Intent.CATEGORY_BROWSABLE);
501 // note: if the user launches this without a default set, she
502 // will always be taken to the default URL above; this is
503 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700504 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 // URL is unavoidably sent to the chosen app.
506 } else {
507 try {
508 intent = Intent.parseUri(mHotseatConfig[i], 0);
509 } catch (java.net.URISyntaxException ex) {
510 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
511 // bogus; leave intent=null
512 }
513 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700514
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400515 if (intent == null) {
516 mHotseats[i] = null;
517 mHotseatLabels[i] = getText(R.string.activity_not_found);
518 continue;
519 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400520
521 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700522 Log.d(TAG, "loadHotseats: hotseat " + i
523 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400524 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400525 + "]");
526 }
527
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400528 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
529 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700530 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400531 Log.d(TAG, "Best match for intent: " + bestMatch);
532 Log.d(TAG, "All matches: ");
533 for (ResolveInfo ri : allMatches) {
534 Log.d(TAG, " --> " + ri);
535 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400536 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400537 // did this resolve to a single app, or the resolver?
538 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700539 // can't find any activity to handle this. let's leave the
540 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400541 // to launch.
542 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400543
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 // set accessibility text to "Not installed"
545 mHotseatLabels[i] = getText(R.string.activity_not_found);
546 } else {
547 boolean found = false;
548 for (ResolveInfo ri : allMatches) {
549 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
550 && bestMatch.activityInfo.applicationInfo.packageName
551 .equals(ri.activityInfo.applicationInfo.packageName)) {
552 found = true;
553 break;
554 }
555 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700556
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400557 if (!found) {
558 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
559 // the bestMatch is probably the ResolveActivity, meaning the
560 // user has not yet selected a default
561 // so: we'll keep the original intent for now
562 mHotseats[i] = intent;
563
564 // set the accessibility text to "Select shortcut"
565 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
566 } else {
567 // we have an app!
568 // now reconstruct the intent to launch it through the front
569 // door
570 ComponentName com = new ComponentName(
571 bestMatch.activityInfo.applicationInfo.packageName,
572 bestMatch.activityInfo.name);
573 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
574
575 // load the app label for accessibility
576 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
577 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578 }
579
580 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700581 Log.d(TAG, "loadHotseats: hotseat " + i
582 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400583 + ((mHotseats[i] == null)
584 ? "null"
585 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400586 + "] label=[" + mHotseatLabels[i]
587 + "]"
588 );
589 }
590 }
591 }
592
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 private void completeAdd(PendingAddArguments args) {
594 switch (args.requestCode) {
595 case REQUEST_PICK_APPLICATION:
596 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
597 break;
598 case REQUEST_PICK_SHORTCUT:
599 processShortcut(args.intent);
600 break;
601 case REQUEST_CREATE_SHORTCUT:
602 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
603 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800604 case REQUEST_PICK_APPWIDGET:
605 addAppWidgetFromPick(args.intent);
606 break;
607 case REQUEST_CREATE_APPWIDGET:
608 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
609 completeAddAppWidget(appWidgetId, args.screen);
610 break;
611 case REQUEST_PICK_WALLPAPER:
612 // We just wanted the activity result here so we can clear mWaitingForResult
613 break;
614 }
615 }
616
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700619 mWaitingForResult = false;
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 // The pattern used here is that a user PICKs a specific application,
622 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
625 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700626
Michael Jurka0280c3b2010-09-17 15:00:07 -0700627 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800628 final PendingAddArguments args = new PendingAddArguments();
629 args.requestCode = requestCode;
630 args.intent = data;
631 args.screen = mAddScreen;
632 args.cellX = mAddIntersectCellX;
633 args.cellY = mAddIntersectCellY;
634
635 // If the loader is still running, defer the add until it is done.
636 if (isWorkspaceLocked()) {
637 sPendingAddList.add(args);
638 } else {
639 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800640 }
Romain Guy18042c82009-11-06 11:44:55 -0800641 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
642 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
643 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700644 // Clean up the appWidgetId if we canceled
645 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
646 if (appWidgetId != -1) {
647 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
649 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
651
652 @Override
653 protected void onResume() {
654 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800655 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700656 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400657 mWorkspaceLoading = true;
658 mModel.startLoader(this, true);
659 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700660 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800662 if (mWaitingForResume != null) {
663 mWaitingForResume.setStayPressed(false);
664 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700665 // When we resume Launcher, a different Activity might be responsible for the app
666 // market intent, so refresh the icon
667 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
669
670 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700671 protected void onPause() {
672 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700673 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800674 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 }
Romain Guycbb89e42009-06-08 15:52:54 -0700676
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700677 @Override
678 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400679 // Flag the loader to stop early before switching
680 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700681 if (mAppsCustomizeContent != null) {
682 mAppsCustomizeContent.surrender();
683 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800684 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700685 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800687 // We can't hide the IME if it was forced open. So don't bother
688 /*
689 @Override
690 public void onWindowFocusChanged(boolean hasFocus) {
691 super.onWindowFocusChanged(hasFocus);
692
693 if (hasFocus) {
694 final InputMethodManager inputManager = (InputMethodManager)
695 getSystemService(Context.INPUT_METHOD_SERVICE);
696 WindowManager.LayoutParams lp = getWindow().getAttributes();
697 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
698 android.os.Handler()) {
699 protected void onReceiveResult(int resultCode, Bundle resultData) {
700 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
701 }
702 });
703 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
704 }
705 }
706 */
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 private boolean acceptFilter() {
709 final InputMethodManager inputManager = (InputMethodManager)
710 getSystemService(Context.INPUT_METHOD_SERVICE);
711 return !inputManager.isFullscreenMode();
712 }
713
714 @Override
715 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700716 final int uniChar = event.getUnicodeChar();
717 final boolean handled = super.onKeyDown(keyCode, event);
718 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
719 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
721 keyCode, event);
722 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700723 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700724 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700725 // showSearchDialog()
726 // If there are multiple keystrokes before the search dialog takes focus,
727 // onSearchRequested() will be called for every keystroke,
728 // but it is idempotent, so it's fine.
729 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800730 }
731 }
732
Joe Onorato8a9625e2010-01-28 15:55:35 -0800733 // Eat the long press event so the keyboard doesn't come up.
734 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
735 return true;
736 }
737
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 return handled;
739 }
740
Karl Rosaen138a0412009-04-23 19:00:21 -0700741 private String getTypedText() {
742 return mDefaultKeySsb.toString();
743 }
744
745 private void clearTypedText() {
746 mDefaultKeySsb.clear();
747 mDefaultKeySsb.clearSpans();
748 Selection.setSelection(mDefaultKeySsb, 0);
749 }
750
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700752 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
753 * State
754 */
755 private static State intToState(int stateOrdinal) {
756 State state = State.WORKSPACE;
757 final State[] stateValues = State.values();
758 for (int i = 0; i < stateValues.length; i++) {
759 if (stateValues[i].ordinal() == stateOrdinal) {
760 state = stateValues[i];
761 break;
762 }
763 }
764 return state;
765 }
766
767 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 * Restores the previous state, if it exists.
769 *
770 * @param savedState The previous state.
771 */
772 private void restoreState(Bundle savedState) {
773 if (savedState == null) {
774 return;
775 }
776
Michael Jurka883f55b2010-10-21 15:47:14 -0700777 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
778
Winson Chungf0ea4d32011-06-06 14:27:16 -0700779 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800780 showAllApps(false);
781 }
782
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
784 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700785 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 }
787
788 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700789
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791 mAddScreen = addScreen;
792 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
793 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 mRestoring = true;
795 }
796
797 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
798 if (renameFolder) {
799 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700800 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 mRestoring = true;
802 }
Winson Chunga12a2502010-12-20 14:41:35 -0800803
Winson Chung785d2eb2011-04-14 16:08:02 -0700804
805 // Restore the AppsCustomize tab
806 if (mAppsCustomizeTabHost != null) {
807 String curTab = savedState.getString("apps_customize_currentTab");
808 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700809 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700810 mAppsCustomizeContent.setContentType(
811 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
812 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
813 }
814
815 // Note: currently we do not restore the page for the AppsCustomize pane because the
816 // change in layout can drastically affect the saved page index
817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
819
820 /**
821 * Finds all the views we need and configure them properly.
822 */
823 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700824 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400825
Winson Chung4c98d922011-05-31 16:50:48 -0700826 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
827 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828
Winson Chung4c98d922011-05-31 16:50:48 -0700829 // Setup the drag layer
830 mDragLayer.setup(this, dragController);
831
832 // Setup the workspace
833 mWorkspace.setHapticFeedbackEnabled(false);
834 mWorkspace.setOnLongClickListener(this);
835 mWorkspace.setup(this, dragController);
836 mWorkspace.setWallpaperDimension();
837
Winson Chungf0ea4d32011-06-06 14:27:16 -0700838 // Get the search/delete bar
839 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700840
Winson Chungf0ea4d32011-06-06 14:27:16 -0700841 // Setup AppsCustomize
842 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
843 findViewById(R.id.apps_customize_pane);
844 mAppsCustomizeContent = (AppsCustomizePagedView)
845 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
846 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400847
Winson Chungf0ea4d32011-06-06 14:27:16 -0700848 // Setup AppsCustomize button
849 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
850 mAllAppsButton.setOnClickListener(new OnClickListener() {
851 public void onClick(View v) {
852 onClickAllAppsButton(v);
853 }
854 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800855
Winson Chungf0ea4d32011-06-06 14:27:16 -0700856 if (!LauncherApplication.isScreenLarge()) {
857 // Setup AppsCustomize button on the phone
858 HandleView handleView = (HandleView) mAllAppsButton;
859 handleView.setLauncher(this);
860 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700861
862 // Setup Hotseat
863 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
864 hotseatLeft.setContentDescription(mHotseatLabels[0]);
865 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
866 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
867 hotseatRight.setContentDescription(mHotseatLabels[1]);
868 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800869 }
870
Winson Chungf0ea4d32011-06-06 14:27:16 -0700871 if (!LauncherApplication.isScreenLarge()) {
872 // Setup keylistener for button cluster
873 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
874 View.OnKeyListener listener = null;
875 if (LauncherApplication.isScreenLarge()) {
876 // For tablets, AllApps lives in the button bar at the top
877 listener = new ButtonBarKeyEventListener();
878 } else {
879 // For phones, AppsCustomize lives in the "dock" at the bottom
880 listener = new DockKeyEventListener();
881 }
882 int buttonCount = mButtonCluster.getChildCount();
883 for (int i = 0; i < buttonCount; ++i) {
884 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
885 }
Winson Chung97d85d22011-04-13 11:27:36 -0700886 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800887
Winson Chung4c98d922011-05-31 16:50:48 -0700888 // Setup the drag controller (the drop targets have to be added in reverse order)
889 dragController.setDragScoller(mWorkspace);
890 dragController.setScrollView(mDragLayer);
891 dragController.setMoveTarget(mWorkspace);
892 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700893 if (mSearchDeleteBar != null) {
894 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 }
897
Romain Guy8a73c512009-11-09 19:19:59 -0800898 @SuppressWarnings({"UnusedDeclaration"})
899 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700900 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800901 mWorkspace.scrollLeft();
902 }
Romain Guy8a73c512009-11-09 19:19:59 -0800903 }
904
905 @SuppressWarnings({"UnusedDeclaration"})
906 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700907 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800908 mWorkspace.scrollRight();
909 }
Romain Guy8a73c512009-11-09 19:19:59 -0800910 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400911
912 @SuppressWarnings({"UnusedDeclaration"})
913 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700914 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400915
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400916 int index = -1;
917 if (v.getId() == R.id.hotseat_left) {
918 index = 0;
919 } else if (v.getId() == R.id.hotseat_right) {
920 index = 1;
921 }
922
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400923 // reload these every tap; you never know when they might change
924 loadHotseats();
925 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400926 startActivitySafely(
927 mHotseats[index],
928 "hotseat"
929 );
930 }
931 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 /**
934 * Creates a view representing a shortcut.
935 *
936 * @param info The data structure describing the shortcut.
937 *
938 * @return A View inflated from R.layout.application.
939 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800940 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700942 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 /**
946 * Creates a view representing a shortcut inflated from the specified resource.
947 *
948 * @param layoutResId The id of the XML layout used to create the shortcut.
949 * @param parent The group the shortcut belongs to.
950 * @param info The data structure describing the shortcut.
951 *
952 * @return A View inflated from layoutResId.
953 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800954 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800955 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
956 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 return favorite;
959 }
960
961 /**
962 * Add an application shortcut to the workspace.
963 *
964 * @param data The intent describing the application.
965 * @param cellInfo The position on screen where to create the shortcut.
966 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800967 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700968 int intersectCellX, int intersectCellY) {
969 final int[] cellXY = mTmpAddItemCellCoordinates;
970 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
971
972 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
973 showOutOfSpaceMessage();
974 return;
975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800977 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800978
Romain Guy73b979d2009-06-09 12:57:21 -0700979 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800980 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700983 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
984 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800985 } else {
986 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 }
988 }
Romain Guycbb89e42009-06-08 15:52:54 -0700989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 /**
991 * Add a shortcut to the workspace.
992 *
993 * @param data The intent describing the shortcut.
994 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 */
Michael Jurka0280c3b2010-09-17 15:00:07 -0700996 private void completeAddShortcut(Intent data, int screen,
997 int intersectCellX, int intersectCellY) {
998 final int[] cellXY = mTmpAddItemCellCoordinates;
999 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001000
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001001 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001002 boolean foundCellSpan = false;
1003 if (touchXY != null) {
1004 // when dragging and dropping, just find the closest free spot
1005 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1006 int[] result = screenLayout.findNearestVacantArea(
1007 touchXY[0], touchXY[1], 1, 1, cellXY);
1008 foundCellSpan = (result != null);
1009 } else {
1010 foundCellSpan = layout.findCellForSpanThatIntersects(
1011 cellXY, 1, 1, intersectCellX, intersectCellY);
1012 }
1013
1014 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015 showOutOfSpaceMessage();
1016 return;
1017 }
1018
1019 final ShortcutInfo info = mModel.addShortcut(
1020 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021
1022 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001024 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026 }
1027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001029 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001030 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001031 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001032 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033 */
Winson Chungf7640c82011-02-28 13:47:29 -08001034 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001035 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001037 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001038 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1039 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001042 // We have saved the position to which the widget was dragged-- this really only matters
1043 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001045
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001046 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001047 boolean foundCellSpan = false;
1048 if (touchXY != null) {
1049 // when dragging and dropping, just find the closest free spot
1050 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1051 int[] result = screenLayout.findNearestVacantArea(
1052 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001053 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001054 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001055 // if we long pressed on an empty cell to bring up a menu,
1056 // make sure we intersect the empty cell
1057 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1058 // findCellForSpanThatIntersects will just ignore them
1059 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1060 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001061 }
1062
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001064 if (appWidgetId != -1) {
1065 // Deleting an app widget ID is a void call but writes to disk before returning
1066 // to the caller...
1067 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1068 new Thread("deleteAppWidgetId") {
1069 public void run() {
1070 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1071 }
1072 }.start();
1073 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001075 return;
1076 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 // Build Launcher-specific widget info and save to database
1079 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 launcherInfo.spanX = spanXY[0];
1081 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001082
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 LauncherModel.addItemToDatabase(this, launcherInfo,
1084 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086
1087 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001088 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001092
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001093 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001095
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001097 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001098
1099 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Adam Cohended9f8d2010-11-03 13:25:16 -07001103 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1104 @Override
1105 public void onReceive(Context context, Intent intent) {
1106 final String action = intent.getAction();
1107 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1108 mUserPresent = false;
1109 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001110
1111 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001112 if (mAppsCustomizeContent != null) {
1113 mAppsCustomizeContent.reset();
1114 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1116 mUserPresent = true;
1117 updateRunning();
1118 }
1119 }
1120 };
1121
1122 @Override
1123 public void onAttachedToWindow() {
1124 super.onAttachedToWindow();
1125
1126 // Listen for broadcasts related to user-presence
1127 final IntentFilter filter = new IntentFilter();
1128 filter.addAction(Intent.ACTION_SCREEN_OFF);
1129 filter.addAction(Intent.ACTION_USER_PRESENT);
1130 registerReceiver(mReceiver, filter);
1131
Adam Cohend113e0c2010-11-11 10:48:05 -08001132 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001133 mVisible = true;
1134 }
1135
1136 @Override
1137 public void onDetachedFromWindow() {
1138 super.onDetachedFromWindow();
1139 mVisible = false;
1140
Adam Cohend113e0c2010-11-11 10:48:05 -08001141 if (mAttached) {
1142 unregisterReceiver(mReceiver);
1143 mAttached = false;
1144 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001145 updateRunning();
1146 }
1147
1148 public void onWindowVisibilityChanged(int visibility) {
1149 mVisible = visibility == View.VISIBLE;
1150 updateRunning();
1151 }
1152
1153 private void sendAdvanceMessage(long delay) {
1154 mHandler.removeMessages(ADVANCE_MSG);
1155 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1156 mHandler.sendMessageDelayed(msg, delay);
1157 mAutoAdvanceSentTime = System.currentTimeMillis();
1158 }
1159
1160 private void updateRunning() {
1161 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1162 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1163 mAutoAdvanceRunning = autoAdvanceRunning;
1164 if (autoAdvanceRunning) {
1165 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1166 sendAdvanceMessage(delay);
1167 } else {
1168 if (!mWidgetsToAdvance.isEmpty()) {
1169 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1170 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1171 }
1172 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001173 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001174 }
1175 }
1176 }
1177
1178 private final Handler mHandler = new Handler() {
1179 @Override
1180 public void handleMessage(Message msg) {
1181 if (msg.what == ADVANCE_MSG) {
1182 int i = 0;
1183 for (View key: mWidgetsToAdvance.keySet()) {
1184 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1185 final int delay = mAdvanceStagger * i;
1186 if (v instanceof Advanceable) {
1187 postDelayed(new Runnable() {
1188 public void run() {
1189 ((Advanceable) v).advance();
1190 }
1191 }, delay);
1192 }
1193 i++;
1194 }
1195 sendAdvanceMessage(mAdvanceInterval);
1196 }
1197 }
1198 };
1199
1200 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1201 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1202 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1203 if (v instanceof Advanceable) {
1204 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001205 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001206 updateRunning();
1207 }
1208 }
1209
1210 void removeWidgetToAutoAdvance(View hostView) {
1211 if (mWidgetsToAdvance.containsKey(hostView)) {
1212 mWidgetsToAdvance.remove(hostView);
1213 updateRunning();
1214 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001215 }
1216
Joe Onorato9c1289c2009-08-17 11:03:03 -04001217 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1218 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001219 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001220 launcherInfo.hostView = null;
1221 }
1222
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 void showOutOfSpaceMessage() {
1224 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1225 }
1226
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 public LauncherAppWidgetHost getAppWidgetHost() {
1228 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
Romain Guycbb89e42009-06-08 15:52:54 -07001230
Winson Chunga9abd0e2010-10-27 17:18:37 -07001231 public LauncherModel getModel() {
1232 return mModel;
1233 }
1234
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001235 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001236 getWindow().closeAllPanels();
1237
1238 try {
1239 dismissDialog(DIALOG_CREATE_SHORTCUT);
1240 // Unlock the workspace if the dialog was showing
1241 } catch (Exception e) {
1242 // An exception is thrown if the dialog is not visible, which is fine
1243 }
1244
1245 try {
1246 dismissDialog(DIALOG_RENAME_FOLDER);
1247 // Unlock the workspace if the dialog was showing
1248 } catch (Exception e) {
1249 // An exception is thrown if the dialog is not visible, which is fine
1250 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001251
1252 // Whatever we were doing is hereby canceled.
1253 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001254 }
1255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 @Override
1257 protected void onNewIntent(Intent intent) {
1258 super.onNewIntent(intent);
1259
1260 // Close the menu
1261 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001262 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001263 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001264
Joe Onorato14f122b2009-11-19 14:06:36 -08001265 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1266 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001267
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001268 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001269 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001270 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001271 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001272 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001273 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001274 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001275
Joe Onorato3a8820b2009-11-10 15:06:42 -08001276 final View v = getWindow().peekDecorView();
1277 if (v != null && v.getWindowToken() != null) {
1278 InputMethodManager imm = (InputMethodManager)getSystemService(
1279 INPUT_METHOD_SERVICE);
1280 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001282
1283 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001284 if (mAppsCustomizeContent != null) {
1285 mAppsCustomizeContent.reset();
1286 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288 }
1289
1290 @Override
1291 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1292 // Do not call super here
1293 mSavedInstanceState = savedInstanceState;
1294 }
1295
1296 @Override
1297 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001298 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299
1300 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1301 if (folders.size() > 0) {
1302 final int count = folders.size();
1303 long[] ids = new long[count];
1304 for (int i = 0; i < count; i++) {
1305 final FolderInfo info = folders.get(i).getInfo();
1306 ids[i] = info.id;
1307 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001308 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 } else {
1310 super.onSaveInstanceState(outState);
1311 }
1312
Michael Jurka883f55b2010-10-21 15:47:14 -07001313 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315 if (mAddScreen > -1 && mWaitingForResult) {
1316 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1317 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1318 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
1321 if (mFolderInfo != null && mWaitingForResult) {
1322 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1323 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1324 }
Michael Jurka946ad472010-07-09 18:05:18 -07001325
Winson Chung785d2eb2011-04-14 16:08:02 -07001326 // Save the current AppsCustomize tab
1327 if (mAppsCustomizeTabHost != null) {
1328 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1329 if (currentTabTag != null) {
1330 outState.putString("apps_customize_currentTab", currentTabTag);
1331 }
1332 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334
1335 @Override
1336 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001338
Winson Chungcd2b0142011-06-08 16:02:26 -07001339 // Stop callbacks from LauncherModel
1340 LauncherApplication app = ((LauncherApplication) getApplication());
1341 mModel.stopLoader();
1342 app.setLauncher(null);
1343
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001345 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001347 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001349 mAppWidgetHost = null;
1350
1351 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
1353 TextKeyListener.getInstance().release();
1354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001357
1358 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001359 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001360
1361 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1362 mWorkspace.removeAllViews();
1363 mWorkspace = null;
1364 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001365
1366 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
1368
Adam Cohena9cf38f2011-05-02 15:36:58 -07001369 public DragController getDragController() {
1370 return mDragController;
1371 }
1372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 @Override
1374 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001375 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 super.startActivityForResult(intent, requestCode);
1377 }
1378
1379 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001380 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001382
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001383 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001384
Karl Rosaen138a0412009-04-23 19:00:21 -07001385 if (initialQuery == null) {
1386 // Use any text typed in the launcher as the initial query
1387 initialQuery = getTypedText();
1388 clearTypedText();
1389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 if (appSearchData == null) {
1391 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001392 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
Romain Guycbb89e42009-06-08 15:52:54 -07001394
Karl Rosaen138a0412009-04-23 19:00:21 -07001395 final SearchManager searchManager =
1396 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001398 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400
1401 @Override
1402 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001403 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001404 return false;
1405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406
1407 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1410 .setIcon(android.R.drawable.ic_menu_add)
1411 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001412 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1413 .setIcon(android.R.drawable.ic_menu_manage)
1414 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001415 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 .setIcon(android.R.drawable.ic_menu_gallery)
1417 .setAlphabeticShortcut('W');
1418 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1419 .setIcon(android.R.drawable.ic_search_category_default)
1420 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1421 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1422 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1423 .setAlphabeticShortcut('N');
1424
1425 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001426 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1427 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428
1429 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1430 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1431 .setIntent(settings);
1432
1433 return true;
1434 }
1435
1436 @Override
1437 public boolean onPrepareOptionsMenu(Menu menu) {
1438 super.onPrepareOptionsMenu(menu);
1439
Winson Chung785d2eb2011-04-14 16:08:02 -07001440 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1441 // related code?
1442 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443
1444 return true;
1445 }
1446
1447 @Override
1448 public boolean onOptionsItemSelected(MenuItem item) {
1449 switch (item.getItemId()) {
1450 case MENU_ADD:
1451 addItems();
1452 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001453 case MENU_MANAGE_APPS:
1454 manageApps();
1455 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 case MENU_WALLPAPER_SETTINGS:
1457 startWallpaper();
1458 return true;
1459 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001460 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 return true;
1462 case MENU_NOTIFICATIONS:
1463 showNotifications();
1464 return true;
1465 }
1466
1467 return super.onOptionsItemSelected(item);
1468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Karl Rosaen138a0412009-04-23 19:00:21 -07001470 /**
1471 * Indicates that we want global search for this activity by setting the globalSearch
1472 * argument for {@link #startSearch} to true.
1473 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001475 @Override
1476 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001477 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001478 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001479 }
1480
Joe Onorato9c1289c2009-08-17 11:03:03 -04001481 public boolean isWorkspaceLocked() {
1482 return mWorkspaceLoading || mWaitingForResult;
1483 }
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001486 showWorkspace(true);
1487 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
Michael Jurka0280c3b2010-09-17 15:00:07 -07001490 private void resetAddInfo() {
1491 mAddScreen = -1;
1492 mAddIntersectCellX = -1;
1493 mAddIntersectCellY = -1;
1494 mAddDropPosition = null;
1495 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001496
Winson Chung55cef262010-10-28 14:14:18 -07001497 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 resetAddInfo();
1499 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001500 mAddDropPosition = position;
1501
Michael Jurkaaf442092010-06-10 17:01:57 -07001502 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001503 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1504 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001505 }
1506
Winson Chung7ad01412010-09-27 11:33:03 -07001507 private void manageApps() {
1508 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1509 }
1510
Michael Jurkaaf442092010-06-10 17:01:57 -07001511 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001512 // TODO: catch bad widget exception when sent
1513 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001514 // TODO: Is this log message meaningful?
1515 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001516 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 }
1518
Winson Chung55cef262010-10-28 14:14:18 -07001519 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001520 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521
Bjorn Bringert7984c942009-12-09 15:38:25 +00001522 if (appWidget.configure != null) {
1523 // Launch over to configure widget, if needed
1524 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1525 intent.setComponent(appWidget.configure);
1526 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001527 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001528 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1529 intent.putExtra(
1530 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1531 info.mimeType);
1532
1533 final String mimeType = info.mimeType;
1534 final ClipData clipData = (ClipData) info.configurationData;
1535 final ClipDescription clipDesc = clipData.getDescription();
1536 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1537 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001538 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001539 final CharSequence stringData = item.getText();
1540 final Uri uriData = item.getUri();
1541 final Intent intentData = item.getIntent();
1542 final String key =
1543 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1544 if (uriData != null) {
1545 intent.putExtra(key, uriData);
1546 } else if (intentData != null) {
1547 intent.putExtra(key, intentData);
1548 } else if (stringData != null) {
1549 intent.putExtra(key, stringData);
1550 }
1551 break;
1552 }
1553 }
1554 }
Winson Chung55cef262010-10-28 14:14:18 -07001555 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001556
Romain Guy8e633c52010-03-04 12:51:36 -08001557 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001559 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 }
1562 }
Romain Guycbb89e42009-06-08 15:52:54 -07001563
Michael Jurka0280c3b2010-09-17 15:00:07 -07001564 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1565 resetAddInfo();
1566 mAddScreen = screen;
1567 mAddDropPosition = position;
1568
1569 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1570 createShortcutIntent.setComponent(componentName);
1571 processShortcut(createShortcutIntent);
1572 }
1573
Joe Onoratodeb98af2010-02-19 14:59:39 -08001574 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001575 // Handle case where user selected "Applications"
1576 String applicationName = getResources().getString(R.string.group_applications);
1577 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001579 if (applicationName != null && applicationName.equals(shortcutName)) {
1580 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1581 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001582
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001583 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1584 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001585 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001586 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001587 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001588 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591
Winson Chung24ab2f12010-09-16 14:10:47 -07001592 void processWallpaper(Intent intent) {
1593 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1594 }
1595
Adam Cohendf035382011-04-11 17:22:04 -07001596 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001597 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 folderInfo.title = getText(R.string.folder_name);
1599
Michael Jurka0280c3b2010-09-17 15:00:07 -07001600 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1601 final int[] cellXY = mTmpAddItemCellCoordinates;
1602 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1603 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001604 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001605 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606
1607 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001608 LauncherModel.addItemToDatabase(this, folderInfo,
1609 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001610 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001611 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 // Create the view
1614 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001615 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1616 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001617 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001618 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 }
Romain Guycbb89e42009-06-08 15:52:54 -07001620
Joe Onorato9c1289c2009-08-17 11:03:03 -04001621 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001622 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623 }
1624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 private void showNotifications() {
1626 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1627 if (statusBar != null) {
1628 statusBar.expand();
1629 }
1630 }
1631
1632 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001633 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001635 Intent chooser = Intent.createChooser(pickWallpaper,
1636 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001637 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1638 // Removed in Eclair MR1
1639// WallpaperManager wm = (WallpaperManager)
1640// getSystemService(Context.WALLPAPER_SERVICE);
1641// WallpaperInfo wi = wm.getWallpaperInfo();
1642// if (wi != null && wi.getSettingsActivity() != null) {
1643// LabeledIntent li = new LabeledIntent(getPackageName(),
1644// R.string.configure_wallpaper, 0);
1645// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1646// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1647// }
Mike Clerona0618e42009-10-22 13:55:21 -07001648 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
1650
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001651 /**
1652 * Registers various content observers. The current implementation registers
1653 * only a favorites observer to keep track of the favorites applications.
1654 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001655 private void registerContentObservers() {
1656 ContentResolver resolver = getContentResolver();
1657 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1658 true, mWidgetObserver);
1659 }
1660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 @Override
1662 public boolean dispatchKeyEvent(KeyEvent event) {
1663 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1664 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001666 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001667 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001668 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001669 dumpState();
1670 return true;
1671 }
1672 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001673 }
1674 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1675 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001676 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677 return true;
1678 }
1679 }
1680
1681 return super.dispatchKeyEvent(event);
1682 }
1683
Joe Onorato88ec0992009-11-19 13:16:06 -08001684 @Override
1685 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001686 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001687 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001688 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001689 Folder openFolder = mWorkspace.getOpenFolder();
1690 if (openFolder.isEditingName()) {
1691 openFolder.dismissEditingName();
1692 } else {
1693 closeFolder();
1694 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001695 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001696 mWorkspace.exitWidgetResizeMode();
1697
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001698 // Back button is a no-op here, but give at least some feedback for the button press
1699 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001700 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001701 }
1702
Adam Cohen2801caf2011-05-13 20:57:39 -07001703 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 Folder folder = mWorkspace.getOpenFolder();
1705 if (folder != null) {
1706 closeFolder(folder);
1707 }
1708 }
1709
1710 void closeFolder(Folder folder) {
1711 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001712
Michael Jurka8c920dd2011-01-20 14:16:56 -08001713 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001715 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001716 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1717 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001718 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001720 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722
1723 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001724 * Re-listen when widgets are reset.
1725 */
1726 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001727 if (mAppWidgetHost != null) {
1728 mAppWidgetHost.startListening();
1729 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001730 }
1731
1732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1734 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 private void unbindDesktopItems() {
1737 for (ItemInfo item: mDesktopItems) {
1738 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001740 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 /**
1744 * Launches the intent referred by the clicked shortcut.
1745 *
1746 * @param v The view representing the clicked shortcut.
1747 */
1748 public void onClick(View v) {
1749 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001750 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001752 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001753 int[] pos = new int[2];
1754 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001755 intent.setSourceBounds(new Rect(pos[0], pos[1],
1756 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001757 boolean success = startActivitySafely(intent, tag);
1758
1759 if (success && v instanceof BubbleTextView) {
1760 mWaitingForResume = (BubbleTextView) v;
1761 mWaitingForResume.setStayPressed(true);
1762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001764 if (v instanceof FolderIcon) {
1765 FolderIcon fi = (FolderIcon) v;
1766 handleFolderClick(fi);
1767 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001768 } else if (v == mAllAppsButton) {
1769 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001770 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001771 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001772 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775 }
1776
Michael Jurka0e260592010-06-30 17:07:39 -07001777 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001778 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001779 // clicking anywhere on the workspace causes the customization drawer to slide down
1780 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001781 return false;
1782 }
1783
Michael Jurkaaf442092010-06-10 17:01:57 -07001784 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001785 * Event handler for the search button
1786 *
1787 * @param v The view that was clicked.
1788 */
1789 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001790 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001791 // Use a custom animation for launching search
1792 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001793 }
1794
1795 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001796 * Event handler for the voice button
1797 *
1798 * @param v The view that was clicked.
1799 */
1800 public void onClickVoiceButton(View v) {
1801 startVoiceSearch();
1802 }
1803
1804 private void startVoiceSearch() {
1805 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1806 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1807 startActivity(intent);
1808 }
1809
1810 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001811 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001812 * enters home screen customization mode.
1813 *
1814 * @param v The view that was clicked.
1815 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001816 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001817 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001818 }
1819
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001820 /**
1821 * Event handler for the "grid" button that appears on the home screen, which
1822 * enters all apps mode.
1823 *
1824 * @param v The view that was clicked.
1825 */
1826 public void onClickAllAppsButton(View v) {
1827 showAllApps(true);
1828 }
1829
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001830 public void onClickAppMarketButton(View v) {
1831 if (mAppMarketIntent != null) {
1832 startActivitySafely(mAppMarketIntent, "app market");
1833 }
1834 }
1835
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001836 void startApplicationDetailsActivity(ComponentName componentName) {
1837 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001838 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1839 Uri.fromParts("package", packageName, null));
1840 startActivity(intent);
1841 }
1842
Patrick Dubroy5539af72010-09-07 15:22:01 -07001843 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1844 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1845 // System applications cannot be installed. For now, show a toast explaining that.
1846 // We may give them the option of disabling apps this way.
1847 int messageId = R.string.uninstall_system_app_text;
1848 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1849 } else {
1850 String packageName = appInfo.componentName.getPackageName();
1851 String className = appInfo.componentName.getClassName();
1852 Intent intent = new Intent(
1853 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1854 startActivity(intent);
1855 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001856 }
1857
Michael Jurkaddd62e92011-02-16 17:49:14 -08001858 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1860 try {
1861 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001862 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 } catch (ActivityNotFoundException e) {
1864 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001865 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 } catch (SecurityException e) {
1867 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001868 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001870 "or use the exported attribute for this activity. "
1871 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001873 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001875
Romain Guy8e633c52010-03-04 12:51:36 -08001876 void startActivityForResultSafely(Intent intent, int requestCode) {
1877 try {
1878 startActivityForResult(intent, requestCode);
1879 } catch (ActivityNotFoundException e) {
1880 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1881 } catch (SecurityException e) {
1882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1883 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1884 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1885 "or use the exported attribute for this activity.", e);
1886 }
1887 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888
Adam Cohena9cf38f2011-05-02 15:36:58 -07001889 private void handleFolderClick(FolderIcon folderIcon) {
1890 final FolderInfo info = folderIcon.mInfo;
1891 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 // Close any open folder
1893 closeFolder();
1894 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001895 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 } else {
1897 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001898 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 int folderScreen;
1900 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001901 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 // .. and close it
1903 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001904 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 // Close any folder open on the current screen
1906 closeFolder();
1907 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001908 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
1910 }
1911 }
1912 }
1913
Adam Cohen2801caf2011-05-13 20:57:39 -07001914 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1915 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1916 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1917 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1918
1919 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1920 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1921 oa.start();
1922 }
1923
1924 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1925 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1926 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1927 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1928
1929 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1930 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1931 oa.start();
1932 }
1933
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001935 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 * is animated relative to the specified View. If the View is null, no animation
1937 * is played.
1938 *
1939 * @param folderInfo The FolderInfo describing the folder to open.
1940 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001941 public void openFolder(FolderIcon folderIcon) {
1942 Folder folder = folderIcon.mFolder;
1943 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944
Adam Cohen2801caf2011-05-13 20:57:39 -07001945 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001946 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Adam Cohena9cf38f2011-05-02 15:36:58 -07001948 mWorkspace.addInFullScreen(folder, info.screen);
1949 folder.animateOpen();
1950 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 }
1952
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001954 if (mState != State.WORKSPACE) {
1955 return false;
1956 }
1957
Romain Guy1fbc1c82009-11-09 20:43:08 -08001958 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001959 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001960 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001961 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1962 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1963 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001964 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001965 return true;
1966 }
1967
Joe Onorato9c1289c2009-08-17 11:03:03 -04001968 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 return false;
1970 }
1971
1972 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001973 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
Michael Jurka0280c3b2010-09-17 15:00:07 -07001976 resetAddInfo();
1977 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001979 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 return true;
1981 }
1982
Michael Jurka0280c3b2010-09-17 15:00:07 -07001983 final View itemUnderLongClick = longClickCellInfo.cell;
1984
Winson Chung304dcde2011-01-07 11:17:23 -08001985 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001986 if (itemUnderLongClick == null) {
1987 // User long pressed on empty space
1988 mWorkspace.setAllowLongPress(false);
1989 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1990 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001991 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001993 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001995 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1996 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001997 mAddIntersectCellX = longClickCellInfo.cellX;
1998 mAddIntersectCellY = longClickCellInfo.cellY;
1999 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001 }
2002 }
2003 return true;
2004 }
2005
Romain Guye6b8e2f2009-11-10 11:56:55 -08002006 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002007 private void dismissPreview(final View v) {
2008 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002009 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2011 public void onDismiss() {
2012 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2013 int count = group.getChildCount();
2014 for (int i = 0; i < count; i++) {
2015 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2016 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002017 ArrayList<Bitmap> bitmaps =
2018 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002019 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2020
2021 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002022 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002023 window.setOnDismissListener(null);
2024 }
2025 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002026 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002027 }
2028 v.setTag(null);
2029 }
2030
Romain Guyf8e6a802009-12-07 17:48:02 -08002031 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002032 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002033 }
2034
Romain Guya6abce82009-11-10 02:54:41 -08002035 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002036 final Resources resources = getResources();
2037 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002038
Romain Guya6abce82009-11-10 02:54:41 -08002039 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002040
Romain Guy9d31e9f2009-11-11 18:54:28 -08002041 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002042
Romain Guyf8e6a802009-12-07 17:48:02 -08002043 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002044 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002045 int extraW = (int) ((r.left + r.right) * max);
2046 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002047
2048 int aW = cell.getWidth() - extraW;
2049 float w = aW / max;
2050
2051 int width = cell.getWidth();
2052 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002053 int x = cell.getPaddingLeft();
2054 int y = cell.getPaddingTop();
2055 width -= (x + cell.getPaddingRight());
2056 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002057
2058 float scale = w / width;
2059
2060 int count = end - start;
2061
2062 final float sWidth = width * scale;
2063 float sHeight = height * scale;
2064
Romain Guye6b8e2f2009-11-10 11:56:55 -08002065 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002066
Romain Guye6b8e2f2009-11-10 11:56:55 -08002067 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2068 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002069
2070 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002071 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002072 cell = (CellLayout) workspace.getChildAt(i);
2073
Romain Guyf8e6a802009-12-07 17:48:02 -08002074 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002075 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002076
2077 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002078 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002079 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002080 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002081
Romain Guy9d31e9f2009-11-11 18:54:28 -08002082 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002083 image.setImageBitmap(bitmap);
2084 image.setTag(i);
2085 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002086 image.setOnFocusChangeListener(handler);
2087 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002088 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002089
2090 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002091 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2092
Winson Chungaafa03c2010-06-11 17:34:16 -07002093 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002094 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002095
2096 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002097 p.setContentView(preview);
2098 p.setWidth((int) (sWidth * count + extraW));
2099 p.setHeight((int) (sHeight + extraH));
2100 p.setAnimationStyle(R.style.AnimationPreview);
2101 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002102 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002103 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002104 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002105
Romain Guye6b8e2f2009-11-10 11:56:55 -08002106 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2107 public void onDismiss() {
2108 dismissPreview(anchor);
2109 }
2110 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002111
2112 anchor.setTag(p);
2113 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002114 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002115 }
2116
Romain Guy9d31e9f2009-11-11 18:54:28 -08002117 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002118 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002119
Romain Guye6b8e2f2009-11-10 11:56:55 -08002120 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002121 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002122 }
2123
2124 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002125 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002126 v.post(this);
2127 }
2128
2129 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002130 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002131 }
2132
2133 public void onFocusChange(View v, boolean hasFocus) {
2134 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002135 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002136 }
Romain Guya6abce82009-11-10 02:54:41 -08002137 }
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 Workspace getWorkspace() {
2141 return mWorkspace;
2142 }
2143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 @Override
2145 protected Dialog onCreateDialog(int id) {
2146 switch (id) {
2147 case DIALOG_CREATE_SHORTCUT:
2148 return new CreateShortcut().createDialog();
2149 case DIALOG_RENAME_FOLDER:
2150 return new RenameFolder().createDialog();
2151 }
2152
2153 return super.onCreateDialog(id);
2154 }
2155
2156 @Override
2157 protected void onPrepareDialog(int id, Dialog dialog) {
2158 switch (id) {
2159 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 break;
2161 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002162 if (mFolderInfo != null) {
2163 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2164 final CharSequence text = mFolderInfo.title;
2165 input.setText(text);
2166 input.setSelection(0, text.length());
2167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 break;
2169 }
2170 }
2171
2172 void showRenameDialog(FolderInfo info) {
2173 mFolderInfo = info;
2174 mWaitingForResult = true;
2175 showDialog(DIALOG_RENAME_FOLDER);
2176 }
2177
Michael Jurka0280c3b2010-09-17 15:00:07 -07002178 private void showAddDialog(int intersectX, int intersectY) {
2179 resetAddInfo();
2180 mAddIntersectCellX = intersectX;
2181 mAddIntersectCellY = intersectY;
2182 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 mWaitingForResult = true;
2184 showDialog(DIALOG_CREATE_SHORTCUT);
2185 }
2186
Joe Onoratodeb98af2010-02-19 14:59:39 -08002187 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002188 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002189 Bundle bundle = new Bundle();
2190
2191 ArrayList<String> shortcutNames = new ArrayList<String>();
2192 shortcutNames.add(getString(R.string.group_applications));
2193 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2194
2195 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2196 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2197 R.drawable.ic_launcher_application));
2198 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2199
2200 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2201 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002202 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002203 pickIntent.putExtras(bundle);
2204
Joe Onoratodeb98af2010-02-19 14:59:39 -08002205 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002206 }
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 private class RenameFolder {
2209 private EditText mInput;
2210
2211 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2213 mInput = (EditText) layout.findViewById(R.id.folder_name);
2214
2215 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2216 builder.setIcon(0);
2217 builder.setTitle(getString(R.string.rename_folder_title));
2218 builder.setCancelable(true);
2219 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2220 public void onCancel(DialogInterface dialog) {
2221 cleanup();
2222 }
2223 });
2224 builder.setNegativeButton(getString(R.string.cancel_action),
2225 new Dialog.OnClickListener() {
2226 public void onClick(DialogInterface dialog, int which) {
2227 cleanup();
2228 }
2229 }
2230 );
2231 builder.setPositiveButton(getString(R.string.rename_action),
2232 new Dialog.OnClickListener() {
2233 public void onClick(DialogInterface dialog, int which) {
2234 changeFolderName();
2235 }
2236 }
2237 );
2238 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002239
2240 final AlertDialog dialog = builder.create();
2241 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2242 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002243 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002244 mInput.requestFocus();
2245 InputMethodManager inputManager = (InputMethodManager)
2246 getSystemService(Context.INPUT_METHOD_SERVICE);
2247 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002248 }
2249 });
2250
2251 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002252 }
2253
2254 private void changeFolderName() {
2255 final String name = mInput.getText().toString();
2256 if (!TextUtils.isEmpty(name)) {
2257 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002258 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 mFolderInfo.title = name;
2260 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2261
Joe Onorato9c1289c2009-08-17 11:03:03 -04002262 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002263 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002264 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 } else {
2266 final FolderIcon folderIcon = (FolderIcon)
2267 mWorkspace.getViewForTag(mFolderInfo);
2268 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002269 // TODO: At some point we'll probably want some version of setting
2270 // the text for a folder icon.
2271 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 getWorkspace().requestLayout();
2273 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002274 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002275 mWorkspaceLoading = true;
2276 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277 }
2278 }
2279 }
2280 cleanup();
2281 }
2282
2283 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 dismissDialog(DIALOG_RENAME_FOLDER);
2285 mWaitingForResult = false;
2286 mFolderInfo = null;
2287 }
2288 }
2289
Daniel Sandler843e8602010-06-07 14:59:01 -04002290 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2291 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002292 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002293 }
2294
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002295 // AllAppsView.Watcher
2296 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002298 mWorkspace.setVisibility(View.GONE);
2299 }
2300 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002301
2302 /**
2303 * Helper method for the cameraZoomIn/cameraZoomOut animations
2304 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 * @param scaleFactor The scale factor used for the zoom
2307 */
2308 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2309 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002310
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002312 // Set pivotY so that at the starting zoom factor, the view is partially
2313 // visible. Modifying initialHeightFactor changes how much of the view is
2314 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002315 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002316 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002317 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002318 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002319 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002320 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002321 }
2322 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002323
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002324 /**
2325 * Zoom the camera out from the workspace to reveal 'toView'.
2326 * Assumes that the view to show is anchored at either the very top or very bottom
2327 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002328 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002329 */
Winson Chung20f71112011-04-06 14:22:04 -07002330 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002332
Winson Chungf0ea4d32011-06-06 14:27:16 -07002333 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2334 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2335 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2336 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002337
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 setPivotsForZoom(toView, toState, scale);
2339
Winson Chung20f71112011-04-06 14:22:04 -07002340
Winson Chungf0ea4d32011-06-06 14:27:16 -07002341 if (springLoaded) {
2342 if (toState == State.APPS_CUSTOMIZE) {
2343 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2344 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2345 } else {
2346 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2347 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
Winson Chung20f71112011-04-06 14:22:04 -07002348 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349 } else {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
2351 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002352 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002353
2354 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002355 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002356 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002357 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2358 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002359 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002360 toView.setFastScaleX(a * scale + b * 1f);
2361 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002362 }
2363 });
Adam Cohen61033d32010-11-15 18:29:44 -08002364
Winson Chungf0ea4d32011-06-06 14:27:16 -07002365 toView.setVisibility(View.VISIBLE);
2366 toView.setFastAlpha(0f);
2367 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2368 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2369 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2370 public void onAnimationUpdate(float a, float b) {
2371 // don't need to invalidate because we do so above
2372 toView.setFastAlpha(a * 0f + b * 1f);
2373 }
2374 });
2375 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002376
Michael Jurkaabded662011-03-04 12:06:57 -08002377 if (toView instanceof LauncherTransitionable) {
2378 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002379 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002380 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002381 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002382 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002383 // Prepare the position
2384 toView.setTranslationX(0.0f);
2385 toView.setTranslationY(0.0f);
2386 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002387 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002388 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002389 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002390 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002391 // If we don't set the final scale values here, if this animation is cancelled
2392 // it will have the wrong scale value and subsequent cameraPan animations will
2393 // not fix that
2394 toView.setScaleX(1.0f);
2395 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002396 if (toView instanceof LauncherTransitionable) {
2397 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002398 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002399 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002400 });
2401
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002403 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002404
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002405 if (mStateAnimation != null) mStateAnimation.cancel();
2406 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002407 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002408 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002409 } else {
2410 toView.setTranslationX(0.0f);
2411 toView.setTranslationY(0.0f);
2412 toView.setScaleX(1.0f);
2413 toView.setScaleY(1.0f);
2414 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002415 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002416 if (toView instanceof LauncherTransitionable) {
2417 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2418 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2419 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002420 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002421 }
2422
2423 /**
2424 * Zoom the camera back into the workspace, hiding 'fromView'.
2425 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002427 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002428 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002429 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002430 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002431
Winson Chungf0ea4d32011-06-06 14:27:16 -07002432 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2433 final float scaleFactor = (float)
2434 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2435 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002436
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002437 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002438
Michael Jurkad3ef3062010-11-23 16:23:58 -08002439 if (!springLoaded) {
2440 mWorkspace.unshrink(animated);
2441 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002442 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002443 if (mStateAnimation != null) mStateAnimation.cancel();
2444 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002445
Michael Jurka742574b2011-02-02 23:51:01 -08002446 final float oldScaleX = fromView.getScaleX();
2447 final float oldScaleY = fromView.getScaleY();
2448
2449 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2450 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002451 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2452 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002453 ((View)fromView.getParent()).fastInvalidate();
2454 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2455 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2456 }
2457 });
Michael Jurkaabded662011-03-04 12:06:57 -08002458 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002459 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002460 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002461 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2462 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002463 // don't need to invalidate because we do so above
2464 fromView.setFastAlpha(a * 1f + b * 0f);
2465 }
2466 });
Michael Jurkaabded662011-03-04 12:06:57 -08002467 if (fromView instanceof LauncherTransitionable) {
2468 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002469 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002470 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002471 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002472 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002474 if (fromView instanceof LauncherTransitionable) {
2475 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002476 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 }
2478 });
2479
Winson Chungf0ea4d32011-06-06 14:27:16 -07002480 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002481 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 } else {
2483 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002484 if (fromView instanceof LauncherTransitionable) {
2485 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2486 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2487 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002488 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002489 }
2490
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002491 void showWorkspace(boolean animated) {
2492 showWorkspace(animated, null);
2493 }
2494
2495 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002496 if (layout != null) {
2497 // always animated, but that's ok since we never specify a layout and
2498 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002499 mWorkspace.unshrink(layout);
2500 } else {
2501 mWorkspace.unshrink(animated);
2502 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002503 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002504 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002505 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002506
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002507 // Change the state *after* we've called all the transition code
2508 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002509
Winson Chung5fb63472011-02-02 17:03:37 -08002510 // Resume the auto-advance of widgets
2511 mUserPresent = true;
2512 updateRunning();
2513
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002514 // send an accessibility event to announce the context change
2515 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002516 }
2517
Michael Jurkad3ef3062010-11-23 16:23:58 -08002518 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002519 // Enter spring loaded mode on a new layout
Michael Jurkad3ef3062010-11-23 16:23:58 -08002520 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002521
2522 if (mState == State.APPS_CUSTOMIZE) {
2523 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2524 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungb26f3d62011-06-02 10:49:29 -07002525 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002526 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002527 }
2528
2529 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002530 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002531 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chungb26f3d62011-06-02 10:49:29 -07002532 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2533 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002534 }
2535 // Otherwise, we are not in spring loaded mode, so don't do anything.
2536 }
2537
2538 /**
2539 * Shows the dock/hotseat area.
2540 */
2541 void showDock(boolean animated) {
2542 if (!LauncherApplication.isScreenLarge()) {
2543 if (animated) {
2544 int duration = mSearchDeleteBar.getTransitionInDuration();
2545 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002546 } else {
2547 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002548 }
2549 }
2550 }
2551
2552 /**
2553 * Hides the dock/hotseat area.
2554 */
2555 void hideDock(boolean animated) {
2556 if (!LauncherApplication.isScreenLarge()) {
2557 if (animated) {
2558 int duration = mSearchDeleteBar.getTransitionOutDuration();
2559 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002560 } else {
2561 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002562 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002563 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002564 }
2565
Winson Chung785d2eb2011-04-14 16:08:02 -07002566 void showAllApps(boolean animated) {
2567 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002568
Winson Chungf0ea4d32011-06-06 14:27:16 -07002569 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2570 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002571
Winson Chungf0ea4d32011-06-06 14:27:16 -07002572 // Hide the search bar and dock
2573 mSearchDeleteBar.hideSearchBar(animated);
2574 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002575
Winson Chungf0ea4d32011-06-06 14:27:16 -07002576 // Change the state *after* we've called all the transition code
2577 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002578
2579 // Pause the auto-advance of widgets until we are out of AllApps
2580 mUserPresent = false;
2581 updateRunning();
2582
2583 // Send an accessibility event to announce the context change
2584 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2585 }
2586
Joe Onoratob2061212009-11-24 16:13:54 -05002587 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002588 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002589 * - Home from workspace
2590 * - from center screen
2591 * - from other screens
2592 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002593 * - from center screen
2594 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002595 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002596 * - from center screen
2597 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002598 * - Launch app from workspace and quit
2599 * - with back
2600 * - with home
2601 * - Launch app from all apps and quit
2602 * - with back
2603 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002604 * - Go to a screen that's not the default, then all
2605 * apps, and launch and app, and go back
2606 * - with back
2607 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002608 * - On workspace, long press power and go back
2609 * - with back
2610 * - with home
2611 * - On all apps, long press power and go back
2612 * - with back
2613 * - with home
2614 * - On workspace, power off
2615 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002616 * - Launch an app and turn off the screen while in that app
2617 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002618 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002619 * - From all apps
2620 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002621 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2622 * - From all apps
2623 * - From the center workspace
2624 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002625 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002626 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002627 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2628 mWorkspace.setVisibility(View.VISIBLE);
2629 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002630
Winson Chungf0ea4d32011-06-06 14:27:16 -07002631 // Show the search bar and dock
2632 mSearchDeleteBar.showSearchBar(animated);
2633 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002634
Winson Chungf0ea4d32011-06-06 14:27:16 -07002635 // Set focus to the AppsCustomize button
2636 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002637 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002638 }
2639
Joe Onorato7c312c12009-08-13 21:36:53 -07002640 void lockAllApps() {
2641 // TODO
2642 }
2643
2644 void unlockAllApps() {
2645 // TODO
2646 }
2647
Patrick Dubroy5f445422011-02-18 14:35:21 -08002648 /**
2649 * Add an item from all apps or customize onto the given workspace screen.
2650 * If layout is null, add to the current screen.
2651 */
2652 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002653 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2654 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002655 } else {
2656 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002657 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002658 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002659
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002660 void onWorkspaceClick(CellLayout layout) {
2661 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002662 }
2663
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002664 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002665 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002666 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002667 // Look for the toolbar icon specified in the activity meta-data
2668 Bundle metaData = packageManager.getActivityInfo(
2669 activityName, PackageManager.GET_META_DATA).metaData;
2670 if (metaData != null) {
2671 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2672 if (iconResId != 0) {
2673 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002674 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002675 }
2676 }
2677 } catch (NameNotFoundException e) {
2678 // Do nothing
2679 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002680 return null;
2681 }
2682
2683 // if successful in getting icon, return it; otherwise, set button to use default drawable
2684 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2685 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2686 TextView button = (TextView) findViewById(buttonId);
2687 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2688
2689 // If we were unable to find the icon via the meta-data, use a generic one
2690 if (toolbarIcon == null) {
2691 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2692 return null;
2693 } else {
2694 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2695 return toolbarIcon.getConstantState();
2696 }
2697 }
2698
2699 // if successful in getting icon, return it; otherwise, set button to use default drawable
2700 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2701 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2702 ImageView button = (ImageView) findViewById(buttonId);
2703 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2704
Michael Jurka0423dcf2010-10-05 14:56:18 -07002705 // If we were unable to find the icon via the meta-data, use a generic one
2706 if (toolbarIcon == null) {
2707 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002708 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002709 } else {
2710 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002711 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002712 }
2713 }
2714
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002715 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2716 TextView button = (TextView) findViewById(buttonId);
2717 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2718 }
2719
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002720 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002721 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002722 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002723 }
2724
Michael Jurka0423dcf2010-10-05 14:56:18 -07002725 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002726 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2727 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002728
Winson Chung1cad91e2011-05-25 17:41:01 -07002729 final SearchManager searchManager =
2730 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2731 ComponentName activityName = searchManager.getGlobalSearchActivity();
2732 if (activityName != null) {
2733 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
2734 // glass icon
2735 boolean iconLoaded = false;
2736 if (!LauncherApplication.isScreenLarge()) {
2737 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
2738 int orientation = getResources().getConfiguration().orientation;
2739 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
2740 searchButton.setImageResource(R.drawable.ic_generic_search);
2741 iconLoaded = true;
2742 sGlobalSearchIcon = null;
2743 }
2744 }
2745 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002746 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08002747 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002748 }
Winson Chung1cad91e2011-05-25 17:41:01 -07002749 searchButton.setVisibility(View.VISIBLE);
2750 searchDivider.setVisibility(View.VISIBLE);
2751 } else {
2752 searchButton.setVisibility(View.GONE);
2753 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002754 }
2755 }
2756
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002757 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002758 updateButtonWithDrawable(R.id.search_button, d);
2759 }
2760
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002761 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002762 final View searchDivider = findViewById(R.id.search_divider);
2763 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002764
Winson Chung1cad91e2011-05-25 17:41:01 -07002765 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2766 ComponentName activityName = intent.resolveActivity(getPackageManager());
2767 if (activityName != null) {
2768 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
2769 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2770 searchDivider.setVisibility(View.VISIBLE);
2771 voiceButton.setVisibility(View.VISIBLE);
2772 } else {
2773 searchDivider.setVisibility(View.GONE);
2774 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002775 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002776 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002777
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002778 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002779 updateButtonWithDrawable(R.id.voice_button, d);
2780 }
2781
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002782 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002783 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002784 */
2785 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002786 final View marketButton = findViewById(R.id.market_button);
2787 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2788 // Find the app market activity by resolving an intent.
2789 // (If multiple app markets are installed, it will return the ResolverActivity.)
2790 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002791 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002792 mAppMarketIntent = intent;
2793 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
2794 R.id.market_button, activityName, R.drawable.app_market_generic);
2795 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002796
2797 // Remove the shop icon text in the Phone UI
2798 if (!LauncherApplication.isScreenLarge()) {
2799 ((TextView) marketButton).setText("");
2800 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002801 } else {
2802 // We should hide and disable the view so that we don't try and restore the visibility
2803 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2804 marketButton.setVisibility(View.GONE);
2805 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002806 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002807 }
2808
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002809 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002810 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002811 }
2812
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002813 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 * Displays the shortcut creation dialog and launches, if necessary, the
2815 * appropriate activity.
2816 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002817 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002818 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2819 DialogInterface.OnShowListener {
2820
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002824 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002825
Winson Chung55b65502011-05-26 12:03:43 -07002826 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2827 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002828 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002829 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002830
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 AlertDialog dialog = builder.create();
2832 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002833 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002834 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 return dialog;
2837 }
2838
2839 public void onCancel(DialogInterface dialog) {
2840 mWaitingForResult = false;
2841 cleanup();
2842 }
2843
Romain Guycbb89e42009-06-08 15:52:54 -07002844 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002845 mWaitingForResult = false;
2846 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002847 }
2848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002850 try {
2851 dismissDialog(DIALOG_CREATE_SHORTCUT);
2852 } catch (Exception e) {
2853 // An exception is thrown if the dialog is not visible, which is fine
2854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 }
2856
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002857 /**
2858 * Handle the action clicked in the "Add to home" dialog.
2859 */
2860 public void onClick(DialogInterface dialog, int which) {
2861 Resources res = getResources();
2862 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002863
Winson Chung55b65502011-05-26 12:03:43 -07002864 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2865 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002866 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002867 if (mAppsCustomizeTabHost != null) {
2868 mAppsCustomizeTabHost.selectWidgetsTab();
2869 }
2870 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002871 break;
2872 }
Winson Chung55b65502011-05-26 12:03:43 -07002873 case AddAdapter.ITEM_APPLICATION: {
2874 if (mAppsCustomizeTabHost != null) {
2875 mAppsCustomizeTabHost.selectAppsTab();
2876 }
2877 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002878 break;
2879 }
Winson Chung55b65502011-05-26 12:03:43 -07002880 case AddAdapter.ITEM_APPWIDGET: {
2881 if (mAppsCustomizeTabHost != null) {
2882 mAppsCustomizeTabHost.selectWidgetsTab();
2883 }
2884 showAllApps(true);
2885 break;
2886 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002887 case AddAdapter.ITEM_WALLPAPER: {
2888 startWallpaper();
2889 break;
2890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 }
2892 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002893
2894 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002895 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 }
2898
2899 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002900 * Receives notifications when applications are added/removed.
2901 */
2902 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2903 @Override
2904 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002905 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002906 String reason = intent.getStringExtra("reason");
2907 if (!"homekey".equals(reason)) {
2908 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002909 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002910 animate = false;
2911 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002912 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002913 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002914 }
2915 }
2916
2917 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002918 * Receives notifications whenever the appwidgets are reset.
2919 */
2920 private class AppWidgetResetObserver extends ContentObserver {
2921 public AppWidgetResetObserver() {
2922 super(new Handler());
2923 }
2924
2925 @Override
2926 public void onChange(boolean selfChange) {
2927 onAppWidgetReset();
2928 }
2929 }
2930
2931 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002932 * If the activity is currently paused, signal that we need to re-run the loader
2933 * in onResume.
2934 *
2935 * This needs to be called from incoming places where resources might have been loaded
2936 * while we are paused. That is becaues the Configuration might be wrong
2937 * when we're not running, and if it comes back to what it was when we
2938 * were paused, we are not restarted.
2939 *
2940 * Implementation of the method from LauncherModel.Callbacks.
2941 *
2942 * @return true if we are currently paused. The caller might be able to
2943 * skip some work in that case since we will come back again.
2944 */
2945 public boolean setLoadOnResume() {
2946 if (mPaused) {
2947 Log.i(TAG, "setLoadOnResume");
2948 mOnResumeNeedsLoad = true;
2949 return true;
2950 } else {
2951 return false;
2952 }
2953 }
2954
2955 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002959 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002960 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002961 } else {
2962 return SCREEN_COUNT / 2;
2963 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002965
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002966
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 /**
2968 * Refreshes the shortcuts shown on the workspace.
2969 *
2970 * Implementation of the method from LauncherModel.Callbacks.
2971 */
2972 public void startBinding() {
2973 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002974
2975 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 int count = workspace.getChildCount();
2977 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002978 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002979 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2980 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002982
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 if (DEBUG_USER_INTERFACE) {
2984 android.widget.Button finishButton = new android.widget.Button(this);
2985 finishButton.setText("Finish");
2986 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2987
2988 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2989 public void onClick(View v) {
2990 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002991 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002994
2995 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
2996 // mDesktopItems -> AppWidgetInfo -> hostView).
2997 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002998 }
2999
3000 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 * Bind the items start-end from the list.
3002 *
3003 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003004 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3006
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003007 setLoadOnResume();
3008
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 final Workspace workspace = mWorkspace;
3010
3011 for (int i=start; i<end; i++) {
3012 final ItemInfo item = shortcuts.get(i);
3013 mDesktopItems.add(item);
3014 switch (item.itemType) {
3015 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3016 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003017 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003018 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3019 false);
3020 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003021 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003023 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003024 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3026 false);
3027 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003029 }
3030
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003032 }
3033
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 /**
3035 * Implementation of the method from LauncherModel.Callbacks.
3036 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003037 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003038 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003039 sFolders.clear();
3040 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042
3043 /**
3044 * Add the views for a widget to the workspace.
3045 *
3046 * Implementation of the method from LauncherModel.Callbacks.
3047 */
3048 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003049 setLoadOnResume();
3050
Daniel Sandler843e8602010-06-07 14:59:01 -04003051 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3052 if (DEBUG_WIDGETS) {
3053 Log.d(TAG, "bindAppWidget: " + item);
3054 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 final Workspace workspace = mWorkspace;
3056
3057 final int appWidgetId = item.appWidgetId;
3058 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003059 if (DEBUG_WIDGETS) {
3060 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3061 }
3062
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3064
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3066 item.hostView.setTag(item);
3067
3068 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3069 item.cellY, item.spanX, item.spanY, false);
3070
Adam Cohended9f8d2010-11-03 13:25:16 -07003071 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3072
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 workspace.requestLayout();
3074
3075 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003076
3077 if (DEBUG_WIDGETS) {
3078 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3079 + (SystemClock.uptimeMillis()-start) + "ms");
3080 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 }
3082
3083 /**
3084 * Callback saying that there aren't any more items to bind.
3085 *
3086 * Implementation of the method from LauncherModel.Callbacks.
3087 */
3088 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003089 setLoadOnResume();
3090
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 if (mSavedState != null) {
3092 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003093 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 }
3095
Adam Cohendf2cc412011-04-27 16:56:57 -07003096 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3097 if (folders != null) {
3098 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003099 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003100 final FolderIcon folderIcon = (FolderIcon)
3101 mWorkspace.getViewForTag(info);
3102 if (folderIcon != null) {
3103 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 }
3105 }
3106 final Folder openFolder = mWorkspace.getOpenFolder();
3107 if (openFolder != null) {
3108 openFolder.requestFocus();
3109 }
3110 }
3111
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 mSavedState = null;
3113 }
3114
3115 if (mSavedInstanceState != null) {
3116 super.onRestoreInstanceState(mSavedInstanceState);
3117 mSavedInstanceState = null;
3118 }
3119
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003121
3122 // If we received the result of any pending adds while the loader was running (e.g. the
3123 // widget configuration forced an orientation change), process them now.
3124 for (int i = 0; i < sPendingAddList.size(); i++) {
3125 completeAdd(sPendingAddList.get(i));
3126 }
3127 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 }
3129
3130 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003131 * Updates the icons on the launcher that are affected by changes to the package list
3132 * on the device.
3133 */
3134 private void updateIconsAffectedByPackageManagerChanges() {
3135 updateAppMarketIcon();
3136 updateGlobalSearchIcon();
3137 updateVoiceSearchIcon();
3138 }
3139
3140 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 * Add the icons for all apps.
3142 *
3143 * Implementation of the method from LauncherModel.Callbacks.
3144 */
3145 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 if (mAppsCustomizeContent != null) {
3147 mAppsCustomizeContent.setApps(apps);
3148 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003149 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 }
3151
3152 /**
3153 * A package was installed.
3154 *
3155 * Implementation of the method from LauncherModel.Callbacks.
3156 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003157 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003158 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003160
Winson Chung785d2eb2011-04-14 16:08:02 -07003161 if (mAppsCustomizeContent != null) {
3162 mAppsCustomizeContent.addApps(apps);
3163 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003164 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165 }
3166
3167 /**
3168 * A package was updated.
3169 *
3170 * Implementation of the method from LauncherModel.Callbacks.
3171 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003172 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003173 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003175 if (mWorkspace != null) {
3176 mWorkspace.updateShortcuts(apps);
3177 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178
Winson Chung785d2eb2011-04-14 16:08:02 -07003179 if (mAppsCustomizeContent != null) {
3180 mAppsCustomizeContent.updateApps(apps);
3181 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003182 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003183 }
3184
3185 /**
3186 * A package was uninstalled.
3187 *
3188 * Implementation of the method from LauncherModel.Callbacks.
3189 */
Joe Onorato36115782010-06-17 13:28:48 -04003190 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003192 if (permanent) {
3193 mWorkspace.removeItems(apps);
3194 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003195
Winson Chung785d2eb2011-04-14 16:08:02 -07003196 if (mAppsCustomizeContent != null) {
3197 mAppsCustomizeContent.removeApps(apps);
3198 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003199 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 }
Joe Onoratobe386092009-11-17 17:32:16 -08003201
3202 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003203 * A number of packages were updated.
3204 */
3205 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003206
Winson Chung785d2eb2011-04-14 16:08:02 -07003207 if (mAppsCustomizeContent != null) {
3208 mAppsCustomizeContent.onPackagesUpdated();
3209 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003210 }
3211
Winson Chung400438b2011-01-16 17:53:48 -08003212 private int mapConfigurationOriActivityInfoOri(int configOri) {
3213 final Display d = getWindowManager().getDefaultDisplay();
3214 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3215 switch (d.getRotation()) {
3216 case Surface.ROTATION_0:
3217 case Surface.ROTATION_180:
3218 // We are currently in the same basic orientation as the natural orientation
3219 naturalOri = configOri;
3220 break;
3221 case Surface.ROTATION_90:
3222 case Surface.ROTATION_270:
3223 // We are currently in the other basic orientation to the natural orientation
3224 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3225 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3226 break;
3227 }
3228
3229 int[] oriMap = {
3230 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3231 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3232 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3233 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3234 };
3235 // Since the map starts at portrait, we need to offset if this device's natural orientation
3236 // is landscape.
3237 int indexOffset = 0;
3238 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3239 indexOffset = 1;
3240 }
3241 return oriMap[(d.getRotation() + indexOffset) % 4];
3242 }
3243 public void lockScreenOrientation() {
3244 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3245 .getConfiguration().orientation));
3246 }
3247 public void unlockScreenOrientation() {
3248 mHandler.postDelayed(new Runnable() {
3249 public void run() {
3250 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3251 }
3252 }, mRestoreScreenOrientationDelay);
3253 }
3254
Winson Chung80baf5a2010-08-09 16:03:15 -07003255 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003256 * Prints out out state for debugging.
3257 */
3258 public void dumpState() {
3259 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003260 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003261 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3262 Log.d(TAG, "mRestoring=" + mRestoring);
3263 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3264 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3265 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003266 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003267 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003268
Winson Chung785d2eb2011-04-14 16:08:02 -07003269 if (mAppsCustomizeContent != null) {
3270 mAppsCustomizeContent.dumpState();
3271 }
Joe Onoratobe386092009-11-17 17:32:16 -08003272 Log.d(TAG, "END launcher2 dump state");
3273 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003274}
Michael Jurka2763be32011-02-24 11:19:57 -08003275
Michael Jurkaabded662011-03-04 12:06:57 -08003276interface LauncherTransitionable {
3277 void onLauncherTransitionStart(Animator animation);
3278 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003279}