blob: 5779c6ad183acf536944d2adc9fa514e6d1f40a8 [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
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.CustomizePagedView.CustomizationType;
23import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080031import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080048import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080052import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080087import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080091import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080092import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070096import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070098import android.widget.ImageView;
99import android.widget.LinearLayout;
100import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700101import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800102import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700103import android.widget.TextView;
104import android.widget.Toast;
Adam Cohen860f4c52011-01-27 20:16:13 -0800105import android.widget.TabHost.OnTabChangeListener;
106import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700107
Michael Jurka742574b2011-02-02 23:51:01 -0800108import java.io.DataInputStream;
109import java.io.DataOutputStream;
110import java.io.FileNotFoundException;
111import java.io.IOException;
112import java.util.ArrayList;
113import java.util.HashMap;
114import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800115
Adam Cohended9f8d2010-11-03 13:25:16 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Michael Jurka946ad472010-07-09 18:05:18 -0700120public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
122 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Joe Onorato9c1289c2009-08-17 11:03:03 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400128 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800131 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700134 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
135 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
137 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700138 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 private static final int REQUEST_CREATE_SHORTCUT = 1;
141 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 private static final int REQUEST_PICK_APPLICATION = 6;
144 private static final int REQUEST_PICK_SHORTCUT = 7;
145 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700146 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700147 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
149 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
150
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800151 static final int SCREEN_COUNT = 5;
152 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Joe Onorato7c312c12009-08-13 21:36:53 -0700154 static final int DIALOG_CREATE_SHORTCUT = 1;
155 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
159 // Type: int
160 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700161 // Type: int
162 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: long
164 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: boolean
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
173 // Type: long
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
175
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 // tags for the customization tabs
177 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700178 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700179 private static final String SHORTCUTS_TAG = "shortcuts";
180 private static final String WALLPAPERS_TAG = "wallpapers";
181
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
183
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700184 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800185 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
186 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700189
Joe Onorato9c1289c2009-08-17 11:03:03 -0400190 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800193 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800195 private final BroadcastReceiver mCloseSystemDialogsReceiver
196 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800197 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private LayoutInflater mInflater;
200
Joe Onorato00acb122009-08-04 16:04:30 -0400201 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700204 private AppWidgetManager mAppWidgetManager;
205 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700206
Michael Jurka0280c3b2010-09-17 15:00:07 -0700207 private int mAddScreen = -1;
208 private int mAddIntersectCellX = -1;
209 private int mAddIntersectCellY = -1;
210 private int[] mAddDropPosition;
211 private int[] mTmpAddItemCellCoordinates = new int[2];
212
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213 private FolderInfo mFolderInfo;
214
Joe Onorato7c312c12009-08-13 21:36:53 -0700215 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700217 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700218 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700220
Michael Jurka838a4ca2011-02-07 13:33:06 -0800221 private View mButtonCluster;
222 private View mAllAppsButton;
223 private View mDivider;
224 private View mConfigureButton;
225
Winson Chunga12a2502010-12-20 14:41:35 -0800226 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700227 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229 private Bundle mSavedState;
230
231 private SpannableStringBuilder mDefaultKeySsb = null;
232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private boolean mWorkspaceLoading = true;
234
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800235 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236 private boolean mRestoring;
237 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700238 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private Bundle mSavedInstanceState;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800243 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800244 private boolean mUserPresent = true;
245 private boolean mVisible = false;
246 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400247
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700248 private static LocaleConfiguration sLocaleConfiguration = null;
249
Romain Guy84f296c2009-11-04 15:00:44 -0800250 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700251
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700252 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700253
Romain Guy1fbc1c82009-11-09 20:43:08 -0800254 private ImageView mPreviousView;
255 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500256
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400257 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400258 private String[] mHotseatConfig = null;
259 private Intent[] mHotseats = null;
260 private Drawable[] mHotseatIcons = null;
261 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400262
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700263 private Intent mAppMarketIntent = null;
264
Adam Cohended9f8d2010-11-03 13:25:16 -0700265 // Related to the auto-advancing of widgets
266 private final int ADVANCE_MSG = 1;
267 private final int mAdvanceInterval = 20000;
268 private final int mAdvanceStagger = 250;
269 private long mAutoAdvanceSentTime;
270 private long mAutoAdvanceTimeLeft = -1;
271 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
272 new HashMap<View, AppWidgetProviderInfo>();
273
Winson Chung400438b2011-01-16 17:53:48 -0800274 // Determines how long to wait after a rotation before restoring the screen orientation to
275 // match the sensor state.
276 private final int mRestoreScreenOrientationDelay = 500;
277
Michael Jurka4ef207b2010-11-29 17:05:45 -0800278 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800279 private static Drawable.ConstantState sGlobalSearchIcon;
280 private static Drawable.ConstantState sVoiceSearchIcon;
281 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800282
Winson Chunga12a2502010-12-20 14:41:35 -0800283 private CustomizationType getCustomizeFilterForTabTag(String tag) {
284 if (tag.equals(WIDGETS_TAG)) {
285 return CustomizationType.WidgetCustomization;
286 } else if (tag.equals(APPLICATIONS_TAG)) {
287 return CustomizationType.ApplicationCustomization;
288 } else if (tag.equals(WALLPAPERS_TAG)) {
289 return CustomizePagedView.CustomizationType.WallpaperCustomization;
290 } else if (tag.equals(SHORTCUTS_TAG)) {
291 return CustomizePagedView.CustomizationType.ShortcutCustomization;
292 }
293 return CustomizationType.WidgetCustomization;
294 }
295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 @Override
297 protected void onCreate(Bundle savedInstanceState) {
298 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700299
Winson Chungaafa03c2010-06-11 17:34:16 -0700300 if (LauncherApplication.isInPlaceRotationEnabled()) {
301 // hide the status bar (temporary until we get the status bar design figured out)
302 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
303 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
304 }
305
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 LauncherApplication app = ((LauncherApplication)getApplication());
307 mModel = app.setLauncher(this);
308 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400309 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700313 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
314 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200317 android.os.Debug.startMethodTracing(
318 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400321 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800323 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800324 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700325 if (mHomeCustomizationDrawer != null) {
326 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700327
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700329 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
330 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 TabContentFactory contentFactory = new TabContentFactory() {
332 public View createTabContent(String tag) {
333 return mCustomizePagedView;
334 }
335 };
336
Winson Chung49767ae2010-11-29 14:48:30 -0800337
338 TextView tabView;
339 TabWidget tabWidget = (TabWidget)
340 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
341
342 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
343 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700344 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800345 .setIndicator(tabView).setContent(contentFactory));
346 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
347 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700348 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800349 .setIndicator(tabView).setContent(contentFactory));
350 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
351 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700352 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800353 .setIndicator(tabView).setContent(contentFactory));
354 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
355 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700356 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800357 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700358 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
359 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800360 final CustomizePagedView.CustomizationType newType =
361 getCustomizeFilterForTabTag(tabId);
362 if (newType != mCustomizePagedView.getCustomizationFilter()) {
363 // animate the changing of the tab content by fading pages in and out
364 final Resources res = getResources();
365 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
366 final float alpha = mCustomizePagedView.getAlpha();
367 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
368 "alpha", alpha, 0.0f);
369 alphaAnim.setDuration(duration);
370 alphaAnim.addListener(new AnimatorListenerAdapter() {
371 @Override
372 public void onAnimationEnd(Animator animation) {
Winson Chunga12a2502010-12-20 14:41:35 -0800373 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700374
Winson Chunga12a2502010-12-20 14:41:35 -0800375 final float alpha = mCustomizePagedView.getAlpha();
376 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
377 mCustomizePagedView, "alpha", alpha, 1.0f);
378 alphaAnim.setDuration(duration);
379 alphaAnim.start();
380 }
381 });
382 alphaAnim.start();
383 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700384 }
385 });
Michael Jurka946ad472010-07-09 18:05:18 -0700386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 setupViews();
388
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800389 registerContentObservers();
390
Joe Onorato7c312c12009-08-13 21:36:53 -0700391 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 mSavedState = savedInstanceState;
394 restoreState(mSavedState);
395
Winson Chunga12a2502010-12-20 14:41:35 -0800396 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800397 if (mCustomizePagedView != null) {
398 mCustomizePagedView.update();
399 }
Winson Chunga12a2502010-12-20 14:41:35 -0800400
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 if (PROFILE_STARTUP) {
402 android.os.Debug.stopMethodTracing();
403 }
404
405 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400406 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 }
408
409 // For handling default keys
410 mDefaultKeySsb = new SpannableStringBuilder();
411 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800412
413 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
414 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800415
416 // If we have a saved version of these external icons, we load them up immediately
417 if (LauncherApplication.isScreenXLarge()) {
418 if (sGlobalSearchIcon != null) {
419 updateGlobalSearchIcon(sGlobalSearchIcon);
420 }
421 if (sVoiceSearchIcon != null) {
422 updateVoiceSearchIcon(sVoiceSearchIcon);
423 }
424 if (sAppMarketIcon != null) {
425 updateAppMarketIcon(sAppMarketIcon);
426 }
427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800430 @Override
431 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
432 super.dispatchPopulateAccessibilityEvent(event);
433
434 // we want to take over text population so it is context dependent
435 event.getText().clear();
436 if (mState == State.ALL_APPS) {
437 event.getText().add(getString(R.string.all_apps_button_label));
438 } else if (mState == State.WORKSPACE) {
439 event.getText().add(getString(R.string.all_apps_home_button_label));
440 }
441
442 return true;
443 }
444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446 if (sLocaleConfiguration == null) {
447 new AsyncTask<Void, Void, LocaleConfiguration>() {
448 @Override
449 protected LocaleConfiguration doInBackground(Void... unused) {
450 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
451 readConfiguration(Launcher.this, localeConfiguration);
452 return localeConfiguration;
453 }
454
455 @Override
456 protected void onPostExecute(LocaleConfiguration result) {
457 sLocaleConfiguration = result;
458 checkForLocaleChange(); // recursive, but now with a locale configuration
459 }
460 }.execute();
461 return;
462 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700463
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 final Configuration configuration = getResources().getConfiguration();
465
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700466 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 final String locale = configuration.locale.toString();
468
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700469 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 final int mcc = configuration.mcc;
471
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 final int mnc = configuration.mnc;
474
Romain Guy84f296c2009-11-04 15:00:44 -0800475 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Romain Guy84f296c2009-11-04 15:00:44 -0800477 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 sLocaleConfiguration.locale = locale;
479 sLocaleConfiguration.mcc = mcc;
480 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700481
Joe Onorato0589f0f2010-02-08 13:44:00 -0800482 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400483 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484
485 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
486 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700487 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700488 public void run() {
489 writeConfiguration(Launcher.this, localeConfiguration);
490 }
491 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700492 }
493 }
494
495 private static class LocaleConfiguration {
496 public String locale;
497 public int mcc = -1;
498 public int mnc = -1;
499 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700500
Romain Guy98d01652009-06-30 16:21:04 -0700501 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
502 DataInputStream in = null;
503 try {
504 in = new DataInputStream(context.openFileInput(PREFERENCES));
505 configuration.locale = in.readUTF();
506 configuration.mcc = in.readInt();
507 configuration.mnc = in.readInt();
508 } catch (FileNotFoundException e) {
509 // Ignore
510 } catch (IOException e) {
511 // Ignore
512 } finally {
513 if (in != null) {
514 try {
515 in.close();
516 } catch (IOException e) {
517 // Ignore
518 }
519 }
520 }
521 }
522
523 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
524 DataOutputStream out = null;
525 try {
526 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
527 out.writeUTF(configuration.locale);
528 out.writeInt(configuration.mcc);
529 out.writeInt(configuration.mnc);
530 out.flush();
531 } catch (FileNotFoundException e) {
532 // Ignore
533 } catch (IOException e) {
534 //noinspection ResultOfMethodCallIgnored
535 context.getFileStreamPath(PREFERENCES).delete();
536 } finally {
537 if (out != null) {
538 try {
539 out.close();
540 } catch (IOException e) {
541 // Ignore
542 }
543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545 }
546
547 static int getScreen() {
548 synchronized (sLock) {
549 return sScreen;
550 }
551 }
552
553 static void setScreen(int screen) {
554 synchronized (sLock) {
555 sScreen = screen;
556 }
557 }
558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 // Note: This doesn't do all the client-id magic that BrowserProvider does
560 // in Browser. (http://b/2425179)
561 private Uri getDefaultBrowserUri() {
562 String url = getString(R.string.default_browser_url);
563 if (url.indexOf("{CID}") != -1) {
564 url = url.replace("{CID}", "android-google");
565 }
566 return Uri.parse(url);
567 }
568
569 // Load the Intent templates from arrays.xml to populate the hotseats. For
570 // each Intent, if it resolves to a single app, use that as the launch
571 // intent & use that app's label as the contentDescription. Otherwise,
572 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 if (mHotseatConfig == null) {
575 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
576 if (mHotseatConfig.length > 0) {
577 mHotseats = new Intent[mHotseatConfig.length];
578 mHotseatLabels = new CharSequence[mHotseatConfig.length];
579 mHotseatIcons = new Drawable[mHotseatConfig.length];
580 } else {
581 mHotseats = null;
582 mHotseatIcons = null;
583 mHotseatLabels = null;
584 }
585
586 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
587 for (int i=0; i<mHotseatConfig.length; i++) {
588 // load icon for this slot; currently unrelated to the actual activity
589 try {
590 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
591 } catch (ArrayIndexOutOfBoundsException ex) {
592 Log.w(TAG, "Missing hotseat_icons array item #" + i);
593 mHotseatIcons[i] = null;
594 }
595 }
596 hotseatIconDrawables.recycle();
597 }
598
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400599 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 for (int i=0; i<mHotseatConfig.length; i++) {
601 Intent intent = null;
602 if (mHotseatConfig[i].equals("*BROWSER*")) {
603 // magic value meaning "launch user's default web browser"
604 // replace it with a generic web request so we can see if there is indeed a default
605 String defaultUri = getString(R.string.default_browser_url);
606 intent = new Intent(
607 Intent.ACTION_VIEW,
608 ((defaultUri != null)
609 ? Uri.parse(defaultUri)
610 : getDefaultBrowserUri())
611 ).addCategory(Intent.CATEGORY_BROWSABLE);
612 // note: if the user launches this without a default set, she
613 // will always be taken to the default URL above; this is
614 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 // URL is unavoidably sent to the chosen app.
617 } else {
618 try {
619 intent = Intent.parseUri(mHotseatConfig[i], 0);
620 } catch (java.net.URISyntaxException ex) {
621 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
622 // bogus; leave intent=null
623 }
624 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700625
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 if (intent == null) {
627 mHotseats[i] = null;
628 mHotseatLabels[i] = getText(R.string.activity_not_found);
629 continue;
630 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631
632 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700633 Log.d(TAG, "loadHotseats: hotseat " + i
634 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400635 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 + "]");
637 }
638
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
640 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 Log.d(TAG, "Best match for intent: " + bestMatch);
643 Log.d(TAG, "All matches: ");
644 for (ResolveInfo ri : allMatches) {
645 Log.d(TAG, " --> " + ri);
646 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400647 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400648 // did this resolve to a single app, or the resolver?
649 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700650 // can't find any activity to handle this. let's leave the
651 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400652 // to launch.
653 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400654
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400655 // set accessibility text to "Not installed"
656 mHotseatLabels[i] = getText(R.string.activity_not_found);
657 } else {
658 boolean found = false;
659 for (ResolveInfo ri : allMatches) {
660 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
661 && bestMatch.activityInfo.applicationInfo.packageName
662 .equals(ri.activityInfo.applicationInfo.packageName)) {
663 found = true;
664 break;
665 }
666 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700667
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400668 if (!found) {
669 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
670 // the bestMatch is probably the ResolveActivity, meaning the
671 // user has not yet selected a default
672 // so: we'll keep the original intent for now
673 mHotseats[i] = intent;
674
675 // set the accessibility text to "Select shortcut"
676 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
677 } else {
678 // we have an app!
679 // now reconstruct the intent to launch it through the front
680 // door
681 ComponentName com = new ComponentName(
682 bestMatch.activityInfo.applicationInfo.packageName,
683 bestMatch.activityInfo.name);
684 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
685
686 // load the app label for accessibility
687 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
688 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400689 }
690
691 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700692 Log.d(TAG, "loadHotseats: hotseat " + i
693 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400694 + ((mHotseats[i] == null)
695 ? "null"
696 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400697 + "] label=[" + mHotseatLabels[i]
698 + "]"
699 );
700 }
701 }
702 }
703
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 @Override
705 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700706 mWaitingForResult = false;
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 // The pattern used here is that a user PICKs a specific application,
709 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
712 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700713
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 switch (requestCode) {
716 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700717 completeAddApplication(
718 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 break;
720 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800721 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
723 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700724 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 break;
726 case REQUEST_PICK_LIVE_FOLDER:
727 addLiveFolder(data);
728 break;
729 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700730 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700733 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700736 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700737 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700739 case REQUEST_PICK_WALLPAPER:
740 // We just wanted the activity result here so we can clear mWaitingForResult
741 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
Romain Guy18042c82009-11-06 11:44:55 -0800743 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
744 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
745 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700746 // Clean up the appWidgetId if we canceled
747 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
748 if (appWidgetId != -1) {
749 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 }
751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 @Override
755 protected void onResume() {
756 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800757 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700758 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 mWorkspaceLoading = true;
760 mModel.startLoader(this, true);
761 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700762 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700764 // When we resume Launcher, a different Activity might be responsible for the app
765 // market intent, so refresh the icon
766 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 }
768
769 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 protected void onPause() {
771 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700772 // Some launcher layouts don't have a previous and next view
773 if (mPreviousView != null) {
774 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700775 }
776 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700777 dismissPreview(mNextView);
778 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700779 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800780 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700781 }
Romain Guycbb89e42009-06-08 15:52:54 -0700782
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700783 @Override
784 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400785 // Flag the loader to stop early before switching
786 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800787 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800788 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700789 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700790
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800791 // We can't hide the IME if it was forced open. So don't bother
792 /*
793 @Override
794 public void onWindowFocusChanged(boolean hasFocus) {
795 super.onWindowFocusChanged(hasFocus);
796
797 if (hasFocus) {
798 final InputMethodManager inputManager = (InputMethodManager)
799 getSystemService(Context.INPUT_METHOD_SERVICE);
800 WindowManager.LayoutParams lp = getWindow().getAttributes();
801 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
802 android.os.Handler()) {
803 protected void onReceiveResult(int resultCode, Bundle resultData) {
804 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
805 }
806 });
807 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
808 }
809 }
810 */
811
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 private boolean acceptFilter() {
813 final InputMethodManager inputManager = (InputMethodManager)
814 getSystemService(Context.INPUT_METHOD_SERVICE);
815 return !inputManager.isFullscreenMode();
816 }
817
818 @Override
819 public boolean onKeyDown(int keyCode, KeyEvent event) {
820 boolean handled = super.onKeyDown(keyCode, event);
821 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
822 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
823 keyCode, event);
824 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700825 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700826 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700827 // showSearchDialog()
828 // If there are multiple keystrokes before the search dialog takes focus,
829 // onSearchRequested() will be called for every keystroke,
830 // but it is idempotent, so it's fine.
831 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833 }
834
Joe Onorato8a9625e2010-01-28 15:55:35 -0800835 // Eat the long press event so the keyboard doesn't come up.
836 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
837 return true;
838 }
839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 return handled;
841 }
842
Karl Rosaen138a0412009-04-23 19:00:21 -0700843 private String getTypedText() {
844 return mDefaultKeySsb.toString();
845 }
846
847 private void clearTypedText() {
848 mDefaultKeySsb.clear();
849 mDefaultKeySsb.clearSpans();
850 Selection.setSelection(mDefaultKeySsb, 0);
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700854 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
855 * State
856 */
857 private static State intToState(int stateOrdinal) {
858 State state = State.WORKSPACE;
859 final State[] stateValues = State.values();
860 for (int i = 0; i < stateValues.length; i++) {
861 if (stateValues[i].ordinal() == stateOrdinal) {
862 state = stateValues[i];
863 break;
864 }
865 }
866 return state;
867 }
868
869 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 * Restores the previous state, if it exists.
871 *
872 * @param savedState The previous state.
873 */
874 private void restoreState(Bundle savedState) {
875 if (savedState == null) {
876 return;
877 }
878
Michael Jurka883f55b2010-10-21 15:47:14 -0700879 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
880
881 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800882 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700883 } else if (state == State.CUSTOMIZE) {
884 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
888 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700889 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 }
891
892 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895 mAddScreen = addScreen;
896 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
897 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 mRestoring = true;
899 }
900
901 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
902 if (renameFolder) {
903 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700904 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 mRestoring = true;
906 }
Winson Chunga12a2502010-12-20 14:41:35 -0800907
908 // Restore the current AllApps drawer tab
909 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
910 String curTab = savedState.getString("allapps_currentTab");
911 if (curTab != null) {
912 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
913 tabhost.setCurrentTabByTag(curTab);
914 }
915 int curPage = savedState.getInt("allapps_currentPage", -1);
916 if (curPage > -1) {
917 mAllAppsPagedView.setRestorePage(curPage);
918 }
919 }
920
921 // Restore the current customization drawer tab
922 if (mHomeCustomizationDrawer != null) {
923 String curTab = savedState.getString("customize_currentTab");
924 if (curTab != null) {
925 // We set this directly so that there is no delay before the tab is set
926 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
927 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
928 }
929
930 // Note: currently we do not restore the page for the customization tray because unlike
931 // AllApps, the page content can change drastically
932 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 }
934
935 /**
936 * Finds all the views we need and configure them properly.
937 */
938 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700939 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400940
Romain Guyb1b69f52009-08-10 15:10:15 -0700941 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400942 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943
Joe Onorato7c312c12009-08-13 21:36:53 -0700944 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700945 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700946 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800947 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700948 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700949 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700950
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700951 if (LauncherApplication.isScreenXLarge()) {
952 // They need to be INVISIBLE initially so that they will be measured in the layout.
953 // Otherwise the animations are messed up when we show them for the first time.
954 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
955 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
956 }
957
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500961 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700962
Joe Onorato7c312c12009-08-13 21:36:53 -0700963 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
964 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700966 View handleView = findViewById(R.id.all_apps_button);
967 if (handleView != null && handleView instanceof HandleView) {
968 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700969 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700970 mHandleView.setLauncher(this);
971 mHandleView.setOnClickListener(this);
972 mHandleView.setOnLongClickListener(this);
973 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800974
Winson Chung80baf5a2010-08-09 16:03:15 -0700975 if (mCustomizePagedView != null) {
976 mCustomizePagedView.setLauncher(this);
977 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700978 } else {
979 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
980 hotseatLeft.setContentDescription(mHotseatLabels[0]);
981 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
982 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
983 hotseatRight.setContentDescription(mHotseatLabels[1]);
984 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400985
Michael Jurkaaf442092010-06-10 17:01:57 -0700986 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
987 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800988
Michael Jurkaaf442092010-06-10 17:01:57 -0700989 Drawable previous = mPreviousView.getDrawable();
990 Drawable next = mNextView.getDrawable();
991 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Michael Jurkaaf442092010-06-10 17:01:57 -0700993 mPreviousView.setHapticFeedbackEnabled(false);
994 mPreviousView.setOnLongClickListener(this);
995 mNextView.setHapticFeedbackEnabled(false);
996 mNextView.setOnLongClickListener(this);
997 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800998
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001000 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -08001002 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003
1004 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001005 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -08001006
1007 final View allAppsButton = findViewById(R.id.all_apps_button);
1008 final View divider = findViewById(R.id.divider);
1009 final View configureButton = findViewById(R.id.configure_button);
1010
Michael Jurka54f7ac32010-08-02 13:56:46 -07001011 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001012 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001013 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001014 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001015 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001016 dragController.addDragListener(deleteZone);
1017
Adam Cohencdc30d52010-12-01 15:09:47 -08001018 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1019 if (allAppsDeleteZone != null) {
1020 allAppsDeleteZone.setLauncher(this);
1021 allAppsDeleteZone.setDragController(dragController);
1022 allAppsDeleteZone.setDragAndDropEnabled(false);
1023 dragController.addDragListener(allAppsDeleteZone);
1024 dragController.addDropTarget(allAppsDeleteZone);
1025 }
1026
1027 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1028 findViewById(R.id.all_apps_info_target);
1029 if (allAppsInfoTarget != null) {
1030 allAppsInfoTarget.setLauncher(this);
1031 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001032 allAppsInfoTarget.setDragAndDropEnabled(false);
1033 View marketButton = findViewById(R.id.market_button);
1034 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001035 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001036 }
1037 }
1038
Joe Onorato00acb122009-08-04 16:04:30 -04001039 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001040 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001041 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001042
Joe Onorato00acb122009-08-04 16:04:30 -04001043 // The order here is bottom to top.
1044 dragController.addDropTarget(workspace);
1045 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001046 if (allAppsInfoTarget != null) {
1047 dragController.addDropTarget(allAppsInfoTarget);
1048 }
1049 if (allAppsDeleteZone != null) {
1050 dragController.addDropTarget(allAppsDeleteZone);
1051 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001052 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1053
1054 mAllAppsButton = findViewById(R.id.all_apps_button);
1055 mDivider = findViewById(R.id.divider);
1056 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001057
1058 // We had previously set these click handlers in XML, but the first time we launched
1059 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1060 // found the right handler. Setting the handlers directly here eliminates that cost.
1061 if (mConfigureButton != null) {
1062 mConfigureButton.setOnClickListener(new OnClickListener() {
1063 public void onClick(View v) {
1064 onClickConfigureButton(v);
1065 }
1066 });
1067 }
1068 if (mDivider != null) {
1069 mDivider.setOnClickListener(new OnClickListener() {
1070 public void onClick(View v) {
1071 onClickAllAppsButton(v);
1072 }
1073 });
1074 }
1075 if (mAllAppsButton != null) {
1076 mAllAppsButton.setOnClickListener(new OnClickListener() {
1077 public void onClick(View v) {
1078 onClickAllAppsButton(v);
1079 }
1080 });
1081 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 }
1083
Romain Guy8a73c512009-11-09 19:19:59 -08001084 @SuppressWarnings({"UnusedDeclaration"})
1085 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001086 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001087 mWorkspace.scrollLeft();
1088 }
Romain Guy8a73c512009-11-09 19:19:59 -08001089 }
1090
1091 @SuppressWarnings({"UnusedDeclaration"})
1092 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001093 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001094 mWorkspace.scrollRight();
1095 }
Romain Guy8a73c512009-11-09 19:19:59 -08001096 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001097
1098 @SuppressWarnings({"UnusedDeclaration"})
1099 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001100 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001101
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001102 int index = -1;
1103 if (v.getId() == R.id.hotseat_left) {
1104 index = 0;
1105 } else if (v.getId() == R.id.hotseat_right) {
1106 index = 1;
1107 }
1108
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001109 // reload these every tap; you never know when they might change
1110 loadHotseats();
1111 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001112 startActivitySafely(
1113 mHotseats[index],
1114 "hotseat"
1115 );
1116 }
1117 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 /**
1120 * Creates a view representing a shortcut.
1121 *
1122 * @param info The data structure describing the shortcut.
1123 *
1124 * @return A View inflated from R.layout.application.
1125 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001128 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130
1131 /**
1132 * Creates a view representing a shortcut inflated from the specified resource.
1133 *
1134 * @param layoutResId The id of the XML layout used to create the shortcut.
1135 * @param parent The group the shortcut belongs to.
1136 * @param info The data structure describing the shortcut.
1137 *
1138 * @return A View inflated from layoutResId.
1139 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001141 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1142 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 return favorite;
1145 }
1146
1147 /**
1148 * Add an application shortcut to the workspace.
1149 *
1150 * @param data The intent describing the application.
1151 * @param cellInfo The position on screen where to create the shortcut.
1152 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 void completeAddApplication(Context context, Intent data, int screen,
1154 int intersectCellX, int intersectCellY) {
1155 final int[] cellXY = mTmpAddItemCellCoordinates;
1156 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1157
1158 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1159 showOutOfSpaceMessage();
1160 return;
1161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162
Joe Onorato0589f0f2010-02-08 13:44:00 -08001163 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1164 data, context);
1165
Romain Guy73b979d2009-06-09 12:57:21 -07001166 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001167 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001169 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001170 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1171 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001172 } else {
1173 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 /**
1178 * Add a shortcut to the workspace.
1179 *
1180 * @param data The intent describing the shortcut.
1181 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 private void completeAddShortcut(Intent data, int screen,
1184 int intersectCellX, int intersectCellY) {
1185 final int[] cellXY = mTmpAddItemCellCoordinates;
1186 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001188 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001189 boolean foundCellSpan = false;
1190 if (touchXY != null) {
1191 // when dragging and dropping, just find the closest free spot
1192 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1193 int[] result = screenLayout.findNearestVacantArea(
1194 touchXY[0], touchXY[1], 1, 1, cellXY);
1195 foundCellSpan = (result != null);
1196 } else {
1197 foundCellSpan = layout.findCellForSpanThatIntersects(
1198 cellXY, 1, 1, intersectCellX, intersectCellY);
1199 }
1200
1201 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001202 showOutOfSpaceMessage();
1203 return;
1204 }
1205
1206 final ShortcutInfo info = mModel.addShortcut(
1207 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208
1209 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
1214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001216 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001218 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001219 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001221 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001223
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001224 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001225 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1226 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001229 // We have saved the position to which the widget was dragged-- this really only matters
1230 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001231 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001232
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001233 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 boolean foundCellSpan = false;
1235 if (touchXY != null) {
1236 // when dragging and dropping, just find the closest free spot
1237 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1238 int[] result = screenLayout.findNearestVacantArea(
1239 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001240 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001241 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001242 // if we long pressed on an empty cell to bring up a menu,
1243 // make sure we intersect the empty cell
1244 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1245 // findCellForSpanThatIntersects will just ignore them
1246 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1247 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001248 }
1249
Michael Jurka0280c3b2010-09-17 15:00:07 -07001250 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001251 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001252 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001253 return;
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001256 // Build Launcher-specific widget info and save to database
1257 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001258 launcherInfo.spanX = spanXY[0];
1259 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 LauncherModel.addItemToDatabase(this, launcherInfo,
1262 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264
1265 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001270
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001271 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001273
Michael Jurka0280c3b2010-09-17 15:00:07 -07001274 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001275 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001276
1277 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
1279 }
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1282 @Override
1283 public void onReceive(Context context, Intent intent) {
1284 final String action = intent.getAction();
1285 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1286 mUserPresent = false;
1287 updateRunning();
1288 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1289 mUserPresent = true;
1290 updateRunning();
1291 }
1292 }
1293 };
1294
1295 @Override
1296 public void onAttachedToWindow() {
1297 super.onAttachedToWindow();
1298
1299 // Listen for broadcasts related to user-presence
1300 final IntentFilter filter = new IntentFilter();
1301 filter.addAction(Intent.ACTION_SCREEN_OFF);
1302 filter.addAction(Intent.ACTION_USER_PRESENT);
1303 registerReceiver(mReceiver, filter);
1304
Adam Cohend113e0c2010-11-11 10:48:05 -08001305 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001306 mVisible = true;
1307 }
1308
1309 @Override
1310 public void onDetachedFromWindow() {
1311 super.onDetachedFromWindow();
1312 mVisible = false;
1313
Adam Cohend113e0c2010-11-11 10:48:05 -08001314 if (mAttached) {
1315 unregisterReceiver(mReceiver);
1316 mAttached = false;
1317 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 updateRunning();
1319 }
1320
1321 public void onWindowVisibilityChanged(int visibility) {
1322 mVisible = visibility == View.VISIBLE;
1323 updateRunning();
1324 }
1325
1326 private void sendAdvanceMessage(long delay) {
1327 mHandler.removeMessages(ADVANCE_MSG);
1328 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1329 mHandler.sendMessageDelayed(msg, delay);
1330 mAutoAdvanceSentTime = System.currentTimeMillis();
1331 }
1332
1333 private void updateRunning() {
1334 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1335 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1336 mAutoAdvanceRunning = autoAdvanceRunning;
1337 if (autoAdvanceRunning) {
1338 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1339 sendAdvanceMessage(delay);
1340 } else {
1341 if (!mWidgetsToAdvance.isEmpty()) {
1342 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1343 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1344 }
1345 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001346 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001347 }
1348 }
1349 }
1350
1351 private final Handler mHandler = new Handler() {
1352 @Override
1353 public void handleMessage(Message msg) {
1354 if (msg.what == ADVANCE_MSG) {
1355 int i = 0;
1356 for (View key: mWidgetsToAdvance.keySet()) {
1357 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1358 final int delay = mAdvanceStagger * i;
1359 if (v instanceof Advanceable) {
1360 postDelayed(new Runnable() {
1361 public void run() {
1362 ((Advanceable) v).advance();
1363 }
1364 }, delay);
1365 }
1366 i++;
1367 }
1368 sendAdvanceMessage(mAdvanceInterval);
1369 }
1370 }
1371 };
1372
1373 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1374 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1375 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1376 if (v instanceof Advanceable) {
1377 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001378 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001379 updateRunning();
1380 }
1381 }
1382
1383 void removeWidgetToAutoAdvance(View hostView) {
1384 if (mWidgetsToAdvance.containsKey(hostView)) {
1385 mWidgetsToAdvance.remove(hostView);
1386 updateRunning();
1387 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001388 }
1389
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1391 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001392 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001393 launcherInfo.hostView = null;
1394 }
1395
Adam Cohended9f8d2010-11-03 13:25:16 -07001396 void showOutOfSpaceMessage() {
1397 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1398 }
1399
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001400 public LauncherAppWidgetHost getAppWidgetHost() {
1401 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Winson Chunga9abd0e2010-10-27 17:18:37 -07001404 public LauncherModel getModel() {
1405 return mModel;
1406 }
1407
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001408 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001409 getWindow().closeAllPanels();
1410
1411 try {
1412 dismissDialog(DIALOG_CREATE_SHORTCUT);
1413 // Unlock the workspace if the dialog was showing
1414 } catch (Exception e) {
1415 // An exception is thrown if the dialog is not visible, which is fine
1416 }
1417
1418 try {
1419 dismissDialog(DIALOG_RENAME_FOLDER);
1420 // Unlock the workspace if the dialog was showing
1421 } catch (Exception e) {
1422 // An exception is thrown if the dialog is not visible, which is fine
1423 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001424
1425 // Whatever we were doing is hereby canceled.
1426 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001427 }
1428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 @Override
1430 protected void onNewIntent(Intent intent) {
1431 super.onNewIntent(intent);
1432
1433 // Close the menu
1434 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001435 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001436 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001437
Joe Onorato14f122b2009-11-19 14:06:36 -08001438 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1439 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001440
Michael Jurka4cb37242010-08-09 21:05:32 -07001441 // in all these cases, only animate if we're already on home
1442 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001443 mWorkspace.unshrink(alreadyOnHome);
1444 }
1445 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001446 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001447 boolean animate = alreadyOnHome &&
1448 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1449 mWorkspace.moveToDefaultScreen(animate);
1450 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001451 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001452 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001453
Joe Onorato3a8820b2009-11-10 15:06:42 -08001454 final View v = getWindow().peekDecorView();
1455 if (v != null && v.getWindowToken() != null) {
1456 InputMethodManager imm = (InputMethodManager)getSystemService(
1457 INPUT_METHOD_SERVICE);
1458 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460 }
1461 }
1462
1463 @Override
1464 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1465 // Do not call super here
1466 mSavedInstanceState = savedInstanceState;
1467 }
1468
1469 @Override
1470 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001471 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1474 if (folders.size() > 0) {
1475 final int count = folders.size();
1476 long[] ids = new long[count];
1477 for (int i = 0; i < count; i++) {
1478 final FolderInfo info = folders.get(i).getInfo();
1479 ids[i] = info.id;
1480 }
1481 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1482 } else {
1483 super.onSaveInstanceState(outState);
1484 }
1485
Michael Jurka883f55b2010-10-21 15:47:14 -07001486 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 if (mAddScreen > -1 && mWaitingForResult) {
1489 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1490 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1491 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 if (mFolderInfo != null && mWaitingForResult) {
1495 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1496 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1497 }
Michael Jurka946ad472010-07-09 18:05:18 -07001498
Winson Chung43b119d2010-12-06 16:45:05 -08001499 // Save the current AllApps drawer tab
1500 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1501 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1502 String currentTabTag = tabhost.getCurrentTabTag();
1503 if (currentTabTag != null) {
1504 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001505 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001506 }
1507 }
1508
1509 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001510 if (mHomeCustomizationDrawer != null) {
1511 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1512 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001513 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001514 }
1515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517
1518 @Override
1519 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001523 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001525 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001527 mAppWidgetHost = null;
1528
1529 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
1531 TextKeyListener.getInstance().release();
1532
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534
Joe Onorato9c1289c2009-08-17 11:03:03 -04001535 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001536
1537 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001538
Patrick Dubroyab962b72010-07-26 12:10:10 -07001539 // Some launcher layouts don't have a previous and next view
1540 if (mPreviousView != null) {
1541 dismissPreview(mPreviousView);
1542 }
1543 if (mNextView != null) {
1544 dismissPreview(mNextView);
1545 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001546
1547 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001548
1549 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1550 mWorkspace.removeAllViews();
1551 mWorkspace = null;
1552 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001553
1554 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 }
1556
1557 @Override
1558 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001559 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 super.startActivityForResult(intent, requestCode);
1561 }
1562
1563 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001564 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001566
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001567 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001568
Karl Rosaen138a0412009-04-23 19:00:21 -07001569 if (initialQuery == null) {
1570 // Use any text typed in the launcher as the initial query
1571 initialQuery = getTypedText();
1572 clearTypedText();
1573 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 if (appSearchData == null) {
1575 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001576 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 }
Romain Guycbb89e42009-06-08 15:52:54 -07001578
Karl Rosaen138a0412009-04-23 19:00:21 -07001579 final SearchManager searchManager =
1580 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001581 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001582 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 }
1584
1585 @Override
1586 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001587 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001588 return false;
1589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
1591 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1594 .setIcon(android.R.drawable.ic_menu_add)
1595 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001596 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1597 .setIcon(android.R.drawable.ic_menu_manage)
1598 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001599 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 .setIcon(android.R.drawable.ic_menu_gallery)
1601 .setAlphabeticShortcut('W');
1602 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1603 .setIcon(android.R.drawable.ic_search_category_default)
1604 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1605 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1606 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1607 .setAlphabeticShortcut('N');
1608
1609 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001610 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1611 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612
1613 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1614 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1615 .setIntent(settings);
1616
1617 return true;
1618 }
1619
1620 @Override
1621 public boolean onPrepareOptionsMenu(Menu menu) {
1622 super.onPrepareOptionsMenu(menu);
1623
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001624 // If all apps is animating, don't show the menu, because we don't know
1625 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001626 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001627 return false;
1628 }
1629
1630 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001631 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001632 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1633 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1634
1635 // Disable add if the workspace is full.
1636 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001637 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1638 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
1641 return true;
1642 }
1643
1644 @Override
1645 public boolean onOptionsItemSelected(MenuItem item) {
1646 switch (item.getItemId()) {
1647 case MENU_ADD:
1648 addItems();
1649 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001650 case MENU_MANAGE_APPS:
1651 manageApps();
1652 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 case MENU_WALLPAPER_SETTINGS:
1654 startWallpaper();
1655 return true;
1656 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001657 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658 return true;
1659 case MENU_NOTIFICATIONS:
1660 showNotifications();
1661 return true;
1662 }
1663
1664 return super.onOptionsItemSelected(item);
1665 }
Romain Guycbb89e42009-06-08 15:52:54 -07001666
Karl Rosaen138a0412009-04-23 19:00:21 -07001667 /**
1668 * Indicates that we want global search for this activity by setting the globalSearch
1669 * argument for {@link #startSearch} to true.
1670 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001672 @Override
1673 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001674 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001675 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001676 }
1677
Joe Onorato9c1289c2009-08-17 11:03:03 -04001678 public boolean isWorkspaceLocked() {
1679 return mWorkspaceLoading || mWaitingForResult;
1680 }
1681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001683 if (LauncherApplication.isScreenXLarge()) {
1684 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001685 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001686 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001687 }
1688 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001689 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 }
1693
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 private void resetAddInfo() {
1695 mAddScreen = -1;
1696 mAddIntersectCellX = -1;
1697 mAddIntersectCellY = -1;
1698 mAddDropPosition = null;
1699 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001700
Winson Chung55cef262010-10-28 14:14:18 -07001701 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 resetAddInfo();
1703 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001704 mAddDropPosition = position;
1705
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001707 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1708 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001709 }
1710
Winson Chung7ad01412010-09-27 11:33:03 -07001711 private void manageApps() {
1712 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1713 }
1714
Michael Jurkaaf442092010-06-10 17:01:57 -07001715 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001716 // TODO: catch bad widget exception when sent
1717 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001718 // TODO: Is this log message meaningful?
1719 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001720 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001721 }
1722
Winson Chung55cef262010-10-28 14:14:18 -07001723 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001724 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725
Bjorn Bringert7984c942009-12-09 15:38:25 +00001726 if (appWidget.configure != null) {
1727 // Launch over to configure widget, if needed
1728 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1729 intent.setComponent(appWidget.configure);
1730 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001731 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001732 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1733 intent.putExtra(
1734 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1735 info.mimeType);
1736
1737 final String mimeType = info.mimeType;
1738 final ClipData clipData = (ClipData) info.configurationData;
1739 final ClipDescription clipDesc = clipData.getDescription();
1740 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1741 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001742 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001743 final CharSequence stringData = item.getText();
1744 final Uri uriData = item.getUri();
1745 final Intent intentData = item.getIntent();
1746 final String key =
1747 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1748 if (uriData != null) {
1749 intent.putExtra(key, uriData);
1750 } else if (intentData != null) {
1751 intent.putExtra(key, intentData);
1752 } else if (stringData != null) {
1753 intent.putExtra(key, stringData);
1754 }
1755 break;
1756 }
1757 }
1758 }
Winson Chung55cef262010-10-28 14:14:18 -07001759 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001760
Romain Guy8e633c52010-03-04 12:51:36 -08001761 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001763 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001764 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766 }
Romain Guycbb89e42009-06-08 15:52:54 -07001767
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1769 resetAddInfo();
1770 mAddScreen = screen;
1771 mAddDropPosition = position;
1772
1773 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1774 createShortcutIntent.setComponent(componentName);
1775 processShortcut(createShortcutIntent);
1776 }
1777
Joe Onoratodeb98af2010-02-19 14:59:39 -08001778 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 // Handle case where user selected "Applications"
1780 String applicationName = getResources().getString(R.string.group_applications);
1781 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001782
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 if (applicationName != null && applicationName.equals(shortcutName)) {
1784 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1785 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001786
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1788 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001789 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001790 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001792 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795
Winson Chung24ab2f12010-09-16 14:10:47 -07001796 void processWallpaper(Intent intent) {
1797 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1798 }
1799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1801 resetAddInfo();
1802 mAddScreen = screen;
1803 mAddDropPosition = position;
1804
1805 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1806 createFolderIntent.setComponent(componentName);
1807
1808 addLiveFolder(createFolderIntent);
1809 }
1810
1811 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001812 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001813 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001814 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001816 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001818 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001819 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001820 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 UserFolderInfo folderInfo = new UserFolderInfo();
1825 folderInfo.title = getText(R.string.folder_name);
1826
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1828 final int[] cellXY = mTmpAddItemCellCoordinates;
1829 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1830 showOutOfSpaceMessage();
1831 return;
1832 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833
1834 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001835 LauncherModel.addItemToDatabase(this, folderInfo,
1836 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001837 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001838 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839
1840 // Create the view
1841 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001842 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1843 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Romain Guycbb89e42009-06-08 15:52:54 -07001846
Joe Onorato9c1289c2009-08-17 11:03:03 -04001847 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001848 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001849 }
1850
Michael Jurka28750fb2010-09-24 17:43:49 -07001851 private void completeAddLiveFolder(
1852 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1854 final int[] cellXY = mTmpAddItemCellCoordinates;
1855 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1856 showOutOfSpaceMessage();
1857 return;
1858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
Michael Jurka0280c3b2010-09-17 15:00:07 -07001860 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861
1862 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001864 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 }
1867 }
1868
1869 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001870 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
1872 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1873 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1874
1875 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 Intent.ShortcutIconResource iconResource = null;
1877
1878 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1879 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1880 try {
1881 iconResource = (Intent.ShortcutIconResource) extra;
1882 final PackageManager packageManager = context.getPackageManager();
1883 Resources resources = packageManager.getResourcesForApplication(
1884 iconResource.packageName);
1885 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1886 icon = resources.getDrawable(id);
1887 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001888 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890 }
1891
1892 if (icon == null) {
1893 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1894 }
1895
1896 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001897 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 info.title = name;
1899 info.iconResource = iconResource;
1900 info.uri = data.getData();
1901 info.baseIntent = baseIntent;
1902 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1903 LiveFolders.DISPLAY_MODE_GRID);
1904
1905 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001906 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001907 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908
1909 return info;
1910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 private void showNotifications() {
1913 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1914 if (statusBar != null) {
1915 statusBar.expand();
1916 }
1917 }
1918
1919 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001920 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001922 Intent chooser = Intent.createChooser(pickWallpaper,
1923 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001924 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1925 // Removed in Eclair MR1
1926// WallpaperManager wm = (WallpaperManager)
1927// getSystemService(Context.WALLPAPER_SERVICE);
1928// WallpaperInfo wi = wm.getWallpaperInfo();
1929// if (wi != null && wi.getSettingsActivity() != null) {
1930// LabeledIntent li = new LabeledIntent(getPackageName(),
1931// R.string.configure_wallpaper, 0);
1932// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1933// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1934// }
Mike Clerona0618e42009-10-22 13:55:21 -07001935 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001938 /**
1939 * Registers various content observers. The current implementation registers
1940 * only a favorites observer to keep track of the favorites applications.
1941 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001942 private void registerContentObservers() {
1943 ContentResolver resolver = getContentResolver();
1944 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1945 true, mWidgetObserver);
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 @Override
1949 public boolean dispatchKeyEvent(KeyEvent event) {
1950 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1951 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001953 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001954 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001955 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001956 dumpState();
1957 return true;
1958 }
1959 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001960 }
1961 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1962 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001963 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 return true;
1965 }
1966 }
1967
1968 return super.dispatchKeyEvent(event);
1969 }
1970
Joe Onorato88ec0992009-11-19 13:16:06 -08001971 @Override
1972 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001973 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1974 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001975 } else {
1976 closeFolder();
1977 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001978 // Some launcher layouts don't have a previous and next view
1979 if (mPreviousView != null) {
1980 dismissPreview(mPreviousView);
1981 dismissPreview(mNextView);
1982 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 private void closeFolder() {
1986 Folder folder = mWorkspace.getOpenFolder();
1987 if (folder != null) {
1988 closeFolder(folder);
1989 }
1990 }
1991
1992 void closeFolder(Folder folder) {
1993 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001994 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001996 CellLayout cl = (CellLayout) parent;
1997 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001998 if (folder instanceof DropTarget) {
1999 // Live folders aren't DropTargets.
2000 mDragController.removeDropTarget((DropTarget)folder);
2001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
2003 folder.onClose();
2004 }
2005
2006 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002007 * Re-listen when widgets are reset.
2008 */
2009 private void onAppWidgetReset() {
2010 mAppWidgetHost.startListening();
2011 }
2012
2013 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002014 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2015 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002017 private void unbindDesktopItems() {
2018 for (ItemInfo item: mDesktopItems) {
2019 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002021 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002024 /**
2025 * Launches the intent referred by the clicked shortcut.
2026 *
2027 * @param v The view representing the clicked shortcut.
2028 */
2029 public void onClick(View v) {
2030 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002031 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002033 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002034 int[] pos = new int[2];
2035 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002036 intent.setSourceBounds(new Rect(pos[0], pos[1],
2037 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002038 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } else if (tag instanceof FolderInfo) {
2040 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002041 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002042 if (mState == State.ALL_APPS) {
2043 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002044 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002045 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
2048 }
2049
Michael Jurka0e260592010-06-30 17:07:39 -07002050 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002051 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002052 // clicking anywhere on the workspace causes the customization drawer to slide down
2053 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002054 return false;
2055 }
2056
Michael Jurkaaf442092010-06-10 17:01:57 -07002057 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002058 * Event handler for the search button
2059 *
2060 * @param v The view that was clicked.
2061 */
2062 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002063 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002064 // Use a custom animation for launching search
2065 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002066 }
2067
2068 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002069 * Event handler for the voice button
2070 *
2071 * @param v The view that was clicked.
2072 */
2073 public void onClickVoiceButton(View v) {
2074 startVoiceSearch();
2075 }
2076
2077 private void startVoiceSearch() {
2078 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2079 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2080 startActivity(intent);
2081 }
2082
2083 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002084 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002085 * enters home screen customization mode.
2086 *
2087 * @param v The view that was clicked.
2088 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002089 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002090 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002091 }
2092
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002093 /**
2094 * Event handler for the "grid" button that appears on the home screen, which
2095 * enters all apps mode.
2096 *
2097 * @param v The view that was clicked.
2098 */
2099 public void onClickAllAppsButton(View v) {
2100 showAllApps(true);
2101 }
2102
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002103 public void onClickAppMarketButton(View v) {
2104 if (mAppMarketIntent != null) {
2105 startActivitySafely(mAppMarketIntent, "app market");
2106 }
2107 }
2108
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002109 void startApplicationDetailsActivity(ComponentName componentName) {
2110 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002111 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2112 Uri.fromParts("package", packageName, null));
2113 startActivity(intent);
2114 }
2115
Patrick Dubroy5539af72010-09-07 15:22:01 -07002116 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2117 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2118 // System applications cannot be installed. For now, show a toast explaining that.
2119 // We may give them the option of disabling apps this way.
2120 int messageId = R.string.uninstall_system_app_text;
2121 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2122 } else {
2123 String packageName = appInfo.componentName.getPackageName();
2124 String className = appInfo.componentName.getClassName();
2125 Intent intent = new Intent(
2126 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2127 startActivity(intent);
2128 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002129 }
2130
Joe Onoratof984e852010-03-25 09:47:45 -07002131 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2133 try {
2134 startActivity(intent);
2135 } catch (ActivityNotFoundException e) {
2136 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002137 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 } catch (SecurityException e) {
2139 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002140 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002142 "or use the exported attribute for this activity. "
2143 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 }
2145 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002146
Romain Guy8e633c52010-03-04 12:51:36 -08002147 void startActivityForResultSafely(Intent intent, int requestCode) {
2148 try {
2149 startActivityForResult(intent, requestCode);
2150 } catch (ActivityNotFoundException e) {
2151 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2152 } catch (SecurityException e) {
2153 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2154 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2155 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2156 "or use the exported attribute for this activity.", e);
2157 }
2158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159
2160 private void handleFolderClick(FolderInfo folderInfo) {
2161 if (!folderInfo.opened) {
2162 // Close any open folder
2163 closeFolder();
2164 // Open the requested folder
2165 openFolder(folderInfo);
2166 } else {
2167 // Find the open folder...
2168 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2169 int folderScreen;
2170 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002171 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 // .. and close it
2173 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002174 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 // Close any folder open on the current screen
2176 closeFolder();
2177 // Pull the folder onto this screen
2178 openFolder(folderInfo);
2179 }
2180 }
2181 }
2182 }
2183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002185 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 * is animated relative to the specified View. If the View is null, no animation
2187 * is played.
2188 *
2189 * @param folderInfo The FolderInfo describing the folder to open.
2190 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002191 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 Folder openFolder;
2193
2194 if (folderInfo instanceof UserFolderInfo) {
2195 openFolder = UserFolder.fromXml(this);
2196 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002197 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 } else {
2199 return;
2200 }
2201
Joe Onorato00acb122009-08-04 16:04:30 -04002202 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 openFolder.setLauncher(this);
2204
2205 openFolder.bind(folderInfo);
2206 folderInfo.opened = true;
2207
Winson Chungaafa03c2010-06-11 17:34:16 -07002208 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2209
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 openFolder.onOpen();
2211 }
2212
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002214 switch (v.getId()) {
2215 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002216 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002217 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2218 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002219 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002220 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002221 return true;
2222 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002223 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002224 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2225 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002226 showPreviews(v);
2227 }
2228 return true;
2229 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002230 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002231 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2232 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2233 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002234 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002235 return true;
2236 }
2237
Joe Onorato9c1289c2009-08-17 11:03:03 -04002238 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 return false;
2240 }
2241
2242 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002243 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 resetAddInfo();
2248 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002250 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 return true;
2252 }
2253
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 final View itemUnderLongClick = longClickCellInfo.cell;
2255
Winson Chung304dcde2011-01-07 11:17:23 -08002256 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 if (itemUnderLongClick == null) {
2258 // User long pressed on empty space
2259 mWorkspace.setAllowLongPress(false);
2260 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2261 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002262 if (LauncherApplication.isScreenXLarge()) {
2263 addItems();
2264 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 }
2267 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002268 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002270 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2271 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002272 mAddIntersectCellX = longClickCellInfo.cellX;
2273 mAddIntersectCellY = longClickCellInfo.cellY;
2274 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 }
2276 }
2277 }
2278 return true;
2279 }
2280
Romain Guye6b8e2f2009-11-10 11:56:55 -08002281 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002282 private void dismissPreview(final View v) {
2283 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002284 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002285 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2286 public void onDismiss() {
2287 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2288 int count = group.getChildCount();
2289 for (int i = 0; i < count; i++) {
2290 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2291 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002292 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2293 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2294
2295 v.setTag(R.id.workspace, null);
2296 v.setTag(R.id.icon, null);
2297 window.setOnDismissListener(null);
2298 }
2299 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002300 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002301 }
2302 v.setTag(null);
2303 }
2304
Romain Guyf8e6a802009-12-07 17:48:02 -08002305 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002306 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002307 }
2308
Romain Guya6abce82009-11-10 02:54:41 -08002309 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002310 final Resources resources = getResources();
2311 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002312
Romain Guya6abce82009-11-10 02:54:41 -08002313 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002314
Romain Guy9d31e9f2009-11-11 18:54:28 -08002315 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002316
Romain Guyf8e6a802009-12-07 17:48:02 -08002317 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002318 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 int extraW = (int) ((r.left + r.right) * max);
2320 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002321
2322 int aW = cell.getWidth() - extraW;
2323 float w = aW / max;
2324
2325 int width = cell.getWidth();
2326 int height = cell.getHeight();
2327 int x = cell.getLeftPadding();
2328 int y = cell.getTopPadding();
2329 width -= (x + cell.getRightPadding());
2330 height -= (y + cell.getBottomPadding());
2331
2332 float scale = w / width;
2333
2334 int count = end - start;
2335
2336 final float sWidth = width * scale;
2337 float sHeight = height * scale;
2338
Romain Guye6b8e2f2009-11-10 11:56:55 -08002339 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002340
Romain Guye6b8e2f2009-11-10 11:56:55 -08002341 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2342 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002343
2344 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002345 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002346 cell = (CellLayout) workspace.getChildAt(i);
2347
Romain Guyf8e6a802009-12-07 17:48:02 -08002348 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002349 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002350
2351 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002352 c.scale(scale, scale);
2353 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002354 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002355
Romain Guy9d31e9f2009-11-11 18:54:28 -08002356 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002357 image.setImageBitmap(bitmap);
2358 image.setTag(i);
2359 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002360 image.setOnFocusChangeListener(handler);
2361 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002362 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002363
2364 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002365 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2366
Winson Chungaafa03c2010-06-11 17:34:16 -07002367 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002368 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002369
2370 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002371 p.setContentView(preview);
2372 p.setWidth((int) (sWidth * count + extraW));
2373 p.setHeight((int) (sHeight + extraH));
2374 p.setAnimationStyle(R.style.AnimationPreview);
2375 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002377 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002378 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002379
Romain Guye6b8e2f2009-11-10 11:56:55 -08002380 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2381 public void onDismiss() {
2382 dismissPreview(anchor);
2383 }
2384 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002385
2386 anchor.setTag(p);
2387 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002388 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002389 }
2390
Romain Guy9d31e9f2009-11-11 18:54:28 -08002391 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002392 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002393
Romain Guye6b8e2f2009-11-10 11:56:55 -08002394 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002395 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002396 }
2397
2398 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002399 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002400 v.post(this);
2401 }
2402
2403 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002404 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002405 }
2406
2407 public void onFocusChange(View v, boolean hasFocus) {
2408 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002409 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002410 }
Romain Guya6abce82009-11-10 02:54:41 -08002411 }
2412 }
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 Workspace getWorkspace() {
2415 return mWorkspace;
2416 }
2417
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 @Override
2419 protected Dialog onCreateDialog(int id) {
2420 switch (id) {
2421 case DIALOG_CREATE_SHORTCUT:
2422 return new CreateShortcut().createDialog();
2423 case DIALOG_RENAME_FOLDER:
2424 return new RenameFolder().createDialog();
2425 }
2426
2427 return super.onCreateDialog(id);
2428 }
2429
2430 @Override
2431 protected void onPrepareDialog(int id, Dialog dialog) {
2432 switch (id) {
2433 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 break;
2435 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002436 if (mFolderInfo != null) {
2437 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2438 final CharSequence text = mFolderInfo.title;
2439 input.setText(text);
2440 input.setSelection(0, text.length());
2441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 break;
2443 }
2444 }
2445
2446 void showRenameDialog(FolderInfo info) {
2447 mFolderInfo = info;
2448 mWaitingForResult = true;
2449 showDialog(DIALOG_RENAME_FOLDER);
2450 }
2451
Michael Jurka0280c3b2010-09-17 15:00:07 -07002452 private void showAddDialog(int intersectX, int intersectY) {
2453 resetAddInfo();
2454 mAddIntersectCellX = intersectX;
2455 mAddIntersectCellY = intersectY;
2456 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457 mWaitingForResult = true;
2458 showDialog(DIALOG_CREATE_SHORTCUT);
2459 }
2460
Joe Onoratodeb98af2010-02-19 14:59:39 -08002461 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002462 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002463 Bundle bundle = new Bundle();
2464
2465 ArrayList<String> shortcutNames = new ArrayList<String>();
2466 shortcutNames.add(getString(R.string.group_applications));
2467 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2468
2469 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2470 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2471 R.drawable.ic_launcher_application));
2472 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2473
2474 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2475 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002476 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002477 pickIntent.putExtras(bundle);
2478
Joe Onoratodeb98af2010-02-19 14:59:39 -08002479 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002480 }
2481
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 private class RenameFolder {
2483 private EditText mInput;
2484
2485 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2487 mInput = (EditText) layout.findViewById(R.id.folder_name);
2488
2489 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2490 builder.setIcon(0);
2491 builder.setTitle(getString(R.string.rename_folder_title));
2492 builder.setCancelable(true);
2493 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2494 public void onCancel(DialogInterface dialog) {
2495 cleanup();
2496 }
2497 });
2498 builder.setNegativeButton(getString(R.string.cancel_action),
2499 new Dialog.OnClickListener() {
2500 public void onClick(DialogInterface dialog, int which) {
2501 cleanup();
2502 }
2503 }
2504 );
2505 builder.setPositiveButton(getString(R.string.rename_action),
2506 new Dialog.OnClickListener() {
2507 public void onClick(DialogInterface dialog, int which) {
2508 changeFolderName();
2509 }
2510 }
2511 );
2512 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002513
2514 final AlertDialog dialog = builder.create();
2515 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2516 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002517 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002518 mInput.requestFocus();
2519 InputMethodManager inputManager = (InputMethodManager)
2520 getSystemService(Context.INPUT_METHOD_SERVICE);
2521 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002522 }
2523 });
2524
2525 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 }
2527
2528 private void changeFolderName() {
2529 final String name = mInput.getText().toString();
2530 if (!TextUtils.isEmpty(name)) {
2531 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002532 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 mFolderInfo.title = name;
2534 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2535
Joe Onorato9c1289c2009-08-17 11:03:03 -04002536 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002537 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002538 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002539 } else {
2540 final FolderIcon folderIcon = (FolderIcon)
2541 mWorkspace.getViewForTag(mFolderInfo);
2542 if (folderIcon != null) {
2543 folderIcon.setText(name);
2544 getWorkspace().requestLayout();
2545 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002546 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 mWorkspaceLoading = true;
2548 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002549 }
2550 }
2551 }
2552 cleanup();
2553 }
2554
2555 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002556 dismissDialog(DIALOG_RENAME_FOLDER);
2557 mWaitingForResult = false;
2558 mFolderInfo = null;
2559 }
2560 }
2561
Daniel Sandler843e8602010-06-07 14:59:01 -04002562 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2563 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002564 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002565 }
2566
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002567 // AllAppsView.Watcher
2568 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002569 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2570 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002571 mWorkspace.setVisibility(View.GONE);
2572 }
2573 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 private void showToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 button.setVisibility(View.VISIBLE);
2577 button.setFocusable(true);
2578 button.setClickable(true);
2579 }
2580
2581 private void hideToolbarButton(View button) {
2582 button.setAlpha(0.0f);
2583 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2584 button.setVisibility(View.INVISIBLE);
2585 button.setFocusable(false);
2586 button.setClickable(false);
2587 }
2588
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002589 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 *
2592 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2593 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002594 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002597 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 final boolean showing = show;
2599 final boolean hiding = !show;
2600
2601 final int duration = show ?
2602 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2603 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2604
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002606 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002607 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002608 anim.addUpdateListener(new AnimatorUpdateListener() {
2609 public void onAnimationUpdate(ValueAnimator animation) {
2610 view.setAlpha((Float) animation.getAnimatedValue());
2611 }
2612 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002613 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002614 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002615 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002618 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002619 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002620 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002621 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002622 });
2623 seq.play(anim);
2624 } else {
2625 if (showing) {
2626 showToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002627 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 } else {
2629 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002631 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 }
2633
2634 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 * Show/hide the appropriate toolbar buttons for newState.
2636 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 *
2638 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002639 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2640 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002641 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002642 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 switch (newState) {
2644 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002645 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2646 mDeleteZone.setOverlappingViews(
2647 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002648 mDeleteZone.setDragAndDropEnabled(true);
2649 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002650 break;
2651 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002652 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002653 mDeleteZone.setDragAndDropEnabled(false);
2654 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 break;
2656 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002657 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002658 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 break;
2660 }
2661 }
2662
2663 /**
2664 * Helper method for the cameraZoomIn/cameraZoomOut animations
2665 * @param view The view being animated
2666 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2667 * @param scaleFactor The scale factor used for the zoom
2668 */
2669 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2670 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002673 // Set pivotY so that at the starting zoom factor, the view is partially
2674 // visible. Modifying initialHeightFactor changes how much of the view is
2675 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002677 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002678 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002680 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002681 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 }
2683 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002684
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002685 /**
2686 * Zoom the camera out from the workspace to reveal 'toView'.
2687 * Assumes that the view to show is anchored at either the very top or very bottom
2688 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002689 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002690 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002692 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002693 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002694
2695 final int duration = toAllApps ?
2696 res.getInteger(R.integer.config_allAppsZoomInTime) :
2697 res.getInteger(R.integer.config_customizeZoomInTime);
2698
2699 final float scale = toAllApps ?
2700 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2701 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2702
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002703 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002704
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002705 setPivotsForZoom(toView, toState, scale);
2706
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002707 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002708 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002709 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002710 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002711 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712
2713 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002714 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2715 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
2716 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2717 public void onAnimationUpdate(ValueAnimator animation) {
2718 final float b = (Float) animation.getAnimatedValue();
2719 final float a = 1f - b;
2720 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002721 toView.setFastScaleX(a * scale + b * 1f);
2722 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002723 }
2724 });
Adam Cohen61033d32010-11-15 18:29:44 -08002725
Adam Cohenf16e5712011-01-13 13:31:45 -08002726 if (toAllApps) {
Michael Jurkad99cf8b2011-01-16 16:53:18 -08002727 toView.setAlpha(0f);
Michael Jurka742574b2011-02-02 23:51:01 -08002728 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurkaea573482011-02-03 19:48:18 -08002729 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002730 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2731 public void onAnimationUpdate(ValueAnimator animation) {
2732 final float b = (Float) animation.getAnimatedValue();
2733 final float a = 1f - b;
2734 // don't need to invalidate because we do so above
2735 toView.setFastAlpha(a * 0f + b * 1f);
2736 }
2737 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002738 alphaAnim.start();
2739 }
2740
Michael Jurkabfadaad2011-01-31 21:35:39 -08002741 // Only use hardware layers in portrait mode, they don't give any gains in landscape
2742 if (mWorkspace.getWidth() < mWorkspace.getHeight()) {
2743 toView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
2744 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002745 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002746 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002747 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748 // Prepare the position
2749 toView.setTranslationX(0.0f);
2750 toView.setTranslationY(0.0f);
2751 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002752 if (!toAllApps) {
2753 toView.setAlpha(1.0f);
2754 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002755 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002756 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002757 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002758 // If we don't set the final scale values here, if this animation is cancelled
2759 // it will have the wrong scale value and subsequent cameraPan animations will
2760 // not fix that
Michael Jurka3eeaea82011-01-31 15:02:36 -08002761 toView.setLayerType(View.LAYER_TYPE_NONE, null);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002762 toView.setScaleX(1.0f);
2763 toView.setScaleY(1.0f);
2764 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002765 });
2766
Chet Haaseb1254a62010-09-07 13:35:00 -07002767 AnimatorSet toolbarHideAnim = new AnimatorSet();
2768 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002769 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2770
2771 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002772 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002774 if (mStateAnimation != null) mStateAnimation.cancel();
2775 mStateAnimation = new AnimatorSet();
2776 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2777 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002778
2779 // Show the new toolbar buttons just as the main animation is ending
2780 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002781 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2782 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002783 } else {
2784 toView.setTranslationX(0.0f);
2785 toView.setTranslationY(0.0f);
2786 toView.setScaleX(1.0f);
2787 toView.setScaleY(1.0f);
2788 toView.setVisibility(View.VISIBLE);
2789 hideAndShowToolbarButtons(toState, null, null);
2790 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002791 }
2792
2793 /**
2794 * Zoom the camera back into the workspace, hiding 'fromView'.
2795 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002796 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2797 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002798 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002800 cameraZoomIn(fromState, animated, false);
2801 }
2802
2803 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002804 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002805 final boolean fromAllApps = (fromState == State.ALL_APPS);
2806
2807 int duration = fromAllApps ?
2808 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2809 res.getInteger(R.integer.config_customizeZoomOutTime);
2810
Michael Jurka742574b2011-02-02 23:51:01 -08002811 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002812 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2813 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2814
2815 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002816
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002817 mCustomizePagedView.endChoiceMode();
2818 mAllAppsPagedView.endChoiceMode();
2819
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002820 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002821
Michael Jurkad3ef3062010-11-23 16:23:58 -08002822 if (!springLoaded) {
2823 mWorkspace.unshrink(animated);
2824 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002825
2826 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002827 if (mStateAnimation != null) mStateAnimation.cancel();
2828 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002829
Michael Jurka742574b2011-02-02 23:51:01 -08002830 final float oldScaleX = fromView.getScaleX();
2831 final float oldScaleY = fromView.getScaleY();
2832
2833 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2834 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2835 scaleAnim.addUpdateListener(new AnimatorUpdateListener() {
2836 public void onAnimationUpdate(ValueAnimator animation) {
2837 final float b = (Float) animation.getAnimatedValue();
2838 final float a = 1f - b;
2839 ((View)fromView.getParent()).fastInvalidate();
2840 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2841 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2842 }
2843 });
2844 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002845 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002846 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka742574b2011-02-02 23:51:01 -08002847 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2848 public void onAnimationUpdate(ValueAnimator animation) {
2849 final float b = (Float) animation.getAnimatedValue();
2850 final float a = 1f - b;
2851 // don't need to invalidate because we do so above
2852 fromView.setFastAlpha(a * 1f + b * 0f);
2853 }
2854 });
2855
Michael Jurkacf6125c2011-01-28 15:20:01 -08002856 fromView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002857 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002858 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002859 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002860 fromView.setVisibility(View.GONE);
Michael Jurkacf6125c2011-01-28 15:20:01 -08002861 fromView.setLayerType(View.LAYER_TYPE_NONE, null);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002862 }
2863 });
2864
Chet Haaseb1254a62010-09-07 13:35:00 -07002865 AnimatorSet toolbarHideAnim = new AnimatorSet();
2866 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002867 if (!springLoaded) {
2868 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2869 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870
Adam Cohen61033d32010-11-15 18:29:44 -08002871 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002872
2873 // Show the new toolbar buttons at the very end of the whole animation
2874 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2875 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002876 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2877 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002878 } else {
2879 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002880 if (!springLoaded) {
2881 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2882 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002883 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002884 }
2885
2886 /**
2887 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2888 * This is the transition used on xlarge screens to go between all apps and
2889 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002890 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2891 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2892 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002893 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002894 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002895 final Resources res = getResources();
2896 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002897 final int workspaceHeight = mWorkspace.getHeight();
2898
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002899 final boolean fromAllApps = (fromState == State.ALL_APPS);
2900 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2901 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002902
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002903 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2904 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2905 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2906 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002907
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002908 mCustomizePagedView.endChoiceMode();
2909 mAllAppsPagedView.endChoiceMode();
2910
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002911 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002912 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002913 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002914 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002915 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002916
2917 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918 if (mStateAnimation != null) mStateAnimation.cancel();
2919 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002920 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002921 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002922 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002923 toView.setVisibility(View.VISIBLE);
2924 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002925 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002926 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002927 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002928 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002929 fromView.setVisibility(View.GONE);
2930 }
2931 });
2932
Chet Haaseb1254a62010-09-07 13:35:00 -07002933 AnimatorSet toolbarHideAnim = new AnimatorSet();
2934 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002935 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2936
Chet Haase472b2812010-10-14 07:02:04 -07002937 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2938 fromViewStartY, fromViewEndY);
2939 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002940 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2941 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2942 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2943 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2944 );
Chet Haase472b2812010-10-14 07:02:04 -07002945 fromAnim.setDuration(duration);
2946 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002947
2948 // Show the new toolbar buttons just as the main animation is ending
2949 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002950 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2951 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002952 } else {
2953 fromView.setY(fromViewEndY);
2954 fromView.setVisibility(View.GONE);
2955 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002956 toView.setScaleX(1.0f);
2957 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002958 toView.setVisibility(View.VISIBLE);
2959 hideAndShowToolbarButtons(toState, null, null);
2960 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002961 }
2962
2963 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002964 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002965 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002966 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002967
Michael Jurka79212d82010-07-30 16:36:20 -07002968 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002969 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002970 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002971 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002972 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002973 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002974 } else {
2975 mAllAppsGrid.zoom(1.0f, animated);
2976 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002977
Romain Guyc16fea72010-03-12 17:17:56 -08002978 ((View) mAllAppsGrid).setFocusable(true);
2979 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002980
Joe Onorato7c312c12009-08-13 21:36:53 -07002981 // TODO: fade these two too
2982 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002983
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002984 // Change the state *after* we've called all the transition code
2985 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002986
Winson Chung5fb63472011-02-02 17:03:37 -08002987 // Pause the auto-advance of widgets until we are out of AllApps
2988 mUserPresent = false;
2989 updateRunning();
2990
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002991 // send an accessibility event to announce the context change
2992 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2993 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002994
2995 void showWorkspace(boolean animated) {
2996 showWorkspace(animated, null);
2997 }
2998
2999 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08003000 if (layout != null) {
3001 // always animated, but that's ok since we never specify a layout and
3002 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003003 mWorkspace.unshrink(layout);
3004 } else {
3005 mWorkspace.unshrink(animated);
3006 }
3007 if (mState == State.ALL_APPS) {
3008 closeAllApps(animated);
3009 } else if (mState == State.CUSTOMIZE) {
3010 hideCustomizationDrawer(animated);
3011 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003012
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003013 // Change the state *after* we've called all the transition code
3014 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003015
Winson Chung5fb63472011-02-02 17:03:37 -08003016 // Resume the auto-advance of widgets
3017 mUserPresent = true;
3018 updateRunning();
3019
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003020 // send an accessibility event to announce the context change
3021 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04003022 }
3023
Michael Jurkad3ef3062010-11-23 16:23:58 -08003024 void enterSpringLoadedDragMode(CellLayout layout) {
3025 mWorkspace.enterSpringLoadedDragMode(layout);
3026 if (mState == State.ALL_APPS) {
3027 cameraZoomIn(State.ALL_APPS, true, true);
3028 mState = State.ALL_APPS_SPRING_LOADED;
3029 } else if (mState == State.CUSTOMIZE) {
3030 cameraZoomIn(State.CUSTOMIZE, true, true);
3031 mState = State.CUSTOMIZE_SPRING_LOADED;
3032 }/* else {
3033 // we're already in spring loaded mode; don't do anything
3034 }*/
3035 }
3036
3037 void exitSpringLoadedDragMode() {
3038 if (mState == State.ALL_APPS_SPRING_LOADED) {
3039 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
3040 cameraZoomOut(State.ALL_APPS, true);
3041 mState = State.ALL_APPS;
3042 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
3043 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
3044 cameraZoomOut(State.CUSTOMIZE, true);
3045 mState = State.CUSTOMIZE;
3046 }/* else {
3047 // we're not in spring loaded mode; don't do anything
3048 }*/
3049 }
3050
Joe Onoratob2061212009-11-24 16:13:54 -05003051 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08003052 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05003053 * - Home from workspace
3054 * - from center screen
3055 * - from other screens
3056 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003057 * - from center screen
3058 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003059 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003060 * - from center screen
3061 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05003062 * - Launch app from workspace and quit
3063 * - with back
3064 * - with home
3065 * - Launch app from all apps and quit
3066 * - with back
3067 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08003068 * - Go to a screen that's not the default, then all
3069 * apps, and launch and app, and go back
3070 * - with back
3071 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003072 * - On workspace, long press power and go back
3073 * - with back
3074 * - with home
3075 * - On all apps, long press power and go back
3076 * - with back
3077 * - with home
3078 * - On workspace, power off
3079 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003080 * - Launch an app and turn off the screen while in that app
3081 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003082 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003083 * - From all apps
3084 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003085 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3086 * - From all apps
3087 * - From the center workspace
3088 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003089 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003090 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003091 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003092 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003093 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003094 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003095 } else {
3096 mAllAppsGrid.zoom(0.0f, animated);
3097 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003098 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003099 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003100 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003101 }
3102
Joe Onorato7c312c12009-08-13 21:36:53 -07003103 void lockAllApps() {
3104 // TODO
3105 }
3106
3107 void unlockAllApps() {
3108 // TODO
3109 }
3110
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003111 // Show the customization drawer (only exists in x-large configuration)
3112 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003113 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003114 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003115 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003116 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003117 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003118 // Change the state *after* we've called all the transition code
3119 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003120
3121 // Pause the auto-advance of widgets until we are out of Customization drawer
3122 mUserPresent = false;
3123 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003124 }
3125
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003126 // Hide the customization drawer (only exists in x-large configuration)
3127 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003128 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003129 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003130 }
3131 }
3132
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003133 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3134 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3135 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003136 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003137 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003138
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003139 void onWorkspaceClick(CellLayout layout) {
3140 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003141 }
3142
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003144 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003145 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3146 ImageView button = (ImageView) findViewById(buttonId);
3147 Drawable toolbarIcon = null;
3148 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003149 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003150 // Look for the toolbar icon specified in the activity meta-data
3151 Bundle metaData = packageManager.getActivityInfo(
3152 activityName, PackageManager.GET_META_DATA).metaData;
3153 if (metaData != null) {
3154 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3155 if (iconResId != 0) {
3156 Resources res = packageManager.getResourcesForActivity(activityName);
3157 toolbarIcon = res.getDrawable(iconResId);
3158 }
3159 }
3160 } catch (NameNotFoundException e) {
3161 // Do nothing
3162 }
3163 // If we were unable to find the icon via the meta-data, use a generic one
3164 if (toolbarIcon == null) {
3165 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003166 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003167 } else {
3168 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003169 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003170 }
3171 }
3172
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003173 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003174 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003175 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003176 }
3177
Michael Jurka0423dcf2010-10-05 14:56:18 -07003178 private void updateGlobalSearchIcon() {
3179 if (LauncherApplication.isScreenXLarge()) {
3180 final SearchManager searchManager =
3181 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3182 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003183 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003184 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003185 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003186 } else {
3187 findViewById(R.id.search_button).setVisibility(View.GONE);
3188 }
3189 }
3190 }
3191
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003192 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003193 updateButtonWithDrawable(R.id.search_button, d);
3194 }
3195
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003196 private void updateVoiceSearchIcon() {
3197 if (LauncherApplication.isScreenXLarge()) {
3198 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3199 ComponentName activityName = intent.resolveActivity(getPackageManager());
3200 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003201 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003202 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3203 } else {
3204 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003205 }
3206 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003207 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003208
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003209 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003210 updateButtonWithDrawable(R.id.voice_button, d);
3211 }
3212
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003213 /**
3214 * Sets the app market icon (shown when all apps is visible on x-large screens)
3215 */
3216 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003217 if (LauncherApplication.isScreenXLarge()) {
3218 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3219 // Find the app market activity by resolving an intent.
3220 // (If multiple app markets are installed, it will return the ResolverActivity.)
3221 ComponentName activityName = intent.resolveActivity(getPackageManager());
3222 if (activityName != null) {
3223 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003224 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003225 R.id.market_button, activityName, R.drawable.app_market_generic);
3226 }
3227 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003228 }
3229
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003230 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003231 updateButtonWithDrawable(R.id.market_button, d);
3232 }
3233
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003234 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235 * Displays the shortcut creation dialog and launches, if necessary, the
3236 * appropriate activity.
3237 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003238 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003239 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3240 DialogInterface.OnShowListener {
3241
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003242 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003245 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003246
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3248 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003249 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 builder.setInverseBackgroundForced(true);
3252
3253 AlertDialog dialog = builder.create();
3254 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003255 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003256 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003258 return dialog;
3259 }
3260
3261 public void onCancel(DialogInterface dialog) {
3262 mWaitingForResult = false;
3263 cleanup();
3264 }
3265
Romain Guycbb89e42009-06-08 15:52:54 -07003266 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003267 }
3268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003269 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003270 try {
3271 dismissDialog(DIALOG_CREATE_SHORTCUT);
3272 } catch (Exception e) {
3273 // An exception is thrown if the dialog is not visible, which is fine
3274 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003275 }
3276
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003277 /**
3278 * Handle the action clicked in the "Add to home" dialog.
3279 */
3280 public void onClick(DialogInterface dialog, int which) {
3281 Resources res = getResources();
3282 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003283
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003284 switch (which) {
3285 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003286 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003287 break;
3288 }
Romain Guycbb89e42009-06-08 15:52:54 -07003289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003290 case AddAdapter.ITEM_APPWIDGET: {
3291 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003292
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003293 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3294 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003295 // start the pick activity
3296 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3297 break;
3298 }
Romain Guycbb89e42009-06-08 15:52:54 -07003299
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003300 case AddAdapter.ITEM_LIVE_FOLDER: {
3301 // Insert extra item to handle inserting folder
3302 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003303
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003304 ArrayList<String> shortcutNames = new ArrayList<String>();
3305 shortcutNames.add(res.getString(R.string.group_folder));
3306 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003307
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003308 ArrayList<ShortcutIconResource> shortcutIcons =
3309 new ArrayList<ShortcutIconResource>();
3310 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3311 R.drawable.ic_launcher_folder));
3312 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3313
3314 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3315 pickIntent.putExtra(Intent.EXTRA_INTENT,
3316 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3317 pickIntent.putExtra(Intent.EXTRA_TITLE,
3318 getText(R.string.title_select_live_folder));
3319 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003320
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003321 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3322 break;
3323 }
3324
3325 case AddAdapter.ITEM_WALLPAPER: {
3326 startWallpaper();
3327 break;
3328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 }
3330 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003331
3332 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003333 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003335 }
3336
3337 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003338 * Receives notifications when applications are added/removed.
3339 */
3340 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3341 @Override
3342 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003343 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003344 String reason = intent.getStringExtra("reason");
3345 if (!"homekey".equals(reason)) {
3346 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003347 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003348 animate = false;
3349 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003350 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003351 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003352 }
3353 }
3354
3355 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003356 * Receives notifications whenever the appwidgets are reset.
3357 */
3358 private class AppWidgetResetObserver extends ContentObserver {
3359 public AppWidgetResetObserver() {
3360 super(new Handler());
3361 }
3362
3363 @Override
3364 public void onChange(boolean selfChange) {
3365 onAppWidgetReset();
3366 }
3367 }
3368
3369 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003370 * If the activity is currently paused, signal that we need to re-run the loader
3371 * in onResume.
3372 *
3373 * This needs to be called from incoming places where resources might have been loaded
3374 * while we are paused. That is becaues the Configuration might be wrong
3375 * when we're not running, and if it comes back to what it was when we
3376 * were paused, we are not restarted.
3377 *
3378 * Implementation of the method from LauncherModel.Callbacks.
3379 *
3380 * @return true if we are currently paused. The caller might be able to
3381 * skip some work in that case since we will come back again.
3382 */
3383 public boolean setLoadOnResume() {
3384 if (mPaused) {
3385 Log.i(TAG, "setLoadOnResume");
3386 mOnResumeNeedsLoad = true;
3387 return true;
3388 } else {
3389 return false;
3390 }
3391 }
3392
3393 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003395 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003397 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003398 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003399 } else {
3400 return SCREEN_COUNT / 2;
3401 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003402 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003403
Winson Chunga12a2502010-12-20 14:41:35 -08003404 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003405 mAllAppsPagedView = view;
3406 }
3407
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 /**
3409 * Refreshes the shortcuts shown on the workspace.
3410 *
3411 * Implementation of the method from LauncherModel.Callbacks.
3412 */
3413 public void startBinding() {
3414 final Workspace workspace = mWorkspace;
3415 int count = workspace.getChildCount();
3416 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003417 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003418 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3419 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003421
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 if (DEBUG_USER_INTERFACE) {
3423 android.widget.Button finishButton = new android.widget.Button(this);
3424 finishButton.setText("Finish");
3425 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3426
3427 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3428 public void onClick(View v) {
3429 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003432 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003433
3434 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3435 // mDesktopItems -> AppWidgetInfo -> hostView).
3436 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003437 }
3438
3439 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 * Bind the items start-end from the list.
3441 *
3442 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003443 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3445
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003446 setLoadOnResume();
3447
Joe Onorato9c1289c2009-08-17 11:03:03 -04003448 final Workspace workspace = mWorkspace;
3449
3450 for (int i=start; i<end; i++) {
3451 final ItemInfo item = shortcuts.get(i);
3452 mDesktopItems.add(item);
3453 switch (item.itemType) {
3454 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3455 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003456 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003457 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3458 false);
3459 break;
3460 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3461 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003462 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003463 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3465 false);
3466 break;
3467 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3468 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3469 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003470 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 (LiveFolderInfo) item);
3472 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3473 false);
3474 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003476 }
3477
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003479 }
3480
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 /**
3482 * Implementation of the method from LauncherModel.Callbacks.
3483 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003484 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003485 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003486 sFolders.clear();
3487 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003488 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489
3490 /**
3491 * Add the views for a widget to the workspace.
3492 *
3493 * Implementation of the method from LauncherModel.Callbacks.
3494 */
3495 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003496 setLoadOnResume();
3497
Daniel Sandler843e8602010-06-07 14:59:01 -04003498 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3499 if (DEBUG_WIDGETS) {
3500 Log.d(TAG, "bindAppWidget: " + item);
3501 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003502 final Workspace workspace = mWorkspace;
3503
3504 final int appWidgetId = item.appWidgetId;
3505 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003506 if (DEBUG_WIDGETS) {
3507 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3508 }
3509
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3511
Joe Onorato9c1289c2009-08-17 11:03:03 -04003512 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3513 item.hostView.setTag(item);
3514
3515 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3516 item.cellY, item.spanX, item.spanY, false);
3517
Adam Cohended9f8d2010-11-03 13:25:16 -07003518 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3519
Joe Onorato9c1289c2009-08-17 11:03:03 -04003520 workspace.requestLayout();
3521
3522 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003523
3524 if (DEBUG_WIDGETS) {
3525 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3526 + (SystemClock.uptimeMillis()-start) + "ms");
3527 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 }
3529
3530 /**
3531 * Callback saying that there aren't any more items to bind.
3532 *
3533 * Implementation of the method from LauncherModel.Callbacks.
3534 */
3535 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003536 setLoadOnResume();
3537
Joe Onorato9c1289c2009-08-17 11:03:03 -04003538 if (mSavedState != null) {
3539 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003540 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003541 }
3542
3543 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3544 if (userFolders != null) {
3545 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003546 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003547 if (info != null) {
3548 openFolder(info);
3549 }
3550 }
3551 final Folder openFolder = mWorkspace.getOpenFolder();
3552 if (openFolder != null) {
3553 openFolder.requestFocus();
3554 }
3555 }
3556
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 mSavedState = null;
3558 }
3559
3560 if (mSavedInstanceState != null) {
3561 super.onRestoreInstanceState(mSavedInstanceState);
3562 mSavedInstanceState = null;
3563 }
3564
Winson Chung32bb5e52011-01-31 14:51:56 -08003565 // Workaround a bug that occurs when rotating the device while the customization mode is
3566 // open, we trigger a new layout on all the CellLayout children.
3567 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3568 final int childCount = mWorkspace.getChildCount();
3569 for (int i = 0; i < childCount; ++i) {
3570 mWorkspace.getChildAt(i).requestLayout();
3571 }
3572 }
3573
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 mWorkspaceLoading = false;
3575 }
3576
3577 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003578 * Updates the icons on the launcher that are affected by changes to the package list
3579 * on the device.
3580 */
3581 private void updateIconsAffectedByPackageManagerChanges() {
3582 updateAppMarketIcon();
3583 updateGlobalSearchIcon();
3584 updateVoiceSearchIcon();
3585 }
3586
3587 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003588 * Add the icons for all apps.
3589 *
3590 * Implementation of the method from LauncherModel.Callbacks.
3591 */
3592 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003593 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003594 if (mCustomizePagedView != null) {
3595 mCustomizePagedView.setApps(apps);
3596 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003597 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003598 }
3599
3600 /**
3601 * A package was installed.
3602 *
3603 * Implementation of the method from LauncherModel.Callbacks.
3604 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003605 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003606 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003607 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003608 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003609 if (mCustomizePagedView != null) {
3610 mCustomizePagedView.addApps(apps);
3611 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003612 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003613 }
3614
3615 /**
3616 * A package was updated.
3617 *
3618 * Implementation of the method from LauncherModel.Callbacks.
3619 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003620 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003621 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003622 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003623 mWorkspace.updateShortcuts(apps);
3624 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003625 if (mCustomizePagedView != null) {
3626 mCustomizePagedView.updateApps(apps);
3627 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003628 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003629 }
3630
3631 /**
3632 * A package was uninstalled.
3633 *
3634 * Implementation of the method from LauncherModel.Callbacks.
3635 */
Joe Onorato36115782010-06-17 13:28:48 -04003636 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003637 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003638 if (permanent) {
3639 mWorkspace.removeItems(apps);
3640 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003641 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003642 if (mCustomizePagedView != null) {
3643 mCustomizePagedView.removeApps(apps);
3644 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003645 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003646 }
Joe Onoratobe386092009-11-17 17:32:16 -08003647
3648 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003649 * A number of packages were updated.
3650 */
3651 public void bindPackagesUpdated() {
3652 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003653 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003654 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003655 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003656 }
3657
Winson Chung400438b2011-01-16 17:53:48 -08003658 private int mapConfigurationOriActivityInfoOri(int configOri) {
3659 final Display d = getWindowManager().getDefaultDisplay();
3660 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3661 switch (d.getRotation()) {
3662 case Surface.ROTATION_0:
3663 case Surface.ROTATION_180:
3664 // We are currently in the same basic orientation as the natural orientation
3665 naturalOri = configOri;
3666 break;
3667 case Surface.ROTATION_90:
3668 case Surface.ROTATION_270:
3669 // We are currently in the other basic orientation to the natural orientation
3670 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3671 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3672 break;
3673 }
3674
3675 int[] oriMap = {
3676 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3677 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3678 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3679 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3680 };
3681 // Since the map starts at portrait, we need to offset if this device's natural orientation
3682 // is landscape.
3683 int indexOffset = 0;
3684 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3685 indexOffset = 1;
3686 }
3687 return oriMap[(d.getRotation() + indexOffset) % 4];
3688 }
3689 public void lockScreenOrientation() {
3690 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3691 .getConfiguration().orientation));
3692 }
3693 public void unlockScreenOrientation() {
3694 mHandler.postDelayed(new Runnable() {
3695 public void run() {
3696 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3697 }
3698 }, mRestoreScreenOrientationDelay);
3699 }
3700
Winson Chung80baf5a2010-08-09 16:03:15 -07003701 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003702 * Prints out out state for debugging.
3703 */
3704 public void dumpState() {
3705 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003706 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003707 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3708 Log.d(TAG, "mRestoring=" + mRestoring);
3709 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3710 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3711 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003712 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003713 mModel.dumpState();
3714 mAllAppsGrid.dumpState();
3715 Log.d(TAG, "END launcher2 dump state");
3716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003717}