blob: 1ec326b2f02b7eabdbd339aeef3acb65c0e92bbb [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 Jurkaaf91de02010-11-23 16:23:58 -080020import com.android.common.Search;
21import com.android.launcher.R;
Winson Chunga12a2502010-12-20 14:41:35 -080022import com.android.launcher2.CustomizePagedView.CustomizationType;
Michael Jurkaaf91de02010-11-23 16:23:58 -080023import 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 Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070036import android.app.WallpaperManager;
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 Chung68846fd2010-10-29 11:00:27 -070048import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070049import 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 Chung68846fd2010-10-29 11:00:27 -070052import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070053import 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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080099import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700100import android.widget.TextView;
101import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700102import android.widget.TabHost.OnTabChangeListener;
103import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700104
Michael Jurkaaf91de02010-11-23 16:23:58 -0800105import java.io.DataInputStream;
106import java.io.DataOutputStream;
107import java.io.FileNotFoundException;
108import java.io.IOException;
109import java.util.ArrayList;
110import java.util.HashMap;
111import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800112
Adam Cohended9f8d2010-11-03 13:25:16 -0700113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114/**
115 * Default launcher application.
116 */
Michael Jurka946ad472010-07-09 18:05:18 -0700117public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700118 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
119 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800120 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400124 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800128 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700131 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
132 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
134 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700135 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 private static final int REQUEST_CREATE_SHORTCUT = 1;
138 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140 private static final int REQUEST_PICK_APPLICATION = 6;
141 private static final int REQUEST_PICK_SHORTCUT = 7;
142 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700143 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700144 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
146 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
147
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800148 static final int SCREEN_COUNT = 5;
149 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Joe Onorato7c312c12009-08-13 21:36:53 -0700151 static final int DIALOG_CREATE_SHORTCUT = 1;
152 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Romain Guy98d01652009-06-30 16:21:04 -0700154 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155
156 // Type: int
157 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700158 // Type: int
159 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: long
161 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
164 // Type: int
165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
166 // Type: int
167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: boolean
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
170 // Type: long
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
172
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 // tags for the customization tabs
174 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700175 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700176 private static final String SHORTCUTS_TAG = "shortcuts";
177 private static final String WALLPAPERS_TAG = "wallpapers";
178
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
180
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800182 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
183 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700184 private State mState = State.WORKSPACE;
185 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186
Joe Onorato9c1289c2009-08-17 11:03:03 -0400187 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800190 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800192 private final BroadcastReceiver mCloseSystemDialogsReceiver
193 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800194 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private LayoutInflater mInflater;
197
Joe Onorato00acb122009-08-04 16:04:30 -0400198 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700201 private AppWidgetManager mAppWidgetManager;
202 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700203
Michael Jurka0280c3b2010-09-17 15:00:07 -0700204 private int mAddScreen = -1;
205 private int mAddIntersectCellX = -1;
206 private int mAddIntersectCellY = -1;
207 private int[] mAddDropPosition;
208 private int[] mTmpAddItemCellCoordinates = new int[2];
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private FolderInfo mFolderInfo;
211
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700215 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700216 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700217
Winson Chunga12a2502010-12-20 14:41:35 -0800218 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700219 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private Bundle mSavedState;
222
223 private SpannableStringBuilder mDefaultKeySsb = null;
224
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 private boolean mWorkspaceLoading = true;
226
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800227 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228 private boolean mRestoring;
229 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700230 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
232 private Bundle mSavedInstanceState;
233
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800236 private boolean mUserPresent = true;
237 private boolean mVisible = false;
238 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static LocaleConfiguration sLocaleConfiguration = null;
241
Romain Guy84f296c2009-11-04 15:00:44 -0800242 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700243
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700244 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700245
Romain Guy1fbc1c82009-11-09 20:43:08 -0800246 private ImageView mPreviousView;
247 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500248
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400249 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400250 private String[] mHotseatConfig = null;
251 private Intent[] mHotseats = null;
252 private Drawable[] mHotseatIcons = null;
253 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400254
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700255 private Intent mAppMarketIntent = null;
256
Adam Cohended9f8d2010-11-03 13:25:16 -0700257 // Related to the auto-advancing of widgets
258 private final int ADVANCE_MSG = 1;
259 private final int mAdvanceInterval = 20000;
260 private final int mAdvanceStagger = 250;
261 private long mAutoAdvanceSentTime;
262 private long mAutoAdvanceTimeLeft = -1;
263 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
264 new HashMap<View, AppWidgetProviderInfo>();
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800267 private static Drawable.ConstantState sGlobalSearchIcon;
268 private static Drawable.ConstantState sVoiceSearchIcon;
269 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Winson Chunga12a2502010-12-20 14:41:35 -0800271 private CustomizationType getCustomizeFilterForTabTag(String tag) {
272 if (tag.equals(WIDGETS_TAG)) {
273 return CustomizationType.WidgetCustomization;
274 } else if (tag.equals(APPLICATIONS_TAG)) {
275 return CustomizationType.ApplicationCustomization;
276 } else if (tag.equals(WALLPAPERS_TAG)) {
277 return CustomizePagedView.CustomizationType.WallpaperCustomization;
278 } else if (tag.equals(SHORTCUTS_TAG)) {
279 return CustomizePagedView.CustomizationType.ShortcutCustomization;
280 }
281 return CustomizationType.WidgetCustomization;
282 }
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
285 protected void onCreate(Bundle savedInstanceState) {
286 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700287
Winson Chungaafa03c2010-06-11 17:34:16 -0700288 if (LauncherApplication.isInPlaceRotationEnabled()) {
289 // hide the status bar (temporary until we get the status bar design figured out)
290 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
291 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
292 }
293
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 LauncherApplication app = ((LauncherApplication)getApplication());
295 mModel = app.setLauncher(this);
296 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700299
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700300 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
302 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200305 android.os.Debug.startMethodTracing(
306 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 }
308
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400309 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 checkForLocaleChange();
311 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800312 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800313 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700314 if (mHomeCustomizationDrawer != null) {
315 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700316
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700318 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
319 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 TabContentFactory contentFactory = new TabContentFactory() {
321 public View createTabContent(String tag) {
322 return mCustomizePagedView;
323 }
324 };
325
Winson Chung49767ae2010-11-29 14:48:30 -0800326
327 TextView tabView;
328 TabWidget tabWidget = (TabWidget)
329 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
330
331 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
332 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800334 .setIndicator(tabView).setContent(contentFactory));
335 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
336 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700337 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800338 .setIndicator(tabView).setContent(contentFactory));
339 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
340 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800342 .setIndicator(tabView).setContent(contentFactory));
343 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
344 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700345 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800346 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
348 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800349 final CustomizePagedView.CustomizationType newType =
350 getCustomizeFilterForTabTag(tabId);
351 if (newType != mCustomizePagedView.getCustomizationFilter()) {
352 // animate the changing of the tab content by fading pages in and out
353 final Resources res = getResources();
354 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
355 final float alpha = mCustomizePagedView.getAlpha();
356 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
357 "alpha", alpha, 0.0f);
358 alphaAnim.setDuration(duration);
359 alphaAnim.addListener(new AnimatorListenerAdapter() {
360 @Override
361 public void onAnimationEnd(Animator animation) {
362 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
363 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700364
Winson Chunga12a2502010-12-20 14:41:35 -0800365 final float alpha = mCustomizePagedView.getAlpha();
366 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
367 mCustomizePagedView, "alpha", alpha, 1.0f);
368 alphaAnim.setDuration(duration);
369 alphaAnim.start();
370 }
371 });
372 alphaAnim.start();
373 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700374 }
375 });
Michael Jurka946ad472010-07-09 18:05:18 -0700376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 setupViews();
378
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800379 registerContentObservers();
380
Joe Onorato7c312c12009-08-13 21:36:53 -0700381 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 mSavedState = savedInstanceState;
384 restoreState(mSavedState);
385
Winson Chunga12a2502010-12-20 14:41:35 -0800386 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800387 if (mCustomizePagedView != null) {
388 mCustomizePagedView.update();
389 }
Winson Chunga12a2502010-12-20 14:41:35 -0800390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 if (PROFILE_STARTUP) {
392 android.os.Debug.stopMethodTracing();
393 }
394
395 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400396 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 }
398
399 // For handling default keys
400 mDefaultKeySsb = new SpannableStringBuilder();
401 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800402
403 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
404 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800405
406 // If we have a saved version of these external icons, we load them up immediately
407 if (LauncherApplication.isScreenXLarge()) {
408 if (sGlobalSearchIcon != null) {
409 updateGlobalSearchIcon(sGlobalSearchIcon);
410 }
411 if (sVoiceSearchIcon != null) {
412 updateVoiceSearchIcon(sVoiceSearchIcon);
413 }
414 if (sAppMarketIcon != null) {
415 updateAppMarketIcon(sAppMarketIcon);
416 }
417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 }
Romain Guycbb89e42009-06-08 15:52:54 -0700419
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800420 @Override
421 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
422 super.dispatchPopulateAccessibilityEvent(event);
423
424 // we want to take over text population so it is context dependent
425 event.getText().clear();
426 if (mState == State.ALL_APPS) {
427 event.getText().add(getString(R.string.all_apps_button_label));
428 } else if (mState == State.WORKSPACE) {
429 event.getText().add(getString(R.string.all_apps_home_button_label));
430 }
431
432 return true;
433 }
434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 if (sLocaleConfiguration == null) {
437 new AsyncTask<Void, Void, LocaleConfiguration>() {
438 @Override
439 protected LocaleConfiguration doInBackground(Void... unused) {
440 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
441 readConfiguration(Launcher.this, localeConfiguration);
442 return localeConfiguration;
443 }
444
445 @Override
446 protected void onPostExecute(LocaleConfiguration result) {
447 sLocaleConfiguration = result;
448 checkForLocaleChange(); // recursive, but now with a locale configuration
449 }
450 }.execute();
451 return;
452 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 final Configuration configuration = getResources().getConfiguration();
455
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 final String locale = configuration.locale.toString();
458
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700459 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 final int mcc = configuration.mcc;
461
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 final int mnc = configuration.mnc;
464
Romain Guy84f296c2009-11-04 15:00:44 -0800465 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466
Romain Guy84f296c2009-11-04 15:00:44 -0800467 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 sLocaleConfiguration.locale = locale;
469 sLocaleConfiguration.mcc = mcc;
470 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700471
Joe Onorato0589f0f2010-02-08 13:44:00 -0800472 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400473 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474
475 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
476 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700477 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700478 public void run() {
479 writeConfiguration(Launcher.this, localeConfiguration);
480 }
481 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700482 }
483 }
484
485 private static class LocaleConfiguration {
486 public String locale;
487 public int mcc = -1;
488 public int mnc = -1;
489 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700490
Romain Guy98d01652009-06-30 16:21:04 -0700491 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
492 DataInputStream in = null;
493 try {
494 in = new DataInputStream(context.openFileInput(PREFERENCES));
495 configuration.locale = in.readUTF();
496 configuration.mcc = in.readInt();
497 configuration.mnc = in.readInt();
498 } catch (FileNotFoundException e) {
499 // Ignore
500 } catch (IOException e) {
501 // Ignore
502 } finally {
503 if (in != null) {
504 try {
505 in.close();
506 } catch (IOException e) {
507 // Ignore
508 }
509 }
510 }
511 }
512
513 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
514 DataOutputStream out = null;
515 try {
516 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
517 out.writeUTF(configuration.locale);
518 out.writeInt(configuration.mcc);
519 out.writeInt(configuration.mnc);
520 out.flush();
521 } catch (FileNotFoundException e) {
522 // Ignore
523 } catch (IOException e) {
524 //noinspection ResultOfMethodCallIgnored
525 context.getFileStreamPath(PREFERENCES).delete();
526 } finally {
527 if (out != null) {
528 try {
529 out.close();
530 } catch (IOException e) {
531 // Ignore
532 }
533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 }
535 }
536
537 static int getScreen() {
538 synchronized (sLock) {
539 return sScreen;
540 }
541 }
542
543 static void setScreen(int screen) {
544 synchronized (sLock) {
545 sScreen = screen;
546 }
547 }
548
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700550 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800551
552 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700553 // TODO: Put back when we decide about scrolling the wallpaper
554 // boolean isPortrait = display.getWidth() < display.getHeight();
555 // final int width = isPortrait ? display.getWidth() : display.getHeight();
556 // final int height = isPortrait ? display.getHeight() : display.getWidth();
557 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
558 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 // Note: This doesn't do all the client-id magic that BrowserProvider does
562 // in Browser. (http://b/2425179)
563 private Uri getDefaultBrowserUri() {
564 String url = getString(R.string.default_browser_url);
565 if (url.indexOf("{CID}") != -1) {
566 url = url.replace("{CID}", "android-google");
567 }
568 return Uri.parse(url);
569 }
570
571 // Load the Intent templates from arrays.xml to populate the hotseats. For
572 // each Intent, if it resolves to a single app, use that as the launch
573 // intent & use that app's label as the contentDescription. Otherwise,
574 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400575 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 if (mHotseatConfig == null) {
577 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
578 if (mHotseatConfig.length > 0) {
579 mHotseats = new Intent[mHotseatConfig.length];
580 mHotseatLabels = new CharSequence[mHotseatConfig.length];
581 mHotseatIcons = new Drawable[mHotseatConfig.length];
582 } else {
583 mHotseats = null;
584 mHotseatIcons = null;
585 mHotseatLabels = null;
586 }
587
588 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
589 for (int i=0; i<mHotseatConfig.length; i++) {
590 // load icon for this slot; currently unrelated to the actual activity
591 try {
592 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
593 } catch (ArrayIndexOutOfBoundsException ex) {
594 Log.w(TAG, "Missing hotseat_icons array item #" + i);
595 mHotseatIcons[i] = null;
596 }
597 }
598 hotseatIconDrawables.recycle();
599 }
600
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 for (int i=0; i<mHotseatConfig.length; i++) {
603 Intent intent = null;
604 if (mHotseatConfig[i].equals("*BROWSER*")) {
605 // magic value meaning "launch user's default web browser"
606 // replace it with a generic web request so we can see if there is indeed a default
607 String defaultUri = getString(R.string.default_browser_url);
608 intent = new Intent(
609 Intent.ACTION_VIEW,
610 ((defaultUri != null)
611 ? Uri.parse(defaultUri)
612 : getDefaultBrowserUri())
613 ).addCategory(Intent.CATEGORY_BROWSABLE);
614 // note: if the user launches this without a default set, she
615 // will always be taken to the default URL above; this is
616 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400618 // URL is unavoidably sent to the chosen app.
619 } else {
620 try {
621 intent = Intent.parseUri(mHotseatConfig[i], 0);
622 } catch (java.net.URISyntaxException ex) {
623 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
624 // bogus; leave intent=null
625 }
626 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 if (intent == null) {
629 mHotseats[i] = null;
630 mHotseatLabels[i] = getText(R.string.activity_not_found);
631 continue;
632 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633
634 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700635 Log.d(TAG, "loadHotseats: hotseat " + i
636 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400637 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638 + "]");
639 }
640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
642 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400644 Log.d(TAG, "Best match for intent: " + bestMatch);
645 Log.d(TAG, "All matches: ");
646 for (ResolveInfo ri : allMatches) {
647 Log.d(TAG, " --> " + ri);
648 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400649 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400650 // did this resolve to a single app, or the resolver?
651 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700652 // can't find any activity to handle this. let's leave the
653 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400654 // to launch.
655 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400656
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400657 // set accessibility text to "Not installed"
658 mHotseatLabels[i] = getText(R.string.activity_not_found);
659 } else {
660 boolean found = false;
661 for (ResolveInfo ri : allMatches) {
662 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
663 && bestMatch.activityInfo.applicationInfo.packageName
664 .equals(ri.activityInfo.applicationInfo.packageName)) {
665 found = true;
666 break;
667 }
668 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700669
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400670 if (!found) {
671 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
672 // the bestMatch is probably the ResolveActivity, meaning the
673 // user has not yet selected a default
674 // so: we'll keep the original intent for now
675 mHotseats[i] = intent;
676
677 // set the accessibility text to "Select shortcut"
678 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
679 } else {
680 // we have an app!
681 // now reconstruct the intent to launch it through the front
682 // door
683 ComponentName com = new ComponentName(
684 bestMatch.activityInfo.applicationInfo.packageName,
685 bestMatch.activityInfo.name);
686 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
687
688 // load the app label for accessibility
689 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
690 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400691 }
692
693 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700694 Log.d(TAG, "loadHotseats: hotseat " + i
695 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400696 + ((mHotseats[i] == null)
697 ? "null"
698 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400699 + "] label=[" + mHotseatLabels[i]
700 + "]"
701 );
702 }
703 }
704 }
705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 @Override
707 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700708 mWaitingForResult = false;
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 // The pattern used here is that a user PICKs a specific application,
711 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
714 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700715
Michael Jurka0280c3b2010-09-17 15:00:07 -0700716 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 switch (requestCode) {
718 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700719 completeAddApplication(
720 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 break;
722 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800723 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
725 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 break;
728 case REQUEST_PICK_LIVE_FOLDER:
729 addLiveFolder(data);
730 break;
731 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700732 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800733 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700735 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700737 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700738 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700739 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700741 case REQUEST_PICK_WALLPAPER:
742 // We just wanted the activity result here so we can clear mWaitingForResult
743 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
Romain Guy18042c82009-11-06 11:44:55 -0800745 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
746 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
747 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700748 // Clean up the appWidgetId if we canceled
749 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
750 if (appWidgetId != -1) {
751 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755
756 @Override
757 protected void onResume() {
758 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800759 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700760 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400761 mWorkspaceLoading = true;
762 mModel.startLoader(this, true);
763 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700764 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700766 // When we resume Launcher, a different Activity might be responsible for the app
767 // market intent, so refresh the icon
768 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
770
771 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700772 protected void onPause() {
773 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700774 // Some launcher layouts don't have a previous and next view
775 if (mPreviousView != null) {
776 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700777 }
778 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700779 dismissPreview(mNextView);
780 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700781 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800782 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700783 }
Romain Guycbb89e42009-06-08 15:52:54 -0700784
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700785 @Override
786 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400787 // Flag the loader to stop early before switching
788 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800789 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800790 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700791 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700792
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800793 // We can't hide the IME if it was forced open. So don't bother
794 /*
795 @Override
796 public void onWindowFocusChanged(boolean hasFocus) {
797 super.onWindowFocusChanged(hasFocus);
798
799 if (hasFocus) {
800 final InputMethodManager inputManager = (InputMethodManager)
801 getSystemService(Context.INPUT_METHOD_SERVICE);
802 WindowManager.LayoutParams lp = getWindow().getAttributes();
803 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
804 android.os.Handler()) {
805 protected void onReceiveResult(int resultCode, Bundle resultData) {
806 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
807 }
808 });
809 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
810 }
811 }
812 */
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 private boolean acceptFilter() {
815 final InputMethodManager inputManager = (InputMethodManager)
816 getSystemService(Context.INPUT_METHOD_SERVICE);
817 return !inputManager.isFullscreenMode();
818 }
819
820 @Override
821 public boolean onKeyDown(int keyCode, KeyEvent event) {
822 boolean handled = super.onKeyDown(keyCode, event);
823 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
824 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
825 keyCode, event);
826 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700827 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700828 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700829 // showSearchDialog()
830 // If there are multiple keystrokes before the search dialog takes focus,
831 // onSearchRequested() will be called for every keystroke,
832 // but it is idempotent, so it's fine.
833 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
835 }
836
Joe Onorato8a9625e2010-01-28 15:55:35 -0800837 // Eat the long press event so the keyboard doesn't come up.
838 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
839 return true;
840 }
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 return handled;
843 }
844
Karl Rosaen138a0412009-04-23 19:00:21 -0700845 private String getTypedText() {
846 return mDefaultKeySsb.toString();
847 }
848
849 private void clearTypedText() {
850 mDefaultKeySsb.clear();
851 mDefaultKeySsb.clearSpans();
852 Selection.setSelection(mDefaultKeySsb, 0);
853 }
854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700856 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
857 * State
858 */
859 private static State intToState(int stateOrdinal) {
860 State state = State.WORKSPACE;
861 final State[] stateValues = State.values();
862 for (int i = 0; i < stateValues.length; i++) {
863 if (stateValues[i].ordinal() == stateOrdinal) {
864 state = stateValues[i];
865 break;
866 }
867 }
868 return state;
869 }
870
871 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 * Restores the previous state, if it exists.
873 *
874 * @param savedState The previous state.
875 */
876 private void restoreState(Bundle savedState) {
877 if (savedState == null) {
878 return;
879 }
880
Michael Jurka883f55b2010-10-21 15:47:14 -0700881 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
882
883 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800884 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700885 } else if (state == State.CUSTOMIZE) {
886 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800887 }
888
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
890 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700891 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800892 }
893
894 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700897 mAddScreen = addScreen;
898 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
899 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 mRestoring = true;
901 }
902
903 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
904 if (renameFolder) {
905 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700906 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 mRestoring = true;
908 }
Winson Chunga12a2502010-12-20 14:41:35 -0800909
910 // Restore the current AllApps drawer tab
911 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
912 String curTab = savedState.getString("allapps_currentTab");
913 if (curTab != null) {
914 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
915 tabhost.setCurrentTabByTag(curTab);
916 }
917 int curPage = savedState.getInt("allapps_currentPage", -1);
918 if (curPage > -1) {
919 mAllAppsPagedView.setRestorePage(curPage);
920 }
921 }
922
923 // Restore the current customization drawer tab
924 if (mHomeCustomizationDrawer != null) {
925 String curTab = savedState.getString("customize_currentTab");
926 if (curTab != null) {
927 // We set this directly so that there is no delay before the tab is set
928 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
929 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
930 }
931
932 // Note: currently we do not restore the page for the customization tray because unlike
933 // AllApps, the page content can change drastically
934 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 }
936
937 /**
938 * Finds all the views we need and configure them properly.
939 */
940 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700941 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400942
Romain Guyb1b69f52009-08-10 15:10:15 -0700943 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400944 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945
Joe Onorato7c312c12009-08-13 21:36:53 -0700946 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700947 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700948 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800949 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700950 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700951 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700952
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700953 if (LauncherApplication.isScreenXLarge()) {
954 // They need to be INVISIBLE initially so that they will be measured in the layout.
955 // Otherwise the animations are messed up when we show them for the first time.
956 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
957 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700961
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500963 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700964
Joe Onorato7c312c12009-08-13 21:36:53 -0700965 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
966 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700968 View handleView = findViewById(R.id.all_apps_button);
969 if (handleView != null && handleView instanceof HandleView) {
970 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700971 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700972 mHandleView.setLauncher(this);
973 mHandleView.setOnClickListener(this);
974 mHandleView.setOnLongClickListener(this);
975 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800976
Winson Chung80baf5a2010-08-09 16:03:15 -0700977 if (mCustomizePagedView != null) {
978 mCustomizePagedView.setLauncher(this);
979 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700980 } else {
981 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
982 hotseatLeft.setContentDescription(mHotseatLabels[0]);
983 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
984 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
985 hotseatRight.setContentDescription(mHotseatLabels[1]);
986 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400987
Michael Jurkaaf442092010-06-10 17:01:57 -0700988 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
989 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800990
Michael Jurkaaf442092010-06-10 17:01:57 -0700991 Drawable previous = mPreviousView.getDrawable();
992 Drawable next = mNextView.getDrawable();
993 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994
Michael Jurkaaf442092010-06-10 17:01:57 -0700995 mPreviousView.setHapticFeedbackEnabled(false);
996 mPreviousView.setOnLongClickListener(this);
997 mNextView.setHapticFeedbackEnabled(false);
998 mNextView.setOnLongClickListener(this);
999 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001002 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004
1005 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -04001006 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -07001007 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -08001008
1009 final View allAppsButton = findViewById(R.id.all_apps_button);
1010 final View divider = findViewById(R.id.divider);
1011 final View configureButton = findViewById(R.id.configure_button);
1012
Michael Jurka54f7ac32010-08-02 13:56:46 -07001013 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001014 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001015 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001016 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001017 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001018 dragController.addDragListener(deleteZone);
1019
Adam Cohencdc30d52010-12-01 15:09:47 -08001020 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1021 if (allAppsDeleteZone != null) {
1022 allAppsDeleteZone.setLauncher(this);
1023 allAppsDeleteZone.setDragController(dragController);
1024 allAppsDeleteZone.setDragAndDropEnabled(false);
1025 dragController.addDragListener(allAppsDeleteZone);
1026 dragController.addDropTarget(allAppsDeleteZone);
1027 }
1028
1029 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1030 findViewById(R.id.all_apps_info_target);
1031 if (allAppsInfoTarget != null) {
1032 allAppsInfoTarget.setLauncher(this);
1033 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001034 allAppsInfoTarget.setDragAndDropEnabled(false);
1035 View marketButton = findViewById(R.id.market_button);
1036 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001037 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001038 }
1039 }
1040
Joe Onorato00acb122009-08-04 16:04:30 -04001041 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001042 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001043 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001044
Joe Onorato00acb122009-08-04 16:04:30 -04001045 // The order here is bottom to top.
1046 dragController.addDropTarget(workspace);
1047 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001048 if (allAppsInfoTarget != null) {
1049 dragController.addDropTarget(allAppsInfoTarget);
1050 }
1051 if (allAppsDeleteZone != null) {
1052 dragController.addDropTarget(allAppsDeleteZone);
1053 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 }
1055
Romain Guy8a73c512009-11-09 19:19:59 -08001056 @SuppressWarnings({"UnusedDeclaration"})
1057 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001058 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001059 mWorkspace.scrollLeft();
1060 }
Romain Guy8a73c512009-11-09 19:19:59 -08001061 }
1062
1063 @SuppressWarnings({"UnusedDeclaration"})
1064 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001065 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001066 mWorkspace.scrollRight();
1067 }
Romain Guy8a73c512009-11-09 19:19:59 -08001068 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001069
1070 @SuppressWarnings({"UnusedDeclaration"})
1071 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001072 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001073
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001074 int index = -1;
1075 if (v.getId() == R.id.hotseat_left) {
1076 index = 0;
1077 } else if (v.getId() == R.id.hotseat_right) {
1078 index = 1;
1079 }
1080
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001081 // reload these every tap; you never know when they might change
1082 loadHotseats();
1083 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1084 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001085 startActivitySafely(
1086 mHotseats[index],
1087 "hotseat"
1088 );
1089 }
1090 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001091
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 /**
1093 * Creates a view representing a shortcut.
1094 *
1095 * @param info The data structure describing the shortcut.
1096 *
1097 * @return A View inflated from R.layout.application.
1098 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001099 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001101 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103
1104 /**
1105 * Creates a view representing a shortcut inflated from the specified resource.
1106 *
1107 * @param layoutResId The id of the XML layout used to create the shortcut.
1108 * @param parent The group the shortcut belongs to.
1109 * @param info The data structure describing the shortcut.
1110 *
1111 * @return A View inflated from layoutResId.
1112 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001113 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001114 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1115 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 return favorite;
1118 }
1119
1120 /**
1121 * Add an application shortcut to the workspace.
1122 *
1123 * @param data The intent describing the application.
1124 * @param cellInfo The position on screen where to create the shortcut.
1125 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001126 void completeAddApplication(Context context, Intent data, int screen,
1127 int intersectCellX, int intersectCellY) {
1128 final int[] cellXY = mTmpAddItemCellCoordinates;
1129 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1130
1131 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1132 showOutOfSpaceMessage();
1133 return;
1134 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135
Joe Onorato0589f0f2010-02-08 13:44:00 -08001136 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1137 data, context);
1138
Romain Guy73b979d2009-06-09 12:57:21 -07001139 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001140 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001142 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1144 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001145 } else {
1146 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
1148 }
Romain Guycbb89e42009-06-08 15:52:54 -07001149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 /**
1151 * Add a shortcut to the workspace.
1152 *
1153 * @param data The intent describing the shortcut.
1154 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 private void completeAddShortcut(Intent data, int screen,
1157 int intersectCellX, int intersectCellY) {
1158 final int[] cellXY = mTmpAddItemCellCoordinates;
1159 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001160
Michael Jurkad3ef3062010-11-23 16:23:58 -08001161 int[] touchXY = null;
1162 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1163 touchXY = mAddDropPosition;
1164 }
1165 boolean foundCellSpan = false;
1166 if (touchXY != null) {
1167 // when dragging and dropping, just find the closest free spot
1168 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1169 int[] result = screenLayout.findNearestVacantArea(
1170 touchXY[0], touchXY[1], 1, 1, cellXY);
1171 foundCellSpan = (result != null);
1172 } else {
1173 foundCellSpan = layout.findCellForSpanThatIntersects(
1174 cellXY, 1, 1, intersectCellX, intersectCellY);
1175 }
1176
1177 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001178 showOutOfSpaceMessage();
1179 return;
1180 }
1181
1182 final ShortcutInfo info = mModel.addShortcut(
1183 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
1185 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189 }
1190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001194 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001195 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001198 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001199
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001200 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001201 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1202 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001205 // We have saved the position to which the widget was dragged-- this really only matters
1206 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001208
1209 // For now, we don't save the coordinate where we dropped the icon because we're not
1210 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1211 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001212 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001213 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001214 touchXY = mAddDropPosition;
1215 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 boolean foundCellSpan = false;
1217 if (touchXY != null) {
1218 // when dragging and dropping, just find the closest free spot
1219 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1220 int[] result = screenLayout.findNearestVacantArea(
1221 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001222 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001224 // if we long pressed on an empty cell to bring up a menu,
1225 // make sure we intersect the empty cell
1226 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1227 // findCellForSpanThatIntersects will just ignore them
1228 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1229 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001230 }
1231
Michael Jurka0280c3b2010-09-17 15:00:07 -07001232 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001233 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001235 return;
1236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 // Build Launcher-specific widget info and save to database
1239 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 launcherInfo.spanX = spanXY[0];
1241 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001242
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 LauncherModel.addItemToDatabase(this, launcherInfo,
1244 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001245 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001248 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001251 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001254 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001255
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001257 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001258
1259 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261 }
Romain Guycbb89e42009-06-08 15:52:54 -07001262
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1264 @Override
1265 public void onReceive(Context context, Intent intent) {
1266 final String action = intent.getAction();
1267 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1268 mUserPresent = false;
1269 updateRunning();
1270 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1271 mUserPresent = true;
1272 updateRunning();
1273 }
1274 }
1275 };
1276
1277 @Override
1278 public void onAttachedToWindow() {
1279 super.onAttachedToWindow();
1280
1281 // Listen for broadcasts related to user-presence
1282 final IntentFilter filter = new IntentFilter();
1283 filter.addAction(Intent.ACTION_SCREEN_OFF);
1284 filter.addAction(Intent.ACTION_USER_PRESENT);
1285 registerReceiver(mReceiver, filter);
1286
Adam Cohend113e0c2010-11-11 10:48:05 -08001287 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 mVisible = true;
1289 }
1290
1291 @Override
1292 public void onDetachedFromWindow() {
1293 super.onDetachedFromWindow();
1294 mVisible = false;
1295
Adam Cohend113e0c2010-11-11 10:48:05 -08001296 if (mAttached) {
1297 unregisterReceiver(mReceiver);
1298 mAttached = false;
1299 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001300 updateRunning();
1301 }
1302
1303 public void onWindowVisibilityChanged(int visibility) {
1304 mVisible = visibility == View.VISIBLE;
1305 updateRunning();
1306 }
1307
1308 private void sendAdvanceMessage(long delay) {
1309 mHandler.removeMessages(ADVANCE_MSG);
1310 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1311 mHandler.sendMessageDelayed(msg, delay);
1312 mAutoAdvanceSentTime = System.currentTimeMillis();
1313 }
1314
1315 private void updateRunning() {
1316 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1317 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1318 mAutoAdvanceRunning = autoAdvanceRunning;
1319 if (autoAdvanceRunning) {
1320 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1321 sendAdvanceMessage(delay);
1322 } else {
1323 if (!mWidgetsToAdvance.isEmpty()) {
1324 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1325 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1326 }
1327 mHandler.removeMessages(ADVANCE_MSG);
1328 }
1329 }
1330 }
1331
1332 private final Handler mHandler = new Handler() {
1333 @Override
1334 public void handleMessage(Message msg) {
1335 if (msg.what == ADVANCE_MSG) {
1336 int i = 0;
1337 for (View key: mWidgetsToAdvance.keySet()) {
1338 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1339 final int delay = mAdvanceStagger * i;
1340 if (v instanceof Advanceable) {
1341 postDelayed(new Runnable() {
1342 public void run() {
1343 ((Advanceable) v).advance();
1344 }
1345 }, delay);
1346 }
1347 i++;
1348 }
1349 sendAdvanceMessage(mAdvanceInterval);
1350 }
1351 }
1352 };
1353
1354 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1355 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1356 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1357 if (v instanceof Advanceable) {
1358 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1359 ((Advanceable) v).willBeAdvancedByHost();
1360 updateRunning();
1361 }
1362 }
1363
1364 void removeWidgetToAutoAdvance(View hostView) {
1365 if (mWidgetsToAdvance.containsKey(hostView)) {
1366 mWidgetsToAdvance.remove(hostView);
1367 updateRunning();
1368 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369 }
1370
Joe Onorato9c1289c2009-08-17 11:03:03 -04001371 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1372 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001373 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 launcherInfo.hostView = null;
1375 }
1376
Adam Cohended9f8d2010-11-03 13:25:16 -07001377 void showOutOfSpaceMessage() {
1378 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1379 }
1380
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001381 public LauncherAppWidgetHost getAppWidgetHost() {
1382 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
Romain Guycbb89e42009-06-08 15:52:54 -07001384
Winson Chunga9abd0e2010-10-27 17:18:37 -07001385 public LauncherModel getModel() {
1386 return mModel;
1387 }
1388
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001389 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001390 getWindow().closeAllPanels();
1391
1392 try {
1393 dismissDialog(DIALOG_CREATE_SHORTCUT);
1394 // Unlock the workspace if the dialog was showing
1395 } catch (Exception e) {
1396 // An exception is thrown if the dialog is not visible, which is fine
1397 }
1398
1399 try {
1400 dismissDialog(DIALOG_RENAME_FOLDER);
1401 // Unlock the workspace if the dialog was showing
1402 } catch (Exception e) {
1403 // An exception is thrown if the dialog is not visible, which is fine
1404 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001405
1406 // Whatever we were doing is hereby canceled.
1407 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001408 }
1409
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 @Override
1411 protected void onNewIntent(Intent intent) {
1412 super.onNewIntent(intent);
1413
1414 // Close the menu
1415 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001416 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001417 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001418
Joe Onorato14f122b2009-11-19 14:06:36 -08001419 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1420 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001421
Michael Jurka4cb37242010-08-09 21:05:32 -07001422 // in all these cases, only animate if we're already on home
1423 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001424 mWorkspace.unshrink(alreadyOnHome);
1425 }
1426 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001427 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001428 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1429 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001430 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001431 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001432
Joe Onorato3a8820b2009-11-10 15:06:42 -08001433 final View v = getWindow().peekDecorView();
1434 if (v != null && v.getWindowToken() != null) {
1435 InputMethodManager imm = (InputMethodManager)getSystemService(
1436 INPUT_METHOD_SERVICE);
1437 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439 }
1440 }
1441
1442 @Override
1443 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1444 // Do not call super here
1445 mSavedInstanceState = savedInstanceState;
1446 }
1447
1448 @Override
1449 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001450 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
1452 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1453 if (folders.size() > 0) {
1454 final int count = folders.size();
1455 long[] ids = new long[count];
1456 for (int i = 0; i < count; i++) {
1457 final FolderInfo info = folders.get(i).getInfo();
1458 ids[i] = info.id;
1459 }
1460 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1461 } else {
1462 super.onSaveInstanceState(outState);
1463 }
1464
Michael Jurka883f55b2010-10-21 15:47:14 -07001465 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466
Michael Jurka0280c3b2010-09-17 15:00:07 -07001467 if (mAddScreen > -1 && mWaitingForResult) {
1468 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1469 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1470 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 }
1472
1473 if (mFolderInfo != null && mWaitingForResult) {
1474 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1475 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1476 }
Michael Jurka946ad472010-07-09 18:05:18 -07001477
Winson Chung43b119d2010-12-06 16:45:05 -08001478 // Save the current AllApps drawer tab
1479 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1480 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1481 String currentTabTag = tabhost.getCurrentTabTag();
1482 if (currentTabTag != null) {
1483 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001484 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001485 }
1486 }
1487
1488 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001489 if (mHomeCustomizationDrawer != null) {
1490 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1491 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001492 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001493 }
1494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 @Override
1498 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001502 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001504 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 }
1506
1507 TextKeyListener.getInstance().release();
1508
Joe Onorato9c1289c2009-08-17 11:03:03 -04001509 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510
Joe Onorato9c1289c2009-08-17 11:03:03 -04001511 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001512
1513 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001514
Patrick Dubroyab962b72010-07-26 12:10:10 -07001515 // Some launcher layouts don't have a previous and next view
1516 if (mPreviousView != null) {
1517 dismissPreview(mPreviousView);
1518 }
1519 if (mNextView != null) {
1520 dismissPreview(mNextView);
1521 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001522
1523 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 }
1525
1526 @Override
1527 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001528 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 super.startActivityForResult(intent, requestCode);
1530 }
1531
1532 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001533 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001535
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001536 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Karl Rosaen138a0412009-04-23 19:00:21 -07001538 if (initialQuery == null) {
1539 // Use any text typed in the launcher as the initial query
1540 initialQuery = getTypedText();
1541 clearTypedText();
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 if (appSearchData == null) {
1544 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001545 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Romain Guycbb89e42009-06-08 15:52:54 -07001547
Karl Rosaen138a0412009-04-23 19:00:21 -07001548 final SearchManager searchManager =
1549 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001550 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001551 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
1554 @Override
1555 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001556 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557 return false;
1558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1563 .setIcon(android.R.drawable.ic_menu_add)
1564 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001565 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1566 .setIcon(android.R.drawable.ic_menu_manage)
1567 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001568 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 .setIcon(android.R.drawable.ic_menu_gallery)
1570 .setAlphabeticShortcut('W');
1571 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1572 .setIcon(android.R.drawable.ic_search_category_default)
1573 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1574 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1575 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1576 .setAlphabeticShortcut('N');
1577
1578 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001579 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1580 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581
1582 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1583 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1584 .setIntent(settings);
1585
1586 return true;
1587 }
1588
1589 @Override
1590 public boolean onPrepareOptionsMenu(Menu menu) {
1591 super.onPrepareOptionsMenu(menu);
1592
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001593 // If all apps is animating, don't show the menu, because we don't know
1594 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001595 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001596 return false;
1597 }
1598
1599 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001600 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001601 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1602 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1603
1604 // Disable add if the workspace is full.
1605 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1607 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001608 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
1610 return true;
1611 }
1612
1613 @Override
1614 public boolean onOptionsItemSelected(MenuItem item) {
1615 switch (item.getItemId()) {
1616 case MENU_ADD:
1617 addItems();
1618 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001619 case MENU_MANAGE_APPS:
1620 manageApps();
1621 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 case MENU_WALLPAPER_SETTINGS:
1623 startWallpaper();
1624 return true;
1625 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001626 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 return true;
1628 case MENU_NOTIFICATIONS:
1629 showNotifications();
1630 return true;
1631 }
1632
1633 return super.onOptionsItemSelected(item);
1634 }
Romain Guycbb89e42009-06-08 15:52:54 -07001635
Karl Rosaen138a0412009-04-23 19:00:21 -07001636 /**
1637 * Indicates that we want global search for this activity by setting the globalSearch
1638 * argument for {@link #startSearch} to true.
1639 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001641 @Override
1642 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001643 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001644 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001645 }
1646
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647 public boolean isWorkspaceLocked() {
1648 return mWorkspaceLoading || mWaitingForResult;
1649 }
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001652 if (LauncherApplication.isScreenXLarge()) {
1653 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001654 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001655 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001656 }
1657 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001658 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001659 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
1662
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 private void resetAddInfo() {
1664 mAddScreen = -1;
1665 mAddIntersectCellX = -1;
1666 mAddIntersectCellY = -1;
1667 mAddDropPosition = null;
1668 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001669
Winson Chung55cef262010-10-28 14:14:18 -07001670 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001671 resetAddInfo();
1672 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673 mAddDropPosition = position;
1674
Michael Jurkaaf442092010-06-10 17:01:57 -07001675 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001676 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1677 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001678 }
1679
Winson Chung7ad01412010-09-27 11:33:03 -07001680 private void manageApps() {
1681 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1682 }
1683
Michael Jurkaaf442092010-06-10 17:01:57 -07001684 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001685 // TODO: catch bad widget exception when sent
1686 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001687 // TODO: Is this log message meaningful?
1688 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001689 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 }
1691
Winson Chung55cef262010-10-28 14:14:18 -07001692 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001693 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694
Bjorn Bringert7984c942009-12-09 15:38:25 +00001695 if (appWidget.configure != null) {
1696 // Launch over to configure widget, if needed
1697 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1698 intent.setComponent(appWidget.configure);
1699 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001700 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001701 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1702 intent.putExtra(
1703 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1704 info.mimeType);
1705
1706 final String mimeType = info.mimeType;
1707 final ClipData clipData = (ClipData) info.configurationData;
1708 final ClipDescription clipDesc = clipData.getDescription();
1709 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1710 if (clipDesc.getMimeType(i).equals(mimeType)) {
1711 final ClipData.Item item = clipData.getItem(i);
1712 final CharSequence stringData = item.getText();
1713 final Uri uriData = item.getUri();
1714 final Intent intentData = item.getIntent();
1715 final String key =
1716 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1717 if (uriData != null) {
1718 intent.putExtra(key, uriData);
1719 } else if (intentData != null) {
1720 intent.putExtra(key, intentData);
1721 } else if (stringData != null) {
1722 intent.putExtra(key, stringData);
1723 }
1724 break;
1725 }
1726 }
1727 }
Winson Chung55cef262010-10-28 14:14:18 -07001728 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001729
Romain Guy8e633c52010-03-04 12:51:36 -08001730 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001732 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001733 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
1735 }
Romain Guycbb89e42009-06-08 15:52:54 -07001736
Michael Jurka0280c3b2010-09-17 15:00:07 -07001737 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1738 resetAddInfo();
1739 mAddScreen = screen;
1740 mAddDropPosition = position;
1741
1742 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1743 createShortcutIntent.setComponent(componentName);
1744 processShortcut(createShortcutIntent);
1745 }
1746
Joe Onoratodeb98af2010-02-19 14:59:39 -08001747 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001748 // Handle case where user selected "Applications"
1749 String applicationName = getResources().getString(R.string.group_applications);
1750 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001751
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 if (applicationName != null && applicationName.equals(shortcutName)) {
1753 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1754 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1757 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001758 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001759 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001761 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 }
1764
Winson Chung24ab2f12010-09-16 14:10:47 -07001765 void processWallpaper(Intent intent) {
1766 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1767 }
1768
Michael Jurka0280c3b2010-09-17 15:00:07 -07001769 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1770 resetAddInfo();
1771 mAddScreen = screen;
1772 mAddDropPosition = position;
1773
1774 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1775 createFolderIntent.setComponent(componentName);
1776
1777 addLiveFolder(createFolderIntent);
1778 }
1779
1780 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001781 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001782 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001783 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001785 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001786 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001787 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001788 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
1791
Michael Jurka0280c3b2010-09-17 15:00:07 -07001792 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 UserFolderInfo folderInfo = new UserFolderInfo();
1794 folderInfo.title = getText(R.string.folder_name);
1795
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1797 final int[] cellXY = mTmpAddItemCellCoordinates;
1798 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1799 showOutOfSpaceMessage();
1800 return;
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802
1803 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 LauncherModel.addItemToDatabase(this, folderInfo,
1805 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001807 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808
1809 // Create the view
1810 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001811 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1812 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001817 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 }
1819
Michael Jurka28750fb2010-09-24 17:43:49 -07001820 private void completeAddLiveFolder(
1821 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1823 final int[] cellXY = mTmpAddItemCellCoordinates;
1824 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1825 showOutOfSpaceMessage();
1826 return;
1827 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830
1831 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001833 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 }
1836 }
1837
1838 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001839 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840
1841 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1842 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1843
1844 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 Intent.ShortcutIconResource iconResource = null;
1846
1847 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1848 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1849 try {
1850 iconResource = (Intent.ShortcutIconResource) extra;
1851 final PackageManager packageManager = context.getPackageManager();
1852 Resources resources = packageManager.getResourcesForApplication(
1853 iconResource.packageName);
1854 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1855 icon = resources.getDrawable(id);
1856 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001857 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
1859 }
1860
1861 if (icon == null) {
1862 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1863 }
1864
1865 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001866 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 info.title = name;
1868 info.iconResource = iconResource;
1869 info.uri = data.getData();
1870 info.baseIntent = baseIntent;
1871 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1872 LiveFolders.DISPLAY_MODE_GRID);
1873
1874 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001875 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001876 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877
1878 return info;
1879 }
1880
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 private void showNotifications() {
1882 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1883 if (statusBar != null) {
1884 statusBar.expand();
1885 }
1886 }
1887
1888 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001889 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001891 Intent chooser = Intent.createChooser(pickWallpaper,
1892 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001893 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1894 // Removed in Eclair MR1
1895// WallpaperManager wm = (WallpaperManager)
1896// getSystemService(Context.WALLPAPER_SERVICE);
1897// WallpaperInfo wi = wm.getWallpaperInfo();
1898// if (wi != null && wi.getSettingsActivity() != null) {
1899// LabeledIntent li = new LabeledIntent(getPackageName(),
1900// R.string.configure_wallpaper, 0);
1901// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1902// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1903// }
Mike Clerona0618e42009-10-22 13:55:21 -07001904 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 }
1906
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001907 /**
1908 * Registers various content observers. The current implementation registers
1909 * only a favorites observer to keep track of the favorites applications.
1910 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001911 private void registerContentObservers() {
1912 ContentResolver resolver = getContentResolver();
1913 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1914 true, mWidgetObserver);
1915 }
1916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 @Override
1918 public boolean dispatchKeyEvent(KeyEvent event) {
1919 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1920 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001922 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001923 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001924 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001925 dumpState();
1926 return true;
1927 }
1928 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001929 }
1930 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1931 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001932 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 return true;
1934 }
1935 }
1936
1937 return super.dispatchKeyEvent(event);
1938 }
1939
Joe Onorato88ec0992009-11-19 13:16:06 -08001940 @Override
1941 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001942 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1943 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001944 } else {
1945 closeFolder();
1946 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001947 // Some launcher layouts don't have a previous and next view
1948 if (mPreviousView != null) {
1949 dismissPreview(mPreviousView);
1950 dismissPreview(mNextView);
1951 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 private void closeFolder() {
1955 Folder folder = mWorkspace.getOpenFolder();
1956 if (folder != null) {
1957 closeFolder(folder);
1958 }
1959 }
1960
1961 void closeFolder(Folder folder) {
1962 folder.getInfo().opened = false;
1963 ViewGroup parent = (ViewGroup) folder.getParent();
1964 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001965 CellLayout cl = (CellLayout) parent;
1966 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001967 if (folder instanceof DropTarget) {
1968 // Live folders aren't DropTargets.
1969 mDragController.removeDropTarget((DropTarget)folder);
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972 folder.onClose();
1973 }
1974
1975 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001976 * Re-listen when widgets are reset.
1977 */
1978 private void onAppWidgetReset() {
1979 mAppWidgetHost.startListening();
1980 }
1981
1982 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001983 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1984 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001986 private void unbindDesktopItems() {
1987 for (ItemInfo item: mDesktopItems) {
1988 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 }
1990 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001991
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 /**
1993 * Launches the intent referred by the clicked shortcut.
1994 *
1995 * @param v The view representing the clicked shortcut.
1996 */
1997 public void onClick(View v) {
1998 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001999 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002001 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002002 int[] pos = new int[2];
2003 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002004 intent.setSourceBounds(new Rect(pos[0], pos[1],
2005 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07002006 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 } else if (tag instanceof FolderInfo) {
2008 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002009 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002010 if (mState == State.ALL_APPS) {
2011 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002012 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002013 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016 }
2017
Michael Jurka0e260592010-06-30 17:07:39 -07002018 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002019 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002020 // clicking anywhere on the workspace causes the customization drawer to slide down
2021 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002022 return false;
2023 }
2024
Michael Jurkaaf442092010-06-10 17:01:57 -07002025 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002026 * Event handler for the search button
2027 *
2028 * @param v The view that was clicked.
2029 */
2030 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002031 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002032 // Use a custom animation for launching search
2033 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002034 }
2035
2036 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002037 * Event handler for the voice button
2038 *
2039 * @param v The view that was clicked.
2040 */
2041 public void onClickVoiceButton(View v) {
2042 startVoiceSearch();
2043 }
2044
2045 private void startVoiceSearch() {
2046 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2047 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2048 startActivity(intent);
2049 }
2050
2051 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002052 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002053 * enters home screen customization mode.
2054 *
2055 * @param v The view that was clicked.
2056 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002057 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002058 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002059 }
2060
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002061 /**
2062 * Event handler for the "grid" button that appears on the home screen, which
2063 * enters all apps mode.
2064 *
2065 * @param v The view that was clicked.
2066 */
2067 public void onClickAllAppsButton(View v) {
2068 showAllApps(true);
2069 }
2070
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002071 public void onClickAppMarketButton(View v) {
2072 if (mAppMarketIntent != null) {
2073 startActivitySafely(mAppMarketIntent, "app market");
2074 }
2075 }
2076
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002077 void startApplicationDetailsActivity(ComponentName componentName) {
2078 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002079 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2080 Uri.fromParts("package", packageName, null));
2081 startActivity(intent);
2082 }
2083
Patrick Dubroy5539af72010-09-07 15:22:01 -07002084 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2085 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2086 // System applications cannot be installed. For now, show a toast explaining that.
2087 // We may give them the option of disabling apps this way.
2088 int messageId = R.string.uninstall_system_app_text;
2089 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2090 } else {
2091 String packageName = appInfo.componentName.getPackageName();
2092 String className = appInfo.componentName.getClassName();
2093 Intent intent = new Intent(
2094 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2095 startActivity(intent);
2096 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002097 }
2098
Joe Onoratof984e852010-03-25 09:47:45 -07002099 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2101 try {
2102 startActivity(intent);
2103 } catch (ActivityNotFoundException e) {
2104 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002105 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 } catch (SecurityException e) {
2107 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002108 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002110 "or use the exported attribute for this activity. "
2111 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002114
Romain Guy8e633c52010-03-04 12:51:36 -08002115 void startActivityForResultSafely(Intent intent, int requestCode) {
2116 try {
2117 startActivityForResult(intent, requestCode);
2118 } catch (ActivityNotFoundException e) {
2119 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2120 } catch (SecurityException e) {
2121 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2122 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2123 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2124 "or use the exported attribute for this activity.", e);
2125 }
2126 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127
2128 private void handleFolderClick(FolderInfo folderInfo) {
2129 if (!folderInfo.opened) {
2130 // Close any open folder
2131 closeFolder();
2132 // Open the requested folder
2133 openFolder(folderInfo);
2134 } else {
2135 // Find the open folder...
2136 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2137 int folderScreen;
2138 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002139 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 // .. and close it
2141 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002142 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 // Close any folder open on the current screen
2144 closeFolder();
2145 // Pull the folder onto this screen
2146 openFolder(folderInfo);
2147 }
2148 }
2149 }
2150 }
2151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002153 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 * is animated relative to the specified View. If the View is null, no animation
2155 * is played.
2156 *
2157 * @param folderInfo The FolderInfo describing the folder to open.
2158 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002159 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 Folder openFolder;
2161
2162 if (folderInfo instanceof UserFolderInfo) {
2163 openFolder = UserFolder.fromXml(this);
2164 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002165 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 } else {
2167 return;
2168 }
2169
Joe Onorato00acb122009-08-04 16:04:30 -04002170 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 openFolder.setLauncher(this);
2172
2173 openFolder.bind(folderInfo);
2174 folderInfo.opened = true;
2175
Winson Chungaafa03c2010-06-11 17:34:16 -07002176 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 openFolder.onOpen();
2179 }
2180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002182 switch (v.getId()) {
2183 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002184 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002185 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2186 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002187 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002188 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002189 return true;
2190 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002191 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002192 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2193 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002194 showPreviews(v);
2195 }
2196 return true;
2197 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002198 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2201 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002202 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002203 return true;
2204 }
2205
Joe Onorato9c1289c2009-08-17 11:03:03 -04002206 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 return false;
2208 }
2209
2210 if (!(v instanceof CellLayout)) {
2211 v = (View) v.getParent();
2212 }
2213
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 resetAddInfo();
2216 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 return true;
2220 }
2221
Michael Jurka0280c3b2010-09-17 15:00:07 -07002222 final View itemUnderLongClick = longClickCellInfo.cell;
2223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002225 if (itemUnderLongClick == null) {
2226 // User long pressed on empty space
2227 mWorkspace.setAllowLongPress(false);
2228 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2229 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002230 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002231 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002234 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002236 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2237 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 mAddIntersectCellX = longClickCellInfo.cellX;
2239 mAddIntersectCellY = longClickCellInfo.cellY;
2240 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 }
2242 }
2243 }
2244 return true;
2245 }
2246
Romain Guye6b8e2f2009-11-10 11:56:55 -08002247 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 private void dismissPreview(final View v) {
2249 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002250 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002251 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2252 public void onDismiss() {
2253 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2254 int count = group.getChildCount();
2255 for (int i = 0; i < count; i++) {
2256 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2257 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002258 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2259 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2260
2261 v.setTag(R.id.workspace, null);
2262 v.setTag(R.id.icon, null);
2263 window.setOnDismissListener(null);
2264 }
2265 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002266 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002267 }
2268 v.setTag(null);
2269 }
2270
Romain Guyf8e6a802009-12-07 17:48:02 -08002271 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002272 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002273 }
2274
Romain Guya6abce82009-11-10 02:54:41 -08002275 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002276 final Resources resources = getResources();
2277 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002278
Romain Guya6abce82009-11-10 02:54:41 -08002279 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002280
Romain Guy9d31e9f2009-11-11 18:54:28 -08002281 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002282
Romain Guyf8e6a802009-12-07 17:48:02 -08002283 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002284 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002285 int extraW = (int) ((r.left + r.right) * max);
2286 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002287
2288 int aW = cell.getWidth() - extraW;
2289 float w = aW / max;
2290
2291 int width = cell.getWidth();
2292 int height = cell.getHeight();
2293 int x = cell.getLeftPadding();
2294 int y = cell.getTopPadding();
2295 width -= (x + cell.getRightPadding());
2296 height -= (y + cell.getBottomPadding());
2297
2298 float scale = w / width;
2299
2300 int count = end - start;
2301
2302 final float sWidth = width * scale;
2303 float sHeight = height * scale;
2304
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002306
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2308 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002309
2310 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002312 cell = (CellLayout) workspace.getChildAt(i);
2313
Romain Guyf8e6a802009-12-07 17:48:02 -08002314 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002315 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002316
2317 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002318 c.scale(scale, scale);
2319 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002320 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002321
Romain Guy9d31e9f2009-11-11 18:54:28 -08002322 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002323 image.setImageBitmap(bitmap);
2324 image.setTag(i);
2325 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 image.setOnFocusChangeListener(handler);
2327 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002328 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002329
2330 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002331 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2332
Winson Chungaafa03c2010-06-11 17:34:16 -07002333 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002334 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002335
2336 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002337 p.setContentView(preview);
2338 p.setWidth((int) (sWidth * count + extraW));
2339 p.setHeight((int) (sHeight + extraH));
2340 p.setAnimationStyle(R.style.AnimationPreview);
2341 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002343 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002344 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002345
Romain Guye6b8e2f2009-11-10 11:56:55 -08002346 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2347 public void onDismiss() {
2348 dismissPreview(anchor);
2349 }
2350 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002351
2352 anchor.setTag(p);
2353 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002354 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002355 }
2356
Romain Guy9d31e9f2009-11-11 18:54:28 -08002357 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002358 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002359
Romain Guye6b8e2f2009-11-10 11:56:55 -08002360 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002361 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002362 }
2363
2364 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002365 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002366 v.post(this);
2367 }
2368
2369 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002370 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002371 }
2372
2373 public void onFocusChange(View v, boolean hasFocus) {
2374 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002375 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002376 }
Romain Guya6abce82009-11-10 02:54:41 -08002377 }
2378 }
2379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 Workspace getWorkspace() {
2381 return mWorkspace;
2382 }
2383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 @Override
2385 protected Dialog onCreateDialog(int id) {
2386 switch (id) {
2387 case DIALOG_CREATE_SHORTCUT:
2388 return new CreateShortcut().createDialog();
2389 case DIALOG_RENAME_FOLDER:
2390 return new RenameFolder().createDialog();
2391 }
2392
2393 return super.onCreateDialog(id);
2394 }
2395
2396 @Override
2397 protected void onPrepareDialog(int id, Dialog dialog) {
2398 switch (id) {
2399 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002400 break;
2401 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002402 if (mFolderInfo != null) {
2403 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2404 final CharSequence text = mFolderInfo.title;
2405 input.setText(text);
2406 input.setSelection(0, text.length());
2407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002408 break;
2409 }
2410 }
2411
2412 void showRenameDialog(FolderInfo info) {
2413 mFolderInfo = info;
2414 mWaitingForResult = true;
2415 showDialog(DIALOG_RENAME_FOLDER);
2416 }
2417
Michael Jurka0280c3b2010-09-17 15:00:07 -07002418 private void showAddDialog(int intersectX, int intersectY) {
2419 resetAddInfo();
2420 mAddIntersectCellX = intersectX;
2421 mAddIntersectCellY = intersectY;
2422 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 mWaitingForResult = true;
2424 showDialog(DIALOG_CREATE_SHORTCUT);
2425 }
2426
Joe Onoratodeb98af2010-02-19 14:59:39 -08002427 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002428 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002429 Bundle bundle = new Bundle();
2430
2431 ArrayList<String> shortcutNames = new ArrayList<String>();
2432 shortcutNames.add(getString(R.string.group_applications));
2433 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2434
2435 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2436 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2437 R.drawable.ic_launcher_application));
2438 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2439
2440 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2441 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002442 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002443 pickIntent.putExtras(bundle);
2444
Joe Onoratodeb98af2010-02-19 14:59:39 -08002445 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002446 }
2447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448 private class RenameFolder {
2449 private EditText mInput;
2450
2451 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2453 mInput = (EditText) layout.findViewById(R.id.folder_name);
2454
2455 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2456 builder.setIcon(0);
2457 builder.setTitle(getString(R.string.rename_folder_title));
2458 builder.setCancelable(true);
2459 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2460 public void onCancel(DialogInterface dialog) {
2461 cleanup();
2462 }
2463 });
2464 builder.setNegativeButton(getString(R.string.cancel_action),
2465 new Dialog.OnClickListener() {
2466 public void onClick(DialogInterface dialog, int which) {
2467 cleanup();
2468 }
2469 }
2470 );
2471 builder.setPositiveButton(getString(R.string.rename_action),
2472 new Dialog.OnClickListener() {
2473 public void onClick(DialogInterface dialog, int which) {
2474 changeFolderName();
2475 }
2476 }
2477 );
2478 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002479
2480 final AlertDialog dialog = builder.create();
2481 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2482 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002483 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002484 mInput.requestFocus();
2485 InputMethodManager inputManager = (InputMethodManager)
2486 getSystemService(Context.INPUT_METHOD_SERVICE);
2487 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002488 }
2489 });
2490
2491 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 }
2493
2494 private void changeFolderName() {
2495 final String name = mInput.getText().toString();
2496 if (!TextUtils.isEmpty(name)) {
2497 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002498 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 mFolderInfo.title = name;
2500 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2501
Joe Onorato9c1289c2009-08-17 11:03:03 -04002502 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002503 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002504 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 } else {
2506 final FolderIcon folderIcon = (FolderIcon)
2507 mWorkspace.getViewForTag(mFolderInfo);
2508 if (folderIcon != null) {
2509 folderIcon.setText(name);
2510 getWorkspace().requestLayout();
2511 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002512 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002513 mWorkspaceLoading = true;
2514 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002515 }
2516 }
2517 }
2518 cleanup();
2519 }
2520
2521 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 dismissDialog(DIALOG_RENAME_FOLDER);
2523 mWaitingForResult = false;
2524 mFolderInfo = null;
2525 }
2526 }
2527
Daniel Sandler843e8602010-06-07 14:59:01 -04002528 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2529 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002530 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002531 }
2532
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002533 // AllAppsView.Watcher
2534 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002535 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2536 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002537 mWorkspace.setVisibility(View.GONE);
2538 }
2539 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 private void showToolbarButton(View button) {
2542 button.setAlpha(1.0f);
2543 button.setVisibility(View.VISIBLE);
2544 button.setFocusable(true);
2545 button.setClickable(true);
2546 }
2547
2548 private void hideToolbarButton(View button) {
2549 button.setAlpha(0.0f);
2550 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2551 button.setVisibility(View.INVISIBLE);
2552 button.setFocusable(false);
2553 button.setClickable(false);
2554 }
2555
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002556 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 *
2559 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2560 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002564 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 final boolean showing = show;
2566 final boolean hiding = !show;
2567
2568 final int duration = show ?
2569 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2570 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2571
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002573 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2574 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002575 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002576 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002577 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002579 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002580 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002581 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584 });
2585 seq.play(anim);
2586 } else {
2587 if (showing) {
2588 showToolbarButton(view);
2589 } else {
2590 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002592 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 }
2594
2595 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 * Show/hide the appropriate toolbar buttons for newState.
2597 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 *
2599 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002600 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2601 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002603 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002604 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002606 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 final View configureButton = findViewById(R.id.configure_button);
2608
2609 switch (newState) {
2610 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002611 hideOrShowToolbarButton(true, searchButton, showSeq);
2612 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002613 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002615 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002616 mDeleteZone.setDragAndDropEnabled(true);
2617 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 break;
2619 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002620 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002621 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002622 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002624 mDeleteZone.setDragAndDropEnabled(false);
2625 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002626 break;
2627 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002628 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002630 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002631 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002632 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 break;
2634 }
2635 }
2636
2637 /**
2638 * Helper method for the cameraZoomIn/cameraZoomOut animations
2639 * @param view The view being animated
2640 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2641 * @param scaleFactor The scale factor used for the zoom
2642 */
2643 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2644 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002645
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002647 // Set pivotY so that at the starting zoom factor, the view is partially
2648 // visible. Modifying initialHeightFactor changes how much of the view is
2649 // initially showing, and hence the perceived angle from which the view enters.
2650 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002652 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002654 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 }
2656 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002657
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 /**
2659 * Zoom the camera out from the workspace to reveal 'toView'.
2660 * Assumes that the view to show is anchored at either the very top or very bottom
2661 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002662 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002663 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002665 final Resources res = getResources();
2666 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2667 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 final boolean toAllApps = (toState == State.ALL_APPS);
2669 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002670
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002671 setPivotsForZoom(toView, toState, scale);
2672
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002673 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002674 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002676 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678
2679 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002680 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2681 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2682 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2683 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002684
2685 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002686 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002687 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002688 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689 // Prepare the position
2690 toView.setTranslationX(0.0f);
2691 toView.setTranslationY(0.0f);
2692 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002693 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002694 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002695 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002696 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002697 // If we don't set the final scale values here, if this animation is cancelled
2698 // it will have the wrong scale value and subsequent cameraPan animations will
2699 // not fix that
2700 toView.setScaleX(1.0f);
2701 toView.setScaleY(1.0f);
2702 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 });
2704
Chet Haaseb1254a62010-09-07 13:35:00 -07002705 AnimatorSet toolbarHideAnim = new AnimatorSet();
2706 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002707 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2708
2709 // toView should appear right at the end of the workspace shrink animation
2710 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2711
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002712 if (mStateAnimation != null) mStateAnimation.cancel();
2713 mStateAnimation = new AnimatorSet();
2714 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2715 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716
2717 // Show the new toolbar buttons just as the main animation is ending
2718 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002719 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2720 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 } else {
2722 toView.setTranslationX(0.0f);
2723 toView.setTranslationY(0.0f);
2724 toView.setScaleX(1.0f);
2725 toView.setScaleY(1.0f);
2726 toView.setVisibility(View.VISIBLE);
2727 hideAndShowToolbarButtons(toState, null, null);
2728 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002729 }
2730
2731 /**
2732 * Zoom the camera back into the workspace, hiding 'fromView'.
2733 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002734 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2735 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002736 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002738 cameraZoomIn(fromState, animated, false);
2739 }
2740
2741 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002742 Resources res = getResources();
2743 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2744 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002745 final View fromView =
2746 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2747
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002748 mCustomizePagedView.endChoiceMode();
2749 mAllAppsPagedView.endChoiceMode();
2750
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002751 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002752
Michael Jurkad3ef3062010-11-23 16:23:58 -08002753 if (!springLoaded) {
2754 mWorkspace.unshrink(animated);
2755 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002756
2757 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002758 if (mStateAnimation != null) mStateAnimation.cancel();
2759 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002760 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2761 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2762 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2763 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002764 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2765
2766 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2767 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2768 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002769 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002770 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002771 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773 }
2774 });
2775
Chet Haaseb1254a62010-09-07 13:35:00 -07002776 AnimatorSet toolbarHideAnim = new AnimatorSet();
2777 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002778 if (!springLoaded) {
2779 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2780 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781
Adam Cohen61033d32010-11-15 18:29:44 -08002782 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002783
2784 // Show the new toolbar buttons at the very end of the whole animation
2785 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2786 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002787 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2788 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002789 } else {
2790 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002791 if (!springLoaded) {
2792 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2793 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002794 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002795 }
2796
2797 /**
2798 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2799 * This is the transition used on xlarge screens to go between all apps and
2800 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002801 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2802 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2803 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002804 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002805 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002806 final Resources res = getResources();
2807 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002808 final int workspaceHeight = mWorkspace.getHeight();
2809
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002810 final boolean fromAllApps = (fromState == State.ALL_APPS);
2811 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2812 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002813
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002814 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2815 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2816 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2817 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002818
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002819 mCustomizePagedView.endChoiceMode();
2820 mAllAppsPagedView.endChoiceMode();
2821
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002822 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002823 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002825 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002826 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002827
2828 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002829 if (mStateAnimation != null) mStateAnimation.cancel();
2830 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002831 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002832 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002833 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 toView.setVisibility(View.VISIBLE);
2835 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002836 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002838 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002839 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002840 fromView.setVisibility(View.GONE);
2841 }
2842 });
2843
Chet Haaseb1254a62010-09-07 13:35:00 -07002844 AnimatorSet toolbarHideAnim = new AnimatorSet();
2845 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002846 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2847
Chet Haase472b2812010-10-14 07:02:04 -07002848 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2849 fromViewStartY, fromViewEndY);
2850 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002851 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2852 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2853 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2854 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2855 );
Chet Haase472b2812010-10-14 07:02:04 -07002856 fromAnim.setDuration(duration);
2857 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858
2859 // Show the new toolbar buttons just as the main animation is ending
2860 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002861 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2862 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002863 } else {
2864 fromView.setY(fromViewEndY);
2865 fromView.setVisibility(View.GONE);
2866 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002867 toView.setScaleX(1.0f);
2868 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002869 toView.setVisibility(View.VISIBLE);
2870 hideAndShowToolbarButtons(toState, null, null);
2871 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002872 }
2873
2874 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002875 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002876 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002877 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002878
Michael Jurka79212d82010-07-30 16:36:20 -07002879 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002880 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002881 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002882 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002883 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002884 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002885 } else {
2886 mAllAppsGrid.zoom(1.0f, animated);
2887 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002888
Romain Guyc16fea72010-03-12 17:17:56 -08002889 ((View) mAllAppsGrid).setFocusable(true);
2890 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002891
Joe Onorato7c312c12009-08-13 21:36:53 -07002892 // TODO: fade these two too
2893 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002894
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002895 // Change the state *after* we've called all the transition code
2896 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002897
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002898 // send an accessibility event to announce the context change
2899 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2900 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002901
2902 void showWorkspace(boolean animated) {
2903 showWorkspace(animated, null);
2904 }
2905
2906 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002907 if (layout != null) {
2908 // always animated, but that's ok since we never specify a layout and
2909 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002910 mWorkspace.unshrink(layout);
2911 } else {
2912 mWorkspace.unshrink(animated);
2913 }
2914 if (mState == State.ALL_APPS) {
2915 closeAllApps(animated);
2916 } else if (mState == State.CUSTOMIZE) {
2917 hideCustomizationDrawer(animated);
2918 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002919
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002920 // Change the state *after* we've called all the transition code
2921 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002922
2923 // send an accessibility event to announce the context change
2924 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002925 }
2926
Michael Jurkad3ef3062010-11-23 16:23:58 -08002927 void enterSpringLoadedDragMode(CellLayout layout) {
2928 mWorkspace.enterSpringLoadedDragMode(layout);
2929 if (mState == State.ALL_APPS) {
2930 cameraZoomIn(State.ALL_APPS, true, true);
2931 mState = State.ALL_APPS_SPRING_LOADED;
2932 } else if (mState == State.CUSTOMIZE) {
2933 cameraZoomIn(State.CUSTOMIZE, true, true);
2934 mState = State.CUSTOMIZE_SPRING_LOADED;
2935 }/* else {
2936 // we're already in spring loaded mode; don't do anything
2937 }*/
2938 }
2939
2940 void exitSpringLoadedDragMode() {
2941 if (mState == State.ALL_APPS_SPRING_LOADED) {
2942 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2943 cameraZoomOut(State.ALL_APPS, true);
2944 mState = State.ALL_APPS;
2945 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2946 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2947 cameraZoomOut(State.CUSTOMIZE, true);
2948 mState = State.CUSTOMIZE;
2949 }/* else {
2950 // we're not in spring loaded mode; don't do anything
2951 }*/
2952 }
2953
Joe Onoratob2061212009-11-24 16:13:54 -05002954 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002955 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002956 * - Home from workspace
2957 * - from center screen
2958 * - from other screens
2959 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002960 * - from center screen
2961 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002962 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002963 * - from center screen
2964 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002965 * - Launch app from workspace and quit
2966 * - with back
2967 * - with home
2968 * - Launch app from all apps and quit
2969 * - with back
2970 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002971 * - Go to a screen that's not the default, then all
2972 * apps, and launch and app, and go back
2973 * - with back
2974 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002975 * - On workspace, long press power and go back
2976 * - with back
2977 * - with home
2978 * - On all apps, long press power and go back
2979 * - with back
2980 * - with home
2981 * - On workspace, power off
2982 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002983 * - Launch an app and turn off the screen while in that app
2984 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002985 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002986 * - From all apps
2987 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002988 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2989 * - From all apps
2990 * - From the center workspace
2991 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002992 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002993 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002994 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002995 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002996 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002997 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002998 } else {
2999 mAllAppsGrid.zoom(0.0f, animated);
3000 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003001 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003002 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003003 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003004 }
3005
Joe Onorato7c312c12009-08-13 21:36:53 -07003006 void lockAllApps() {
3007 // TODO
3008 }
3009
3010 void unlockAllApps() {
3011 // TODO
3012 }
3013
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003014 // Show the customization drawer (only exists in x-large configuration)
3015 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003016 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003017 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003018 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003019 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003020 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003021 // Change the state *after* we've called all the transition code
3022 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003023 }
3024
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003025 // Hide the customization drawer (only exists in x-large configuration)
3026 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003027 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003028 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003029 }
3030 }
3031
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003032 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3033 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3034 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003035 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003036 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003037
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003038 void onWorkspaceClick(CellLayout layout) {
3039 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003040 }
3041
Michael Jurka4ef207b2010-11-29 17:05:45 -08003042 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003043 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003044 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3045 ImageView button = (ImageView) findViewById(buttonId);
3046 Drawable toolbarIcon = null;
3047 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003048 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003049 // Look for the toolbar icon specified in the activity meta-data
3050 Bundle metaData = packageManager.getActivityInfo(
3051 activityName, PackageManager.GET_META_DATA).metaData;
3052 if (metaData != null) {
3053 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3054 if (iconResId != 0) {
3055 Resources res = packageManager.getResourcesForActivity(activityName);
3056 toolbarIcon = res.getDrawable(iconResId);
3057 }
3058 }
3059 } catch (NameNotFoundException e) {
3060 // Do nothing
3061 }
3062 // If we were unable to find the icon via the meta-data, use a generic one
3063 if (toolbarIcon == null) {
3064 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003065 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003066 } else {
3067 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003068 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003069 }
3070 }
3071
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003072 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003073 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003074 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003075 }
3076
Michael Jurka0423dcf2010-10-05 14:56:18 -07003077 private void updateGlobalSearchIcon() {
3078 if (LauncherApplication.isScreenXLarge()) {
3079 final SearchManager searchManager =
3080 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3081 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003082 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003083 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003084 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003085 } else {
3086 findViewById(R.id.search_button).setVisibility(View.GONE);
3087 }
3088 }
3089 }
3090
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003091 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003092 updateButtonWithDrawable(R.id.search_button, d);
3093 }
3094
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003095 private void updateVoiceSearchIcon() {
3096 if (LauncherApplication.isScreenXLarge()) {
3097 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3098 ComponentName activityName = intent.resolveActivity(getPackageManager());
3099 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003100 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003101 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3102 } else {
3103 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003104 }
3105 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003106 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003108 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003109 updateButtonWithDrawable(R.id.voice_button, d);
3110 }
3111
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003112 /**
3113 * Sets the app market icon (shown when all apps is visible on x-large screens)
3114 */
3115 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003116 if (LauncherApplication.isScreenXLarge()) {
3117 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3118 // Find the app market activity by resolving an intent.
3119 // (If multiple app markets are installed, it will return the ResolverActivity.)
3120 ComponentName activityName = intent.resolveActivity(getPackageManager());
3121 if (activityName != null) {
3122 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003124 R.id.market_button, activityName, R.drawable.app_market_generic);
3125 }
3126 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003127 }
3128
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003129 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003130 updateButtonWithDrawable(R.id.market_button, d);
3131 }
3132
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003133 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 * Displays the shortcut creation dialog and launches, if necessary, the
3135 * appropriate activity.
3136 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003137 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003138 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3139 DialogInterface.OnShowListener {
3140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003144 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003146 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3147 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003148 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 builder.setInverseBackgroundForced(true);
3151
3152 AlertDialog dialog = builder.create();
3153 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003154 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003155 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 return dialog;
3158 }
3159
3160 public void onCancel(DialogInterface dialog) {
3161 mWaitingForResult = false;
3162 cleanup();
3163 }
3164
Romain Guycbb89e42009-06-08 15:52:54 -07003165 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003166 }
3167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003169 try {
3170 dismissDialog(DIALOG_CREATE_SHORTCUT);
3171 } catch (Exception e) {
3172 // An exception is thrown if the dialog is not visible, which is fine
3173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
3175
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003176 /**
3177 * Handle the action clicked in the "Add to home" dialog.
3178 */
3179 public void onClick(DialogInterface dialog, int which) {
3180 Resources res = getResources();
3181 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003182
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003183 switch (which) {
3184 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003185 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003186 break;
3187 }
Romain Guycbb89e42009-06-08 15:52:54 -07003188
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003189 case AddAdapter.ITEM_APPWIDGET: {
3190 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003191
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003192 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3193 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003194 // start the pick activity
3195 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3196 break;
3197 }
Romain Guycbb89e42009-06-08 15:52:54 -07003198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003199 case AddAdapter.ITEM_LIVE_FOLDER: {
3200 // Insert extra item to handle inserting folder
3201 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003202
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003203 ArrayList<String> shortcutNames = new ArrayList<String>();
3204 shortcutNames.add(res.getString(R.string.group_folder));
3205 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003206
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003207 ArrayList<ShortcutIconResource> shortcutIcons =
3208 new ArrayList<ShortcutIconResource>();
3209 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3210 R.drawable.ic_launcher_folder));
3211 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3212
3213 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3214 pickIntent.putExtra(Intent.EXTRA_INTENT,
3215 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3216 pickIntent.putExtra(Intent.EXTRA_TITLE,
3217 getText(R.string.title_select_live_folder));
3218 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003219
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003220 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3221 break;
3222 }
3223
3224 case AddAdapter.ITEM_WALLPAPER: {
3225 startWallpaper();
3226 break;
3227 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 }
3229 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003230
3231 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003232 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003233 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003234 }
3235
3236 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003237 * Receives notifications when applications are added/removed.
3238 */
3239 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3240 @Override
3241 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003242 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003243 String reason = intent.getStringExtra("reason");
3244 if (!"homekey".equals(reason)) {
3245 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003246 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003247 animate = false;
3248 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003249 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003250 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003251 }
3252 }
3253
3254 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003255 * Receives notifications whenever the appwidgets are reset.
3256 */
3257 private class AppWidgetResetObserver extends ContentObserver {
3258 public AppWidgetResetObserver() {
3259 super(new Handler());
3260 }
3261
3262 @Override
3263 public void onChange(boolean selfChange) {
3264 onAppWidgetReset();
3265 }
3266 }
3267
3268 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003269 * If the activity is currently paused, signal that we need to re-run the loader
3270 * in onResume.
3271 *
3272 * This needs to be called from incoming places where resources might have been loaded
3273 * while we are paused. That is becaues the Configuration might be wrong
3274 * when we're not running, and if it comes back to what it was when we
3275 * were paused, we are not restarted.
3276 *
3277 * Implementation of the method from LauncherModel.Callbacks.
3278 *
3279 * @return true if we are currently paused. The caller might be able to
3280 * skip some work in that case since we will come back again.
3281 */
3282 public boolean setLoadOnResume() {
3283 if (mPaused) {
3284 Log.i(TAG, "setLoadOnResume");
3285 mOnResumeNeedsLoad = true;
3286 return true;
3287 } else {
3288 return false;
3289 }
3290 }
3291
3292 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003296 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003297 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003298 } else {
3299 return SCREEN_COUNT / 2;
3300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003301 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003302
Winson Chunga12a2502010-12-20 14:41:35 -08003303 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003304 mAllAppsPagedView = view;
3305 }
3306
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 /**
3308 * Refreshes the shortcuts shown on the workspace.
3309 *
3310 * Implementation of the method from LauncherModel.Callbacks.
3311 */
3312 public void startBinding() {
3313 final Workspace workspace = mWorkspace;
3314 int count = workspace.getChildCount();
3315 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003316 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3318 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003319
Joe Onorato9c1289c2009-08-17 11:03:03 -04003320 if (DEBUG_USER_INTERFACE) {
3321 android.widget.Button finishButton = new android.widget.Button(this);
3322 finishButton.setText("Finish");
3323 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3324
3325 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3326 public void onClick(View v) {
3327 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003328 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003330 }
3331 }
3332
3333 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003334 * Bind the items start-end from the list.
3335 *
3336 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003337 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3339
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003340 setLoadOnResume();
3341
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 final Workspace workspace = mWorkspace;
3343
3344 for (int i=start; i<end; i++) {
3345 final ItemInfo item = shortcuts.get(i);
3346 mDesktopItems.add(item);
3347 switch (item.itemType) {
3348 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3349 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003350 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3352 false);
3353 break;
3354 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3355 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003356 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003357 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3359 false);
3360 break;
3361 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3362 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3363 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003364 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 (LiveFolderInfo) item);
3366 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3367 false);
3368 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003370 }
3371
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003373 }
3374
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 /**
3376 * Implementation of the method from LauncherModel.Callbacks.
3377 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003378 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003379 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003380 sFolders.clear();
3381 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003382 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383
3384 /**
3385 * Add the views for a widget to the workspace.
3386 *
3387 * Implementation of the method from LauncherModel.Callbacks.
3388 */
3389 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003390 setLoadOnResume();
3391
Daniel Sandler843e8602010-06-07 14:59:01 -04003392 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3393 if (DEBUG_WIDGETS) {
3394 Log.d(TAG, "bindAppWidget: " + item);
3395 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396 final Workspace workspace = mWorkspace;
3397
3398 final int appWidgetId = item.appWidgetId;
3399 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003400 if (DEBUG_WIDGETS) {
3401 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3402 }
3403
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3405
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3407 item.hostView.setTag(item);
3408
3409 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3410 item.cellY, item.spanX, item.spanY, false);
3411
Adam Cohended9f8d2010-11-03 13:25:16 -07003412 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3413
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 workspace.requestLayout();
3415
3416 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003417
3418 if (DEBUG_WIDGETS) {
3419 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3420 + (SystemClock.uptimeMillis()-start) + "ms");
3421 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003422 }
3423
3424 /**
3425 * Callback saying that there aren't any more items to bind.
3426 *
3427 * Implementation of the method from LauncherModel.Callbacks.
3428 */
3429 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003430 setLoadOnResume();
3431
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 if (mSavedState != null) {
3433 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003434 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003435 }
3436
3437 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3438 if (userFolders != null) {
3439 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003440 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 if (info != null) {
3442 openFolder(info);
3443 }
3444 }
3445 final Folder openFolder = mWorkspace.getOpenFolder();
3446 if (openFolder != null) {
3447 openFolder.requestFocus();
3448 }
3449 }
3450
Joe Onorato9c1289c2009-08-17 11:03:03 -04003451 mSavedState = null;
3452 }
3453
3454 if (mSavedInstanceState != null) {
3455 super.onRestoreInstanceState(mSavedInstanceState);
3456 mSavedInstanceState = null;
3457 }
3458
Joe Onorato9c1289c2009-08-17 11:03:03 -04003459 mWorkspaceLoading = false;
3460 }
3461
3462 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003463 * Updates the icons on the launcher that are affected by changes to the package list
3464 * on the device.
3465 */
3466 private void updateIconsAffectedByPackageManagerChanges() {
3467 updateAppMarketIcon();
3468 updateGlobalSearchIcon();
3469 updateVoiceSearchIcon();
3470 }
3471
3472 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003473 * Add the icons for all apps.
3474 *
3475 * Implementation of the method from LauncherModel.Callbacks.
3476 */
3477 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003478 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003479 if (mCustomizePagedView != null) {
3480 mCustomizePagedView.setApps(apps);
3481 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003482 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003483 }
3484
3485 /**
3486 * A package was installed.
3487 *
3488 * Implementation of the method from LauncherModel.Callbacks.
3489 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003490 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003491 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003492 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003493 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003494 if (mCustomizePagedView != null) {
3495 mCustomizePagedView.addApps(apps);
3496 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003497 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 }
3499
3500 /**
3501 * A package was updated.
3502 *
3503 * Implementation of the method from LauncherModel.Callbacks.
3504 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003505 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003506 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003508 mWorkspace.updateShortcuts(apps);
3509 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003510 if (mCustomizePagedView != null) {
3511 mCustomizePagedView.updateApps(apps);
3512 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003513 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 }
3515
3516 /**
3517 * A package was uninstalled.
3518 *
3519 * Implementation of the method from LauncherModel.Callbacks.
3520 */
Joe Onorato36115782010-06-17 13:28:48 -04003521 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003522 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003523 if (permanent) {
3524 mWorkspace.removeItems(apps);
3525 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003526 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003527 if (mCustomizePagedView != null) {
3528 mCustomizePagedView.removeApps(apps);
3529 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003530 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003531 }
Joe Onoratobe386092009-11-17 17:32:16 -08003532
3533 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003534 * A number of packages were updated.
3535 */
3536 public void bindPackagesUpdated() {
3537 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003538 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003539 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003540 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003541 }
3542
3543 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003544 * Prints out out state for debugging.
3545 */
3546 public void dumpState() {
3547 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003548 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003549 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3550 Log.d(TAG, "mRestoring=" + mRestoring);
3551 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3552 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3553 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003554 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003555 mModel.dumpState();
3556 mAllAppsGrid.dumpState();
3557 Log.d(TAG, "END launcher2 dump state");
3558 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003559}