blob: 2dfce622f5e0fc4b937e832bb60e3cc9851063e7 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Adam Cohen860f4c52011-01-27 20:16:13 -080042import android.content.Intent.ShortcutIconResource;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.pm.PackageManager.NameNotFoundException;
Winson Chungf0ea4d32011-06-06 14:27:16 -070047import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Michael Jurkae0f5a612011-02-07 16:45:41 -080081import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080082import android.view.View.OnLongClickListener;
Winson Chungf0ea4d32011-06-06 14:27:16 -070083import android.view.ViewGroup;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
97import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: long
Adam Cohendf2cc412011-04-27 16:56:57 -0700152 private static final String RUNTIME_STATE_FOLDERS = "launcher.folder";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: boolean
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
161 // Type: long
162 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
163
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700164 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
165
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700166 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700167 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700168 private State mState = State.WORKSPACE;
169 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700170
Joe Onorato9c1289c2009-08-17 11:03:03 -0400171 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800176 private final BroadcastReceiver mCloseSystemDialogsReceiver
177 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800178 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private LayoutInflater mInflater;
181
Joe Onorato00acb122009-08-04 16:04:30 -0400182 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private AppWidgetManager mAppWidgetManager;
186 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700187
Michael Jurka0280c3b2010-09-17 15:00:07 -0700188 private int mAddScreen = -1;
189 private int mAddIntersectCellX = -1;
190 private int mAddIntersectCellY = -1;
191 private int[] mAddDropPosition;
192 private int[] mTmpAddItemCellCoordinates = new int[2];
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private FolderInfo mFolderInfo;
195
Joe Onorato7c312c12009-08-13 21:36:53 -0700196 private DeleteZone mDeleteZone;
Winson Chung97d85d22011-04-13 11:27:36 -0700197 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800198 private View mAllAppsButton;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700199 private SearchDropTargetBar mSearchDeleteBar;
200 private AppsCustomizeTabHost mAppsCustomizeTabHost;
201 private AppsCustomizePagedView mAppsCustomizeContent;
202 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Bundle mSavedState;
205
206 private SpannableStringBuilder mDefaultKeySsb = null;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 private boolean mWorkspaceLoading = true;
209
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800210 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private boolean mRestoring;
212 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700213 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
215 private Bundle mSavedInstanceState;
216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800219 private boolean mUserPresent = true;
220 private boolean mVisible = false;
221 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700223 private static LocaleConfiguration sLocaleConfiguration = null;
224
Romain Guy84f296c2009-11-04 15:00:44 -0800225 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700228
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800229 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800230 private ImageView mPreviousView;
231 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500232
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400233 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400234 private String[] mHotseatConfig = null;
235 private Intent[] mHotseats = null;
236 private Drawable[] mHotseatIcons = null;
237 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400238
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700239 private Intent mAppMarketIntent = null;
240
Adam Cohended9f8d2010-11-03 13:25:16 -0700241 // Related to the auto-advancing of widgets
242 private final int ADVANCE_MSG = 1;
243 private final int mAdvanceInterval = 20000;
244 private final int mAdvanceStagger = 250;
245 private long mAutoAdvanceSentTime;
246 private long mAutoAdvanceTimeLeft = -1;
247 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
248 new HashMap<View, AppWidgetProviderInfo>();
249
Winson Chung400438b2011-01-16 17:53:48 -0800250 // Determines how long to wait after a rotation before restoring the screen orientation to
251 // match the sensor state.
252 private final int mRestoreScreenOrientationDelay = 500;
253
Michael Jurka4ef207b2010-11-29 17:05:45 -0800254 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800255 private static Drawable.ConstantState sGlobalSearchIcon;
256 private static Drawable.ConstantState sVoiceSearchIcon;
257 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800258
Adam Cohen2801caf2011-05-13 20:57:39 -0700259 private DragLayer mDragLayer;
260
Michael Jurkaddd62e92011-02-16 17:49:14 -0800261 private BubbleTextView mWaitingForResume;
262
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800263 private static ArrayList<PendingAddArguments> sPendingAddList
264 = new ArrayList<PendingAddArguments>();
265
266 private static class PendingAddArguments {
267 int requestCode;
268 Intent intent;
269 int screen;
270 int cellX;
271 int cellY;
272 }
273
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 @Override
275 protected void onCreate(Bundle savedInstanceState) {
276 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 LauncherApplication app = ((LauncherApplication)getApplication());
278 mModel = app.setLauncher(this);
279 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400280 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700282
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700283 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700284 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
285 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200288 android.os.Debug.startMethodTracing(
289 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400292 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 setContentView(R.layout.launcher);
295 setupViews();
296
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800297 registerContentObservers();
298
Joe Onorato7c312c12009-08-13 21:36:53 -0700299 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 mSavedState = savedInstanceState;
302 restoreState(mSavedState);
303
Winson Chunga12a2502010-12-20 14:41:35 -0800304 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700305 if (mAppsCustomizeContent != null) {
306 mAppsCustomizeContent.onPackagesUpdated();
307 }
Winson Chunga12a2502010-12-20 14:41:35 -0800308
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 if (PROFILE_STARTUP) {
310 android.os.Debug.stopMethodTracing();
311 }
312
313 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400314 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 }
316
317 // For handling default keys
318 mDefaultKeySsb = new SpannableStringBuilder();
319 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800320
321 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
322 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800323
324 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700325 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
326 updateIconsAffectedByPackageManagerChanges();
327 }
328 if (sGlobalSearchIcon != null) {
329 updateGlobalSearchIcon(sGlobalSearchIcon);
330 }
331 if (sVoiceSearchIcon != null) {
332 updateVoiceSearchIcon(sVoiceSearchIcon);
333 }
334 if (sAppMarketIcon != null) {
335 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
Romain Guycbb89e42009-06-08 15:52:54 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700340 if (sLocaleConfiguration == null) {
341 new AsyncTask<Void, Void, LocaleConfiguration>() {
342 @Override
343 protected LocaleConfiguration doInBackground(Void... unused) {
344 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
345 readConfiguration(Launcher.this, localeConfiguration);
346 return localeConfiguration;
347 }
348
349 @Override
350 protected void onPostExecute(LocaleConfiguration result) {
351 sLocaleConfiguration = result;
352 checkForLocaleChange(); // recursive, but now with a locale configuration
353 }
354 }.execute();
355 return;
356 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final Configuration configuration = getResources().getConfiguration();
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final String locale = configuration.locale.toString();
362
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700363 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 final int mcc = configuration.mcc;
365
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 final int mnc = configuration.mnc;
368
Romain Guy84f296c2009-11-04 15:00:44 -0800369 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370
Romain Guy84f296c2009-11-04 15:00:44 -0800371 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372 sLocaleConfiguration.locale = locale;
373 sLocaleConfiguration.mcc = mcc;
374 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700375
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400377 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700378
379 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
380 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700381 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 public void run() {
383 writeConfiguration(Launcher.this, localeConfiguration);
384 }
385 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700386 }
387 }
388
389 private static class LocaleConfiguration {
390 public String locale;
391 public int mcc = -1;
392 public int mnc = -1;
393 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700394
Romain Guy98d01652009-06-30 16:21:04 -0700395 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
396 DataInputStream in = null;
397 try {
398 in = new DataInputStream(context.openFileInput(PREFERENCES));
399 configuration.locale = in.readUTF();
400 configuration.mcc = in.readInt();
401 configuration.mnc = in.readInt();
402 } catch (FileNotFoundException e) {
403 // Ignore
404 } catch (IOException e) {
405 // Ignore
406 } finally {
407 if (in != null) {
408 try {
409 in.close();
410 } catch (IOException e) {
411 // Ignore
412 }
413 }
414 }
415 }
416
417 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
418 DataOutputStream out = null;
419 try {
420 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
421 out.writeUTF(configuration.locale);
422 out.writeInt(configuration.mcc);
423 out.writeInt(configuration.mnc);
424 out.flush();
425 } catch (FileNotFoundException e) {
426 // Ignore
427 } catch (IOException e) {
428 //noinspection ResultOfMethodCallIgnored
429 context.getFileStreamPath(PREFERENCES).delete();
430 } finally {
431 if (out != null) {
432 try {
433 out.close();
434 } catch (IOException e) {
435 // Ignore
436 }
437 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 }
439 }
440
441 static int getScreen() {
442 synchronized (sLock) {
443 return sScreen;
444 }
445 }
446
447 static void setScreen(int screen) {
448 synchronized (sLock) {
449 sScreen = screen;
450 }
451 }
452
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400453 // Note: This doesn't do all the client-id magic that BrowserProvider does
454 // in Browser. (http://b/2425179)
455 private Uri getDefaultBrowserUri() {
456 String url = getString(R.string.default_browser_url);
457 if (url.indexOf("{CID}") != -1) {
458 url = url.replace("{CID}", "android-google");
459 }
460 return Uri.parse(url);
461 }
462
463 // Load the Intent templates from arrays.xml to populate the hotseats. For
464 // each Intent, if it resolves to a single app, use that as the launch
465 // intent & use that app's label as the contentDescription. Otherwise,
466 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400467 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400468 if (mHotseatConfig == null) {
469 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
470 if (mHotseatConfig.length > 0) {
471 mHotseats = new Intent[mHotseatConfig.length];
472 mHotseatLabels = new CharSequence[mHotseatConfig.length];
473 mHotseatIcons = new Drawable[mHotseatConfig.length];
474 } else {
475 mHotseats = null;
476 mHotseatIcons = null;
477 mHotseatLabels = null;
478 }
479
480 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
481 for (int i=0; i<mHotseatConfig.length; i++) {
482 // load icon for this slot; currently unrelated to the actual activity
483 try {
484 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
485 } catch (ArrayIndexOutOfBoundsException ex) {
486 Log.w(TAG, "Missing hotseat_icons array item #" + i);
487 mHotseatIcons[i] = null;
488 }
489 }
490 hotseatIconDrawables.recycle();
491 }
492
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400493 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400494 for (int i=0; i<mHotseatConfig.length; i++) {
495 Intent intent = null;
496 if (mHotseatConfig[i].equals("*BROWSER*")) {
497 // magic value meaning "launch user's default web browser"
498 // replace it with a generic web request so we can see if there is indeed a default
499 String defaultUri = getString(R.string.default_browser_url);
500 intent = new Intent(
501 Intent.ACTION_VIEW,
502 ((defaultUri != null)
503 ? Uri.parse(defaultUri)
504 : getDefaultBrowserUri())
505 ).addCategory(Intent.CATEGORY_BROWSABLE);
506 // note: if the user launches this without a default set, she
507 // will always be taken to the default URL above; this is
508 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700509 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400510 // URL is unavoidably sent to the chosen app.
511 } else {
512 try {
513 intent = Intent.parseUri(mHotseatConfig[i], 0);
514 } catch (java.net.URISyntaxException ex) {
515 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
516 // bogus; leave intent=null
517 }
518 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700519
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 if (intent == null) {
521 mHotseats[i] = null;
522 mHotseatLabels[i] = getText(R.string.activity_not_found);
523 continue;
524 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400525
526 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700527 Log.d(TAG, "loadHotseats: hotseat " + i
528 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400529 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400530 + "]");
531 }
532
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
534 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700535 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400536 Log.d(TAG, "Best match for intent: " + bestMatch);
537 Log.d(TAG, "All matches: ");
538 for (ResolveInfo ri : allMatches) {
539 Log.d(TAG, " --> " + ri);
540 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400541 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400542 // did this resolve to a single app, or the resolver?
543 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700544 // can't find any activity to handle this. let's leave the
545 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 // to launch.
547 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 // set accessibility text to "Not installed"
550 mHotseatLabels[i] = getText(R.string.activity_not_found);
551 } else {
552 boolean found = false;
553 for (ResolveInfo ri : allMatches) {
554 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
555 && bestMatch.activityInfo.applicationInfo.packageName
556 .equals(ri.activityInfo.applicationInfo.packageName)) {
557 found = true;
558 break;
559 }
560 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700561
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 if (!found) {
563 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
564 // the bestMatch is probably the ResolveActivity, meaning the
565 // user has not yet selected a default
566 // so: we'll keep the original intent for now
567 mHotseats[i] = intent;
568
569 // set the accessibility text to "Select shortcut"
570 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
571 } else {
572 // we have an app!
573 // now reconstruct the intent to launch it through the front
574 // door
575 ComponentName com = new ComponentName(
576 bestMatch.activityInfo.applicationInfo.packageName,
577 bestMatch.activityInfo.name);
578 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
579
580 // load the app label for accessibility
581 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
582 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 }
584
585 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 Log.d(TAG, "loadHotseats: hotseat " + i
587 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 + ((mHotseats[i] == null)
589 ? "null"
590 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400591 + "] label=[" + mHotseatLabels[i]
592 + "]"
593 );
594 }
595 }
596 }
597
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800598 private void completeAdd(PendingAddArguments args) {
599 switch (args.requestCode) {
600 case REQUEST_PICK_APPLICATION:
601 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
602 break;
603 case REQUEST_PICK_SHORTCUT:
604 processShortcut(args.intent);
605 break;
606 case REQUEST_CREATE_SHORTCUT:
607 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
608 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800609 case REQUEST_PICK_APPWIDGET:
610 addAppWidgetFromPick(args.intent);
611 break;
612 case REQUEST_CREATE_APPWIDGET:
613 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
614 completeAddAppWidget(appWidgetId, args.screen);
615 break;
616 case REQUEST_PICK_WALLPAPER:
617 // We just wanted the activity result here so we can clear mWaitingForResult
618 break;
619 }
620 }
621
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800622 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700624 mWaitingForResult = false;
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 // The pattern used here is that a user PICKs a specific application,
627 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
630 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700631
Michael Jurka0280c3b2010-09-17 15:00:07 -0700632 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800633 final PendingAddArguments args = new PendingAddArguments();
634 args.requestCode = requestCode;
635 args.intent = data;
636 args.screen = mAddScreen;
637 args.cellX = mAddIntersectCellX;
638 args.cellY = mAddIntersectCellY;
639
640 // If the loader is still running, defer the add until it is done.
641 if (isWorkspaceLocked()) {
642 sPendingAddList.add(args);
643 } else {
644 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 }
Romain Guy18042c82009-11-06 11:44:55 -0800646 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
647 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
648 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700649 // Clean up the appWidgetId if we canceled
650 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
651 if (appWidgetId != -1) {
652 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 }
656
657 @Override
658 protected void onResume() {
659 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800660 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700661 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400662 mWorkspaceLoading = true;
663 mModel.startLoader(this, true);
664 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700665 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800667 if (mWaitingForResume != null) {
668 mWaitingForResume.setStayPressed(false);
669 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700670 // When we resume Launcher, a different Activity might be responsible for the app
671 // market intent, so refresh the icon
672 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
674
675 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700676 protected void onPause() {
677 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 // Some launcher layouts don't have a previous and next view
679 if (mPreviousView != null) {
680 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700681 }
682 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 dismissPreview(mNextView);
684 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700685 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800686 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 }
Romain Guycbb89e42009-06-08 15:52:54 -0700688
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700689 @Override
690 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 // Flag the loader to stop early before switching
692 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 if (mAppsCustomizeContent != null) {
694 mAppsCustomizeContent.surrender();
695 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800696 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700697 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800699 // We can't hide the IME if it was forced open. So don't bother
700 /*
701 @Override
702 public void onWindowFocusChanged(boolean hasFocus) {
703 super.onWindowFocusChanged(hasFocus);
704
705 if (hasFocus) {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 WindowManager.LayoutParams lp = getWindow().getAttributes();
709 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
710 android.os.Handler()) {
711 protected void onReceiveResult(int resultCode, Bundle resultData) {
712 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
713 }
714 });
715 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
716 }
717 }
718 */
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 private boolean acceptFilter() {
721 final InputMethodManager inputManager = (InputMethodManager)
722 getSystemService(Context.INPUT_METHOD_SERVICE);
723 return !inputManager.isFullscreenMode();
724 }
725
726 @Override
727 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700728 final int uniChar = event.getUnicodeChar();
729 final boolean handled = super.onKeyDown(keyCode, event);
730 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
731 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
733 keyCode, event);
734 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700735 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700736 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700737 // showSearchDialog()
738 // If there are multiple keystrokes before the search dialog takes focus,
739 // onSearchRequested() will be called for every keystroke,
740 // but it is idempotent, so it's fine.
741 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743 }
744
Joe Onorato8a9625e2010-01-28 15:55:35 -0800745 // Eat the long press event so the keyboard doesn't come up.
746 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
747 return true;
748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return handled;
751 }
752
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 private String getTypedText() {
754 return mDefaultKeySsb.toString();
755 }
756
757 private void clearTypedText() {
758 mDefaultKeySsb.clear();
759 mDefaultKeySsb.clearSpans();
760 Selection.setSelection(mDefaultKeySsb, 0);
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700764 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
765 * State
766 */
767 private static State intToState(int stateOrdinal) {
768 State state = State.WORKSPACE;
769 final State[] stateValues = State.values();
770 for (int i = 0; i < stateValues.length; i++) {
771 if (stateValues[i].ordinal() == stateOrdinal) {
772 state = stateValues[i];
773 break;
774 }
775 }
776 return state;
777 }
778
779 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 * Restores the previous state, if it exists.
781 *
782 * @param savedState The previous state.
783 */
784 private void restoreState(Bundle savedState) {
785 if (savedState == null) {
786 return;
787 }
788
Michael Jurka883f55b2010-10-21 15:47:14 -0700789 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
790
Winson Chungf0ea4d32011-06-06 14:27:16 -0700791 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800792 showAllApps(false);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
796 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700797 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
800 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803 mAddScreen = addScreen;
804 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
805 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 mRestoring = true;
807 }
808
809 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
810 if (renameFolder) {
811 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700812 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
Winson Chunga12a2502010-12-20 14:41:35 -0800815
Winson Chung785d2eb2011-04-14 16:08:02 -0700816
817 // Restore the AppsCustomize tab
818 if (mAppsCustomizeTabHost != null) {
819 String curTab = savedState.getString("apps_customize_currentTab");
820 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700821 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 mAppsCustomizeContent.setContentType(
823 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
824 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
825 }
826
827 // Note: currently we do not restore the page for the AppsCustomize pane because the
828 // change in layout can drastically affect the saved page index
829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
832 /**
833 * Finds all the views we need and configure them properly.
834 */
835 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700836 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400837
Winson Chung4c98d922011-05-31 16:50:48 -0700838 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
839 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
Winson Chung4c98d922011-05-31 16:50:48 -0700841 // Setup the drag layer
842 mDragLayer.setup(this, dragController);
843
844 // Setup the workspace
845 mWorkspace.setHapticFeedbackEnabled(false);
846 mWorkspace.setOnLongClickListener(this);
847 mWorkspace.setup(this, dragController);
848 mWorkspace.setWallpaperDimension();
849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Get the search/delete bar
851 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700852
Winson Chungf0ea4d32011-06-06 14:27:16 -0700853 // Setup AppsCustomize
854 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
855 findViewById(R.id.apps_customize_pane);
856 mAppsCustomizeContent = (AppsCustomizePagedView)
857 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
858 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400859
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 // Setup AppsCustomize button
861 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
862 mAllAppsButton.setOnClickListener(new OnClickListener() {
863 public void onClick(View v) {
864 onClickAllAppsButton(v);
865 }
866 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800867
Winson Chungf0ea4d32011-06-06 14:27:16 -0700868 if (!LauncherApplication.isScreenLarge()) {
869 // Setup AppsCustomize button on the phone
870 HandleView handleView = (HandleView) mAllAppsButton;
871 handleView.setLauncher(this);
872 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700873
874 // Setup Hotseat
875 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
876 hotseatLeft.setContentDescription(mHotseatLabels[0]);
877 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
878 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
879 hotseatRight.setContentDescription(mHotseatLabels[1]);
880 hotseatRight.setImageDrawable(mHotseatIcons[1]);
881
882 View.OnKeyListener listener = new IndicatorKeyEventListener();
883 mPreviousView = (ImageView) mDragLayer.findViewById(R.id.previous_screen);
884 mPreviousView.setOnKeyListener(listener);
885 mNextView = (ImageView) mDragLayer.findViewById(R.id.next_screen);
886 mNextView.setOnKeyListener(listener);
887
888 Drawable previous = mPreviousView.getDrawable();
889 Drawable next = mNextView.getDrawable();
890 mWorkspace.setIndicators(previous, next);
891
892 mPreviousView.setHapticFeedbackEnabled(false);
893 mPreviousView.setOnLongClickListener(this);
894 mNextView.setHapticFeedbackEnabled(false);
895 mNextView.setOnLongClickListener(this);
Adam Cohencdc30d52010-12-01 15:09:47 -0800896 }
897
Winson Chungf0ea4d32011-06-06 14:27:16 -0700898 if (!LauncherApplication.isScreenLarge()) {
899 // Setup keylistener for button cluster
900 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
901 View.OnKeyListener listener = null;
902 if (LauncherApplication.isScreenLarge()) {
903 // For tablets, AllApps lives in the button bar at the top
904 listener = new ButtonBarKeyEventListener();
905 } else {
906 // For phones, AppsCustomize lives in the "dock" at the bottom
907 listener = new DockKeyEventListener();
908 }
909 int buttonCount = mButtonCluster.getChildCount();
910 for (int i = 0; i < buttonCount; ++i) {
911 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
912 }
Winson Chung97d85d22011-04-13 11:27:36 -0700913 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800914
Winson Chung4c98d922011-05-31 16:50:48 -0700915 // Setup the drag controller (the drop targets have to be added in reverse order)
916 dragController.setDragScoller(mWorkspace);
917 dragController.setScrollView(mDragLayer);
918 dragController.setMoveTarget(mWorkspace);
919 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700920 if (mSearchDeleteBar != null) {
921 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 }
924
Romain Guy8a73c512009-11-09 19:19:59 -0800925 @SuppressWarnings({"UnusedDeclaration"})
926 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700927 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800928 mWorkspace.scrollLeft();
929 }
Romain Guy8a73c512009-11-09 19:19:59 -0800930 }
931
932 @SuppressWarnings({"UnusedDeclaration"})
933 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700934 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800935 mWorkspace.scrollRight();
936 }
Romain Guy8a73c512009-11-09 19:19:59 -0800937 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400938
939 @SuppressWarnings({"UnusedDeclaration"})
940 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700941 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400942
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400943 int index = -1;
944 if (v.getId() == R.id.hotseat_left) {
945 index = 0;
946 } else if (v.getId() == R.id.hotseat_right) {
947 index = 1;
948 }
949
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400950 // reload these every tap; you never know when they might change
951 loadHotseats();
952 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400953 startActivitySafely(
954 mHotseats[index],
955 "hotseat"
956 );
957 }
958 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 /**
961 * Creates a view representing a shortcut.
962 *
963 * @param info The data structure describing the shortcut.
964 *
965 * @return A View inflated from R.layout.application.
966 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800967 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700969 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 /**
973 * Creates a view representing a shortcut inflated from the specified resource.
974 *
975 * @param layoutResId The id of the XML layout used to create the shortcut.
976 * @param parent The group the shortcut belongs to.
977 * @param info The data structure describing the shortcut.
978 *
979 * @return A View inflated from layoutResId.
980 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800981 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800982 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
983 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 return favorite;
986 }
987
988 /**
989 * Add an application shortcut to the workspace.
990 *
991 * @param data The intent describing the application.
992 * @param cellInfo The position on screen where to create the shortcut.
993 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800994 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700995 int intersectCellX, int intersectCellY) {
996 final int[] cellXY = mTmpAddItemCellCoordinates;
997 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
998
999 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1000 showOutOfSpaceMessage();
1001 return;
1002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001004 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001005
Romain Guy73b979d2009-06-09 12:57:21 -07001006 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001007 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001009 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1011 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001012 } else {
1013 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 }
1015 }
Romain Guycbb89e42009-06-08 15:52:54 -07001016
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 /**
1018 * Add a shortcut to the workspace.
1019 *
1020 * @param data The intent describing the shortcut.
1021 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001023 private void completeAddShortcut(Intent data, int screen,
1024 int intersectCellX, int intersectCellY) {
1025 final int[] cellXY = mTmpAddItemCellCoordinates;
1026 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001027
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001028 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001029 boolean foundCellSpan = false;
1030 if (touchXY != null) {
1031 // when dragging and dropping, just find the closest free spot
1032 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1033 int[] result = screenLayout.findNearestVacantArea(
1034 touchXY[0], touchXY[1], 1, 1, cellXY);
1035 foundCellSpan = (result != null);
1036 } else {
1037 foundCellSpan = layout.findCellForSpanThatIntersects(
1038 cellXY, 1, 1, intersectCellX, intersectCellY);
1039 }
1040
1041 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 showOutOfSpaceMessage();
1043 return;
1044 }
1045
1046 final ShortcutInfo info = mModel.addShortcut(
1047 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
1049 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053 }
1054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001058 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 */
Winson Chungf7640c82011-02-28 13:47:29 -08001061 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001062 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1066 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001069 // We have saved the position to which the widget was dragged-- this really only matters
1070 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001072
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001073 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 boolean foundCellSpan = false;
1075 if (touchXY != null) {
1076 // when dragging and dropping, just find the closest free spot
1077 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1078 int[] result = screenLayout.findNearestVacantArea(
1079 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001080 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001082 // if we long pressed on an empty cell to bring up a menu,
1083 // make sure we intersect the empty cell
1084 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1085 // findCellForSpanThatIntersects will just ignore them
1086 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1087 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001088 }
1089
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001091 if (appWidgetId != -1) {
1092 // Deleting an app widget ID is a void call but writes to disk before returning
1093 // to the caller...
1094 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1095 new Thread("deleteAppWidgetId") {
1096 public void run() {
1097 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1098 }
1099 }.start();
1100 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001102 return;
1103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 // Build Launcher-specific widget info and save to database
1106 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 launcherInfo.spanX = spanXY[0];
1108 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 LauncherModel.addItemToDatabase(this, launcherInfo,
1111 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
1114 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001115 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001119
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001120 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001122
Michael Jurka0280c3b2010-09-17 15:00:07 -07001123 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001125
1126 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128 }
Romain Guycbb89e42009-06-08 15:52:54 -07001129
Adam Cohended9f8d2010-11-03 13:25:16 -07001130 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1131 @Override
1132 public void onReceive(Context context, Intent intent) {
1133 final String action = intent.getAction();
1134 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1135 mUserPresent = false;
1136 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001137
1138 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001139 if (mAppsCustomizeContent != null) {
1140 mAppsCustomizeContent.reset();
1141 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001142 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1143 mUserPresent = true;
1144 updateRunning();
1145 }
1146 }
1147 };
1148
1149 @Override
1150 public void onAttachedToWindow() {
1151 super.onAttachedToWindow();
1152
1153 // Listen for broadcasts related to user-presence
1154 final IntentFilter filter = new IntentFilter();
1155 filter.addAction(Intent.ACTION_SCREEN_OFF);
1156 filter.addAction(Intent.ACTION_USER_PRESENT);
1157 registerReceiver(mReceiver, filter);
1158
Adam Cohend113e0c2010-11-11 10:48:05 -08001159 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001160 mVisible = true;
1161 }
1162
1163 @Override
1164 public void onDetachedFromWindow() {
1165 super.onDetachedFromWindow();
1166 mVisible = false;
1167
Adam Cohend113e0c2010-11-11 10:48:05 -08001168 if (mAttached) {
1169 unregisterReceiver(mReceiver);
1170 mAttached = false;
1171 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 updateRunning();
1173 }
1174
1175 public void onWindowVisibilityChanged(int visibility) {
1176 mVisible = visibility == View.VISIBLE;
1177 updateRunning();
1178 }
1179
1180 private void sendAdvanceMessage(long delay) {
1181 mHandler.removeMessages(ADVANCE_MSG);
1182 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1183 mHandler.sendMessageDelayed(msg, delay);
1184 mAutoAdvanceSentTime = System.currentTimeMillis();
1185 }
1186
1187 private void updateRunning() {
1188 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1189 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1190 mAutoAdvanceRunning = autoAdvanceRunning;
1191 if (autoAdvanceRunning) {
1192 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1193 sendAdvanceMessage(delay);
1194 } else {
1195 if (!mWidgetsToAdvance.isEmpty()) {
1196 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1197 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1198 }
1199 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001200 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001201 }
1202 }
1203 }
1204
1205 private final Handler mHandler = new Handler() {
1206 @Override
1207 public void handleMessage(Message msg) {
1208 if (msg.what == ADVANCE_MSG) {
1209 int i = 0;
1210 for (View key: mWidgetsToAdvance.keySet()) {
1211 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1212 final int delay = mAdvanceStagger * i;
1213 if (v instanceof Advanceable) {
1214 postDelayed(new Runnable() {
1215 public void run() {
1216 ((Advanceable) v).advance();
1217 }
1218 }, delay);
1219 }
1220 i++;
1221 }
1222 sendAdvanceMessage(mAdvanceInterval);
1223 }
1224 }
1225 };
1226
1227 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1228 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1229 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1230 if (v instanceof Advanceable) {
1231 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001232 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 updateRunning();
1234 }
1235 }
1236
1237 void removeWidgetToAutoAdvance(View hostView) {
1238 if (mWidgetsToAdvance.containsKey(hostView)) {
1239 mWidgetsToAdvance.remove(hostView);
1240 updateRunning();
1241 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 }
1243
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1245 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001247 launcherInfo.hostView = null;
1248 }
1249
Adam Cohended9f8d2010-11-03 13:25:16 -07001250 void showOutOfSpaceMessage() {
1251 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1252 }
1253
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 public LauncherAppWidgetHost getAppWidgetHost() {
1255 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
Romain Guycbb89e42009-06-08 15:52:54 -07001257
Winson Chunga9abd0e2010-10-27 17:18:37 -07001258 public LauncherModel getModel() {
1259 return mModel;
1260 }
1261
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001262 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001263 getWindow().closeAllPanels();
1264
1265 try {
1266 dismissDialog(DIALOG_CREATE_SHORTCUT);
1267 // Unlock the workspace if the dialog was showing
1268 } catch (Exception e) {
1269 // An exception is thrown if the dialog is not visible, which is fine
1270 }
1271
1272 try {
1273 dismissDialog(DIALOG_RENAME_FOLDER);
1274 // Unlock the workspace if the dialog was showing
1275 } catch (Exception e) {
1276 // An exception is thrown if the dialog is not visible, which is fine
1277 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001278
1279 // Whatever we were doing is hereby canceled.
1280 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001281 }
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 @Override
1284 protected void onNewIntent(Intent intent) {
1285 super.onNewIntent(intent);
1286
1287 // Close the menu
1288 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001289 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001290 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001291
Joe Onorato14f122b2009-11-19 14:06:36 -08001292 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1293 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001294
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001295 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001296 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001297 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001298 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001299 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001300 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001301 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001302
Joe Onorato3a8820b2009-11-10 15:06:42 -08001303 final View v = getWindow().peekDecorView();
1304 if (v != null && v.getWindowToken() != null) {
1305 InputMethodManager imm = (InputMethodManager)getSystemService(
1306 INPUT_METHOD_SERVICE);
1307 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001309
1310 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001311 if (mAppsCustomizeContent != null) {
1312 mAppsCustomizeContent.reset();
1313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315 }
1316
1317 @Override
1318 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1319 // Do not call super here
1320 mSavedInstanceState = savedInstanceState;
1321 }
1322
1323 @Override
1324 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001325 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001326
1327 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1328 if (folders.size() > 0) {
1329 final int count = folders.size();
1330 long[] ids = new long[count];
1331 for (int i = 0; i < count; i++) {
1332 final FolderInfo info = folders.get(i).getInfo();
1333 ids[i] = info.id;
1334 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001335 outState.putLongArray(RUNTIME_STATE_FOLDERS, ids);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 } else {
1337 super.onSaveInstanceState(outState);
1338 }
1339
Michael Jurka883f55b2010-10-21 15:47:14 -07001340 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
Michael Jurka0280c3b2010-09-17 15:00:07 -07001342 if (mAddScreen > -1 && mWaitingForResult) {
1343 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1344 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1345 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 if (mFolderInfo != null && mWaitingForResult) {
1349 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1350 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1351 }
Michael Jurka946ad472010-07-09 18:05:18 -07001352
Winson Chung785d2eb2011-04-14 16:08:02 -07001353 // Save the current AppsCustomize tab
1354 if (mAppsCustomizeTabHost != null) {
1355 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1356 if (currentTabTag != null) {
1357 outState.putString("apps_customize_currentTab", currentTabTag);
1358 }
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361
1362 @Override
1363 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001365
Winson Chungcd2b0142011-06-08 16:02:26 -07001366 // Stop callbacks from LauncherModel
1367 LauncherApplication app = ((LauncherApplication) getApplication());
1368 mModel.stopLoader();
1369 app.setLauncher(null);
1370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001372 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001374 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001375 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001376 mAppWidgetHost = null;
1377
1378 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379
1380 TextKeyListener.getInstance().release();
1381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Joe Onorato9c1289c2009-08-17 11:03:03 -04001383 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001384
1385 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001386
Patrick Dubroyab962b72010-07-26 12:10:10 -07001387 // Some launcher layouts don't have a previous and next view
1388 if (mPreviousView != null) {
1389 dismissPreview(mPreviousView);
1390 }
1391 if (mNextView != null) {
1392 dismissPreview(mNextView);
1393 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001394
1395 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001396
1397 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1398 mWorkspace.removeAllViews();
1399 mWorkspace = null;
1400 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001401
1402 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 }
1404
Adam Cohena9cf38f2011-05-02 15:36:58 -07001405 public DragController getDragController() {
1406 return mDragController;
1407 }
1408
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 @Override
1410 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001411 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 super.startActivityForResult(intent, requestCode);
1413 }
1414
1415 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001416 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001418
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001419 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001420
Karl Rosaen138a0412009-04-23 19:00:21 -07001421 if (initialQuery == null) {
1422 // Use any text typed in the launcher as the initial query
1423 initialQuery = getTypedText();
1424 clearTypedText();
1425 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 if (appSearchData == null) {
1427 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001428 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Romain Guycbb89e42009-06-08 15:52:54 -07001430
Karl Rosaen138a0412009-04-23 19:00:21 -07001431 final SearchManager searchManager =
1432 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001433 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001434 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436
1437 @Override
1438 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001439 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001440 return false;
1441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
1443 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001444
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1446 .setIcon(android.R.drawable.ic_menu_add)
1447 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001448 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1449 .setIcon(android.R.drawable.ic_menu_manage)
1450 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001451 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 .setIcon(android.R.drawable.ic_menu_gallery)
1453 .setAlphabeticShortcut('W');
1454 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1455 .setIcon(android.R.drawable.ic_search_category_default)
1456 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1457 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1458 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1459 .setAlphabeticShortcut('N');
1460
1461 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001462 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1463 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464
1465 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1466 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1467 .setIntent(settings);
1468
1469 return true;
1470 }
1471
1472 @Override
1473 public boolean onPrepareOptionsMenu(Menu menu) {
1474 super.onPrepareOptionsMenu(menu);
1475
Winson Chung785d2eb2011-04-14 16:08:02 -07001476 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1477 // related code?
1478 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479
1480 return true;
1481 }
1482
1483 @Override
1484 public boolean onOptionsItemSelected(MenuItem item) {
1485 switch (item.getItemId()) {
1486 case MENU_ADD:
1487 addItems();
1488 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001489 case MENU_MANAGE_APPS:
1490 manageApps();
1491 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 case MENU_WALLPAPER_SETTINGS:
1493 startWallpaper();
1494 return true;
1495 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001496 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 return true;
1498 case MENU_NOTIFICATIONS:
1499 showNotifications();
1500 return true;
1501 }
1502
1503 return super.onOptionsItemSelected(item);
1504 }
Romain Guycbb89e42009-06-08 15:52:54 -07001505
Karl Rosaen138a0412009-04-23 19:00:21 -07001506 /**
1507 * Indicates that we want global search for this activity by setting the globalSearch
1508 * argument for {@link #startSearch} to true.
1509 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001511 @Override
1512 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001513 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001514 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001515 }
1516
Joe Onorato9c1289c2009-08-17 11:03:03 -04001517 public boolean isWorkspaceLocked() {
1518 return mWorkspaceLoading || mWaitingForResult;
1519 }
1520
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001521 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1522 private boolean isPreviewVisible() {
1523 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1524 (mNextView != null && mNextView.getTag() != null);
1525 }
1526
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001528 showWorkspace(true);
1529 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
Michael Jurka0280c3b2010-09-17 15:00:07 -07001532 private void resetAddInfo() {
1533 mAddScreen = -1;
1534 mAddIntersectCellX = -1;
1535 mAddIntersectCellY = -1;
1536 mAddDropPosition = null;
1537 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001538
Winson Chung55cef262010-10-28 14:14:18 -07001539 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001540 resetAddInfo();
1541 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001542 mAddDropPosition = position;
1543
Michael Jurkaaf442092010-06-10 17:01:57 -07001544 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001545 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1546 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001547 }
1548
Winson Chung7ad01412010-09-27 11:33:03 -07001549 private void manageApps() {
1550 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1551 }
1552
Michael Jurkaaf442092010-06-10 17:01:57 -07001553 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001554 // TODO: catch bad widget exception when sent
1555 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001556 // TODO: Is this log message meaningful?
1557 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001558 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001559 }
1560
Winson Chung55cef262010-10-28 14:14:18 -07001561 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001562 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
Bjorn Bringert7984c942009-12-09 15:38:25 +00001564 if (appWidget.configure != null) {
1565 // Launch over to configure widget, if needed
1566 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1567 intent.setComponent(appWidget.configure);
1568 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001569 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001570 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1571 intent.putExtra(
1572 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1573 info.mimeType);
1574
1575 final String mimeType = info.mimeType;
1576 final ClipData clipData = (ClipData) info.configurationData;
1577 final ClipDescription clipDesc = clipData.getDescription();
1578 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1579 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001580 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001581 final CharSequence stringData = item.getText();
1582 final Uri uriData = item.getUri();
1583 final Intent intentData = item.getIntent();
1584 final String key =
1585 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1586 if (uriData != null) {
1587 intent.putExtra(key, uriData);
1588 } else if (intentData != null) {
1589 intent.putExtra(key, intentData);
1590 } else if (stringData != null) {
1591 intent.putExtra(key, stringData);
1592 }
1593 break;
1594 }
1595 }
1596 }
Winson Chung55cef262010-10-28 14:14:18 -07001597 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001598
Romain Guy8e633c52010-03-04 12:51:36 -08001599 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001601 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001602 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 }
1604 }
Romain Guycbb89e42009-06-08 15:52:54 -07001605
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1607 resetAddInfo();
1608 mAddScreen = screen;
1609 mAddDropPosition = position;
1610
1611 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1612 createShortcutIntent.setComponent(componentName);
1613 processShortcut(createShortcutIntent);
1614 }
1615
Joe Onoratodeb98af2010-02-19 14:59:39 -08001616 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001617 // Handle case where user selected "Applications"
1618 String applicationName = getResources().getString(R.string.group_applications);
1619 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001620
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001621 if (applicationName != null && applicationName.equals(shortcutName)) {
1622 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1623 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001624
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001625 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1626 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001627 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001628 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001629 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001630 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
1633
Winson Chung24ab2f12010-09-16 14:10:47 -07001634 void processWallpaper(Intent intent) {
1635 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1636 }
1637
Adam Cohendf035382011-04-11 17:22:04 -07001638 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
Adam Cohendf2cc412011-04-27 16:56:57 -07001639 FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 folderInfo.title = getText(R.string.folder_name);
1641
Michael Jurka0280c3b2010-09-17 15:00:07 -07001642 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1643 final int[] cellXY = mTmpAddItemCellCoordinates;
1644 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1645 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001646 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648
1649 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650 LauncherModel.addItemToDatabase(this, folderInfo,
1651 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001652 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001653 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654
1655 // Create the view
1656 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001657 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1658 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001659 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001660 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
Joe Onorato9c1289c2009-08-17 11:03:03 -04001663 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001664 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001665 }
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 private void showNotifications() {
1668 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1669 if (statusBar != null) {
1670 statusBar.expand();
1671 }
1672 }
1673
1674 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001675 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001677 Intent chooser = Intent.createChooser(pickWallpaper,
1678 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001679 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1680 // Removed in Eclair MR1
1681// WallpaperManager wm = (WallpaperManager)
1682// getSystemService(Context.WALLPAPER_SERVICE);
1683// WallpaperInfo wi = wm.getWallpaperInfo();
1684// if (wi != null && wi.getSettingsActivity() != null) {
1685// LabeledIntent li = new LabeledIntent(getPackageName(),
1686// R.string.configure_wallpaper, 0);
1687// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1688// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1689// }
Mike Clerona0618e42009-10-22 13:55:21 -07001690 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 }
1692
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001693 /**
1694 * Registers various content observers. The current implementation registers
1695 * only a favorites observer to keep track of the favorites applications.
1696 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001697 private void registerContentObservers() {
1698 ContentResolver resolver = getContentResolver();
1699 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1700 true, mWidgetObserver);
1701 }
1702
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 @Override
1704 public boolean dispatchKeyEvent(KeyEvent event) {
1705 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1706 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001708 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001709 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001710 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001711 dumpState();
1712 return true;
1713 }
1714 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001715 }
1716 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1717 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001718 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 return true;
1720 }
1721 }
1722
1723 return super.dispatchKeyEvent(event);
1724 }
1725
Joe Onorato88ec0992009-11-19 13:16:06 -08001726 @Override
1727 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001728 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001729 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001730 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001731 Folder openFolder = mWorkspace.getOpenFolder();
1732 if (openFolder.isEditingName()) {
1733 openFolder.dismissEditingName();
1734 } else {
1735 closeFolder();
1736 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001737 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001738 dismissPreview(mPreviousView);
1739 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001740 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001741 mWorkspace.exitWidgetResizeMode();
1742
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001743 // Back button is a no-op here, but give at least some feedback for the button press
1744 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001745 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001746 }
1747
Adam Cohen2801caf2011-05-13 20:57:39 -07001748 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 Folder folder = mWorkspace.getOpenFolder();
1750 if (folder != null) {
1751 closeFolder(folder);
1752 }
1753 }
1754
1755 void closeFolder(Folder folder) {
1756 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001757
Michael Jurka8c920dd2011-01-20 14:16:56 -08001758 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001760 CellLayout cl = (CellLayout) parent;
Adam Cohen2801caf2011-05-13 20:57:39 -07001761 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1762 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001763 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001765 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 }
1767
1768 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001769 * Re-listen when widgets are reset.
1770 */
1771 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001772 if (mAppWidgetHost != null) {
1773 mAppWidgetHost.startListening();
1774 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001775 }
1776
1777 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001778 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1779 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001781 private void unbindDesktopItems() {
1782 for (ItemInfo item: mDesktopItems) {
1783 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001785 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001787
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 /**
1789 * Launches the intent referred by the clicked shortcut.
1790 *
1791 * @param v The view representing the clicked shortcut.
1792 */
1793 public void onClick(View v) {
1794 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001795 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001797 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001798 int[] pos = new int[2];
1799 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001800 intent.setSourceBounds(new Rect(pos[0], pos[1],
1801 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001802 boolean success = startActivitySafely(intent, tag);
1803
1804 if (success && v instanceof BubbleTextView) {
1805 mWaitingForResume = (BubbleTextView) v;
1806 mWaitingForResume.setStayPressed(true);
1807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001809 if (v instanceof FolderIcon) {
1810 FolderIcon fi = (FolderIcon) v;
1811 handleFolderClick(fi);
1812 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001813 } else if (v == mAllAppsButton) {
1814 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001815 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001816 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001817 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
1821
Michael Jurka0e260592010-06-30 17:07:39 -07001822 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001823 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001824 // clicking anywhere on the workspace causes the customization drawer to slide down
1825 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001826 return false;
1827 }
1828
Michael Jurkaaf442092010-06-10 17:01:57 -07001829 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001830 * Event handler for the search button
1831 *
1832 * @param v The view that was clicked.
1833 */
1834 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001835 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001836 // Use a custom animation for launching search
1837 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001838 }
1839
1840 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001841 * Event handler for the voice button
1842 *
1843 * @param v The view that was clicked.
1844 */
1845 public void onClickVoiceButton(View v) {
1846 startVoiceSearch();
1847 }
1848
1849 private void startVoiceSearch() {
1850 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1851 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1852 startActivity(intent);
1853 }
1854
1855 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001856 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001857 * enters home screen customization mode.
1858 *
1859 * @param v The view that was clicked.
1860 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001861 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001862 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001863 }
1864
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001865 /**
1866 * Event handler for the "grid" button that appears on the home screen, which
1867 * enters all apps mode.
1868 *
1869 * @param v The view that was clicked.
1870 */
1871 public void onClickAllAppsButton(View v) {
1872 showAllApps(true);
1873 }
1874
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001875 public void onClickAppMarketButton(View v) {
1876 if (mAppMarketIntent != null) {
1877 startActivitySafely(mAppMarketIntent, "app market");
1878 }
1879 }
1880
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001881 void startApplicationDetailsActivity(ComponentName componentName) {
1882 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001883 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1884 Uri.fromParts("package", packageName, null));
1885 startActivity(intent);
1886 }
1887
Patrick Dubroy5539af72010-09-07 15:22:01 -07001888 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1889 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1890 // System applications cannot be installed. For now, show a toast explaining that.
1891 // We may give them the option of disabling apps this way.
1892 int messageId = R.string.uninstall_system_app_text;
1893 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1894 } else {
1895 String packageName = appInfo.componentName.getPackageName();
1896 String className = appInfo.componentName.getClassName();
1897 Intent intent = new Intent(
1898 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1899 startActivity(intent);
1900 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001901 }
1902
Michael Jurkaddd62e92011-02-16 17:49:14 -08001903 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1905 try {
1906 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001907 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 } catch (ActivityNotFoundException e) {
1909 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001910 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 } catch (SecurityException e) {
1912 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001913 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001915 "or use the exported attribute for this activity. "
1916 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001918 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001920
Romain Guy8e633c52010-03-04 12:51:36 -08001921 void startActivityForResultSafely(Intent intent, int requestCode) {
1922 try {
1923 startActivityForResult(intent, requestCode);
1924 } catch (ActivityNotFoundException e) {
1925 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1926 } catch (SecurityException e) {
1927 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1928 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1929 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1930 "or use the exported attribute for this activity.", e);
1931 }
1932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933
Adam Cohena9cf38f2011-05-02 15:36:58 -07001934 private void handleFolderClick(FolderIcon folderIcon) {
1935 final FolderInfo info = folderIcon.mInfo;
1936 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 // Close any open folder
1938 closeFolder();
1939 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001940 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 } else {
1942 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001943 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 int folderScreen;
1945 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001946 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 // .. and close it
1948 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001949 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // Close any folder open on the current screen
1951 closeFolder();
1952 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001953 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955 }
1956 }
1957 }
1958
Adam Cohen2801caf2011-05-13 20:57:39 -07001959 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1960 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1961 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1962 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1963
1964 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1965 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1966 oa.start();
1967 }
1968
1969 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1970 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1971 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1972 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1973
1974 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1975 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1976 oa.start();
1977 }
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001980 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 * is animated relative to the specified View. If the View is null, no animation
1982 * is played.
1983 *
1984 * @param folderInfo The FolderInfo describing the folder to open.
1985 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001986 public void openFolder(FolderIcon folderIcon) {
1987 Folder folder = folderIcon.mFolder;
1988 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohen2801caf2011-05-13 20:57:39 -07001990 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Adam Cohena9cf38f2011-05-02 15:36:58 -07001993 mWorkspace.addInFullScreen(folder, info.screen);
1994 folder.animateOpen();
1995 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001999 if (mState != State.WORKSPACE) {
2000 return false;
2001 }
2002
Romain Guy1fbc1c82009-11-09 20:43:08 -08002003 switch (v.getId()) {
2004 case R.id.previous_screen:
Winson Chungf0ea4d32011-06-06 14:27:16 -07002005 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002006 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2007 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002008 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002009 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002010 return true;
2011 case R.id.next_screen:
Winson Chungf0ea4d32011-06-06 14:27:16 -07002012 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002013 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2014 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002015 showPreviews(v);
2016 }
2017 return true;
2018 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07002019 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002020 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2021 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2022 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002023 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002024 return true;
2025 }
2026
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 return false;
2029 }
2030
2031 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002032 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 }
2034
Michael Jurka0280c3b2010-09-17 15:00:07 -07002035 resetAddInfo();
2036 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002038 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 return true;
2040 }
2041
Michael Jurka0280c3b2010-09-17 15:00:07 -07002042 final View itemUnderLongClick = longClickCellInfo.cell;
2043
Winson Chung304dcde2011-01-07 11:17:23 -08002044 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002045 if (itemUnderLongClick == null) {
2046 // User long pressed on empty space
2047 mWorkspace.setAllowLongPress(false);
2048 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2049 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002050 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002052 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002054 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2055 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002056 mAddIntersectCellX = longClickCellInfo.cellX;
2057 mAddIntersectCellY = longClickCellInfo.cellY;
2058 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
2060 }
2061 }
2062 return true;
2063 }
2064
Romain Guye6b8e2f2009-11-10 11:56:55 -08002065 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002066 private void dismissPreview(final View v) {
2067 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002068 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002069 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2070 public void onDismiss() {
2071 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2072 int count = group.getChildCount();
2073 for (int i = 0; i < count; i++) {
2074 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2075 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002076 ArrayList<Bitmap> bitmaps =
2077 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002078 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2079
2080 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002081 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002082 window.setOnDismissListener(null);
2083 }
2084 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002085 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002086 }
2087 v.setTag(null);
2088 }
2089
Romain Guyf8e6a802009-12-07 17:48:02 -08002090 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002091 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002092 }
2093
Romain Guya6abce82009-11-10 02:54:41 -08002094 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002095 final Resources resources = getResources();
2096 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002097
Romain Guya6abce82009-11-10 02:54:41 -08002098 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002099
Romain Guy9d31e9f2009-11-11 18:54:28 -08002100 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002101
Romain Guyf8e6a802009-12-07 17:48:02 -08002102 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002103 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002104 int extraW = (int) ((r.left + r.right) * max);
2105 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002106
2107 int aW = cell.getWidth() - extraW;
2108 float w = aW / max;
2109
2110 int width = cell.getWidth();
2111 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002112 int x = cell.getPaddingLeft();
2113 int y = cell.getPaddingTop();
2114 width -= (x + cell.getPaddingRight());
2115 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002116
2117 float scale = w / width;
2118
2119 int count = end - start;
2120
2121 final float sWidth = width * scale;
2122 float sHeight = height * scale;
2123
Romain Guye6b8e2f2009-11-10 11:56:55 -08002124 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002125
Romain Guye6b8e2f2009-11-10 11:56:55 -08002126 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2127 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002128
2129 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002130 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002131 cell = (CellLayout) workspace.getChildAt(i);
2132
Romain Guyf8e6a802009-12-07 17:48:02 -08002133 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002134 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002135
2136 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002137 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002138 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002139 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002140
Romain Guy9d31e9f2009-11-11 18:54:28 -08002141 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002142 image.setImageBitmap(bitmap);
2143 image.setTag(i);
2144 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002145 image.setOnFocusChangeListener(handler);
2146 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002147 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002148
2149 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002150 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2151
Winson Chungaafa03c2010-06-11 17:34:16 -07002152 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002153 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002154
2155 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002156 p.setContentView(preview);
2157 p.setWidth((int) (sWidth * count + extraW));
2158 p.setHeight((int) (sHeight + extraH));
2159 p.setAnimationStyle(R.style.AnimationPreview);
2160 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002161 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002162 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002163 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002164
Romain Guye6b8e2f2009-11-10 11:56:55 -08002165 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2166 public void onDismiss() {
2167 dismissPreview(anchor);
2168 }
2169 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002170
2171 anchor.setTag(p);
2172 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002173 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002174 }
2175
Romain Guy9d31e9f2009-11-11 18:54:28 -08002176 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002177 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002178
Romain Guye6b8e2f2009-11-10 11:56:55 -08002179 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002180 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002181 }
2182
2183 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002184 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002185 v.post(this);
2186 }
2187
2188 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002189 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002190 }
2191
2192 public void onFocusChange(View v, boolean hasFocus) {
2193 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002194 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002195 }
Romain Guya6abce82009-11-10 02:54:41 -08002196 }
2197 }
2198
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 Workspace getWorkspace() {
2200 return mWorkspace;
2201 }
2202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 @Override
2204 protected Dialog onCreateDialog(int id) {
2205 switch (id) {
2206 case DIALOG_CREATE_SHORTCUT:
2207 return new CreateShortcut().createDialog();
2208 case DIALOG_RENAME_FOLDER:
2209 return new RenameFolder().createDialog();
2210 }
2211
2212 return super.onCreateDialog(id);
2213 }
2214
2215 @Override
2216 protected void onPrepareDialog(int id, Dialog dialog) {
2217 switch (id) {
2218 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 break;
2220 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002221 if (mFolderInfo != null) {
2222 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2223 final CharSequence text = mFolderInfo.title;
2224 input.setText(text);
2225 input.setSelection(0, text.length());
2226 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 break;
2228 }
2229 }
2230
2231 void showRenameDialog(FolderInfo info) {
2232 mFolderInfo = info;
2233 mWaitingForResult = true;
2234 showDialog(DIALOG_RENAME_FOLDER);
2235 }
2236
Michael Jurka0280c3b2010-09-17 15:00:07 -07002237 private void showAddDialog(int intersectX, int intersectY) {
2238 resetAddInfo();
2239 mAddIntersectCellX = intersectX;
2240 mAddIntersectCellY = intersectY;
2241 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 mWaitingForResult = true;
2243 showDialog(DIALOG_CREATE_SHORTCUT);
2244 }
2245
Joe Onoratodeb98af2010-02-19 14:59:39 -08002246 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002247 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002248 Bundle bundle = new Bundle();
2249
2250 ArrayList<String> shortcutNames = new ArrayList<String>();
2251 shortcutNames.add(getString(R.string.group_applications));
2252 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2253
2254 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2255 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2256 R.drawable.ic_launcher_application));
2257 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2258
2259 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2260 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002261 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002262 pickIntent.putExtras(bundle);
2263
Joe Onoratodeb98af2010-02-19 14:59:39 -08002264 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002265 }
2266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002267 private class RenameFolder {
2268 private EditText mInput;
2269
2270 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2272 mInput = (EditText) layout.findViewById(R.id.folder_name);
2273
2274 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2275 builder.setIcon(0);
2276 builder.setTitle(getString(R.string.rename_folder_title));
2277 builder.setCancelable(true);
2278 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2279 public void onCancel(DialogInterface dialog) {
2280 cleanup();
2281 }
2282 });
2283 builder.setNegativeButton(getString(R.string.cancel_action),
2284 new Dialog.OnClickListener() {
2285 public void onClick(DialogInterface dialog, int which) {
2286 cleanup();
2287 }
2288 }
2289 );
2290 builder.setPositiveButton(getString(R.string.rename_action),
2291 new Dialog.OnClickListener() {
2292 public void onClick(DialogInterface dialog, int which) {
2293 changeFolderName();
2294 }
2295 }
2296 );
2297 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002298
2299 final AlertDialog dialog = builder.create();
2300 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2301 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002302 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002303 mInput.requestFocus();
2304 InputMethodManager inputManager = (InputMethodManager)
2305 getSystemService(Context.INPUT_METHOD_SERVICE);
2306 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002307 }
2308 });
2309
2310 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
2312
2313 private void changeFolderName() {
2314 final String name = mInput.getText().toString();
2315 if (!TextUtils.isEmpty(name)) {
2316 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002317 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 mFolderInfo.title = name;
2319 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2320
Joe Onorato9c1289c2009-08-17 11:03:03 -04002321 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002322 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002323 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 } else {
2325 final FolderIcon folderIcon = (FolderIcon)
2326 mWorkspace.getViewForTag(mFolderInfo);
2327 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002328 // TODO: At some point we'll probably want some version of setting
2329 // the text for a folder icon.
2330 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002331 getWorkspace().requestLayout();
2332 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002333 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002334 mWorkspaceLoading = true;
2335 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 }
2337 }
2338 }
2339 cleanup();
2340 }
2341
2342 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 dismissDialog(DIALOG_RENAME_FOLDER);
2344 mWaitingForResult = false;
2345 mFolderInfo = null;
2346 }
2347 }
2348
Daniel Sandler843e8602010-06-07 14:59:01 -04002349 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2350 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002351 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002352 }
2353
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002354 // AllAppsView.Watcher
2355 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002356 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002357 mWorkspace.setVisibility(View.GONE);
2358 }
2359 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002360
2361 /**
2362 * Helper method for the cameraZoomIn/cameraZoomOut animations
2363 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002364 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002365 * @param scaleFactor The scale factor used for the zoom
2366 */
2367 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2368 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002369
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002370 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002371 // Set pivotY so that at the starting zoom factor, the view is partially
2372 // visible. Modifying initialHeightFactor changes how much of the view is
2373 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002375 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002376 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002377 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002378 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002379 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002380 }
2381 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002382
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002383 /**
2384 * Zoom the camera out from the workspace to reveal 'toView'.
2385 * Assumes that the view to show is anchored at either the very top or very bottom
2386 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002387 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002388 */
Winson Chung20f71112011-04-06 14:22:04 -07002389 private void cameraZoomOut(State toState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002390 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002391
Winson Chungf0ea4d32011-06-06 14:27:16 -07002392 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2393 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2394 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2395 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002396
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002397 setPivotsForZoom(toView, toState, scale);
2398
Winson Chung20f71112011-04-06 14:22:04 -07002399
Winson Chungf0ea4d32011-06-06 14:27:16 -07002400 if (springLoaded) {
2401 if (toState == State.APPS_CUSTOMIZE) {
2402 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2403 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2404 } else {
2405 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2406 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
Winson Chung20f71112011-04-06 14:22:04 -07002407 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002408 } else {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002409 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
2410 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002411 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002412
2413 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002414 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002415 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002416 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2417 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002418 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002419 toView.setFastScaleX(a * scale + b * 1f);
2420 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002421 }
2422 });
Adam Cohen61033d32010-11-15 18:29:44 -08002423
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 toView.setVisibility(View.VISIBLE);
2425 toView.setFastAlpha(0f);
2426 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2427 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2428 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2429 public void onAnimationUpdate(float a, float b) {
2430 // don't need to invalidate because we do so above
2431 toView.setFastAlpha(a * 0f + b * 1f);
2432 }
2433 });
2434 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002435
Michael Jurkaabded662011-03-04 12:06:57 -08002436 if (toView instanceof LauncherTransitionable) {
2437 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002438 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002439 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002440 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002441 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002442 // Prepare the position
2443 toView.setTranslationX(0.0f);
2444 toView.setTranslationY(0.0f);
2445 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002446 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002447 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002448 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002449 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002450 // If we don't set the final scale values here, if this animation is cancelled
2451 // it will have the wrong scale value and subsequent cameraPan animations will
2452 // not fix that
2453 toView.setScaleX(1.0f);
2454 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002455 if (toView instanceof LauncherTransitionable) {
2456 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002457 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002458 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002459 });
2460
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002461 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002462 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002464 if (mStateAnimation != null) mStateAnimation.cancel();
2465 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002466 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002467 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002468 } else {
2469 toView.setTranslationX(0.0f);
2470 toView.setTranslationY(0.0f);
2471 toView.setScaleX(1.0f);
2472 toView.setScaleY(1.0f);
2473 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002474 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002475 if (toView instanceof LauncherTransitionable) {
2476 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2477 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2478 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002480 }
2481
2482 /**
2483 * Zoom the camera back into the workspace, hiding 'fromView'.
2484 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002485 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002486 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002487 */
Michael Jurkad3ef3062010-11-23 16:23:58 -08002488 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002489 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002490
Winson Chungf0ea4d32011-06-06 14:27:16 -07002491 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2492 final float scaleFactor = (float)
2493 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2494 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002495
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002497
Michael Jurkad3ef3062010-11-23 16:23:58 -08002498 if (!springLoaded) {
2499 mWorkspace.unshrink(animated);
2500 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002501 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002502 if (mStateAnimation != null) mStateAnimation.cancel();
2503 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002504
Michael Jurka742574b2011-02-02 23:51:01 -08002505 final float oldScaleX = fromView.getScaleX();
2506 final float oldScaleY = fromView.getScaleY();
2507
2508 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2509 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002510 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2511 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002512 ((View)fromView.getParent()).fastInvalidate();
2513 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2514 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2515 }
2516 });
Michael Jurkaabded662011-03-04 12:06:57 -08002517 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002518 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002519 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002520 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2521 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002522 // don't need to invalidate because we do so above
2523 fromView.setFastAlpha(a * 1f + b * 0f);
2524 }
2525 });
Michael Jurkaabded662011-03-04 12:06:57 -08002526 if (fromView instanceof LauncherTransitionable) {
2527 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002528 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002529 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002530 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002531 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002533 if (fromView instanceof LauncherTransitionable) {
2534 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002535 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002536 }
2537 });
2538
Winson Chungf0ea4d32011-06-06 14:27:16 -07002539 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002540 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 } else {
2542 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002543 if (fromView instanceof LauncherTransitionable) {
2544 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2545 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2546 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002548 }
2549
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002550 void showWorkspace(boolean animated) {
2551 showWorkspace(animated, null);
2552 }
2553
2554 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002555 if (layout != null) {
2556 // always animated, but that's ok since we never specify a layout and
2557 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002558 mWorkspace.unshrink(layout);
2559 } else {
2560 mWorkspace.unshrink(animated);
2561 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002562 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002563 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002564 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002565
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 // Change the state *after* we've called all the transition code
2567 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002568
Winson Chung5fb63472011-02-02 17:03:37 -08002569 // Resume the auto-advance of widgets
2570 mUserPresent = true;
2571 updateRunning();
2572
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002573 // send an accessibility event to announce the context change
2574 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002575 }
2576
Michael Jurkad3ef3062010-11-23 16:23:58 -08002577 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002578 // Enter spring loaded mode on a new layout
Michael Jurkad3ef3062010-11-23 16:23:58 -08002579 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002580
2581 if (mState == State.APPS_CUSTOMIZE) {
2582 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
2583 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungb26f3d62011-06-02 10:49:29 -07002584 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002585 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002586 }
2587
2588 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002589 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002590 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chungb26f3d62011-06-02 10:49:29 -07002591 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2592 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002593 }
2594 // Otherwise, we are not in spring loaded mode, so don't do anything.
2595 }
2596
2597 /**
2598 * Shows the dock/hotseat area.
2599 */
2600 void showDock(boolean animated) {
2601 if (!LauncherApplication.isScreenLarge()) {
2602 if (animated) {
2603 int duration = mSearchDeleteBar.getTransitionInDuration();
2604 mButtonCluster.animate().alpha(1f).setDuration(duration);
2605 mPreviousView.animate().alpha(1f).setDuration(duration);
2606 mNextView.animate().alpha(1f).setDuration(duration);
2607 } else {
2608 mButtonCluster.setAlpha(1f);
2609 mPreviousView.setAlpha(1f);
2610 mNextView.setAlpha(1f);
2611 }
2612 }
2613 }
2614
2615 /**
2616 * Hides the dock/hotseat area.
2617 */
2618 void hideDock(boolean animated) {
2619 if (!LauncherApplication.isScreenLarge()) {
2620 if (animated) {
2621 int duration = mSearchDeleteBar.getTransitionOutDuration();
2622 mButtonCluster.animate().alpha(0f).setDuration(duration);
2623 mPreviousView.animate().alpha(0f).setDuration(duration);
2624 mNextView.animate().alpha(0f).setDuration(duration);
2625 } else {
2626 mButtonCluster.setAlpha(0f);
2627 mPreviousView.setAlpha(0f);
2628 mNextView.setAlpha(0f);
2629 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002630 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002631 }
2632
Winson Chung785d2eb2011-04-14 16:08:02 -07002633 void showAllApps(boolean animated) {
2634 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002635
Winson Chungf0ea4d32011-06-06 14:27:16 -07002636 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2637 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002638
Winson Chungf0ea4d32011-06-06 14:27:16 -07002639 // Hide the search bar and dock
2640 mSearchDeleteBar.hideSearchBar(animated);
2641 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002642
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 // Change the state *after* we've called all the transition code
2644 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002645
2646 // Pause the auto-advance of widgets until we are out of AllApps
2647 mUserPresent = false;
2648 updateRunning();
2649
2650 // Send an accessibility event to announce the context change
2651 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2652 }
2653
Joe Onoratob2061212009-11-24 16:13:54 -05002654 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002655 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002656 * - Home from workspace
2657 * - from center screen
2658 * - from other screens
2659 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002660 * - from center screen
2661 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002662 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002663 * - from center screen
2664 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002665 * - Launch app from workspace and quit
2666 * - with back
2667 * - with home
2668 * - Launch app from all apps and quit
2669 * - with back
2670 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002671 * - Go to a screen that's not the default, then all
2672 * apps, and launch and app, and go back
2673 * - with back
2674 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002675 * - On workspace, long press power and go back
2676 * - with back
2677 * - with home
2678 * - On all apps, long press power and go back
2679 * - with back
2680 * - with home
2681 * - On workspace, power off
2682 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002683 * - Launch an app and turn off the screen while in that app
2684 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002685 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002686 * - From all apps
2687 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002688 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2689 * - From all apps
2690 * - From the center workspace
2691 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002692 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002693 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002694 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2695 mWorkspace.setVisibility(View.VISIBLE);
2696 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002697
Winson Chungf0ea4d32011-06-06 14:27:16 -07002698 // Show the search bar and dock
2699 mSearchDeleteBar.showSearchBar(animated);
2700 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002701
Winson Chungf0ea4d32011-06-06 14:27:16 -07002702 // Set focus to the AppsCustomize button
2703 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002704 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002705 }
2706
Joe Onorato7c312c12009-08-13 21:36:53 -07002707 void lockAllApps() {
2708 // TODO
2709 }
2710
2711 void unlockAllApps() {
2712 // TODO
2713 }
2714
Patrick Dubroy5f445422011-02-18 14:35:21 -08002715 /**
2716 * Add an item from all apps or customize onto the given workspace screen.
2717 * If layout is null, add to the current screen.
2718 */
2719 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002720 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2721 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002722 } else {
2723 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002724 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002725 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002726
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002727 void onWorkspaceClick(CellLayout layout) {
2728 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002729 }
2730
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002731 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002732 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002733 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002734 // Look for the toolbar icon specified in the activity meta-data
2735 Bundle metaData = packageManager.getActivityInfo(
2736 activityName, PackageManager.GET_META_DATA).metaData;
2737 if (metaData != null) {
2738 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2739 if (iconResId != 0) {
2740 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002741 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002742 }
2743 }
2744 } catch (NameNotFoundException e) {
2745 // Do nothing
2746 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002747 return null;
2748 }
2749
2750 // if successful in getting icon, return it; otherwise, set button to use default drawable
2751 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2752 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2753 TextView button = (TextView) findViewById(buttonId);
2754 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2755
2756 // If we were unable to find the icon via the meta-data, use a generic one
2757 if (toolbarIcon == null) {
2758 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2759 return null;
2760 } else {
2761 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2762 return toolbarIcon.getConstantState();
2763 }
2764 }
2765
2766 // if successful in getting icon, return it; otherwise, set button to use default drawable
2767 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2768 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2769 ImageView button = (ImageView) findViewById(buttonId);
2770 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2771
Michael Jurka0423dcf2010-10-05 14:56:18 -07002772 // If we were unable to find the icon via the meta-data, use a generic one
2773 if (toolbarIcon == null) {
2774 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002775 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002776 } else {
2777 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002778 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002779 }
2780 }
2781
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002782 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2783 TextView button = (TextView) findViewById(buttonId);
2784 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2785 }
2786
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002787 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002788 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002789 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002790 }
2791
Michael Jurka0423dcf2010-10-05 14:56:18 -07002792 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2794 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002795
Winson Chung1cad91e2011-05-25 17:41:01 -07002796 final SearchManager searchManager =
2797 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2798 ComponentName activityName = searchManager.getGlobalSearchActivity();
2799 if (activityName != null) {
2800 // In landscape mode on the Phone UI, we only have enough space to show the magnifying
2801 // glass icon
2802 boolean iconLoaded = false;
2803 if (!LauncherApplication.isScreenLarge()) {
2804 // TODO-APPS_CUSTOMIZE: Remove when the QSB fixes itself?
2805 int orientation = getResources().getConfiguration().orientation;
2806 if (orientation == Configuration.ORIENTATION_LANDSCAPE) {
2807 searchButton.setImageResource(R.drawable.ic_generic_search);
2808 iconLoaded = true;
2809 sGlobalSearchIcon = null;
2810 }
2811 }
2812 if (!iconLoaded) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002813 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08002814 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002815 }
Winson Chung1cad91e2011-05-25 17:41:01 -07002816 searchButton.setVisibility(View.VISIBLE);
2817 searchDivider.setVisibility(View.VISIBLE);
2818 } else {
2819 searchButton.setVisibility(View.GONE);
2820 searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002821 }
2822 }
2823
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002824 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002825 updateButtonWithDrawable(R.id.search_button, d);
2826 }
2827
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002828 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002829 final View searchDivider = findViewById(R.id.search_divider);
2830 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002831
Winson Chung1cad91e2011-05-25 17:41:01 -07002832 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2833 ComponentName activityName = intent.resolveActivity(getPackageManager());
2834 if (activityName != null) {
2835 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
2836 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2837 searchDivider.setVisibility(View.VISIBLE);
2838 voiceButton.setVisibility(View.VISIBLE);
2839 } else {
2840 searchDivider.setVisibility(View.GONE);
2841 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002842 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002843 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002844
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002845 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002846 updateButtonWithDrawable(R.id.voice_button, d);
2847 }
2848
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002849 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002850 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002851 */
2852 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002853 final View marketButton = findViewById(R.id.market_button);
2854 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2855 // Find the app market activity by resolving an intent.
2856 // (If multiple app markets are installed, it will return the ResolverActivity.)
2857 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002858 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002859 mAppMarketIntent = intent;
2860 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
2861 R.id.market_button, activityName, R.drawable.app_market_generic);
2862 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002863
2864 // Remove the shop icon text in the Phone UI
2865 if (!LauncherApplication.isScreenLarge()) {
2866 ((TextView) marketButton).setText("");
2867 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002868 } else {
2869 // We should hide and disable the view so that we don't try and restore the visibility
2870 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2871 marketButton.setVisibility(View.GONE);
2872 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002873 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002874 }
2875
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002876 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002877 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002878 }
2879
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002880 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 * Displays the shortcut creation dialog and launches, if necessary, the
2882 * appropriate activity.
2883 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002884 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002885 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2886 DialogInterface.OnShowListener {
2887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002892
Winson Chung55b65502011-05-26 12:03:43 -07002893 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2894 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002896 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 AlertDialog dialog = builder.create();
2899 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002900 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002901 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002903 return dialog;
2904 }
2905
2906 public void onCancel(DialogInterface dialog) {
2907 mWaitingForResult = false;
2908 cleanup();
2909 }
2910
Romain Guycbb89e42009-06-08 15:52:54 -07002911 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002912 mWaitingForResult = false;
2913 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002914 }
2915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002917 try {
2918 dismissDialog(DIALOG_CREATE_SHORTCUT);
2919 } catch (Exception e) {
2920 // An exception is thrown if the dialog is not visible, which is fine
2921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
2923
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002924 /**
2925 * Handle the action clicked in the "Add to home" dialog.
2926 */
2927 public void onClick(DialogInterface dialog, int which) {
2928 Resources res = getResources();
2929 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002930
Winson Chung55b65502011-05-26 12:03:43 -07002931 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2932 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002933 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002934 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002935 break;
2936 }
Winson Chung55b65502011-05-26 12:03:43 -07002937 case AddAdapter.ITEM_APPLICATION: {
2938 if (mAppsCustomizeTabHost != null) {
2939 mAppsCustomizeTabHost.selectAppsTab();
2940 }
2941 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002942 break;
2943 }
Winson Chung55b65502011-05-26 12:03:43 -07002944 case AddAdapter.ITEM_APPWIDGET: {
2945 if (mAppsCustomizeTabHost != null) {
2946 mAppsCustomizeTabHost.selectWidgetsTab();
2947 }
2948 showAllApps(true);
2949 break;
2950 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002951 case AddAdapter.ITEM_WALLPAPER: {
2952 startWallpaper();
2953 break;
2954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 }
2956 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002957
2958 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002959 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 }
2962
2963 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002964 * Receives notifications when applications are added/removed.
2965 */
2966 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2967 @Override
2968 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002969 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002970 String reason = intent.getStringExtra("reason");
2971 if (!"homekey".equals(reason)) {
2972 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002973 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002974 animate = false;
2975 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002976 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002977 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002978 }
2979 }
2980
2981 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002982 * Receives notifications whenever the appwidgets are reset.
2983 */
2984 private class AppWidgetResetObserver extends ContentObserver {
2985 public AppWidgetResetObserver() {
2986 super(new Handler());
2987 }
2988
2989 @Override
2990 public void onChange(boolean selfChange) {
2991 onAppWidgetReset();
2992 }
2993 }
2994
2995 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002996 * If the activity is currently paused, signal that we need to re-run the loader
2997 * in onResume.
2998 *
2999 * This needs to be called from incoming places where resources might have been loaded
3000 * while we are paused. That is becaues the Configuration might be wrong
3001 * when we're not running, and if it comes back to what it was when we
3002 * were paused, we are not restarted.
3003 *
3004 * Implementation of the method from LauncherModel.Callbacks.
3005 *
3006 * @return true if we are currently paused. The caller might be able to
3007 * skip some work in that case since we will come back again.
3008 */
3009 public boolean setLoadOnResume() {
3010 if (mPaused) {
3011 Log.i(TAG, "setLoadOnResume");
3012 mOnResumeNeedsLoad = true;
3013 return true;
3014 } else {
3015 return false;
3016 }
3017 }
3018
3019 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003020 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003023 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003024 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003025 } else {
3026 return SCREEN_COUNT / 2;
3027 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003029
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003030
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 /**
3032 * Refreshes the shortcuts shown on the workspace.
3033 *
3034 * Implementation of the method from LauncherModel.Callbacks.
3035 */
3036 public void startBinding() {
3037 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003038
3039 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 int count = workspace.getChildCount();
3041 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003042 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003043 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3044 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003046
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 if (DEBUG_USER_INTERFACE) {
3048 android.widget.Button finishButton = new android.widget.Button(this);
3049 finishButton.setText("Finish");
3050 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3051
3052 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3053 public void onClick(View v) {
3054 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003058
3059 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3060 // mDesktopItems -> AppWidgetInfo -> hostView).
3061 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 }
3063
3064 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 * Bind the items start-end from the list.
3066 *
3067 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003068 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3070
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003071 setLoadOnResume();
3072
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 final Workspace workspace = mWorkspace;
3074
3075 for (int i=start; i<end; i++) {
3076 final ItemInfo item = shortcuts.get(i);
3077 mDesktopItems.add(item);
3078 switch (item.itemType) {
3079 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3080 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003081 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3083 false);
3084 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003085 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003087 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003088 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3090 false);
3091 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003092 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 }
3094
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 }
3097
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 /**
3099 * Implementation of the method from LauncherModel.Callbacks.
3100 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003101 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003102 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003103 sFolders.clear();
3104 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106
3107 /**
3108 * Add the views for a widget to the workspace.
3109 *
3110 * Implementation of the method from LauncherModel.Callbacks.
3111 */
3112 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003113 setLoadOnResume();
3114
Daniel Sandler843e8602010-06-07 14:59:01 -04003115 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3116 if (DEBUG_WIDGETS) {
3117 Log.d(TAG, "bindAppWidget: " + item);
3118 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003119 final Workspace workspace = mWorkspace;
3120
3121 final int appWidgetId = item.appWidgetId;
3122 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003123 if (DEBUG_WIDGETS) {
3124 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3125 }
3126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3128
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3130 item.hostView.setTag(item);
3131
3132 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3133 item.cellY, item.spanX, item.spanY, false);
3134
Adam Cohended9f8d2010-11-03 13:25:16 -07003135 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3136
Joe Onorato9c1289c2009-08-17 11:03:03 -04003137 workspace.requestLayout();
3138
3139 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003140
3141 if (DEBUG_WIDGETS) {
3142 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3143 + (SystemClock.uptimeMillis()-start) + "ms");
3144 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 }
3146
3147 /**
3148 * Callback saying that there aren't any more items to bind.
3149 *
3150 * Implementation of the method from LauncherModel.Callbacks.
3151 */
3152 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003153 setLoadOnResume();
3154
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 if (mSavedState != null) {
3156 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003157 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 }
3159
Adam Cohendf2cc412011-04-27 16:56:57 -07003160 final long[] folders = mSavedState.getLongArray(RUNTIME_STATE_FOLDERS);
3161 if (folders != null) {
3162 for (long folderId : folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003163 final FolderInfo info = sFolders.get(folderId);
Adam Cohena9cf38f2011-05-02 15:36:58 -07003164 final FolderIcon folderIcon = (FolderIcon)
3165 mWorkspace.getViewForTag(info);
3166 if (folderIcon != null) {
3167 openFolder(folderIcon);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 }
3169 }
3170 final Folder openFolder = mWorkspace.getOpenFolder();
3171 if (openFolder != null) {
3172 openFolder.requestFocus();
3173 }
3174 }
3175
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 mSavedState = null;
3177 }
3178
3179 if (mSavedInstanceState != null) {
3180 super.onRestoreInstanceState(mSavedInstanceState);
3181 mSavedInstanceState = null;
3182 }
3183
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003185
3186 // If we received the result of any pending adds while the loader was running (e.g. the
3187 // widget configuration forced an orientation change), process them now.
3188 for (int i = 0; i < sPendingAddList.size(); i++) {
3189 completeAdd(sPendingAddList.get(i));
3190 }
3191 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 }
3193
3194 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003195 * Updates the icons on the launcher that are affected by changes to the package list
3196 * on the device.
3197 */
3198 private void updateIconsAffectedByPackageManagerChanges() {
3199 updateAppMarketIcon();
3200 updateGlobalSearchIcon();
3201 updateVoiceSearchIcon();
3202 }
3203
3204 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205 * Add the icons for all apps.
3206 *
3207 * Implementation of the method from LauncherModel.Callbacks.
3208 */
3209 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003210 if (mAppsCustomizeContent != null) {
3211 mAppsCustomizeContent.setApps(apps);
3212 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003213 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 }
3215
3216 /**
3217 * A package was installed.
3218 *
3219 * Implementation of the method from LauncherModel.Callbacks.
3220 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003221 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003222 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003224
Winson Chung785d2eb2011-04-14 16:08:02 -07003225 if (mAppsCustomizeContent != null) {
3226 mAppsCustomizeContent.addApps(apps);
3227 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003228 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 }
3230
3231 /**
3232 * A package was updated.
3233 *
3234 * Implementation of the method from LauncherModel.Callbacks.
3235 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003236 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003237 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003239 if (mWorkspace != null) {
3240 mWorkspace.updateShortcuts(apps);
3241 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003242
Winson Chung785d2eb2011-04-14 16:08:02 -07003243 if (mAppsCustomizeContent != null) {
3244 mAppsCustomizeContent.updateApps(apps);
3245 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003246 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 }
3248
3249 /**
3250 * A package was uninstalled.
3251 *
3252 * Implementation of the method from LauncherModel.Callbacks.
3253 */
Joe Onorato36115782010-06-17 13:28:48 -04003254 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003255 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003256 if (permanent) {
3257 mWorkspace.removeItems(apps);
3258 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003259
Winson Chung785d2eb2011-04-14 16:08:02 -07003260 if (mAppsCustomizeContent != null) {
3261 mAppsCustomizeContent.removeApps(apps);
3262 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003263 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003264 }
Joe Onoratobe386092009-11-17 17:32:16 -08003265
3266 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003267 * A number of packages were updated.
3268 */
3269 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003270
Winson Chung785d2eb2011-04-14 16:08:02 -07003271 if (mAppsCustomizeContent != null) {
3272 mAppsCustomizeContent.onPackagesUpdated();
3273 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003274 }
3275
Winson Chung400438b2011-01-16 17:53:48 -08003276 private int mapConfigurationOriActivityInfoOri(int configOri) {
3277 final Display d = getWindowManager().getDefaultDisplay();
3278 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3279 switch (d.getRotation()) {
3280 case Surface.ROTATION_0:
3281 case Surface.ROTATION_180:
3282 // We are currently in the same basic orientation as the natural orientation
3283 naturalOri = configOri;
3284 break;
3285 case Surface.ROTATION_90:
3286 case Surface.ROTATION_270:
3287 // We are currently in the other basic orientation to the natural orientation
3288 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3289 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3290 break;
3291 }
3292
3293 int[] oriMap = {
3294 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3295 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3296 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3297 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3298 };
3299 // Since the map starts at portrait, we need to offset if this device's natural orientation
3300 // is landscape.
3301 int indexOffset = 0;
3302 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3303 indexOffset = 1;
3304 }
3305 return oriMap[(d.getRotation() + indexOffset) % 4];
3306 }
3307 public void lockScreenOrientation() {
3308 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3309 .getConfiguration().orientation));
3310 }
3311 public void unlockScreenOrientation() {
3312 mHandler.postDelayed(new Runnable() {
3313 public void run() {
3314 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3315 }
3316 }, mRestoreScreenOrientationDelay);
3317 }
3318
Winson Chung80baf5a2010-08-09 16:03:15 -07003319 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003320 * Prints out out state for debugging.
3321 */
3322 public void dumpState() {
3323 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003324 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003325 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3326 Log.d(TAG, "mRestoring=" + mRestoring);
3327 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3328 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3329 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003330 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003331 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003332
Winson Chung785d2eb2011-04-14 16:08:02 -07003333 if (mAppsCustomizeContent != null) {
3334 mAppsCustomizeContent.dumpState();
3335 }
Joe Onoratobe386092009-11-17 17:32:16 -08003336 Log.d(TAG, "END launcher2 dump state");
3337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003338}
Michael Jurka2763be32011-02-24 11:19:57 -08003339
Michael Jurkaabded662011-03-04 12:06:57 -08003340interface LauncherTransitionable {
3341 void onLauncherTransitionStart(Animator animation);
3342 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003343}