blob: e5d22996f0b3c824c5a300538c4eff4a7039c695 [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;
Michael Jurkac5b262c2011-01-12 20:24:50 -080030import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070031import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070032import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.Dialog;
35import android.app.SearchManager;
36import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson 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;
Joe Onorato0d44e942009-11-16 18:20:51 -080080import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.KeyEvent;
82import android.view.LayoutInflater;
83import android.view.Menu;
84import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070085import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.View;
87import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070088import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070089import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
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();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800312 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700313 if (mHomeCustomizationDrawer != null) {
314 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700315
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700317 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
318 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700319 TabContentFactory contentFactory = new TabContentFactory() {
320 public View createTabContent(String tag) {
321 return mCustomizePagedView;
322 }
323 };
324
Winson Chung49767ae2010-11-29 14:48:30 -0800325
326 TextView tabView;
327 TabWidget tabWidget = (TabWidget)
328 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
329
330 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
331 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700332 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800333 .setIndicator(tabView).setContent(contentFactory));
334 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
335 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700336 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800337 .setIndicator(tabView).setContent(contentFactory));
338 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
339 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800341 .setIndicator(tabView).setContent(contentFactory));
342 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
343 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700344 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800345 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
347 public void onTabChanged(String tabId) {
Winson Chunga12a2502010-12-20 14:41:35 -0800348 final CustomizePagedView.CustomizationType newType =
349 getCustomizeFilterForTabTag(tabId);
350 if (newType != mCustomizePagedView.getCustomizationFilter()) {
351 // animate the changing of the tab content by fading pages in and out
352 final Resources res = getResources();
353 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
354 final float alpha = mCustomizePagedView.getAlpha();
355 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
356 "alpha", alpha, 0.0f);
357 alphaAnim.setDuration(duration);
358 alphaAnim.addListener(new AnimatorListenerAdapter() {
359 @Override
360 public void onAnimationEnd(Animator animation) {
361 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
362 mCustomizePagedView.setCustomizationFilter(newType);
Winson Chung80baf5a2010-08-09 16:03:15 -0700363
Winson Chunga12a2502010-12-20 14:41:35 -0800364 final float alpha = mCustomizePagedView.getAlpha();
365 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
366 mCustomizePagedView, "alpha", alpha, 1.0f);
367 alphaAnim.setDuration(duration);
368 alphaAnim.start();
369 }
370 });
371 alphaAnim.start();
372 }
Winson Chung80baf5a2010-08-09 16:03:15 -0700373 }
374 });
Michael Jurka946ad472010-07-09 18:05:18 -0700375 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 setupViews();
377
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800378 registerContentObservers();
379
Joe Onorato7c312c12009-08-13 21:36:53 -0700380 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 mSavedState = savedInstanceState;
383 restoreState(mSavedState);
384
Winson Chunga12a2502010-12-20 14:41:35 -0800385 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800386 if (mCustomizePagedView != null) {
387 mCustomizePagedView.update();
388 }
Winson Chunga12a2502010-12-20 14:41:35 -0800389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 if (PROFILE_STARTUP) {
391 android.os.Debug.stopMethodTracing();
392 }
393
394 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397
398 // For handling default keys
399 mDefaultKeySsb = new SpannableStringBuilder();
400 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800401
402 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
403 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800404
405 // If we have a saved version of these external icons, we load them up immediately
406 if (LauncherApplication.isScreenXLarge()) {
407 if (sGlobalSearchIcon != null) {
408 updateGlobalSearchIcon(sGlobalSearchIcon);
409 }
410 if (sVoiceSearchIcon != null) {
411 updateVoiceSearchIcon(sVoiceSearchIcon);
412 }
413 if (sAppMarketIcon != null) {
414 updateAppMarketIcon(sAppMarketIcon);
415 }
416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
Romain Guycbb89e42009-06-08 15:52:54 -0700418
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800419 @Override
420 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
421 super.dispatchPopulateAccessibilityEvent(event);
422
423 // we want to take over text population so it is context dependent
424 event.getText().clear();
425 if (mState == State.ALL_APPS) {
426 event.getText().add(getString(R.string.all_apps_button_label));
427 } else if (mState == State.WORKSPACE) {
428 event.getText().add(getString(R.string.all_apps_home_button_label));
429 }
430
431 return true;
432 }
433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700435 if (sLocaleConfiguration == null) {
436 new AsyncTask<Void, Void, LocaleConfiguration>() {
437 @Override
438 protected LocaleConfiguration doInBackground(Void... unused) {
439 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
440 readConfiguration(Launcher.this, localeConfiguration);
441 return localeConfiguration;
442 }
443
444 @Override
445 protected void onPostExecute(LocaleConfiguration result) {
446 sLocaleConfiguration = result;
447 checkForLocaleChange(); // recursive, but now with a locale configuration
448 }
449 }.execute();
450 return;
451 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 final Configuration configuration = getResources().getConfiguration();
454
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700455 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 final String locale = configuration.locale.toString();
457
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700458 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 final int mcc = configuration.mcc;
460
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700461 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 final int mnc = configuration.mnc;
463
Romain Guy84f296c2009-11-04 15:00:44 -0800464 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800465
Romain Guy84f296c2009-11-04 15:00:44 -0800466 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700467 sLocaleConfiguration.locale = locale;
468 sLocaleConfiguration.mcc = mcc;
469 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700470
Joe Onorato0589f0f2010-02-08 13:44:00 -0800471 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400472 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700473
474 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
475 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700476 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477 public void run() {
478 writeConfiguration(Launcher.this, localeConfiguration);
479 }
480 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700481 }
482 }
483
484 private static class LocaleConfiguration {
485 public String locale;
486 public int mcc = -1;
487 public int mnc = -1;
488 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700489
Romain Guy98d01652009-06-30 16:21:04 -0700490 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
491 DataInputStream in = null;
492 try {
493 in = new DataInputStream(context.openFileInput(PREFERENCES));
494 configuration.locale = in.readUTF();
495 configuration.mcc = in.readInt();
496 configuration.mnc = in.readInt();
497 } catch (FileNotFoundException e) {
498 // Ignore
499 } catch (IOException e) {
500 // Ignore
501 } finally {
502 if (in != null) {
503 try {
504 in.close();
505 } catch (IOException e) {
506 // Ignore
507 }
508 }
509 }
510 }
511
512 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
513 DataOutputStream out = null;
514 try {
515 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
516 out.writeUTF(configuration.locale);
517 out.writeInt(configuration.mcc);
518 out.writeInt(configuration.mnc);
519 out.flush();
520 } catch (FileNotFoundException e) {
521 // Ignore
522 } catch (IOException e) {
523 //noinspection ResultOfMethodCallIgnored
524 context.getFileStreamPath(PREFERENCES).delete();
525 } finally {
526 if (out != null) {
527 try {
528 out.close();
529 } catch (IOException e) {
530 // Ignore
531 }
532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 }
534 }
535
536 static int getScreen() {
537 synchronized (sLock) {
538 return sScreen;
539 }
540 }
541
542 static void setScreen(int screen) {
543 synchronized (sLock) {
544 sScreen = screen;
545 }
546 }
547
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400548 // Note: This doesn't do all the client-id magic that BrowserProvider does
549 // in Browser. (http://b/2425179)
550 private Uri getDefaultBrowserUri() {
551 String url = getString(R.string.default_browser_url);
552 if (url.indexOf("{CID}") != -1) {
553 url = url.replace("{CID}", "android-google");
554 }
555 return Uri.parse(url);
556 }
557
558 // Load the Intent templates from arrays.xml to populate the hotseats. For
559 // each Intent, if it resolves to a single app, use that as the launch
560 // intent & use that app's label as the contentDescription. Otherwise,
561 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400562 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 if (mHotseatConfig == null) {
564 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
565 if (mHotseatConfig.length > 0) {
566 mHotseats = new Intent[mHotseatConfig.length];
567 mHotseatLabels = new CharSequence[mHotseatConfig.length];
568 mHotseatIcons = new Drawable[mHotseatConfig.length];
569 } else {
570 mHotseats = null;
571 mHotseatIcons = null;
572 mHotseatLabels = null;
573 }
574
575 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
576 for (int i=0; i<mHotseatConfig.length; i++) {
577 // load icon for this slot; currently unrelated to the actual activity
578 try {
579 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
580 } catch (ArrayIndexOutOfBoundsException ex) {
581 Log.w(TAG, "Missing hotseat_icons array item #" + i);
582 mHotseatIcons[i] = null;
583 }
584 }
585 hotseatIconDrawables.recycle();
586 }
587
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 for (int i=0; i<mHotseatConfig.length; i++) {
590 Intent intent = null;
591 if (mHotseatConfig[i].equals("*BROWSER*")) {
592 // magic value meaning "launch user's default web browser"
593 // replace it with a generic web request so we can see if there is indeed a default
594 String defaultUri = getString(R.string.default_browser_url);
595 intent = new Intent(
596 Intent.ACTION_VIEW,
597 ((defaultUri != null)
598 ? Uri.parse(defaultUri)
599 : getDefaultBrowserUri())
600 ).addCategory(Intent.CATEGORY_BROWSABLE);
601 // note: if the user launches this without a default set, she
602 // will always be taken to the default URL above; this is
603 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700604 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400605 // URL is unavoidably sent to the chosen app.
606 } else {
607 try {
608 intent = Intent.parseUri(mHotseatConfig[i], 0);
609 } catch (java.net.URISyntaxException ex) {
610 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
611 // bogus; leave intent=null
612 }
613 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700614
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 if (intent == null) {
616 mHotseats[i] = null;
617 mHotseatLabels[i] = getText(R.string.activity_not_found);
618 continue;
619 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400620
621 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700622 Log.d(TAG, "loadHotseats: hotseat " + i
623 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400624 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400625 + "]");
626 }
627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
629 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700630 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400631 Log.d(TAG, "Best match for intent: " + bestMatch);
632 Log.d(TAG, "All matches: ");
633 for (ResolveInfo ri : allMatches) {
634 Log.d(TAG, " --> " + ri);
635 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400637 // did this resolve to a single app, or the resolver?
638 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 // can't find any activity to handle this. let's leave the
640 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 // to launch.
642 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400643
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400644 // set accessibility text to "Not installed"
645 mHotseatLabels[i] = getText(R.string.activity_not_found);
646 } else {
647 boolean found = false;
648 for (ResolveInfo ri : allMatches) {
649 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
650 && bestMatch.activityInfo.applicationInfo.packageName
651 .equals(ri.activityInfo.applicationInfo.packageName)) {
652 found = true;
653 break;
654 }
655 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700656
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400657 if (!found) {
658 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
659 // the bestMatch is probably the ResolveActivity, meaning the
660 // user has not yet selected a default
661 // so: we'll keep the original intent for now
662 mHotseats[i] = intent;
663
664 // set the accessibility text to "Select shortcut"
665 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
666 } else {
667 // we have an app!
668 // now reconstruct the intent to launch it through the front
669 // door
670 ComponentName com = new ComponentName(
671 bestMatch.activityInfo.applicationInfo.packageName,
672 bestMatch.activityInfo.name);
673 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
674
675 // load the app label for accessibility
676 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
677 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400678 }
679
680 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700681 Log.d(TAG, "loadHotseats: hotseat " + i
682 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400683 + ((mHotseats[i] == null)
684 ? "null"
685 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400686 + "] label=[" + mHotseatLabels[i]
687 + "]"
688 );
689 }
690 }
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 @Override
694 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700695 mWaitingForResult = false;
696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 // The pattern used here is that a user PICKs a specific application,
698 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700699
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
701 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700702
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 switch (requestCode) {
705 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700706 completeAddApplication(
707 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 break;
709 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800710 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 break;
712 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700713 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 break;
715 case REQUEST_PICK_LIVE_FOLDER:
716 addLiveFolder(data);
717 break;
718 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700719 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700724 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700725 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700726 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700728 case REQUEST_PICK_WALLPAPER:
729 // We just wanted the activity result here so we can clear mWaitingForResult
730 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 }
Romain Guy18042c82009-11-06 11:44:55 -0800732 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
733 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
734 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735 // Clean up the appWidgetId if we canceled
736 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
737 if (appWidgetId != -1) {
738 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 }
740 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
743 @Override
744 protected void onResume() {
745 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800746 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700747 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400748 mWorkspaceLoading = true;
749 mModel.startLoader(this, true);
750 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700751 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700753 // When we resume Launcher, a different Activity might be responsible for the app
754 // market intent, so refresh the icon
755 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 }
757
758 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700759 protected void onPause() {
760 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700761 // Some launcher layouts don't have a previous and next view
762 if (mPreviousView != null) {
763 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700764 }
765 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700766 dismissPreview(mNextView);
767 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700768 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800769 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700770 }
Romain Guycbb89e42009-06-08 15:52:54 -0700771
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700772 @Override
773 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400774 // Flag the loader to stop early before switching
775 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800776 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800777 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700778 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700779
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800780 // We can't hide the IME if it was forced open. So don't bother
781 /*
782 @Override
783 public void onWindowFocusChanged(boolean hasFocus) {
784 super.onWindowFocusChanged(hasFocus);
785
786 if (hasFocus) {
787 final InputMethodManager inputManager = (InputMethodManager)
788 getSystemService(Context.INPUT_METHOD_SERVICE);
789 WindowManager.LayoutParams lp = getWindow().getAttributes();
790 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
791 android.os.Handler()) {
792 protected void onReceiveResult(int resultCode, Bundle resultData) {
793 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
794 }
795 });
796 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
797 }
798 }
799 */
800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 private boolean acceptFilter() {
802 final InputMethodManager inputManager = (InputMethodManager)
803 getSystemService(Context.INPUT_METHOD_SERVICE);
804 return !inputManager.isFullscreenMode();
805 }
806
807 @Override
808 public boolean onKeyDown(int keyCode, KeyEvent event) {
809 boolean handled = super.onKeyDown(keyCode, event);
810 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
811 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
812 keyCode, event);
813 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700814 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700815 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700816 // showSearchDialog()
817 // If there are multiple keystrokes before the search dialog takes focus,
818 // onSearchRequested() will be called for every keystroke,
819 // but it is idempotent, so it's fine.
820 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
822 }
823
Joe Onorato8a9625e2010-01-28 15:55:35 -0800824 // Eat the long press event so the keyboard doesn't come up.
825 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
826 return true;
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 return handled;
830 }
831
Karl Rosaen138a0412009-04-23 19:00:21 -0700832 private String getTypedText() {
833 return mDefaultKeySsb.toString();
834 }
835
836 private void clearTypedText() {
837 mDefaultKeySsb.clear();
838 mDefaultKeySsb.clearSpans();
839 Selection.setSelection(mDefaultKeySsb, 0);
840 }
841
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700843 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
844 * State
845 */
846 private static State intToState(int stateOrdinal) {
847 State state = State.WORKSPACE;
848 final State[] stateValues = State.values();
849 for (int i = 0; i < stateValues.length; i++) {
850 if (stateValues[i].ordinal() == stateOrdinal) {
851 state = stateValues[i];
852 break;
853 }
854 }
855 return state;
856 }
857
858 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 * Restores the previous state, if it exists.
860 *
861 * @param savedState The previous state.
862 */
863 private void restoreState(Bundle savedState) {
864 if (savedState == null) {
865 return;
866 }
867
Michael Jurka883f55b2010-10-21 15:47:14 -0700868 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
869
870 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800871 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700872 } else if (state == State.CUSTOMIZE) {
873 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800874 }
875
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
877 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700878 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 }
880
881 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700884 mAddScreen = addScreen;
885 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
886 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 mRestoring = true;
888 }
889
890 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
891 if (renameFolder) {
892 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700893 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 mRestoring = true;
895 }
Winson Chunga12a2502010-12-20 14:41:35 -0800896
897 // Restore the current AllApps drawer tab
898 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
899 String curTab = savedState.getString("allapps_currentTab");
900 if (curTab != null) {
901 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
902 tabhost.setCurrentTabByTag(curTab);
903 }
904 int curPage = savedState.getInt("allapps_currentPage", -1);
905 if (curPage > -1) {
906 mAllAppsPagedView.setRestorePage(curPage);
907 }
908 }
909
910 // Restore the current customization drawer tab
911 if (mHomeCustomizationDrawer != null) {
912 String curTab = savedState.getString("customize_currentTab");
913 if (curTab != null) {
914 // We set this directly so that there is no delay before the tab is set
915 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
916 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
917 }
918
919 // Note: currently we do not restore the page for the customization tray because unlike
920 // AllApps, the page content can change drastically
921 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 }
923
924 /**
925 * Finds all the views we need and configure them properly.
926 */
927 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700928 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400929
Romain Guyb1b69f52009-08-10 15:10:15 -0700930 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400931 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932
Joe Onorato7c312c12009-08-13 21:36:53 -0700933 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700934 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700935 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800936 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700937 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700938 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700939
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700940 if (LauncherApplication.isScreenXLarge()) {
941 // They need to be INVISIBLE initially so that they will be measured in the layout.
942 // Otherwise the animations are messed up when we show them for the first time.
943 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
944 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
945 }
946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500950 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700951
Joe Onorato7c312c12009-08-13 21:36:53 -0700952 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
953 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700955 View handleView = findViewById(R.id.all_apps_button);
956 if (handleView != null && handleView instanceof HandleView) {
957 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700958 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700959 mHandleView.setLauncher(this);
960 mHandleView.setOnClickListener(this);
961 mHandleView.setOnLongClickListener(this);
962 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800963
Winson Chung80baf5a2010-08-09 16:03:15 -0700964 if (mCustomizePagedView != null) {
965 mCustomizePagedView.setLauncher(this);
966 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700967 } else {
968 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
969 hotseatLeft.setContentDescription(mHotseatLabels[0]);
970 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
971 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
972 hotseatRight.setContentDescription(mHotseatLabels[1]);
973 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400974
Michael Jurkaaf442092010-06-10 17:01:57 -0700975 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
976 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800977
Michael Jurkaaf442092010-06-10 17:01:57 -0700978 Drawable previous = mPreviousView.getDrawable();
979 Drawable next = mNextView.getDrawable();
980 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981
Michael Jurkaaf442092010-06-10 17:01:57 -0700982 mPreviousView.setHapticFeedbackEnabled(false);
983 mPreviousView.setOnLongClickListener(this);
984 mNextView.setHapticFeedbackEnabled(false);
985 mNextView.setOnLongClickListener(this);
986 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400989 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800991 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
993 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400994 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700995 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -0800996
997 final View allAppsButton = findViewById(R.id.all_apps_button);
998 final View divider = findViewById(R.id.divider);
999 final View configureButton = findViewById(R.id.configure_button);
1000
Michael Jurka54f7ac32010-08-02 13:56:46 -07001001 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001002 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -07001003 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001004 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001005 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001006 dragController.addDragListener(deleteZone);
1007
Adam Cohencdc30d52010-12-01 15:09:47 -08001008 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1009 if (allAppsDeleteZone != null) {
1010 allAppsDeleteZone.setLauncher(this);
1011 allAppsDeleteZone.setDragController(dragController);
1012 allAppsDeleteZone.setDragAndDropEnabled(false);
1013 dragController.addDragListener(allAppsDeleteZone);
1014 dragController.addDropTarget(allAppsDeleteZone);
1015 }
1016
1017 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1018 findViewById(R.id.all_apps_info_target);
1019 if (allAppsInfoTarget != null) {
1020 allAppsInfoTarget.setLauncher(this);
1021 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001022 allAppsInfoTarget.setDragAndDropEnabled(false);
1023 View marketButton = findViewById(R.id.market_button);
1024 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001025 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001026 }
1027 }
1028
Joe Onorato00acb122009-08-04 16:04:30 -04001029 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001030 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001031 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001032
Joe Onorato00acb122009-08-04 16:04:30 -04001033 // The order here is bottom to top.
1034 dragController.addDropTarget(workspace);
1035 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001036 if (allAppsInfoTarget != null) {
1037 dragController.addDropTarget(allAppsInfoTarget);
1038 }
1039 if (allAppsDeleteZone != null) {
1040 dragController.addDropTarget(allAppsDeleteZone);
1041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
Romain Guy8a73c512009-11-09 19:19:59 -08001044 @SuppressWarnings({"UnusedDeclaration"})
1045 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001046 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001047 mWorkspace.scrollLeft();
1048 }
Romain Guy8a73c512009-11-09 19:19:59 -08001049 }
1050
1051 @SuppressWarnings({"UnusedDeclaration"})
1052 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001053 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001054 mWorkspace.scrollRight();
1055 }
Romain Guy8a73c512009-11-09 19:19:59 -08001056 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001057
1058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001060 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001061
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001062 int index = -1;
1063 if (v.getId() == R.id.hotseat_left) {
1064 index = 0;
1065 } else if (v.getId() == R.id.hotseat_right) {
1066 index = 1;
1067 }
1068
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001069 // reload these every tap; you never know when they might change
1070 loadHotseats();
1071 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1072 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001073 startActivitySafely(
1074 mHotseats[index],
1075 "hotseat"
1076 );
1077 }
1078 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 /**
1081 * Creates a view representing a shortcut.
1082 *
1083 * @param info The data structure describing the shortcut.
1084 *
1085 * @return A View inflated from R.layout.application.
1086 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001087 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001089 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 /**
1093 * Creates a view representing a shortcut inflated from the specified resource.
1094 *
1095 * @param layoutResId The id of the XML layout used to create the shortcut.
1096 * @param parent The group the shortcut belongs to.
1097 * @param info The data structure describing the shortcut.
1098 *
1099 * @return A View inflated from layoutResId.
1100 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001101 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001102 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1103 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 return favorite;
1106 }
1107
1108 /**
1109 * Add an application shortcut to the workspace.
1110 *
1111 * @param data The intent describing the application.
1112 * @param cellInfo The position on screen where to create the shortcut.
1113 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 void completeAddApplication(Context context, Intent data, int screen,
1115 int intersectCellX, int intersectCellY) {
1116 final int[] cellXY = mTmpAddItemCellCoordinates;
1117 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1118
1119 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1120 showOutOfSpaceMessage();
1121 return;
1122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1125 data, context);
1126
Romain Guy73b979d2009-06-09 12:57:21 -07001127 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001128 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001130 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1132 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 } else {
1134 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136 }
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 /**
1139 * Add a shortcut to the workspace.
1140 *
1141 * @param data The intent describing the shortcut.
1142 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 private void completeAddShortcut(Intent data, int screen,
1145 int intersectCellX, int intersectCellY) {
1146 final int[] cellXY = mTmpAddItemCellCoordinates;
1147 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 int[] touchXY = null;
1150 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1151 touchXY = mAddDropPosition;
1152 }
1153 boolean foundCellSpan = false;
1154 if (touchXY != null) {
1155 // when dragging and dropping, just find the closest free spot
1156 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1157 int[] result = screenLayout.findNearestVacantArea(
1158 touchXY[0], touchXY[1], 1, 1, cellXY);
1159 foundCellSpan = (result != null);
1160 } else {
1161 foundCellSpan = layout.findCellForSpanThatIntersects(
1162 cellXY, 1, 1, intersectCellX, intersectCellY);
1163 }
1164
1165 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 showOutOfSpaceMessage();
1167 return;
1168 }
1169
1170 final ShortcutInfo info = mModel.addShortcut(
1171 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001182 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1190 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001193 // We have saved the position to which the widget was dragged-- this really only matters
1194 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001196
1197 // For now, we don't save the coordinate where we dropped the icon because we're not
1198 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1199 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001200 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001201 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001202 touchXY = mAddDropPosition;
1203 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 boolean foundCellSpan = false;
1205 if (touchXY != null) {
1206 // when dragging and dropping, just find the closest free spot
1207 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1208 int[] result = screenLayout.findNearestVacantArea(
1209 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001210 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001212 // if we long pressed on an empty cell to bring up a menu,
1213 // make sure we intersect the empty cell
1214 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1215 // findCellForSpanThatIntersects will just ignore them
1216 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1217 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001218 }
1219
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001221 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001223 return;
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001226 // Build Launcher-specific widget info and save to database
1227 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 launcherInfo.spanX = spanXY[0];
1229 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 LauncherModel.addItemToDatabase(this, launcherInfo,
1232 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234
1235 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001240
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001241 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001246
1247 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1252 @Override
1253 public void onReceive(Context context, Intent intent) {
1254 final String action = intent.getAction();
1255 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1256 mUserPresent = false;
1257 updateRunning();
1258 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1259 mUserPresent = true;
1260 updateRunning();
1261 }
1262 }
1263 };
1264
1265 @Override
1266 public void onAttachedToWindow() {
1267 super.onAttachedToWindow();
1268
1269 // Listen for broadcasts related to user-presence
1270 final IntentFilter filter = new IntentFilter();
1271 filter.addAction(Intent.ACTION_SCREEN_OFF);
1272 filter.addAction(Intent.ACTION_USER_PRESENT);
1273 registerReceiver(mReceiver, filter);
1274
Adam Cohend113e0c2010-11-11 10:48:05 -08001275 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001276 mVisible = true;
1277 }
1278
1279 @Override
1280 public void onDetachedFromWindow() {
1281 super.onDetachedFromWindow();
1282 mVisible = false;
1283
Adam Cohend113e0c2010-11-11 10:48:05 -08001284 if (mAttached) {
1285 unregisterReceiver(mReceiver);
1286 mAttached = false;
1287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 updateRunning();
1289 }
1290
1291 public void onWindowVisibilityChanged(int visibility) {
1292 mVisible = visibility == View.VISIBLE;
1293 updateRunning();
1294 }
1295
1296 private void sendAdvanceMessage(long delay) {
1297 mHandler.removeMessages(ADVANCE_MSG);
1298 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1299 mHandler.sendMessageDelayed(msg, delay);
1300 mAutoAdvanceSentTime = System.currentTimeMillis();
1301 }
1302
1303 private void updateRunning() {
1304 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1305 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1306 mAutoAdvanceRunning = autoAdvanceRunning;
1307 if (autoAdvanceRunning) {
1308 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1309 sendAdvanceMessage(delay);
1310 } else {
1311 if (!mWidgetsToAdvance.isEmpty()) {
1312 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1313 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1314 }
1315 mHandler.removeMessages(ADVANCE_MSG);
1316 }
1317 }
1318 }
1319
1320 private final Handler mHandler = new Handler() {
1321 @Override
1322 public void handleMessage(Message msg) {
1323 if (msg.what == ADVANCE_MSG) {
1324 int i = 0;
1325 for (View key: mWidgetsToAdvance.keySet()) {
1326 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1327 final int delay = mAdvanceStagger * i;
1328 if (v instanceof Advanceable) {
1329 postDelayed(new Runnable() {
1330 public void run() {
1331 ((Advanceable) v).advance();
1332 }
1333 }, delay);
1334 }
1335 i++;
1336 }
1337 sendAdvanceMessage(mAdvanceInterval);
1338 }
1339 }
1340 };
1341
1342 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1343 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1344 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1345 if (v instanceof Advanceable) {
1346 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1347 ((Advanceable) v).willBeAdvancedByHost();
1348 updateRunning();
1349 }
1350 }
1351
1352 void removeWidgetToAutoAdvance(View hostView) {
1353 if (mWidgetsToAdvance.containsKey(hostView)) {
1354 mWidgetsToAdvance.remove(hostView);
1355 updateRunning();
1356 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357 }
1358
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1360 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 launcherInfo.hostView = null;
1363 }
1364
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 void showOutOfSpaceMessage() {
1366 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1367 }
1368
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001369 public LauncherAppWidgetHost getAppWidgetHost() {
1370 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Winson Chunga9abd0e2010-10-27 17:18:37 -07001373 public LauncherModel getModel() {
1374 return mModel;
1375 }
1376
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 getWindow().closeAllPanels();
1379
1380 try {
1381 dismissDialog(DIALOG_CREATE_SHORTCUT);
1382 // Unlock the workspace if the dialog was showing
1383 } catch (Exception e) {
1384 // An exception is thrown if the dialog is not visible, which is fine
1385 }
1386
1387 try {
1388 dismissDialog(DIALOG_RENAME_FOLDER);
1389 // Unlock the workspace if the dialog was showing
1390 } catch (Exception e) {
1391 // An exception is thrown if the dialog is not visible, which is fine
1392 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001393
1394 // Whatever we were doing is hereby canceled.
1395 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 @Override
1399 protected void onNewIntent(Intent intent) {
1400 super.onNewIntent(intent);
1401
1402 // Close the menu
1403 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001404 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001405 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001406
Joe Onorato14f122b2009-11-19 14:06:36 -08001407 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1408 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001409
Michael Jurka4cb37242010-08-09 21:05:32 -07001410 // in all these cases, only animate if we're already on home
1411 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001412 mWorkspace.unshrink(alreadyOnHome);
1413 }
1414 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001415 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac5b262c2011-01-12 20:24:50 -08001416 boolean animate = alreadyOnHome &&
1417 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS);
1418 mWorkspace.moveToDefaultScreen(animate);
1419 if (!animate) mWorkspace.updateWallpaperOffsetImmediately();
Joe Onorato3a8820b2009-11-10 15:06:42 -08001420 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001421 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001422
Joe Onorato3a8820b2009-11-10 15:06:42 -08001423 final View v = getWindow().peekDecorView();
1424 if (v != null && v.getWindowToken() != null) {
1425 InputMethodManager imm = (InputMethodManager)getSystemService(
1426 INPUT_METHOD_SERVICE);
1427 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429 }
1430 }
1431
1432 @Override
1433 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1434 // Do not call super here
1435 mSavedInstanceState = savedInstanceState;
1436 }
1437
1438 @Override
1439 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001440 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441
1442 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1443 if (folders.size() > 0) {
1444 final int count = folders.size();
1445 long[] ids = new long[count];
1446 for (int i = 0; i < count; i++) {
1447 final FolderInfo info = folders.get(i).getInfo();
1448 ids[i] = info.id;
1449 }
1450 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1451 } else {
1452 super.onSaveInstanceState(outState);
1453 }
1454
Michael Jurka883f55b2010-10-21 15:47:14 -07001455 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
Michael Jurka0280c3b2010-09-17 15:00:07 -07001457 if (mAddScreen > -1 && mWaitingForResult) {
1458 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1459 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1460 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 }
1462
1463 if (mFolderInfo != null && mWaitingForResult) {
1464 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1465 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1466 }
Michael Jurka946ad472010-07-09 18:05:18 -07001467
Winson Chung43b119d2010-12-06 16:45:05 -08001468 // Save the current AllApps drawer tab
1469 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1470 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1471 String currentTabTag = tabhost.getCurrentTabTag();
1472 if (currentTabTag != null) {
1473 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001474 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001475 }
1476 }
1477
1478 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001479 if (mHomeCustomizationDrawer != null) {
1480 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1481 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001482 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001483 }
1484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 @Override
1488 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001490
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001492 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001494 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 TextKeyListener.getInstance().release();
1498
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001502
1503 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001504
Patrick Dubroyab962b72010-07-26 12:10:10 -07001505 // Some launcher layouts don't have a previous and next view
1506 if (mPreviousView != null) {
1507 dismissPreview(mPreviousView);
1508 }
1509 if (mNextView != null) {
1510 dismissPreview(mNextView);
1511 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001512
1513 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
1516 @Override
1517 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001518 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 super.startActivityForResult(intent, requestCode);
1520 }
1521
1522 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001523 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001525
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001526 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Karl Rosaen138a0412009-04-23 19:00:21 -07001528 if (initialQuery == null) {
1529 // Use any text typed in the launcher as the initial query
1530 initialQuery = getTypedText();
1531 clearTypedText();
1532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 if (appSearchData == null) {
1534 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001535 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Karl Rosaen138a0412009-04-23 19:00:21 -07001538 final SearchManager searchManager =
1539 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001540 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001541 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
1543
1544 @Override
1545 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001546 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001547 return false;
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1553 .setIcon(android.R.drawable.ic_menu_add)
1554 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001555 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1556 .setIcon(android.R.drawable.ic_menu_manage)
1557 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001558 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 .setIcon(android.R.drawable.ic_menu_gallery)
1560 .setAlphabeticShortcut('W');
1561 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1562 .setIcon(android.R.drawable.ic_search_category_default)
1563 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1564 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1565 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1566 .setAlphabeticShortcut('N');
1567
1568 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001569 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1570 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571
1572 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1573 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1574 .setIntent(settings);
1575
1576 return true;
1577 }
1578
1579 @Override
1580 public boolean onPrepareOptionsMenu(Menu menu) {
1581 super.onPrepareOptionsMenu(menu);
1582
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001583 // If all apps is animating, don't show the menu, because we don't know
1584 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001585 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001586 return false;
1587 }
1588
1589 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001590 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001591 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1592 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1593
1594 // Disable add if the workspace is full.
1595 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001596 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1597 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599
1600 return true;
1601 }
1602
1603 @Override
1604 public boolean onOptionsItemSelected(MenuItem item) {
1605 switch (item.getItemId()) {
1606 case MENU_ADD:
1607 addItems();
1608 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001609 case MENU_MANAGE_APPS:
1610 manageApps();
1611 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 case MENU_WALLPAPER_SETTINGS:
1613 startWallpaper();
1614 return true;
1615 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001616 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 return true;
1618 case MENU_NOTIFICATIONS:
1619 showNotifications();
1620 return true;
1621 }
1622
1623 return super.onOptionsItemSelected(item);
1624 }
Romain Guycbb89e42009-06-08 15:52:54 -07001625
Karl Rosaen138a0412009-04-23 19:00:21 -07001626 /**
1627 * Indicates that we want global search for this activity by setting the globalSearch
1628 * argument for {@link #startSearch} to true.
1629 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001631 @Override
1632 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001633 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001634 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001635 }
1636
Joe Onorato9c1289c2009-08-17 11:03:03 -04001637 public boolean isWorkspaceLocked() {
1638 return mWorkspaceLoading || mWaitingForResult;
1639 }
1640
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001642 if (LauncherApplication.isScreenXLarge()) {
1643 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001644 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001645 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001646 }
1647 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001648 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001649 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652
Michael Jurka0280c3b2010-09-17 15:00:07 -07001653 private void resetAddInfo() {
1654 mAddScreen = -1;
1655 mAddIntersectCellX = -1;
1656 mAddIntersectCellY = -1;
1657 mAddDropPosition = null;
1658 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001659
Winson Chung55cef262010-10-28 14:14:18 -07001660 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 resetAddInfo();
1662 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 mAddDropPosition = position;
1664
Michael Jurkaaf442092010-06-10 17:01:57 -07001665 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001666 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1667 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001668 }
1669
Winson Chung7ad01412010-09-27 11:33:03 -07001670 private void manageApps() {
1671 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1672 }
1673
Michael Jurkaaf442092010-06-10 17:01:57 -07001674 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001675 // TODO: catch bad widget exception when sent
1676 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001677 // TODO: Is this log message meaningful?
1678 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001679 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001680 }
1681
Winson Chung55cef262010-10-28 14:14:18 -07001682 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001683 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684
Bjorn Bringert7984c942009-12-09 15:38:25 +00001685 if (appWidget.configure != null) {
1686 // Launch over to configure widget, if needed
1687 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1688 intent.setComponent(appWidget.configure);
1689 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001690 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001691 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1692 intent.putExtra(
1693 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1694 info.mimeType);
1695
1696 final String mimeType = info.mimeType;
1697 final ClipData clipData = (ClipData) info.configurationData;
1698 final ClipDescription clipDesc = clipData.getDescription();
1699 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1700 if (clipDesc.getMimeType(i).equals(mimeType)) {
1701 final ClipData.Item item = clipData.getItem(i);
1702 final CharSequence stringData = item.getText();
1703 final Uri uriData = item.getUri();
1704 final Intent intentData = item.getIntent();
1705 final String key =
1706 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1707 if (uriData != null) {
1708 intent.putExtra(key, uriData);
1709 } else if (intentData != null) {
1710 intent.putExtra(key, intentData);
1711 } else if (stringData != null) {
1712 intent.putExtra(key, stringData);
1713 }
1714 break;
1715 }
1716 }
1717 }
Winson Chung55cef262010-10-28 14:14:18 -07001718 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001719
Romain Guy8e633c52010-03-04 12:51:36 -08001720 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001722 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001723 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 }
1725 }
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Michael Jurka0280c3b2010-09-17 15:00:07 -07001727 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1728 resetAddInfo();
1729 mAddScreen = screen;
1730 mAddDropPosition = position;
1731
1732 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1733 createShortcutIntent.setComponent(componentName);
1734 processShortcut(createShortcutIntent);
1735 }
1736
Joe Onoratodeb98af2010-02-19 14:59:39 -08001737 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001738 // Handle case where user selected "Applications"
1739 String applicationName = getResources().getString(R.string.group_applications);
1740 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001741
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001742 if (applicationName != null && applicationName.equals(shortcutName)) {
1743 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1744 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001745
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001746 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1747 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001748 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001749 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001750 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001751 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 }
1754
Winson Chung24ab2f12010-09-16 14:10:47 -07001755 void processWallpaper(Intent intent) {
1756 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1757 }
1758
Michael Jurka0280c3b2010-09-17 15:00:07 -07001759 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1760 resetAddInfo();
1761 mAddScreen = screen;
1762 mAddDropPosition = position;
1763
1764 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1765 createFolderIntent.setComponent(componentName);
1766
1767 addLiveFolder(createFolderIntent);
1768 }
1769
1770 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001772 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001774
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001778 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001780 }
1781
Michael Jurka0280c3b2010-09-17 15:00:07 -07001782 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 UserFolderInfo folderInfo = new UserFolderInfo();
1784 folderInfo.title = getText(R.string.folder_name);
1785
Michael Jurka0280c3b2010-09-17 15:00:07 -07001786 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1787 final int[] cellXY = mTmpAddItemCellCoordinates;
1788 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1789 showOutOfSpaceMessage();
1790 return;
1791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792
1793 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001794 LauncherModel.addItemToDatabase(this, folderInfo,
1795 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001796 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001797 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798
1799 // Create the view
1800 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001801 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1802 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001803 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
Romain Guycbb89e42009-06-08 15:52:54 -07001805
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001807 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001808 }
1809
Michael Jurka28750fb2010-09-24 17:43:49 -07001810 private void completeAddLiveFolder(
1811 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001812 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1813 final int[] cellXY = mTmpAddItemCellCoordinates;
1814 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1815 showOutOfSpaceMessage();
1816 return;
1817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818
Michael Jurka0280c3b2010-09-17 15:00:07 -07001819 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820
1821 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001823 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001824 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 }
1826 }
1827
1828 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001829 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001830
1831 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1832 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1833
1834 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 Intent.ShortcutIconResource iconResource = null;
1836
1837 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1838 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1839 try {
1840 iconResource = (Intent.ShortcutIconResource) extra;
1841 final PackageManager packageManager = context.getPackageManager();
1842 Resources resources = packageManager.getResourcesForApplication(
1843 iconResource.packageName);
1844 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1845 icon = resources.getDrawable(id);
1846 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001847 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
1849 }
1850
1851 if (icon == null) {
1852 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1853 }
1854
1855 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001856 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 info.title = name;
1858 info.iconResource = iconResource;
1859 info.uri = data.getData();
1860 info.baseIntent = baseIntent;
1861 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1862 LiveFolders.DISPLAY_MODE_GRID);
1863
1864 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001865 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001866 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867
1868 return info;
1869 }
1870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 private void showNotifications() {
1872 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1873 if (statusBar != null) {
1874 statusBar.expand();
1875 }
1876 }
1877
1878 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001879 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001881 Intent chooser = Intent.createChooser(pickWallpaper,
1882 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001883 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1884 // Removed in Eclair MR1
1885// WallpaperManager wm = (WallpaperManager)
1886// getSystemService(Context.WALLPAPER_SERVICE);
1887// WallpaperInfo wi = wm.getWallpaperInfo();
1888// if (wi != null && wi.getSettingsActivity() != null) {
1889// LabeledIntent li = new LabeledIntent(getPackageName(),
1890// R.string.configure_wallpaper, 0);
1891// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1892// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1893// }
Mike Clerona0618e42009-10-22 13:55:21 -07001894 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
1896
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 /**
1898 * Registers various content observers. The current implementation registers
1899 * only a favorites observer to keep track of the favorites applications.
1900 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001901 private void registerContentObservers() {
1902 ContentResolver resolver = getContentResolver();
1903 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1904 true, mWidgetObserver);
1905 }
1906
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 @Override
1908 public boolean dispatchKeyEvent(KeyEvent event) {
1909 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1910 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001912 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001913 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001914 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001915 dumpState();
1916 return true;
1917 }
1918 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001919 }
1920 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1921 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001922 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 return true;
1924 }
1925 }
1926
1927 return super.dispatchKeyEvent(event);
1928 }
1929
Joe Onorato88ec0992009-11-19 13:16:06 -08001930 @Override
1931 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001932 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1933 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001934 } else {
1935 closeFolder();
1936 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001937 // Some launcher layouts don't have a previous and next view
1938 if (mPreviousView != null) {
1939 dismissPreview(mPreviousView);
1940 dismissPreview(mNextView);
1941 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001942 }
1943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 private void closeFolder() {
1945 Folder folder = mWorkspace.getOpenFolder();
1946 if (folder != null) {
1947 closeFolder(folder);
1948 }
1949 }
1950
1951 void closeFolder(Folder folder) {
1952 folder.getInfo().opened = false;
1953 ViewGroup parent = (ViewGroup) folder.getParent();
1954 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001955 CellLayout cl = (CellLayout) parent;
1956 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001957 if (folder instanceof DropTarget) {
1958 // Live folders aren't DropTargets.
1959 mDragController.removeDropTarget((DropTarget)folder);
1960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962 folder.onClose();
1963 }
1964
1965 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001966 * Re-listen when widgets are reset.
1967 */
1968 private void onAppWidgetReset() {
1969 mAppWidgetHost.startListening();
1970 }
1971
1972 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001973 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1974 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001976 private void unbindDesktopItems() {
1977 for (ItemInfo item: mDesktopItems) {
1978 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001981
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 /**
1983 * Launches the intent referred by the clicked shortcut.
1984 *
1985 * @param v The view representing the clicked shortcut.
1986 */
1987 public void onClick(View v) {
1988 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001989 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001991 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001992 int[] pos = new int[2];
1993 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001994 intent.setSourceBounds(new Rect(pos[0], pos[1],
1995 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001996 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 } else if (tag instanceof FolderInfo) {
1998 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001999 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002000 if (mState == State.ALL_APPS) {
2001 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002002 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002003 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
2006 }
2007
Michael Jurka0e260592010-06-30 17:07:39 -07002008 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002009 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002010 // clicking anywhere on the workspace causes the customization drawer to slide down
2011 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002012 return false;
2013 }
2014
Michael Jurkaaf442092010-06-10 17:01:57 -07002015 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002016 * Event handler for the search button
2017 *
2018 * @param v The view that was clicked.
2019 */
2020 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002021 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002022 // Use a custom animation for launching search
2023 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002024 }
2025
2026 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002027 * Event handler for the voice button
2028 *
2029 * @param v The view that was clicked.
2030 */
2031 public void onClickVoiceButton(View v) {
2032 startVoiceSearch();
2033 }
2034
2035 private void startVoiceSearch() {
2036 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2037 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2038 startActivity(intent);
2039 }
2040
2041 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002042 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002043 * enters home screen customization mode.
2044 *
2045 * @param v The view that was clicked.
2046 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002047 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002048 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002049 }
2050
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002051 /**
2052 * Event handler for the "grid" button that appears on the home screen, which
2053 * enters all apps mode.
2054 *
2055 * @param v The view that was clicked.
2056 */
2057 public void onClickAllAppsButton(View v) {
2058 showAllApps(true);
2059 }
2060
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002061 public void onClickAppMarketButton(View v) {
2062 if (mAppMarketIntent != null) {
2063 startActivitySafely(mAppMarketIntent, "app market");
2064 }
2065 }
2066
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002067 void startApplicationDetailsActivity(ComponentName componentName) {
2068 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002069 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2070 Uri.fromParts("package", packageName, null));
2071 startActivity(intent);
2072 }
2073
Patrick Dubroy5539af72010-09-07 15:22:01 -07002074 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2075 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2076 // System applications cannot be installed. For now, show a toast explaining that.
2077 // We may give them the option of disabling apps this way.
2078 int messageId = R.string.uninstall_system_app_text;
2079 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2080 } else {
2081 String packageName = appInfo.componentName.getPackageName();
2082 String className = appInfo.componentName.getClassName();
2083 Intent intent = new Intent(
2084 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2085 startActivity(intent);
2086 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002087 }
2088
Joe Onoratof984e852010-03-25 09:47:45 -07002089 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2091 try {
2092 startActivity(intent);
2093 } catch (ActivityNotFoundException e) {
2094 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002095 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 } catch (SecurityException e) {
2097 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002098 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002100 "or use the exported attribute for this activity. "
2101 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 }
2103 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002104
Romain Guy8e633c52010-03-04 12:51:36 -08002105 void startActivityForResultSafely(Intent intent, int requestCode) {
2106 try {
2107 startActivityForResult(intent, requestCode);
2108 } catch (ActivityNotFoundException e) {
2109 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2110 } catch (SecurityException e) {
2111 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2112 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2113 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2114 "or use the exported attribute for this activity.", e);
2115 }
2116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117
2118 private void handleFolderClick(FolderInfo folderInfo) {
2119 if (!folderInfo.opened) {
2120 // Close any open folder
2121 closeFolder();
2122 // Open the requested folder
2123 openFolder(folderInfo);
2124 } else {
2125 // Find the open folder...
2126 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2127 int folderScreen;
2128 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002129 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 // .. and close it
2131 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002132 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 // Close any folder open on the current screen
2134 closeFolder();
2135 // Pull the folder onto this screen
2136 openFolder(folderInfo);
2137 }
2138 }
2139 }
2140 }
2141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002143 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 * is animated relative to the specified View. If the View is null, no animation
2145 * is played.
2146 *
2147 * @param folderInfo The FolderInfo describing the folder to open.
2148 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002149 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 Folder openFolder;
2151
2152 if (folderInfo instanceof UserFolderInfo) {
2153 openFolder = UserFolder.fromXml(this);
2154 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002155 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 } else {
2157 return;
2158 }
2159
Joe Onorato00acb122009-08-04 16:04:30 -04002160 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 openFolder.setLauncher(this);
2162
2163 openFolder.bind(folderInfo);
2164 folderInfo.opened = true;
2165
Winson Chungaafa03c2010-06-11 17:34:16 -07002166 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 openFolder.onOpen();
2169 }
2170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002172 switch (v.getId()) {
2173 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002174 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002175 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2176 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002177 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002178 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002179 return true;
2180 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002181 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002182 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2183 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002184 showPreviews(v);
2185 }
2186 return true;
2187 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002188 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002189 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2190 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2191 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002192 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002193 return true;
2194 }
2195
Joe Onorato9c1289c2009-08-17 11:03:03 -04002196 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 return false;
2198 }
2199
2200 if (!(v instanceof CellLayout)) {
2201 v = (View) v.getParent();
2202 }
2203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204
Michael Jurka0280c3b2010-09-17 15:00:07 -07002205 resetAddInfo();
2206 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002208 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 return true;
2210 }
2211
Michael Jurka0280c3b2010-09-17 15:00:07 -07002212 final View itemUnderLongClick = longClickCellInfo.cell;
2213
Winson Chung304dcde2011-01-07 11:17:23 -08002214 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 if (itemUnderLongClick == null) {
2216 // User long pressed on empty space
2217 mWorkspace.setAllowLongPress(false);
2218 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2219 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002220 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 }
2223 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002224 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002226 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2227 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002228 mAddIntersectCellX = longClickCellInfo.cellX;
2229 mAddIntersectCellY = longClickCellInfo.cellY;
2230 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
2232 }
2233 }
2234 return true;
2235 }
2236
Romain Guye6b8e2f2009-11-10 11:56:55 -08002237 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002238 private void dismissPreview(final View v) {
2239 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002240 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002241 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2242 public void onDismiss() {
2243 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2244 int count = group.getChildCount();
2245 for (int i = 0; i < count; i++) {
2246 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2247 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2249 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2250
2251 v.setTag(R.id.workspace, null);
2252 v.setTag(R.id.icon, null);
2253 window.setOnDismissListener(null);
2254 }
2255 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002256 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002257 }
2258 v.setTag(null);
2259 }
2260
Romain Guyf8e6a802009-12-07 17:48:02 -08002261 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002262 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002263 }
2264
Romain Guya6abce82009-11-10 02:54:41 -08002265 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002266 final Resources resources = getResources();
2267 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002268
Romain Guya6abce82009-11-10 02:54:41 -08002269 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002270
Romain Guy9d31e9f2009-11-11 18:54:28 -08002271 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002272
Romain Guyf8e6a802009-12-07 17:48:02 -08002273 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002274 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002275 int extraW = (int) ((r.left + r.right) * max);
2276 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002277
2278 int aW = cell.getWidth() - extraW;
2279 float w = aW / max;
2280
2281 int width = cell.getWidth();
2282 int height = cell.getHeight();
2283 int x = cell.getLeftPadding();
2284 int y = cell.getTopPadding();
2285 width -= (x + cell.getRightPadding());
2286 height -= (y + cell.getBottomPadding());
2287
2288 float scale = w / width;
2289
2290 int count = end - start;
2291
2292 final float sWidth = width * scale;
2293 float sHeight = height * scale;
2294
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002296
Romain Guye6b8e2f2009-11-10 11:56:55 -08002297 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2298 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002299
2300 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002301 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002302 cell = (CellLayout) workspace.getChildAt(i);
2303
Romain Guyf8e6a802009-12-07 17:48:02 -08002304 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002306
2307 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002308 c.scale(scale, scale);
2309 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002310 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002311
Romain Guy9d31e9f2009-11-11 18:54:28 -08002312 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313 image.setImageBitmap(bitmap);
2314 image.setTag(i);
2315 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 image.setOnFocusChangeListener(handler);
2317 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002318 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319
2320 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002321 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2322
Winson Chungaafa03c2010-06-11 17:34:16 -07002323 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002324 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002325
2326 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002327 p.setContentView(preview);
2328 p.setWidth((int) (sWidth * count + extraW));
2329 p.setHeight((int) (sHeight + extraH));
2330 p.setAnimationStyle(R.style.AnimationPreview);
2331 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002332 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002333 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002334 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002335
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2337 public void onDismiss() {
2338 dismissPreview(anchor);
2339 }
2340 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002341
2342 anchor.setTag(p);
2343 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002344 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002345 }
2346
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002348 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002349
Romain Guye6b8e2f2009-11-10 11:56:55 -08002350 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002351 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002352 }
2353
2354 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002355 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002356 v.post(this);
2357 }
2358
2359 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002360 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002361 }
2362
2363 public void onFocusChange(View v, boolean hasFocus) {
2364 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002365 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002366 }
Romain Guya6abce82009-11-10 02:54:41 -08002367 }
2368 }
2369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 Workspace getWorkspace() {
2371 return mWorkspace;
2372 }
2373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 @Override
2375 protected Dialog onCreateDialog(int id) {
2376 switch (id) {
2377 case DIALOG_CREATE_SHORTCUT:
2378 return new CreateShortcut().createDialog();
2379 case DIALOG_RENAME_FOLDER:
2380 return new RenameFolder().createDialog();
2381 }
2382
2383 return super.onCreateDialog(id);
2384 }
2385
2386 @Override
2387 protected void onPrepareDialog(int id, Dialog dialog) {
2388 switch (id) {
2389 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 break;
2391 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002392 if (mFolderInfo != null) {
2393 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2394 final CharSequence text = mFolderInfo.title;
2395 input.setText(text);
2396 input.setSelection(0, text.length());
2397 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 break;
2399 }
2400 }
2401
2402 void showRenameDialog(FolderInfo info) {
2403 mFolderInfo = info;
2404 mWaitingForResult = true;
2405 showDialog(DIALOG_RENAME_FOLDER);
2406 }
2407
Michael Jurka0280c3b2010-09-17 15:00:07 -07002408 private void showAddDialog(int intersectX, int intersectY) {
2409 resetAddInfo();
2410 mAddIntersectCellX = intersectX;
2411 mAddIntersectCellY = intersectY;
2412 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 mWaitingForResult = true;
2414 showDialog(DIALOG_CREATE_SHORTCUT);
2415 }
2416
Joe Onoratodeb98af2010-02-19 14:59:39 -08002417 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002418 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002419 Bundle bundle = new Bundle();
2420
2421 ArrayList<String> shortcutNames = new ArrayList<String>();
2422 shortcutNames.add(getString(R.string.group_applications));
2423 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2424
2425 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2426 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2427 R.drawable.ic_launcher_application));
2428 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2429
2430 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2431 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002432 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002433 pickIntent.putExtras(bundle);
2434
Joe Onoratodeb98af2010-02-19 14:59:39 -08002435 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002436 }
2437
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 private class RenameFolder {
2439 private EditText mInput;
2440
2441 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2443 mInput = (EditText) layout.findViewById(R.id.folder_name);
2444
2445 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2446 builder.setIcon(0);
2447 builder.setTitle(getString(R.string.rename_folder_title));
2448 builder.setCancelable(true);
2449 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2450 public void onCancel(DialogInterface dialog) {
2451 cleanup();
2452 }
2453 });
2454 builder.setNegativeButton(getString(R.string.cancel_action),
2455 new Dialog.OnClickListener() {
2456 public void onClick(DialogInterface dialog, int which) {
2457 cleanup();
2458 }
2459 }
2460 );
2461 builder.setPositiveButton(getString(R.string.rename_action),
2462 new Dialog.OnClickListener() {
2463 public void onClick(DialogInterface dialog, int which) {
2464 changeFolderName();
2465 }
2466 }
2467 );
2468 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002469
2470 final AlertDialog dialog = builder.create();
2471 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2472 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002473 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002474 mInput.requestFocus();
2475 InputMethodManager inputManager = (InputMethodManager)
2476 getSystemService(Context.INPUT_METHOD_SERVICE);
2477 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002478 }
2479 });
2480
2481 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002482 }
2483
2484 private void changeFolderName() {
2485 final String name = mInput.getText().toString();
2486 if (!TextUtils.isEmpty(name)) {
2487 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002488 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 mFolderInfo.title = name;
2490 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2491
Joe Onorato9c1289c2009-08-17 11:03:03 -04002492 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002493 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002494 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 } else {
2496 final FolderIcon folderIcon = (FolderIcon)
2497 mWorkspace.getViewForTag(mFolderInfo);
2498 if (folderIcon != null) {
2499 folderIcon.setText(name);
2500 getWorkspace().requestLayout();
2501 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002502 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002503 mWorkspaceLoading = true;
2504 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 }
2506 }
2507 }
2508 cleanup();
2509 }
2510
2511 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 dismissDialog(DIALOG_RENAME_FOLDER);
2513 mWaitingForResult = false;
2514 mFolderInfo = null;
2515 }
2516 }
2517
Daniel Sandler843e8602010-06-07 14:59:01 -04002518 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2519 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002520 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002521 }
2522
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002523 // AllAppsView.Watcher
2524 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002525 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2526 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002527 mWorkspace.setVisibility(View.GONE);
2528 }
2529 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002530
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002531 private void showToolbarButton(View button) {
2532 button.setAlpha(1.0f);
2533 button.setVisibility(View.VISIBLE);
2534 button.setFocusable(true);
2535 button.setClickable(true);
2536 }
2537
2538 private void hideToolbarButton(View button) {
2539 button.setAlpha(0.0f);
2540 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2541 button.setVisibility(View.INVISIBLE);
2542 button.setFocusable(false);
2543 button.setClickable(false);
2544 }
2545
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002546 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548 *
2549 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2550 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002551 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002552 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002554 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002555 final boolean showing = show;
2556 final boolean hiding = !show;
2557
2558 final int duration = show ?
2559 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2560 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2561
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002563 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2564 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002565 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002566 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002567 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002569 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002570 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002571 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002574 });
2575 seq.play(anim);
2576 } else {
2577 if (showing) {
2578 showToolbarButton(view);
2579 } else {
2580 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002581 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 }
2584
2585 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002586 * Show/hide the appropriate toolbar buttons for newState.
2587 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 *
2589 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002590 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2591 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002593 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002594 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002596 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002597 final View configureButton = findViewById(R.id.configure_button);
2598
2599 switch (newState) {
2600 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 hideOrShowToolbarButton(true, searchButton, showSeq);
2602 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002603 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002605 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002606 mDeleteZone.setDragAndDropEnabled(true);
2607 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002608 break;
2609 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002610 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002611 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002612 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002614 mDeleteZone.setDragAndDropEnabled(false);
2615 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 break;
2617 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002618 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002619 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002620 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002621 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002622 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002623 break;
2624 }
2625 }
2626
2627 /**
2628 * Helper method for the cameraZoomIn/cameraZoomOut animations
2629 * @param view The view being animated
2630 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2631 * @param scaleFactor The scale factor used for the zoom
2632 */
2633 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2634 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002635
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002637 // Set pivotY so that at the starting zoom factor, the view is partially
2638 // visible. Modifying initialHeightFactor changes how much of the view is
2639 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 if (state == State.ALL_APPS) {
Adam Cohenf16e5712011-01-13 13:31:45 -08002641 final float initialHeightFactor = 0.165f;
2642 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002644 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002645 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 }
2647 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002648
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002649 /**
2650 * Zoom the camera out from the workspace to reveal 'toView'.
2651 * Assumes that the view to show is anchored at either the very top or very bottom
2652 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002654 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002656 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002658
2659 final int duration = toAllApps ?
2660 res.getInteger(R.integer.config_allAppsZoomInTime) :
2661 res.getInteger(R.integer.config_customizeZoomInTime);
2662
2663 final float scale = toAllApps ?
2664 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2665 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2666
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002667 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002668
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 setPivotsForZoom(toView, toState, scale);
2670
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002671 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002672 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Adam Cohenf16e5712011-01-13 13:31:45 -08002673 toView.setAlpha(0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002675 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002677
2678 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002679 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2680 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2681 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
Adam Cohenf16e5712011-01-13 13:31:45 -08002682
Chet Haase472b2812010-10-14 07:02:04 -07002683 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002684
Adam Cohenf16e5712011-01-13 13:31:45 -08002685 if (toAllApps) {
2686 ObjectAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2687 PropertyValuesHolder.ofFloat("alpha", 1.0f));
2688 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2689 alphaAnim.setDuration(duration);
2690 alphaAnim.start();
2691 }
2692
Adam Cohen61033d32010-11-15 18:29:44 -08002693 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002694 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002695 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002696 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002697 // Prepare the position
2698 toView.setTranslationX(0.0f);
2699 toView.setTranslationY(0.0f);
2700 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002701 if (!toAllApps) {
2702 toView.setAlpha(1.0f);
2703 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002704 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002705 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002706 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002707 // If we don't set the final scale values here, if this animation is cancelled
2708 // it will have the wrong scale value and subsequent cameraPan animations will
2709 // not fix that
2710 toView.setScaleX(1.0f);
2711 toView.setScaleY(1.0f);
2712 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002713 });
2714
Chet Haaseb1254a62010-09-07 13:35:00 -07002715 AnimatorSet toolbarHideAnim = new AnimatorSet();
2716 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2718
2719 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002720 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002722 if (mStateAnimation != null) mStateAnimation.cancel();
2723 mStateAnimation = new AnimatorSet();
2724 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2725 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726
2727 // Show the new toolbar buttons just as the main animation is ending
2728 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002729 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2730 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002731 } else {
2732 toView.setTranslationX(0.0f);
2733 toView.setTranslationY(0.0f);
2734 toView.setScaleX(1.0f);
2735 toView.setScaleY(1.0f);
2736 toView.setVisibility(View.VISIBLE);
2737 hideAndShowToolbarButtons(toState, null, null);
2738 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002739 mWorkspace.setVerticalWallpaperOffset(toAllApps ?
2740 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002741 }
2742
2743 /**
2744 * Zoom the camera back into the workspace, hiding 'fromView'.
2745 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2747 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002748 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002749 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002750 cameraZoomIn(fromState, animated, false);
2751 }
2752
2753 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002754 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002755 final boolean fromAllApps = (fromState == State.ALL_APPS);
2756
2757 int duration = fromAllApps ?
2758 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2759 res.getInteger(R.integer.config_customizeZoomOutTime);
2760
2761 float scaleFactor = fromAllApps ?
2762 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2763 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2764
2765 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002766
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002767 mCustomizePagedView.endChoiceMode();
2768 mAllAppsPagedView.endChoiceMode();
2769
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002770 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771
Michael Jurkad3ef3062010-11-23 16:23:58 -08002772 if (!springLoaded) {
2773 mWorkspace.unshrink(animated);
2774 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775
2776 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002777 if (mStateAnimation != null) mStateAnimation.cancel();
2778 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002779 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2780 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2781 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2782 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002783 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2784
2785 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2786 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2787 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Adam Cohenf16e5712011-01-13 13:31:45 -08002788 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002789 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002790 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002791 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002793 }
2794 });
2795
Chet Haaseb1254a62010-09-07 13:35:00 -07002796 AnimatorSet toolbarHideAnim = new AnimatorSet();
2797 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002798 if (!springLoaded) {
2799 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2800 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002801
Adam Cohen61033d32010-11-15 18:29:44 -08002802 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803
2804 // Show the new toolbar buttons at the very end of the whole animation
2805 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2806 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002807 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2808 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 } else {
2810 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002811 if (!springLoaded) {
2812 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2813 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002815 mWorkspace.setVerticalWallpaperOffset(Workspace.WallpaperVerticalOffset.MIDDLE);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002816 }
2817
2818 /**
2819 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2820 * This is the transition used on xlarge screens to go between all apps and
2821 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002822 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2823 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2824 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827 final Resources res = getResources();
2828 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002829 final int workspaceHeight = mWorkspace.getHeight();
2830
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002831 final boolean fromAllApps = (fromState == State.ALL_APPS);
2832 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2833 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002834
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002835 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2836 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2837 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2838 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002839
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002840 mCustomizePagedView.endChoiceMode();
2841 mAllAppsPagedView.endChoiceMode();
2842
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002844 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002845 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002846 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002847 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002848
2849 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002850 if (mStateAnimation != null) mStateAnimation.cancel();
2851 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002852 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002853 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002854 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002855 toView.setVisibility(View.VISIBLE);
2856 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002857 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002858 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002859 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002860 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002861 fromView.setVisibility(View.GONE);
2862 }
2863 });
2864
Chet Haaseb1254a62010-09-07 13:35:00 -07002865 AnimatorSet toolbarHideAnim = new AnimatorSet();
2866 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002867 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2868
Chet Haase472b2812010-10-14 07:02:04 -07002869 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2870 fromViewStartY, fromViewEndY);
2871 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002872 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2873 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2874 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2875 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2876 );
Chet Haase472b2812010-10-14 07:02:04 -07002877 fromAnim.setDuration(duration);
2878 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002879
2880 // Show the new toolbar buttons just as the main animation is ending
2881 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002882 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2883 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002884 } else {
2885 fromView.setY(fromViewEndY);
2886 fromView.setVisibility(View.GONE);
2887 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002888 toView.setScaleX(1.0f);
2889 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002890 toView.setVisibility(View.VISIBLE);
2891 hideAndShowToolbarButtons(toState, null, null);
2892 }
Michael Jurkac5b262c2011-01-12 20:24:50 -08002893 mWorkspace.setVerticalWallpaperOffset((toState == State.ALL_APPS) ?
2894 Workspace.WallpaperVerticalOffset.TOP : Workspace.WallpaperVerticalOffset.BOTTOM);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002895 }
2896
2897 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002898 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002899 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002900 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002901
Michael Jurka79212d82010-07-30 16:36:20 -07002902 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002903 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002904 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002905 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002906 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002907 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002908 } else {
2909 mAllAppsGrid.zoom(1.0f, animated);
2910 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002911
Romain Guyc16fea72010-03-12 17:17:56 -08002912 ((View) mAllAppsGrid).setFocusable(true);
2913 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002914
Joe Onorato7c312c12009-08-13 21:36:53 -07002915 // TODO: fade these two too
2916 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002917
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002918 // Change the state *after* we've called all the transition code
2919 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002920
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002921 // send an accessibility event to announce the context change
2922 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2923 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002924
2925 void showWorkspace(boolean animated) {
2926 showWorkspace(animated, null);
2927 }
2928
2929 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002930 if (layout != null) {
2931 // always animated, but that's ok since we never specify a layout and
2932 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002933 mWorkspace.unshrink(layout);
2934 } else {
2935 mWorkspace.unshrink(animated);
2936 }
2937 if (mState == State.ALL_APPS) {
2938 closeAllApps(animated);
2939 } else if (mState == State.CUSTOMIZE) {
2940 hideCustomizationDrawer(animated);
2941 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002942
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002943 // Change the state *after* we've called all the transition code
2944 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002945
2946 // send an accessibility event to announce the context change
2947 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002948 }
2949
Michael Jurkad3ef3062010-11-23 16:23:58 -08002950 void enterSpringLoadedDragMode(CellLayout layout) {
2951 mWorkspace.enterSpringLoadedDragMode(layout);
2952 if (mState == State.ALL_APPS) {
2953 cameraZoomIn(State.ALL_APPS, true, true);
2954 mState = State.ALL_APPS_SPRING_LOADED;
2955 } else if (mState == State.CUSTOMIZE) {
2956 cameraZoomIn(State.CUSTOMIZE, true, true);
2957 mState = State.CUSTOMIZE_SPRING_LOADED;
2958 }/* else {
2959 // we're already in spring loaded mode; don't do anything
2960 }*/
2961 }
2962
2963 void exitSpringLoadedDragMode() {
2964 if (mState == State.ALL_APPS_SPRING_LOADED) {
2965 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2966 cameraZoomOut(State.ALL_APPS, true);
2967 mState = State.ALL_APPS;
2968 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2969 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2970 cameraZoomOut(State.CUSTOMIZE, true);
2971 mState = State.CUSTOMIZE;
2972 }/* else {
2973 // we're not in spring loaded mode; don't do anything
2974 }*/
2975 }
2976
Joe Onoratob2061212009-11-24 16:13:54 -05002977 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002978 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002979 * - Home from workspace
2980 * - from center screen
2981 * - from other screens
2982 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002983 * - from center screen
2984 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002985 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002986 * - from center screen
2987 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002988 * - Launch app from workspace and quit
2989 * - with back
2990 * - with home
2991 * - Launch app from all apps and quit
2992 * - with back
2993 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002994 * - Go to a screen that's not the default, then all
2995 * apps, and launch and app, and go back
2996 * - with back
2997 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002998 * - On workspace, long press power and go back
2999 * - with back
3000 * - with home
3001 * - On all apps, long press power and go back
3002 * - with back
3003 * - with home
3004 * - On workspace, power off
3005 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003006 * - Launch an app and turn off the screen while in that app
3007 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003008 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003009 * - From all apps
3010 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003011 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3012 * - From all apps
3013 * - From the center workspace
3014 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003015 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003016 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003017 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003018 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003019 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003020 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003021 } else {
3022 mAllAppsGrid.zoom(0.0f, animated);
3023 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003024 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07003025 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003026 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003027 }
3028
Joe Onorato7c312c12009-08-13 21:36:53 -07003029 void lockAllApps() {
3030 // TODO
3031 }
3032
3033 void unlockAllApps() {
3034 // TODO
3035 }
3036
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003037 // Show the customization drawer (only exists in x-large configuration)
3038 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003039 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003040 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003041 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003042 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003043 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003044 // Change the state *after* we've called all the transition code
3045 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003046 }
3047
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003048 // Hide the customization drawer (only exists in x-large configuration)
3049 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003050 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003051 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003052 }
3053 }
3054
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003055 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3056 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3057 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003058 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003059 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003060
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003061 void onWorkspaceClick(CellLayout layout) {
3062 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003063 }
3064
Michael Jurka4ef207b2010-11-29 17:05:45 -08003065 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003066 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003067 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3068 ImageView button = (ImageView) findViewById(buttonId);
3069 Drawable toolbarIcon = null;
3070 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003071 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003072 // Look for the toolbar icon specified in the activity meta-data
3073 Bundle metaData = packageManager.getActivityInfo(
3074 activityName, PackageManager.GET_META_DATA).metaData;
3075 if (metaData != null) {
3076 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3077 if (iconResId != 0) {
3078 Resources res = packageManager.getResourcesForActivity(activityName);
3079 toolbarIcon = res.getDrawable(iconResId);
3080 }
3081 }
3082 } catch (NameNotFoundException e) {
3083 // Do nothing
3084 }
3085 // If we were unable to find the icon via the meta-data, use a generic one
3086 if (toolbarIcon == null) {
3087 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003088 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 } else {
3090 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003091 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003092 }
3093 }
3094
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003095 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003096 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003097 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003098 }
3099
Michael Jurka0423dcf2010-10-05 14:56:18 -07003100 private void updateGlobalSearchIcon() {
3101 if (LauncherApplication.isScreenXLarge()) {
3102 final SearchManager searchManager =
3103 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3104 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003105 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003106 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003107 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003108 } else {
3109 findViewById(R.id.search_button).setVisibility(View.GONE);
3110 }
3111 }
3112 }
3113
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003114 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003115 updateButtonWithDrawable(R.id.search_button, d);
3116 }
3117
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003118 private void updateVoiceSearchIcon() {
3119 if (LauncherApplication.isScreenXLarge()) {
3120 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3121 ComponentName activityName = intent.resolveActivity(getPackageManager());
3122 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003123 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003124 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3125 } else {
3126 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003127 }
3128 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003129 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003130
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003131 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003132 updateButtonWithDrawable(R.id.voice_button, d);
3133 }
3134
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003135 /**
3136 * Sets the app market icon (shown when all apps is visible on x-large screens)
3137 */
3138 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003139 if (LauncherApplication.isScreenXLarge()) {
3140 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3141 // Find the app market activity by resolving an intent.
3142 // (If multiple app markets are installed, it will return the ResolverActivity.)
3143 ComponentName activityName = intent.resolveActivity(getPackageManager());
3144 if (activityName != null) {
3145 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003146 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003147 R.id.market_button, activityName, R.drawable.app_market_generic);
3148 }
3149 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003150 }
3151
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003152 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003153 updateButtonWithDrawable(R.id.market_button, d);
3154 }
3155
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003156 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003157 * Displays the shortcut creation dialog and launches, if necessary, the
3158 * appropriate activity.
3159 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003160 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003161 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3162 DialogInterface.OnShowListener {
3163
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003166 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003169 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3170 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003171 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 builder.setInverseBackgroundForced(true);
3174
3175 AlertDialog dialog = builder.create();
3176 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003177 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003178 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003180 return dialog;
3181 }
3182
3183 public void onCancel(DialogInterface dialog) {
3184 mWaitingForResult = false;
3185 cleanup();
3186 }
3187
Romain Guycbb89e42009-06-08 15:52:54 -07003188 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003189 }
3190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003192 try {
3193 dismissDialog(DIALOG_CREATE_SHORTCUT);
3194 } catch (Exception e) {
3195 // An exception is thrown if the dialog is not visible, which is fine
3196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 }
3198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003199 /**
3200 * Handle the action clicked in the "Add to home" dialog.
3201 */
3202 public void onClick(DialogInterface dialog, int which) {
3203 Resources res = getResources();
3204 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003205
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003206 switch (which) {
3207 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003208 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003209 break;
3210 }
Romain Guycbb89e42009-06-08 15:52:54 -07003211
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003212 case AddAdapter.ITEM_APPWIDGET: {
3213 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003214
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003215 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3216 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003217 // start the pick activity
3218 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3219 break;
3220 }
Romain Guycbb89e42009-06-08 15:52:54 -07003221
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003222 case AddAdapter.ITEM_LIVE_FOLDER: {
3223 // Insert extra item to handle inserting folder
3224 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003225
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003226 ArrayList<String> shortcutNames = new ArrayList<String>();
3227 shortcutNames.add(res.getString(R.string.group_folder));
3228 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003229
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003230 ArrayList<ShortcutIconResource> shortcutIcons =
3231 new ArrayList<ShortcutIconResource>();
3232 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3233 R.drawable.ic_launcher_folder));
3234 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3235
3236 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3237 pickIntent.putExtra(Intent.EXTRA_INTENT,
3238 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3239 pickIntent.putExtra(Intent.EXTRA_TITLE,
3240 getText(R.string.title_select_live_folder));
3241 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003242
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003243 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3244 break;
3245 }
3246
3247 case AddAdapter.ITEM_WALLPAPER: {
3248 startWallpaper();
3249 break;
3250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 }
3252 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003253
3254 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003255 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257 }
3258
3259 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003260 * Receives notifications when applications are added/removed.
3261 */
3262 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3263 @Override
3264 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003265 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003266 String reason = intent.getStringExtra("reason");
3267 if (!"homekey".equals(reason)) {
3268 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003269 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003270 animate = false;
3271 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003272 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003273 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003274 }
3275 }
3276
3277 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003278 * Receives notifications whenever the appwidgets are reset.
3279 */
3280 private class AppWidgetResetObserver extends ContentObserver {
3281 public AppWidgetResetObserver() {
3282 super(new Handler());
3283 }
3284
3285 @Override
3286 public void onChange(boolean selfChange) {
3287 onAppWidgetReset();
3288 }
3289 }
3290
3291 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003292 * If the activity is currently paused, signal that we need to re-run the loader
3293 * in onResume.
3294 *
3295 * This needs to be called from incoming places where resources might have been loaded
3296 * while we are paused. That is becaues the Configuration might be wrong
3297 * when we're not running, and if it comes back to what it was when we
3298 * were paused, we are not restarted.
3299 *
3300 * Implementation of the method from LauncherModel.Callbacks.
3301 *
3302 * @return true if we are currently paused. The caller might be able to
3303 * skip some work in that case since we will come back again.
3304 */
3305 public boolean setLoadOnResume() {
3306 if (mPaused) {
3307 Log.i(TAG, "setLoadOnResume");
3308 mOnResumeNeedsLoad = true;
3309 return true;
3310 } else {
3311 return false;
3312 }
3313 }
3314
3315 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003317 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003318 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003319 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003320 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003321 } else {
3322 return SCREEN_COUNT / 2;
3323 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003325
Winson Chunga12a2502010-12-20 14:41:35 -08003326 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003327 mAllAppsPagedView = view;
3328 }
3329
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 /**
3331 * Refreshes the shortcuts shown on the workspace.
3332 *
3333 * Implementation of the method from LauncherModel.Callbacks.
3334 */
3335 public void startBinding() {
3336 final Workspace workspace = mWorkspace;
3337 int count = workspace.getChildCount();
3338 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003339 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3341 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003342
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 if (DEBUG_USER_INTERFACE) {
3344 android.widget.Button finishButton = new android.widget.Button(this);
3345 finishButton.setText("Finish");
3346 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3347
3348 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3349 public void onClick(View v) {
3350 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003351 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003353 }
3354 }
3355
3356 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 * Bind the items start-end from the list.
3358 *
3359 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003360 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3362
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003363 setLoadOnResume();
3364
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 final Workspace workspace = mWorkspace;
3366
3367 for (int i=start; i<end; i++) {
3368 final ItemInfo item = shortcuts.get(i);
3369 mDesktopItems.add(item);
3370 switch (item.itemType) {
3371 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3372 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003373 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3375 false);
3376 break;
3377 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3378 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003379 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003380 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3382 false);
3383 break;
3384 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3385 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3386 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003387 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 (LiveFolderInfo) item);
3389 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3390 false);
3391 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003393 }
3394
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003396 }
3397
Joe Onorato9c1289c2009-08-17 11:03:03 -04003398 /**
3399 * Implementation of the method from LauncherModel.Callbacks.
3400 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003401 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003402 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003403 sFolders.clear();
3404 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003405 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406
3407 /**
3408 * Add the views for a widget to the workspace.
3409 *
3410 * Implementation of the method from LauncherModel.Callbacks.
3411 */
3412 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003413 setLoadOnResume();
3414
Daniel Sandler843e8602010-06-07 14:59:01 -04003415 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3416 if (DEBUG_WIDGETS) {
3417 Log.d(TAG, "bindAppWidget: " + item);
3418 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 final Workspace workspace = mWorkspace;
3420
3421 final int appWidgetId = item.appWidgetId;
3422 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003423 if (DEBUG_WIDGETS) {
3424 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3425 }
3426
Joe Onorato9c1289c2009-08-17 11:03:03 -04003427 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3428
Joe Onorato9c1289c2009-08-17 11:03:03 -04003429 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3430 item.hostView.setTag(item);
3431
3432 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3433 item.cellY, item.spanX, item.spanY, false);
3434
Adam Cohended9f8d2010-11-03 13:25:16 -07003435 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3436
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 workspace.requestLayout();
3438
3439 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003440
3441 if (DEBUG_WIDGETS) {
3442 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3443 + (SystemClock.uptimeMillis()-start) + "ms");
3444 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003445 }
3446
3447 /**
3448 * Callback saying that there aren't any more items to bind.
3449 *
3450 * Implementation of the method from LauncherModel.Callbacks.
3451 */
3452 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003453 setLoadOnResume();
3454
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455 if (mSavedState != null) {
3456 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003457 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 }
3459
3460 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3461 if (userFolders != null) {
3462 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003463 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003464 if (info != null) {
3465 openFolder(info);
3466 }
3467 }
3468 final Folder openFolder = mWorkspace.getOpenFolder();
3469 if (openFolder != null) {
3470 openFolder.requestFocus();
3471 }
3472 }
3473
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 mSavedState = null;
3475 }
3476
3477 if (mSavedInstanceState != null) {
3478 super.onRestoreInstanceState(mSavedInstanceState);
3479 mSavedInstanceState = null;
3480 }
3481
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 mWorkspaceLoading = false;
3483 }
3484
3485 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003486 * Updates the icons on the launcher that are affected by changes to the package list
3487 * on the device.
3488 */
3489 private void updateIconsAffectedByPackageManagerChanges() {
3490 updateAppMarketIcon();
3491 updateGlobalSearchIcon();
3492 updateVoiceSearchIcon();
3493 }
3494
3495 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 * Add the icons for all apps.
3497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 */
3500 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003501 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003502 if (mCustomizePagedView != null) {
3503 mCustomizePagedView.setApps(apps);
3504 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003505 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003506 }
3507
3508 /**
3509 * A package was installed.
3510 *
3511 * Implementation of the method from LauncherModel.Callbacks.
3512 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003513 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003514 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003516 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003517 if (mCustomizePagedView != null) {
3518 mCustomizePagedView.addApps(apps);
3519 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003520 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003521 }
3522
3523 /**
3524 * A package was updated.
3525 *
3526 * Implementation of the method from LauncherModel.Callbacks.
3527 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003528 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003529 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003530 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003531 mWorkspace.updateShortcuts(apps);
3532 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003533 if (mCustomizePagedView != null) {
3534 mCustomizePagedView.updateApps(apps);
3535 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003536 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003537 }
3538
3539 /**
3540 * A package was uninstalled.
3541 *
3542 * Implementation of the method from LauncherModel.Callbacks.
3543 */
Joe Onorato36115782010-06-17 13:28:48 -04003544 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003546 if (permanent) {
3547 mWorkspace.removeItems(apps);
3548 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003549 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003550 if (mCustomizePagedView != null) {
3551 mCustomizePagedView.removeApps(apps);
3552 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003553 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003554 }
Joe Onoratobe386092009-11-17 17:32:16 -08003555
3556 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003557 * A number of packages were updated.
3558 */
3559 public void bindPackagesUpdated() {
3560 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003561 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003562 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003563 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003564 }
3565
3566 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003567 * Prints out out state for debugging.
3568 */
3569 public void dumpState() {
3570 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003571 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003572 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3573 Log.d(TAG, "mRestoring=" + mRestoring);
3574 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3575 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3576 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003577 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003578 mModel.dumpState();
3579 mAllAppsGrid.dumpState();
3580 Log.d(TAG, "END launcher2 dump state");
3581 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003582}