blob: 54b7b9c2c130529f9c65a026c28004f25bb8265e [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.pm.ResolveInfo;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070081import android.view.ViewGroup;
Michael Jurkae0f5a612011-02-07 16:45:41 -080082import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080083import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
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
Adam Cohen716b51e2011-06-30 12:09:54 -0700436 public DragLayer getDragLayer() {
437 return mDragLayer;
438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 static int getScreen() {
441 synchronized (sLock) {
442 return sScreen;
443 }
444 }
445
446 static void setScreen(int screen) {
447 synchronized (sLock) {
448 sScreen = screen;
449 }
450 }
451
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400452 // Note: This doesn't do all the client-id magic that BrowserProvider does
453 // in Browser. (http://b/2425179)
454 private Uri getDefaultBrowserUri() {
455 String url = getString(R.string.default_browser_url);
456 if (url.indexOf("{CID}") != -1) {
457 url = url.replace("{CID}", "android-google");
458 }
459 return Uri.parse(url);
460 }
461
462 // Load the Intent templates from arrays.xml to populate the hotseats. For
463 // each Intent, if it resolves to a single app, use that as the launch
464 // intent & use that app's label as the contentDescription. Otherwise,
465 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400466 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400467 if (mHotseatConfig == null) {
468 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
469 if (mHotseatConfig.length > 0) {
470 mHotseats = new Intent[mHotseatConfig.length];
471 mHotseatLabels = new CharSequence[mHotseatConfig.length];
472 mHotseatIcons = new Drawable[mHotseatConfig.length];
473 } else {
474 mHotseats = null;
475 mHotseatIcons = null;
476 mHotseatLabels = null;
477 }
478
479 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
480 for (int i=0; i<mHotseatConfig.length; i++) {
481 // load icon for this slot; currently unrelated to the actual activity
482 try {
483 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
484 } catch (ArrayIndexOutOfBoundsException ex) {
485 Log.w(TAG, "Missing hotseat_icons array item #" + i);
486 mHotseatIcons[i] = null;
487 }
488 }
489 hotseatIconDrawables.recycle();
490 }
491
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400492 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 for (int i=0; i<mHotseatConfig.length; i++) {
494 Intent intent = null;
495 if (mHotseatConfig[i].equals("*BROWSER*")) {
496 // magic value meaning "launch user's default web browser"
497 // replace it with a generic web request so we can see if there is indeed a default
498 String defaultUri = getString(R.string.default_browser_url);
499 intent = new Intent(
500 Intent.ACTION_VIEW,
501 ((defaultUri != null)
502 ? Uri.parse(defaultUri)
503 : getDefaultBrowserUri())
504 ).addCategory(Intent.CATEGORY_BROWSABLE);
505 // note: if the user launches this without a default set, she
506 // will always be taken to the default URL above; this is
507 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700508 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400509 // URL is unavoidably sent to the chosen app.
510 } else {
511 try {
512 intent = Intent.parseUri(mHotseatConfig[i], 0);
513 } catch (java.net.URISyntaxException ex) {
514 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
515 // bogus; leave intent=null
516 }
517 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700518
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 if (intent == null) {
520 mHotseats[i] = null;
521 mHotseatLabels[i] = getText(R.string.activity_not_found);
522 continue;
523 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400524
525 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700526 Log.d(TAG, "loadHotseats: hotseat " + i
527 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400528 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400529 + "]");
530 }
531
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400532 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
533 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700534 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400535 Log.d(TAG, "Best match for intent: " + bestMatch);
536 Log.d(TAG, "All matches: ");
537 for (ResolveInfo ri : allMatches) {
538 Log.d(TAG, " --> " + ri);
539 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400540 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400541 // did this resolve to a single app, or the resolver?
542 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700543 // can't find any activity to handle this. let's leave the
544 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 // to launch.
546 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400547
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400548 // set accessibility text to "Not installed"
549 mHotseatLabels[i] = getText(R.string.activity_not_found);
550 } else {
551 boolean found = false;
552 for (ResolveInfo ri : allMatches) {
553 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
554 && bestMatch.activityInfo.applicationInfo.packageName
555 .equals(ri.activityInfo.applicationInfo.packageName)) {
556 found = true;
557 break;
558 }
559 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 if (!found) {
562 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
563 // the bestMatch is probably the ResolveActivity, meaning the
564 // user has not yet selected a default
565 // so: we'll keep the original intent for now
566 mHotseats[i] = intent;
567
568 // set the accessibility text to "Select shortcut"
569 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
570 } else {
571 // we have an app!
572 // now reconstruct the intent to launch it through the front
573 // door
574 ComponentName com = new ComponentName(
575 bestMatch.activityInfo.applicationInfo.packageName,
576 bestMatch.activityInfo.name);
577 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
578
579 // load the app label for accessibility
580 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
581 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582 }
583
584 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700585 Log.d(TAG, "loadHotseats: hotseat " + i
586 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400587 + ((mHotseats[i] == null)
588 ? "null"
589 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400590 + "] label=[" + mHotseatLabels[i]
591 + "]"
592 );
593 }
594 }
595 }
596
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 private void completeAdd(PendingAddArguments args) {
598 switch (args.requestCode) {
599 case REQUEST_PICK_APPLICATION:
600 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
601 break;
602 case REQUEST_PICK_SHORTCUT:
603 processShortcut(args.intent);
604 break;
605 case REQUEST_CREATE_SHORTCUT:
606 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
607 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800608 case REQUEST_PICK_APPWIDGET:
609 addAppWidgetFromPick(args.intent);
610 break;
611 case REQUEST_CREATE_APPWIDGET:
612 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
613 completeAddAppWidget(appWidgetId, args.screen);
614 break;
615 case REQUEST_PICK_WALLPAPER:
616 // We just wanted the activity result here so we can clear mWaitingForResult
617 break;
618 }
619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700623 mWaitingForResult = false;
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 // The pattern used here is that a user PICKs a specific application,
626 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
629 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700630
Michael Jurka0280c3b2010-09-17 15:00:07 -0700631 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800632 final PendingAddArguments args = new PendingAddArguments();
633 args.requestCode = requestCode;
634 args.intent = data;
635 args.screen = mAddScreen;
636 args.cellX = mAddIntersectCellX;
637 args.cellY = mAddIntersectCellY;
638
639 // If the loader is still running, defer the add until it is done.
640 if (isWorkspaceLocked()) {
641 sPendingAddList.add(args);
642 } else {
643 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 }
Romain Guy18042c82009-11-06 11:44:55 -0800645 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
646 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
647 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700648 // Clean up the appWidgetId if we canceled
649 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
650 if (appWidgetId != -1) {
651 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800652 }
653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 }
655
656 @Override
657 protected void onResume() {
658 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800659 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700660 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400661 mWorkspaceLoading = true;
662 mModel.startLoader(this, true);
663 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700664 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800666 if (mWaitingForResume != null) {
667 mWaitingForResume.setStayPressed(false);
668 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700669 // When we resume Launcher, a different Activity might be responsible for the app
670 // market intent, so refresh the icon
671 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
673
674 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700675 protected void onPause() {
676 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700677 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800678 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700679 }
Romain Guycbb89e42009-06-08 15:52:54 -0700680
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700681 @Override
682 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400683 // Flag the loader to stop early before switching
684 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700685 if (mAppsCustomizeContent != null) {
686 mAppsCustomizeContent.surrender();
687 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800688 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700689 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700690
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800691 // We can't hide the IME if it was forced open. So don't bother
692 /*
693 @Override
694 public void onWindowFocusChanged(boolean hasFocus) {
695 super.onWindowFocusChanged(hasFocus);
696
697 if (hasFocus) {
698 final InputMethodManager inputManager = (InputMethodManager)
699 getSystemService(Context.INPUT_METHOD_SERVICE);
700 WindowManager.LayoutParams lp = getWindow().getAttributes();
701 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
702 android.os.Handler()) {
703 protected void onReceiveResult(int resultCode, Bundle resultData) {
704 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
705 }
706 });
707 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
708 }
709 }
710 */
711
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 private boolean acceptFilter() {
713 final InputMethodManager inputManager = (InputMethodManager)
714 getSystemService(Context.INPUT_METHOD_SERVICE);
715 return !inputManager.isFullscreenMode();
716 }
717
718 @Override
719 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700720 final int uniChar = event.getUnicodeChar();
721 final boolean handled = super.onKeyDown(keyCode, event);
722 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
723 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
725 keyCode, event);
726 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700727 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700728 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700729 // showSearchDialog()
730 // If there are multiple keystrokes before the search dialog takes focus,
731 // onSearchRequested() will be called for every keystroke,
732 // but it is idempotent, so it's fine.
733 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735 }
736
Joe Onorato8a9625e2010-01-28 15:55:35 -0800737 // Eat the long press event so the keyboard doesn't come up.
738 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
739 return true;
740 }
741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 return handled;
743 }
744
Karl Rosaen138a0412009-04-23 19:00:21 -0700745 private String getTypedText() {
746 return mDefaultKeySsb.toString();
747 }
748
749 private void clearTypedText() {
750 mDefaultKeySsb.clear();
751 mDefaultKeySsb.clearSpans();
752 Selection.setSelection(mDefaultKeySsb, 0);
753 }
754
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700756 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
757 * State
758 */
759 private static State intToState(int stateOrdinal) {
760 State state = State.WORKSPACE;
761 final State[] stateValues = State.values();
762 for (int i = 0; i < stateValues.length; i++) {
763 if (stateValues[i].ordinal() == stateOrdinal) {
764 state = stateValues[i];
765 break;
766 }
767 }
768 return state;
769 }
770
771 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 * Restores the previous state, if it exists.
773 *
774 * @param savedState The previous state.
775 */
776 private void restoreState(Bundle savedState) {
777 if (savedState == null) {
778 return;
779 }
780
Michael Jurka883f55b2010-10-21 15:47:14 -0700781 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
782
Winson Chungf0ea4d32011-06-06 14:27:16 -0700783 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800784 showAllApps(false);
785 }
786
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
788 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700789 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790 }
791
792 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700795 mAddScreen = addScreen;
796 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
797 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 mRestoring = true;
799 }
800
801 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
802 if (renameFolder) {
803 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700804 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 mRestoring = true;
806 }
Winson Chunga12a2502010-12-20 14:41:35 -0800807
Winson Chung785d2eb2011-04-14 16:08:02 -0700808
809 // Restore the AppsCustomize tab
810 if (mAppsCustomizeTabHost != null) {
811 String curTab = savedState.getString("apps_customize_currentTab");
812 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700813 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700814 mAppsCustomizeContent.setContentType(
815 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
816 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
817 }
818
819 // Note: currently we do not restore the page for the AppsCustomize pane because the
820 // change in layout can drastically affect the saved page index
821 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
823
824 /**
825 * Finds all the views we need and configure them properly.
826 */
827 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700828 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400829
Winson Chung4c98d922011-05-31 16:50:48 -0700830 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
831 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832
Winson Chung4c98d922011-05-31 16:50:48 -0700833 // Setup the drag layer
834 mDragLayer.setup(this, dragController);
835
836 // Setup the workspace
837 mWorkspace.setHapticFeedbackEnabled(false);
838 mWorkspace.setOnLongClickListener(this);
839 mWorkspace.setup(this, dragController);
840 mWorkspace.setWallpaperDimension();
841
Winson Chungf0ea4d32011-06-06 14:27:16 -0700842 // Get the search/delete bar
843 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700844
Winson Chungf0ea4d32011-06-06 14:27:16 -0700845 // Setup AppsCustomize
846 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
847 findViewById(R.id.apps_customize_pane);
848 mAppsCustomizeContent = (AppsCustomizePagedView)
849 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
850 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400851
Winson Chungf0ea4d32011-06-06 14:27:16 -0700852 // Setup AppsCustomize button
853 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
854 mAllAppsButton.setOnClickListener(new OnClickListener() {
855 public void onClick(View v) {
856 onClickAllAppsButton(v);
857 }
858 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800859
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 if (!LauncherApplication.isScreenLarge()) {
861 // Setup AppsCustomize button on the phone
862 HandleView handleView = (HandleView) mAllAppsButton;
863 handleView.setLauncher(this);
864 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700865
866 // Setup Hotseat
867 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
868 hotseatLeft.setContentDescription(mHotseatLabels[0]);
869 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
870 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
871 hotseatRight.setContentDescription(mHotseatLabels[1]);
872 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800873 }
874
Winson Chungf0ea4d32011-06-06 14:27:16 -0700875 if (!LauncherApplication.isScreenLarge()) {
876 // Setup keylistener for button cluster
877 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
878 View.OnKeyListener listener = null;
879 if (LauncherApplication.isScreenLarge()) {
880 // For tablets, AllApps lives in the button bar at the top
881 listener = new ButtonBarKeyEventListener();
882 } else {
883 // For phones, AppsCustomize lives in the "dock" at the bottom
884 listener = new DockKeyEventListener();
885 }
886 int buttonCount = mButtonCluster.getChildCount();
887 for (int i = 0; i < buttonCount; ++i) {
888 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
889 }
Winson Chung97d85d22011-04-13 11:27:36 -0700890 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800891
Winson Chung4c98d922011-05-31 16:50:48 -0700892 // Setup the drag controller (the drop targets have to be added in reverse order)
893 dragController.setDragScoller(mWorkspace);
894 dragController.setScrollView(mDragLayer);
895 dragController.setMoveTarget(mWorkspace);
896 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700897 if (mSearchDeleteBar != null) {
898 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 }
901
Romain Guy8a73c512009-11-09 19:19:59 -0800902 @SuppressWarnings({"UnusedDeclaration"})
903 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700904 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800905 mWorkspace.scrollLeft();
906 }
Romain Guy8a73c512009-11-09 19:19:59 -0800907 }
908
909 @SuppressWarnings({"UnusedDeclaration"})
910 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700911 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800912 mWorkspace.scrollRight();
913 }
Romain Guy8a73c512009-11-09 19:19:59 -0800914 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400915
916 @SuppressWarnings({"UnusedDeclaration"})
917 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700918 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400919
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400920 int index = -1;
921 if (v.getId() == R.id.hotseat_left) {
922 index = 0;
923 } else if (v.getId() == R.id.hotseat_right) {
924 index = 1;
925 }
926
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400927 // reload these every tap; you never know when they might change
928 loadHotseats();
929 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400930 startActivitySafely(
931 mHotseats[index],
932 "hotseat"
933 );
934 }
935 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700936
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 /**
938 * Creates a view representing a shortcut.
939 *
940 * @param info The data structure describing the shortcut.
941 *
942 * @return A View inflated from R.layout.application.
943 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800944 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700946 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948
949 /**
950 * Creates a view representing a shortcut inflated from the specified resource.
951 *
952 * @param layoutResId The id of the XML layout used to create the shortcut.
953 * @param parent The group the shortcut belongs to.
954 * @param info The data structure describing the shortcut.
955 *
956 * @return A View inflated from layoutResId.
957 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800958 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800959 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
960 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 return favorite;
963 }
964
965 /**
966 * Add an application shortcut to the workspace.
967 *
968 * @param data The intent describing the application.
969 * @param cellInfo The position on screen where to create the shortcut.
970 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800971 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700972 int intersectCellX, int intersectCellY) {
973 final int[] cellXY = mTmpAddItemCellCoordinates;
974 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
975
976 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
977 showOutOfSpaceMessage();
978 return;
979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800981 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800982
Romain Guy73b979d2009-06-09 12:57:21 -0700983 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800984 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800986 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700987 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
988 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800989 } else {
990 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 /**
995 * Add a shortcut to the workspace.
996 *
997 * @param data The intent describing the shortcut.
998 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001000 private void completeAddShortcut(Intent data, int screen,
1001 int intersectCellX, int intersectCellY) {
1002 final int[] cellXY = mTmpAddItemCellCoordinates;
1003 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001004
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001005 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001006 boolean foundCellSpan = false;
1007 if (touchXY != null) {
1008 // when dragging and dropping, just find the closest free spot
1009 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1010 int[] result = screenLayout.findNearestVacantArea(
1011 touchXY[0], touchXY[1], 1, 1, cellXY);
1012 foundCellSpan = (result != null);
1013 } else {
1014 foundCellSpan = layout.findCellForSpanThatIntersects(
1015 cellXY, 1, 1, intersectCellX, intersectCellY);
1016 }
1017
1018 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001019 showOutOfSpaceMessage();
1020 return;
1021 }
1022
1023 final ShortcutInfo info = mModel.addShortcut(
1024 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
1026 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030 }
1031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001033 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001035 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001036 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 */
Winson Chungf7640c82011-02-28 13:47:29 -08001038 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001039 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001041 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1043 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001046 // We have saved the position to which the widget was dragged-- this really only matters
1047 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001049
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001050 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 boolean foundCellSpan = false;
1052 if (touchXY != null) {
1053 // when dragging and dropping, just find the closest free spot
1054 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1055 int[] result = screenLayout.findNearestVacantArea(
1056 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001057 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001058 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001059 // if we long pressed on an empty cell to bring up a menu,
1060 // make sure we intersect the empty cell
1061 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1062 // findCellForSpanThatIntersects will just ignore them
1063 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1064 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001065 }
1066
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001068 if (appWidgetId != -1) {
1069 // Deleting an app widget ID is a void call but writes to disk before returning
1070 // to the caller...
1071 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1072 new Thread("deleteAppWidgetId") {
1073 public void run() {
1074 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1075 }
1076 }.start();
1077 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001079 return;
1080 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 // Build Launcher-specific widget info and save to database
1083 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001084 launcherInfo.spanX = spanXY[0];
1085 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 LauncherModel.addItemToDatabase(this, launcherInfo,
1088 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
1091 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001092 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001096
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001097 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001099
Michael Jurka0280c3b2010-09-17 15:00:07 -07001100 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001102
1103 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 }
1105 }
Romain Guycbb89e42009-06-08 15:52:54 -07001106
Adam Cohended9f8d2010-11-03 13:25:16 -07001107 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1108 @Override
1109 public void onReceive(Context context, Intent intent) {
1110 final String action = intent.getAction();
1111 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1112 mUserPresent = false;
1113 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001114
1115 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001116 if (mAppsCustomizeContent != null) {
1117 mAppsCustomizeContent.reset();
1118 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001119 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1120 mUserPresent = true;
1121 updateRunning();
1122 }
1123 }
1124 };
1125
1126 @Override
1127 public void onAttachedToWindow() {
1128 super.onAttachedToWindow();
1129
1130 // Listen for broadcasts related to user-presence
1131 final IntentFilter filter = new IntentFilter();
1132 filter.addAction(Intent.ACTION_SCREEN_OFF);
1133 filter.addAction(Intent.ACTION_USER_PRESENT);
1134 registerReceiver(mReceiver, filter);
1135
Adam Cohend113e0c2010-11-11 10:48:05 -08001136 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001137 mVisible = true;
1138 }
1139
1140 @Override
1141 public void onDetachedFromWindow() {
1142 super.onDetachedFromWindow();
1143 mVisible = false;
1144
Adam Cohend113e0c2010-11-11 10:48:05 -08001145 if (mAttached) {
1146 unregisterReceiver(mReceiver);
1147 mAttached = false;
1148 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001149 updateRunning();
1150 }
1151
1152 public void onWindowVisibilityChanged(int visibility) {
1153 mVisible = visibility == View.VISIBLE;
1154 updateRunning();
1155 }
1156
1157 private void sendAdvanceMessage(long delay) {
1158 mHandler.removeMessages(ADVANCE_MSG);
1159 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1160 mHandler.sendMessageDelayed(msg, delay);
1161 mAutoAdvanceSentTime = System.currentTimeMillis();
1162 }
1163
1164 private void updateRunning() {
1165 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1166 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1167 mAutoAdvanceRunning = autoAdvanceRunning;
1168 if (autoAdvanceRunning) {
1169 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1170 sendAdvanceMessage(delay);
1171 } else {
1172 if (!mWidgetsToAdvance.isEmpty()) {
1173 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1174 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1175 }
1176 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001177 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001178 }
1179 }
1180 }
1181
1182 private final Handler mHandler = new Handler() {
1183 @Override
1184 public void handleMessage(Message msg) {
1185 if (msg.what == ADVANCE_MSG) {
1186 int i = 0;
1187 for (View key: mWidgetsToAdvance.keySet()) {
1188 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1189 final int delay = mAdvanceStagger * i;
1190 if (v instanceof Advanceable) {
1191 postDelayed(new Runnable() {
1192 public void run() {
1193 ((Advanceable) v).advance();
1194 }
1195 }, delay);
1196 }
1197 i++;
1198 }
1199 sendAdvanceMessage(mAdvanceInterval);
1200 }
1201 }
1202 };
1203
1204 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1205 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1206 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1207 if (v instanceof Advanceable) {
1208 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001209 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001210 updateRunning();
1211 }
1212 }
1213
1214 void removeWidgetToAutoAdvance(View hostView) {
1215 if (mWidgetsToAdvance.containsKey(hostView)) {
1216 mWidgetsToAdvance.remove(hostView);
1217 updateRunning();
1218 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001219 }
1220
Joe Onorato9c1289c2009-08-17 11:03:03 -04001221 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1222 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001224 launcherInfo.hostView = null;
1225 }
1226
Adam Cohended9f8d2010-11-03 13:25:16 -07001227 void showOutOfSpaceMessage() {
1228 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1229 }
1230
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001231 public LauncherAppWidgetHost getAppWidgetHost() {
1232 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
Romain Guycbb89e42009-06-08 15:52:54 -07001234
Winson Chunga9abd0e2010-10-27 17:18:37 -07001235 public LauncherModel getModel() {
1236 return mModel;
1237 }
1238
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001239 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001240 getWindow().closeAllPanels();
1241
1242 try {
1243 dismissDialog(DIALOG_CREATE_SHORTCUT);
1244 // Unlock the workspace if the dialog was showing
1245 } catch (Exception e) {
1246 // An exception is thrown if the dialog is not visible, which is fine
1247 }
1248
1249 try {
1250 dismissDialog(DIALOG_RENAME_FOLDER);
1251 // Unlock the workspace if the dialog was showing
1252 } catch (Exception e) {
1253 // An exception is thrown if the dialog is not visible, which is fine
1254 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001255
1256 // Whatever we were doing is hereby canceled.
1257 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001258 }
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 @Override
1261 protected void onNewIntent(Intent intent) {
1262 super.onNewIntent(intent);
1263
1264 // Close the menu
1265 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001266 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001267 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001268
Joe Onorato14f122b2009-11-19 14:06:36 -08001269 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1270 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001271
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001272 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001273 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001274 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001275 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001276 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001277 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001278 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001279
Joe Onorato3a8820b2009-11-10 15:06:42 -08001280 final View v = getWindow().peekDecorView();
1281 if (v != null && v.getWindowToken() != null) {
1282 InputMethodManager imm = (InputMethodManager)getSystemService(
1283 INPUT_METHOD_SERVICE);
1284 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001286
1287 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001288 if (mAppsCustomizeContent != null) {
1289 mAppsCustomizeContent.reset();
1290 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292 }
1293
1294 @Override
1295 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1296 // Do not call super here
1297 mSavedInstanceState = savedInstanceState;
1298 }
1299
1300 @Override
1301 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001302 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303
Adam Cohen8e776a62011-06-28 18:10:06 -07001304 final Folder folder = mWorkspace.getOpenFolder();
1305 if (folder != null) {
1306 long[] ids = new long[1];
1307 ids[0] = folder.getInfo().id;
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) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001715 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
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 Cohen8e776a62011-06-28 18:10:06 -07001948 mDragLayer.addView(folder);
1949 mDragController.addDropTarget((DropTarget) folder);
1950
Adam Cohena9cf38f2011-05-02 15:36:58 -07001951 folder.animateOpen();
1952 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001956 if (mState != State.WORKSPACE) {
1957 return false;
1958 }
1959
Romain Guy1fbc1c82009-11-09 20:43:08 -08001960 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001961 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001962 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001963 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1964 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1965 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001966 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001967 return true;
1968 }
1969
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 return false;
1972 }
1973
1974 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001975 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 }
1977
Michael Jurka0280c3b2010-09-17 15:00:07 -07001978 resetAddInfo();
1979 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001981 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 return true;
1983 }
1984
Michael Jurka0280c3b2010-09-17 15:00:07 -07001985 final View itemUnderLongClick = longClickCellInfo.cell;
1986
Winson Chung304dcde2011-01-07 11:17:23 -08001987 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001988 if (itemUnderLongClick == null) {
1989 // User long pressed on empty space
1990 mWorkspace.setAllowLongPress(false);
1991 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1992 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07001993 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001995 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001997 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1998 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001999 mAddIntersectCellX = longClickCellInfo.cellX;
2000 mAddIntersectCellY = longClickCellInfo.cellY;
2001 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003 }
2004 }
2005 return true;
2006 }
2007
Romain Guye6b8e2f2009-11-10 11:56:55 -08002008 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002009 private void dismissPreview(final View v) {
2010 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002011 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002012 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2013 public void onDismiss() {
2014 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2015 int count = group.getChildCount();
2016 for (int i = 0; i < count; i++) {
2017 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2018 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002019 ArrayList<Bitmap> bitmaps =
2020 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002021 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2022
2023 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002024 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002025 window.setOnDismissListener(null);
2026 }
2027 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002028 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002029 }
2030 v.setTag(null);
2031 }
2032
Romain Guyf8e6a802009-12-07 17:48:02 -08002033 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002034 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002035 }
2036
Romain Guya6abce82009-11-10 02:54:41 -08002037 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002038 final Resources resources = getResources();
2039 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002040
Romain Guya6abce82009-11-10 02:54:41 -08002041 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002042
Romain Guy9d31e9f2009-11-11 18:54:28 -08002043 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002044
Romain Guyf8e6a802009-12-07 17:48:02 -08002045 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002047 int extraW = (int) ((r.left + r.right) * max);
2048 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002049
2050 int aW = cell.getWidth() - extraW;
2051 float w = aW / max;
2052
2053 int width = cell.getWidth();
2054 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002055 int x = cell.getPaddingLeft();
2056 int y = cell.getPaddingTop();
2057 width -= (x + cell.getPaddingRight());
2058 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002059
2060 float scale = w / width;
2061
2062 int count = end - start;
2063
2064 final float sWidth = width * scale;
2065 float sHeight = height * scale;
2066
Romain Guye6b8e2f2009-11-10 11:56:55 -08002067 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002068
Romain Guye6b8e2f2009-11-10 11:56:55 -08002069 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2070 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002071
2072 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002073 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002074 cell = (CellLayout) workspace.getChildAt(i);
2075
Romain Guyf8e6a802009-12-07 17:48:02 -08002076 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002077 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002078
2079 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002080 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002081 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002082 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002083
Romain Guy9d31e9f2009-11-11 18:54:28 -08002084 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002085 image.setImageBitmap(bitmap);
2086 image.setTag(i);
2087 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002088 image.setOnFocusChangeListener(handler);
2089 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002090 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002091
2092 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002093 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2094
Winson Chungaafa03c2010-06-11 17:34:16 -07002095 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002096 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002097
2098 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002099 p.setContentView(preview);
2100 p.setWidth((int) (sWidth * count + extraW));
2101 p.setHeight((int) (sHeight + extraH));
2102 p.setAnimationStyle(R.style.AnimationPreview);
2103 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002104 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002105 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002106 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002107
Romain Guye6b8e2f2009-11-10 11:56:55 -08002108 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2109 public void onDismiss() {
2110 dismissPreview(anchor);
2111 }
2112 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002113
2114 anchor.setTag(p);
2115 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002116 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002117 }
2118
Romain Guy9d31e9f2009-11-11 18:54:28 -08002119 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002120 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002121
Romain Guye6b8e2f2009-11-10 11:56:55 -08002122 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002123 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002124 }
2125
2126 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002127 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002128 v.post(this);
2129 }
2130
2131 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002132 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002133 }
2134
2135 public void onFocusChange(View v, boolean hasFocus) {
2136 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002137 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002138 }
Romain Guya6abce82009-11-10 02:54:41 -08002139 }
2140 }
2141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 Workspace getWorkspace() {
2143 return mWorkspace;
2144 }
2145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 @Override
2147 protected Dialog onCreateDialog(int id) {
2148 switch (id) {
2149 case DIALOG_CREATE_SHORTCUT:
2150 return new CreateShortcut().createDialog();
2151 case DIALOG_RENAME_FOLDER:
2152 return new RenameFolder().createDialog();
2153 }
2154
2155 return super.onCreateDialog(id);
2156 }
2157
2158 @Override
2159 protected void onPrepareDialog(int id, Dialog dialog) {
2160 switch (id) {
2161 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 break;
2163 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002164 if (mFolderInfo != null) {
2165 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2166 final CharSequence text = mFolderInfo.title;
2167 input.setText(text);
2168 input.setSelection(0, text.length());
2169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 break;
2171 }
2172 }
2173
2174 void showRenameDialog(FolderInfo info) {
2175 mFolderInfo = info;
2176 mWaitingForResult = true;
2177 showDialog(DIALOG_RENAME_FOLDER);
2178 }
2179
Michael Jurka0280c3b2010-09-17 15:00:07 -07002180 private void showAddDialog(int intersectX, int intersectY) {
2181 resetAddInfo();
2182 mAddIntersectCellX = intersectX;
2183 mAddIntersectCellY = intersectY;
2184 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 mWaitingForResult = true;
2186 showDialog(DIALOG_CREATE_SHORTCUT);
2187 }
2188
Joe Onoratodeb98af2010-02-19 14:59:39 -08002189 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002190 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002191 Bundle bundle = new Bundle();
2192
2193 ArrayList<String> shortcutNames = new ArrayList<String>();
2194 shortcutNames.add(getString(R.string.group_applications));
2195 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2196
2197 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2198 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2199 R.drawable.ic_launcher_application));
2200 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2201
2202 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2203 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002204 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002205 pickIntent.putExtras(bundle);
2206
Joe Onoratodeb98af2010-02-19 14:59:39 -08002207 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002208 }
2209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 private class RenameFolder {
2211 private EditText mInput;
2212
2213 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2215 mInput = (EditText) layout.findViewById(R.id.folder_name);
2216
2217 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2218 builder.setIcon(0);
2219 builder.setTitle(getString(R.string.rename_folder_title));
2220 builder.setCancelable(true);
2221 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2222 public void onCancel(DialogInterface dialog) {
2223 cleanup();
2224 }
2225 });
2226 builder.setNegativeButton(getString(R.string.cancel_action),
2227 new Dialog.OnClickListener() {
2228 public void onClick(DialogInterface dialog, int which) {
2229 cleanup();
2230 }
2231 }
2232 );
2233 builder.setPositiveButton(getString(R.string.rename_action),
2234 new Dialog.OnClickListener() {
2235 public void onClick(DialogInterface dialog, int which) {
2236 changeFolderName();
2237 }
2238 }
2239 );
2240 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002241
2242 final AlertDialog dialog = builder.create();
2243 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2244 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002245 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002246 mInput.requestFocus();
2247 InputMethodManager inputManager = (InputMethodManager)
2248 getSystemService(Context.INPUT_METHOD_SERVICE);
2249 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002250 }
2251 });
2252
2253 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 }
2255
2256 private void changeFolderName() {
2257 final String name = mInput.getText().toString();
2258 if (!TextUtils.isEmpty(name)) {
2259 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002260 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 mFolderInfo.title = name;
2262 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2263
Joe Onorato9c1289c2009-08-17 11:03:03 -04002264 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002265 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002266 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 } else {
2268 final FolderIcon folderIcon = (FolderIcon)
2269 mWorkspace.getViewForTag(mFolderInfo);
2270 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002271 // TODO: At some point we'll probably want some version of setting
2272 // the text for a folder icon.
2273 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 getWorkspace().requestLayout();
2275 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002276 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 mWorkspaceLoading = true;
2278 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280 }
2281 }
2282 cleanup();
2283 }
2284
2285 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 dismissDialog(DIALOG_RENAME_FOLDER);
2287 mWaitingForResult = false;
2288 mFolderInfo = null;
2289 }
2290 }
2291
Daniel Sandler843e8602010-06-07 14:59:01 -04002292 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2293 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002294 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002295 }
2296
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002297 // AllAppsView.Watcher
2298 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002300 mWorkspace.setVisibility(View.GONE);
2301 }
2302 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002303
2304 /**
2305 * Helper method for the cameraZoomIn/cameraZoomOut animations
2306 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002307 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002308 * @param scaleFactor The scale factor used for the zoom
2309 */
2310 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2311 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002312
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002314 // Set pivotY so that at the starting zoom factor, the view is partially
2315 // visible. Modifying initialHeightFactor changes how much of the view is
2316 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002317 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002318 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002319 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002320 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002321 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002322 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002323 }
2324 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002325
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002326 /**
2327 * Zoom the camera out from the workspace to reveal 'toView'.
2328 * Assumes that the view to show is anchored at either the very top or very bottom
2329 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 */
Winson Chung20f71112011-04-06 14:22:04 -07002332 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002333 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002334
Winson Chungf0ea4d32011-06-06 14:27:16 -07002335 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2336 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2337 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2338 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002339
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002340 setPivotsForZoom(toView, toState, scale);
2341
Winson Chung20f71112011-04-06 14:22:04 -07002342
Winson Chungf0ea4d32011-06-06 14:27:16 -07002343 if (springLoaded) {
2344 if (toState == State.APPS_CUSTOMIZE) {
2345 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2346 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2347 } else {
2348 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2349 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
Winson Chung20f71112011-04-06 14:22:04 -07002350 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 } else {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002352 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
2353 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002354 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355
2356 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002357 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002358 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002359 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2360 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002361 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002362 toView.setFastScaleX(a * scale + b * 1f);
2363 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002364 }
2365 });
Adam Cohen61033d32010-11-15 18:29:44 -08002366
Winson Chungf0ea4d32011-06-06 14:27:16 -07002367 toView.setVisibility(View.VISIBLE);
2368 toView.setFastAlpha(0f);
2369 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2370 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2371 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2372 public void onAnimationUpdate(float a, float b) {
2373 // don't need to invalidate because we do so above
2374 toView.setFastAlpha(a * 0f + b * 1f);
2375 }
2376 });
2377 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002378
Michael Jurkaabded662011-03-04 12:06:57 -08002379 if (toView instanceof LauncherTransitionable) {
2380 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002381 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002382 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002383 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002384 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002385 // Prepare the position
2386 toView.setTranslationX(0.0f);
2387 toView.setTranslationY(0.0f);
2388 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002389 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002390 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002391 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002392 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002393 // If we don't set the final scale values here, if this animation is cancelled
2394 // it will have the wrong scale value and subsequent cameraPan animations will
2395 // not fix that
2396 toView.setScaleX(1.0f);
2397 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002398 if (toView instanceof LauncherTransitionable) {
2399 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002400 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002401 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002402 });
2403
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002404 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002405 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002407 if (mStateAnimation != null) mStateAnimation.cancel();
2408 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002409 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002410 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002411 } else {
2412 toView.setTranslationX(0.0f);
2413 toView.setTranslationY(0.0f);
2414 toView.setScaleX(1.0f);
2415 toView.setScaleY(1.0f);
2416 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002417 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002418 if (toView instanceof LauncherTransitionable) {
2419 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2420 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2421 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002423 }
2424
2425 /**
2426 * Zoom the camera back into the workspace, hiding 'fromView'.
2427 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002429 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002430 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002431 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002432 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002433
Winson Chungf0ea4d32011-06-06 14:27:16 -07002434 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2435 final float scaleFactor = (float)
2436 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2437 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002438
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002439 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002440
Michael Jurkad3ef3062010-11-23 16:23:58 -08002441 if (!springLoaded) {
2442 mWorkspace.unshrink(animated);
2443 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002445 if (mStateAnimation != null) mStateAnimation.cancel();
2446 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002447
Michael Jurka742574b2011-02-02 23:51:01 -08002448 final float oldScaleX = fromView.getScaleX();
2449 final float oldScaleY = fromView.getScaleY();
2450
2451 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2452 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002453 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2454 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002455 ((View)fromView.getParent()).fastInvalidate();
2456 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2457 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2458 }
2459 });
Michael Jurkaabded662011-03-04 12:06:57 -08002460 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002461 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002462 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002463 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2464 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002465 // don't need to invalidate because we do so above
2466 fromView.setFastAlpha(a * 1f + b * 0f);
2467 }
2468 });
Michael Jurkaabded662011-03-04 12:06:57 -08002469 if (fromView instanceof LauncherTransitionable) {
2470 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002471 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002472 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002473 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002474 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002476 if (fromView instanceof LauncherTransitionable) {
2477 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002478 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 }
2480 });
2481
Winson Chungf0ea4d32011-06-06 14:27:16 -07002482 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002483 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002484 } else {
2485 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002486 if (fromView instanceof LauncherTransitionable) {
2487 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2488 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2489 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002491 }
2492
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002493 void showWorkspace(boolean animated) {
2494 showWorkspace(animated, null);
2495 }
2496
2497 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002498 if (layout != null) {
2499 // always animated, but that's ok since we never specify a layout and
2500 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002501 mWorkspace.unshrink(layout);
2502 } else {
2503 mWorkspace.unshrink(animated);
2504 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002505 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002506 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002507 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002508
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002509 // Change the state *after* we've called all the transition code
2510 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002511
Winson Chung5fb63472011-02-02 17:03:37 -08002512 // Resume the auto-advance of widgets
2513 mUserPresent = true;
2514 updateRunning();
2515
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002516 // send an accessibility event to announce the context change
2517 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002518 }
2519
Michael Jurkad3ef3062010-11-23 16:23:58 -08002520 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002521 // Enter spring loaded mode on a new layout
Michael Jurkad3ef3062010-11-23 16:23:58 -08002522 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002523
2524 if (mState == State.APPS_CUSTOMIZE) {
2525 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2526 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungb26f3d62011-06-02 10:49:29 -07002527 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002528 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002529 }
2530
2531 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002532 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002533 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chungb26f3d62011-06-02 10:49:29 -07002534 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2535 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002536 }
2537 // Otherwise, we are not in spring loaded mode, so don't do anything.
2538 }
2539
2540 /**
2541 * Shows the dock/hotseat area.
2542 */
2543 void showDock(boolean animated) {
2544 if (!LauncherApplication.isScreenLarge()) {
2545 if (animated) {
2546 int duration = mSearchDeleteBar.getTransitionInDuration();
2547 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002548 } else {
2549 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 }
2551 }
2552 }
2553
2554 /**
2555 * Hides the dock/hotseat area.
2556 */
2557 void hideDock(boolean animated) {
2558 if (!LauncherApplication.isScreenLarge()) {
2559 if (animated) {
2560 int duration = mSearchDeleteBar.getTransitionOutDuration();
2561 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002562 } else {
2563 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002564 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002565 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002566 }
2567
Winson Chung785d2eb2011-04-14 16:08:02 -07002568 void showAllApps(boolean animated) {
2569 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002570
Winson Chungf0ea4d32011-06-06 14:27:16 -07002571 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2572 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002573
Winson Chungf0ea4d32011-06-06 14:27:16 -07002574 // Hide the search bar and dock
2575 mSearchDeleteBar.hideSearchBar(animated);
2576 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002577
Winson Chungf0ea4d32011-06-06 14:27:16 -07002578 // Change the state *after* we've called all the transition code
2579 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002580
2581 // Pause the auto-advance of widgets until we are out of AllApps
2582 mUserPresent = false;
2583 updateRunning();
2584
2585 // Send an accessibility event to announce the context change
2586 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2587 }
2588
Joe Onoratob2061212009-11-24 16:13:54 -05002589 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002590 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002591 * - Home from workspace
2592 * - from center screen
2593 * - from other screens
2594 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002595 * - from center screen
2596 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002597 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002598 * - from center screen
2599 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002600 * - Launch app from workspace and quit
2601 * - with back
2602 * - with home
2603 * - Launch app from all apps and quit
2604 * - with back
2605 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002606 * - Go to a screen that's not the default, then all
2607 * apps, and launch and app, and go back
2608 * - with back
2609 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002610 * - On workspace, long press power and go back
2611 * - with back
2612 * - with home
2613 * - On all apps, long press power and go back
2614 * - with back
2615 * - with home
2616 * - On workspace, power off
2617 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002618 * - Launch an app and turn off the screen while in that app
2619 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002620 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002621 * - From all apps
2622 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002623 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2624 * - From all apps
2625 * - From the center workspace
2626 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002627 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002628 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002629 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2630 mWorkspace.setVisibility(View.VISIBLE);
2631 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002632
Winson Chungf0ea4d32011-06-06 14:27:16 -07002633 // Show the search bar and dock
2634 mSearchDeleteBar.showSearchBar(animated);
2635 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002636
Winson Chungf0ea4d32011-06-06 14:27:16 -07002637 // Set focus to the AppsCustomize button
2638 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002639 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002640 }
2641
Joe Onorato7c312c12009-08-13 21:36:53 -07002642 void lockAllApps() {
2643 // TODO
2644 }
2645
2646 void unlockAllApps() {
2647 // TODO
2648 }
2649
Patrick Dubroy5f445422011-02-18 14:35:21 -08002650 /**
2651 * Add an item from all apps or customize onto the given workspace screen.
2652 * If layout is null, add to the current screen.
2653 */
2654 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002655 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2656 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002657 } else {
2658 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002659 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002660 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002661
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002662 void onWorkspaceClick(CellLayout layout) {
2663 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002664 }
2665
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002666 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002667 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002668 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002669 // Look for the toolbar icon specified in the activity meta-data
2670 Bundle metaData = packageManager.getActivityInfo(
2671 activityName, PackageManager.GET_META_DATA).metaData;
2672 if (metaData != null) {
2673 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2674 if (iconResId != 0) {
2675 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002676 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002677 }
2678 }
2679 } catch (NameNotFoundException e) {
2680 // Do nothing
2681 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002682 return null;
2683 }
2684
2685 // if successful in getting icon, return it; otherwise, set button to use default drawable
2686 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2687 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2688 TextView button = (TextView) findViewById(buttonId);
2689 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2690
2691 // If we were unable to find the icon via the meta-data, use a generic one
2692 if (toolbarIcon == null) {
2693 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2694 return null;
2695 } else {
2696 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2697 return toolbarIcon.getConstantState();
2698 }
2699 }
2700
2701 // if successful in getting icon, return it; otherwise, set button to use default drawable
2702 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2703 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2704 ImageView button = (ImageView) findViewById(buttonId);
2705 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2706
Michael Jurka0423dcf2010-10-05 14:56:18 -07002707 // If we were unable to find the icon via the meta-data, use a generic one
2708 if (toolbarIcon == null) {
2709 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002710 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002711 } else {
2712 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002713 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002714 }
2715 }
2716
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002717 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2718 TextView button = (TextView) findViewById(buttonId);
2719 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2720 }
2721
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002722 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002723 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002724 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002725 }
2726
Michael Jurka0423dcf2010-10-05 14:56:18 -07002727 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002728 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2729 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002730
Winson Chung1cad91e2011-05-25 17:41:01 -07002731 final SearchManager searchManager =
2732 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2733 ComponentName activityName = searchManager.getGlobalSearchActivity();
2734 if (activityName != null) {
2735 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
2736 // glass icon
2737 boolean iconLoaded = false;
2738 if (!LauncherApplication.isScreenLarge()) {
2739 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
2740 int orientation = getResources().getConfiguration().orientation;
2741 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
2742 searchButton.setImageResource(R.drawable.ic_generic_search);
2743 iconLoaded = true;
2744 sGlobalSearchIcon = null;
2745 }
2746 }
2747 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002748 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08002749 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002750 }
Winson Chung1cad91e2011-05-25 17:41:01 -07002751 searchButton.setVisibility(View.VISIBLE);
2752 searchDivider.setVisibility(View.VISIBLE);
2753 } else {
2754 searchButton.setVisibility(View.GONE);
2755 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002756 }
2757 }
2758
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002759 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002760 updateButtonWithDrawable(R.id.search_button, d);
2761 }
2762
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002763 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002764 final View searchDivider = findViewById(R.id.search_divider);
2765 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002766
Winson Chung1cad91e2011-05-25 17:41:01 -07002767 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2768 ComponentName activityName = intent.resolveActivity(getPackageManager());
2769 if (activityName != null) {
2770 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
2771 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2772 searchDivider.setVisibility(View.VISIBLE);
2773 voiceButton.setVisibility(View.VISIBLE);
2774 } else {
2775 searchDivider.setVisibility(View.GONE);
2776 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002777 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002778 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002780 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002781 updateButtonWithDrawable(R.id.voice_button, d);
2782 }
2783
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002784 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002785 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002786 */
2787 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002788 final View marketButton = findViewById(R.id.market_button);
2789 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2790 // Find the app market activity by resolving an intent.
2791 // (If multiple app markets are installed, it will return the ResolverActivity.)
2792 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002793 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002794 mAppMarketIntent = intent;
2795 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
2796 R.id.market_button, activityName, R.drawable.app_market_generic);
2797 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002798
2799 // Remove the shop icon text in the Phone UI
2800 if (!LauncherApplication.isScreenLarge()) {
2801 ((TextView) marketButton).setText("");
2802 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002803 } else {
2804 // We should hide and disable the view so that we don't try and restore the visibility
2805 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2806 marketButton.setVisibility(View.GONE);
2807 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002808 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002809 }
2810
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002811 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002812 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002813 }
2814
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002815 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002816 * Displays the shortcut creation dialog and launches, if necessary, the
2817 * appropriate activity.
2818 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002819 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002820 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2821 DialogInterface.OnShowListener {
2822
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002824
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002827
Winson Chung55b65502011-05-26 12:03:43 -07002828 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2829 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002830 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002831 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002832
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 AlertDialog dialog = builder.create();
2834 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002835 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002836 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 return dialog;
2839 }
2840
2841 public void onCancel(DialogInterface dialog) {
2842 mWaitingForResult = false;
2843 cleanup();
2844 }
2845
Romain Guycbb89e42009-06-08 15:52:54 -07002846 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002847 mWaitingForResult = false;
2848 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002849 }
2850
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002852 try {
2853 dismissDialog(DIALOG_CREATE_SHORTCUT);
2854 } catch (Exception e) {
2855 // An exception is thrown if the dialog is not visible, which is fine
2856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 }
2858
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002859 /**
2860 * Handle the action clicked in the "Add to home" dialog.
2861 */
2862 public void onClick(DialogInterface dialog, int which) {
2863 Resources res = getResources();
2864 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002865
Winson Chung55b65502011-05-26 12:03:43 -07002866 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2867 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002868 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002869 if (mAppsCustomizeTabHost != null) {
2870 mAppsCustomizeTabHost.selectWidgetsTab();
2871 }
2872 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002873 break;
2874 }
Winson Chung55b65502011-05-26 12:03:43 -07002875 case AddAdapter.ITEM_APPLICATION: {
2876 if (mAppsCustomizeTabHost != null) {
2877 mAppsCustomizeTabHost.selectAppsTab();
2878 }
2879 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002880 break;
2881 }
Winson Chung55b65502011-05-26 12:03:43 -07002882 case AddAdapter.ITEM_APPWIDGET: {
2883 if (mAppsCustomizeTabHost != null) {
2884 mAppsCustomizeTabHost.selectWidgetsTab();
2885 }
2886 showAllApps(true);
2887 break;
2888 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002889 case AddAdapter.ITEM_WALLPAPER: {
2890 startWallpaper();
2891 break;
2892 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 }
2894 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002895
2896 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002897 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 }
2900
2901 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002902 * Receives notifications when applications are added/removed.
2903 */
2904 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2905 @Override
2906 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002907 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002908 String reason = intent.getStringExtra("reason");
2909 if (!"homekey".equals(reason)) {
2910 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002911 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002912 animate = false;
2913 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002914 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002915 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002916 }
2917 }
2918
2919 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002920 * Receives notifications whenever the appwidgets are reset.
2921 */
2922 private class AppWidgetResetObserver extends ContentObserver {
2923 public AppWidgetResetObserver() {
2924 super(new Handler());
2925 }
2926
2927 @Override
2928 public void onChange(boolean selfChange) {
2929 onAppWidgetReset();
2930 }
2931 }
2932
2933 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002934 * If the activity is currently paused, signal that we need to re-run the loader
2935 * in onResume.
2936 *
2937 * This needs to be called from incoming places where resources might have been loaded
2938 * while we are paused. That is becaues the Configuration might be wrong
2939 * when we're not running, and if it comes back to what it was when we
2940 * were paused, we are not restarted.
2941 *
2942 * Implementation of the method from LauncherModel.Callbacks.
2943 *
2944 * @return true if we are currently paused. The caller might be able to
2945 * skip some work in that case since we will come back again.
2946 */
2947 public boolean setLoadOnResume() {
2948 if (mPaused) {
2949 Log.i(TAG, "setLoadOnResume");
2950 mOnResumeNeedsLoad = true;
2951 return true;
2952 } else {
2953 return false;
2954 }
2955 }
2956
2957 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002961 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002962 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002963 } else {
2964 return SCREEN_COUNT / 2;
2965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002967
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002968
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 /**
2970 * Refreshes the shortcuts shown on the workspace.
2971 *
2972 * Implementation of the method from LauncherModel.Callbacks.
2973 */
2974 public void startBinding() {
2975 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002976
2977 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002978 int count = workspace.getChildCount();
2979 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002980 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002981 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2982 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002984
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 if (DEBUG_USER_INTERFACE) {
2986 android.widget.Button finishButton = new android.widget.Button(this);
2987 finishButton.setText("Finish");
2988 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2989
2990 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2991 public void onClick(View v) {
2992 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08002996
2997 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
2998 // mDesktopItems -> AppWidgetInfo -> hostView).
2999 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 }
3001
3002 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 * Bind the items start-end from the list.
3004 *
3005 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3008
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003009 setLoadOnResume();
3010
Joe Onorato9c1289c2009-08-17 11:03:03 -04003011 final Workspace workspace = mWorkspace;
3012
3013 for (int i=start; i<end; i++) {
3014 final ItemInfo item = shortcuts.get(i);
3015 mDesktopItems.add(item);
3016 switch (item.itemType) {
3017 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3018 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003019 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3021 false);
3022 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003023 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003025 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003026 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3028 false);
3029 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003031 }
3032
Joe Onorato9c1289c2009-08-17 11:03:03 -04003033 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 }
3035
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 /**
3037 * Implementation of the method from LauncherModel.Callbacks.
3038 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003039 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003040 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003041 sFolders.clear();
3042 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044
3045 /**
3046 * Add the views for a widget to the workspace.
3047 *
3048 * Implementation of the method from LauncherModel.Callbacks.
3049 */
3050 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003051 setLoadOnResume();
3052
Daniel Sandler843e8602010-06-07 14:59:01 -04003053 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3054 if (DEBUG_WIDGETS) {
3055 Log.d(TAG, "bindAppWidget: " + item);
3056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 final Workspace workspace = mWorkspace;
3058
3059 final int appWidgetId = item.appWidgetId;
3060 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003061 if (DEBUG_WIDGETS) {
3062 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3063 }
3064
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3066
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3068 item.hostView.setTag(item);
3069
3070 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3071 item.cellY, item.spanX, item.spanY, false);
3072
Adam Cohended9f8d2010-11-03 13:25:16 -07003073 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3074
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 workspace.requestLayout();
3076
3077 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003078
3079 if (DEBUG_WIDGETS) {
3080 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3081 + (SystemClock.uptimeMillis()-start) + "ms");
3082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 }
3084
3085 /**
3086 * Callback saying that there aren't any more items to bind.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
3090 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003091 setLoadOnResume();
3092
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 if (mSavedState != null) {
3094 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003095 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 }
3097
Adam Cohendf2cc412011-04-27 16:56:57 -07003098 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3099 if (folders != null) {
3100 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003101 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 final FolderIcon folderIcon = (FolderIcon)
3103 mWorkspace.getViewForTag(info);
3104 if (folderIcon != null) {
3105 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 }
3107 }
3108 final Folder openFolder = mWorkspace.getOpenFolder();
3109 if (openFolder != null) {
3110 openFolder.requestFocus();
3111 }
3112 }
3113
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 mSavedState = null;
3115 }
3116
3117 if (mSavedInstanceState != null) {
3118 super.onRestoreInstanceState(mSavedInstanceState);
3119 mSavedInstanceState = null;
3120 }
3121
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003123
3124 // If we received the result of any pending adds while the loader was running (e.g. the
3125 // widget configuration forced an orientation change), process them now.
3126 for (int i = 0; i < sPendingAddList.size(); i++) {
3127 completeAdd(sPendingAddList.get(i));
3128 }
3129 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 }
3131
3132 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003133 * Updates the icons on the launcher that are affected by changes to the package list
3134 * on the device.
3135 */
3136 private void updateIconsAffectedByPackageManagerChanges() {
3137 updateAppMarketIcon();
3138 updateGlobalSearchIcon();
3139 updateVoiceSearchIcon();
3140 }
3141
3142 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 * Add the icons for all apps.
3144 *
3145 * Implementation of the method from LauncherModel.Callbacks.
3146 */
3147 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 if (mAppsCustomizeContent != null) {
3149 mAppsCustomizeContent.setApps(apps);
3150 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003151 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 }
3153
3154 /**
3155 * A package was installed.
3156 *
3157 * Implementation of the method from LauncherModel.Callbacks.
3158 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003159 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003160 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003162
Winson Chung785d2eb2011-04-14 16:08:02 -07003163 if (mAppsCustomizeContent != null) {
3164 mAppsCustomizeContent.addApps(apps);
3165 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 }
3168
3169 /**
3170 * A package was updated.
3171 *
3172 * Implementation of the method from LauncherModel.Callbacks.
3173 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003174 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003175 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003177 if (mWorkspace != null) {
3178 mWorkspace.updateShortcuts(apps);
3179 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003180
Winson Chung785d2eb2011-04-14 16:08:02 -07003181 if (mAppsCustomizeContent != null) {
3182 mAppsCustomizeContent.updateApps(apps);
3183 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003184 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 }
3186
3187 /**
3188 * A package was uninstalled.
3189 *
3190 * Implementation of the method from LauncherModel.Callbacks.
3191 */
Joe Onorato36115782010-06-17 13:28:48 -04003192 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003194 if (permanent) {
3195 mWorkspace.removeItems(apps);
3196 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003197
Winson Chung785d2eb2011-04-14 16:08:02 -07003198 if (mAppsCustomizeContent != null) {
3199 mAppsCustomizeContent.removeApps(apps);
3200 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003201 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202 }
Joe Onoratobe386092009-11-17 17:32:16 -08003203
3204 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003205 * A number of packages were updated.
3206 */
3207 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003208
Winson Chung785d2eb2011-04-14 16:08:02 -07003209 if (mAppsCustomizeContent != null) {
3210 mAppsCustomizeContent.onPackagesUpdated();
3211 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003212 }
3213
Winson Chung400438b2011-01-16 17:53:48 -08003214 private int mapConfigurationOriActivityInfoOri(int configOri) {
3215 final Display d = getWindowManager().getDefaultDisplay();
3216 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3217 switch (d.getRotation()) {
3218 case Surface.ROTATION_0:
3219 case Surface.ROTATION_180:
3220 // We are currently in the same basic orientation as the natural orientation
3221 naturalOri = configOri;
3222 break;
3223 case Surface.ROTATION_90:
3224 case Surface.ROTATION_270:
3225 // We are currently in the other basic orientation to the natural orientation
3226 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3227 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3228 break;
3229 }
3230
3231 int[] oriMap = {
3232 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3233 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3234 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3235 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3236 };
3237 // Since the map starts at portrait, we need to offset if this device's natural orientation
3238 // is landscape.
3239 int indexOffset = 0;
3240 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3241 indexOffset = 1;
3242 }
3243 return oriMap[(d.getRotation() + indexOffset) % 4];
3244 }
3245 public void lockScreenOrientation() {
3246 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3247 .getConfiguration().orientation));
3248 }
3249 public void unlockScreenOrientation() {
3250 mHandler.postDelayed(new Runnable() {
3251 public void run() {
3252 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3253 }
3254 }, mRestoreScreenOrientationDelay);
3255 }
3256
Winson Chung80baf5a2010-08-09 16:03:15 -07003257 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003258 * Prints out out state for debugging.
3259 */
3260 public void dumpState() {
3261 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003262 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003263 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3264 Log.d(TAG, "mRestoring=" + mRestoring);
3265 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3266 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3267 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003268 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003269 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003270
Winson Chung785d2eb2011-04-14 16:08:02 -07003271 if (mAppsCustomizeContent != null) {
3272 mAppsCustomizeContent.dumpState();
3273 }
Joe Onoratobe386092009-11-17 17:32:16 -08003274 Log.d(TAG, "END launcher2 dump state");
3275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276}
Michael Jurka2763be32011-02-24 11:19:57 -08003277
Michael Jurkaabded662011-03-04 12:06:57 -08003278interface LauncherTransitionable {
3279 void onLauncherTransitionStart(Animator animation);
3280 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003281}