blob: 3c1c336574e0d242aac53bd1004ad93bd0adec4a [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurkaaf91de02010-11-23 16:23:58 -080020import com.android.common.Search;
21import com.android.launcher.R;
Winson Chunga12a2502010-12-20 14:41:35 -080022import com.android.launcher2.CustomizePagedView.CustomizationType;
Michael Jurkaaf91de02010-11-23 16:23:58 -080023import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070024
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080026import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070029import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070030import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070031import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.app.Dialog;
34import android.app.SearchManager;
35import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070036import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070037import android.appwidget.AppWidgetManager;
38import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080040import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070041import android.content.ClipData;
42import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040043import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080044import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.Context;
46import android.content.DialogInterface;
47import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070048import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070049import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070050import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070052import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070053import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070055import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040056import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080059import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080061import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040063import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070064import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020066import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080067import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070068import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040070import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080071import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070072import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070073import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080074import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.text.Selection;
76import android.text.SpannableStringBuilder;
77import android.text.TextUtils;
78import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070079import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080081import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.KeyEvent;
83import android.view.LayoutInflater;
84import android.view.Menu;
85import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070086import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.View;
88import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070089import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070092import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070094import android.widget.ImageView;
95import android.widget.LinearLayout;
96import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070097import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080098import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -070099import android.widget.TextView;
100import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700101import android.widget.TabHost.OnTabChangeListener;
102import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700103
Michael Jurkaaf91de02010-11-23 16:23:58 -0800104import java.io.DataInputStream;
105import java.io.DataOutputStream;
106import java.io.FileNotFoundException;
107import java.io.IOException;
108import java.util.ArrayList;
109import java.util.HashMap;
110import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800111
Adam Cohended9f8d2010-11-03 13:25:16 -0700112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113/**
114 * Default launcher application.
115 */
Michael Jurka946ad472010-07-09 18:05:18 -0700116public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700117 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
118 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800119 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700120 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400123 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800127 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700130 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
131 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
133 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700134 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 private static final int REQUEST_CREATE_SHORTCUT = 1;
137 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final int REQUEST_PICK_APPLICATION = 6;
140 private static final int REQUEST_PICK_SHORTCUT = 7;
141 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700142 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700143 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
145 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
146
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800147 static final int SCREEN_COUNT = 5;
148 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Joe Onorato7c312c12009-08-13 21:36:53 -0700150 static final int DIALOG_CREATE_SHORTCUT = 1;
151 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Romain Guy98d01652009-06-30 16:21:04 -0700153 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
155 // Type: int
156 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700157 // Type: int
158 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159 // Type: long
160 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: boolean
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
169 // Type: long
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
171
Winson Chung80baf5a2010-08-09 16:03:15 -0700172 // tags for the customization tabs
173 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700174 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700175 private static final String SHORTCUTS_TAG = "shortcuts";
176 private static final String WALLPAPERS_TAG = "wallpapers";
177
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700178 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
179
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700180 /** The different states that Launcher can be in. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800181 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
182 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700183 private State mState = State.WORKSPACE;
184 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185
Joe Onorato9c1289c2009-08-17 11:03:03 -0400186 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800189 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800191 private final BroadcastReceiver mCloseSystemDialogsReceiver
192 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800193 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private LayoutInflater mInflater;
196
Joe Onorato00acb122009-08-04 16:04:30 -0400197 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700200 private AppWidgetManager mAppWidgetManager;
201 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
Michael Jurka0280c3b2010-09-17 15:00:07 -0700203 private int mAddScreen = -1;
204 private int mAddIntersectCellX = -1;
205 private int mAddIntersectCellY = -1;
206 private int[] mAddDropPosition;
207 private int[] mTmpAddItemCellCoordinates = new int[2];
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private FolderInfo mFolderInfo;
210
Joe Onorato7c312c12009-08-13 21:36:53 -0700211 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700212 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700213 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700214 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700215 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700216
Winson Chunga12a2502010-12-20 14:41:35 -0800217 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700218 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private Bundle mSavedState;
221
222 private SpannableStringBuilder mDefaultKeySsb = null;
223
Joe Onorato9c1289c2009-08-17 11:03:03 -0400224 private boolean mWorkspaceLoading = true;
225
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800226 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227 private boolean mRestoring;
228 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700229 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
231 private Bundle mSavedInstanceState;
232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800234 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800235 private boolean mUserPresent = true;
236 private boolean mVisible = false;
237 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static LocaleConfiguration sLocaleConfiguration = null;
240
Romain Guy84f296c2009-11-04 15:00:44 -0800241 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700242
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700243 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700244
Romain Guy1fbc1c82009-11-09 20:43:08 -0800245 private ImageView mPreviousView;
246 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500247
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400248 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400249 private String[] mHotseatConfig = null;
250 private Intent[] mHotseats = null;
251 private Drawable[] mHotseatIcons = null;
252 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400253
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700254 private Intent mAppMarketIntent = null;
255
Adam Cohended9f8d2010-11-03 13:25:16 -0700256 // Related to the auto-advancing of widgets
257 private final int ADVANCE_MSG = 1;
258 private final int mAdvanceInterval = 20000;
259 private final int mAdvanceStagger = 250;
260 private long mAutoAdvanceSentTime;
261 private long mAutoAdvanceTimeLeft = -1;
262 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
263 new HashMap<View, AppWidgetProviderInfo>();
264
Michael Jurka4ef207b2010-11-29 17:05:45 -0800265 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800266 private static Drawable.ConstantState sGlobalSearchIcon;
267 private static Drawable.ConstantState sVoiceSearchIcon;
268 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800269
Winson Chunga12a2502010-12-20 14:41:35 -0800270 private CustomizationType getCustomizeFilterForTabTag(String tag) {
271 if (tag.equals(WIDGETS_TAG)) {
272 return CustomizationType.WidgetCustomization;
273 } else if (tag.equals(APPLICATIONS_TAG)) {
274 return CustomizationType.ApplicationCustomization;
275 } else if (tag.equals(WALLPAPERS_TAG)) {
276 return CustomizePagedView.CustomizationType.WallpaperCustomization;
277 } else if (tag.equals(SHORTCUTS_TAG)) {
278 return CustomizePagedView.CustomizationType.ShortcutCustomization;
279 }
280 return CustomizationType.WidgetCustomization;
281 }
282
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 @Override
284 protected void onCreate(Bundle savedInstanceState) {
285 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700286
Winson Chungaafa03c2010-06-11 17:34:16 -0700287 if (LauncherApplication.isInPlaceRotationEnabled()) {
288 // hide the status bar (temporary until we get the status bar design figured out)
289 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
290 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
291 }
292
Joe Onorato0589f0f2010-02-08 13:44:00 -0800293 LauncherApplication app = ((LauncherApplication)getApplication());
294 mModel = app.setLauncher(this);
295 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400296 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700298
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700299 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700300 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
301 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200304 android.os.Debug.startMethodTracing(
305 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 }
307
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400308 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 checkForLocaleChange();
310 setWallpaperDimension();
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
386 mCustomizePagedView.update();
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 if (PROFILE_STARTUP) {
389 android.os.Debug.stopMethodTracing();
390 }
391
392 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400393 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 }
395
396 // For handling default keys
397 mDefaultKeySsb = new SpannableStringBuilder();
398 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800399
400 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
401 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800402
403 // If we have a saved version of these external icons, we load them up immediately
404 if (LauncherApplication.isScreenXLarge()) {
405 if (sGlobalSearchIcon != null) {
406 updateGlobalSearchIcon(sGlobalSearchIcon);
407 }
408 if (sVoiceSearchIcon != null) {
409 updateVoiceSearchIcon(sVoiceSearchIcon);
410 }
411 if (sAppMarketIcon != null) {
412 updateAppMarketIcon(sAppMarketIcon);
413 }
414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 }
Romain Guycbb89e42009-06-08 15:52:54 -0700416
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 if (sLocaleConfiguration == null) {
419 new AsyncTask<Void, Void, LocaleConfiguration>() {
420 @Override
421 protected LocaleConfiguration doInBackground(Void... unused) {
422 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
423 readConfiguration(Launcher.this, localeConfiguration);
424 return localeConfiguration;
425 }
426
427 @Override
428 protected void onPostExecute(LocaleConfiguration result) {
429 sLocaleConfiguration = result;
430 checkForLocaleChange(); // recursive, but now with a locale configuration
431 }
432 }.execute();
433 return;
434 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 final Configuration configuration = getResources().getConfiguration();
437
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700438 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 final String locale = configuration.locale.toString();
440
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700441 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 final int mcc = configuration.mcc;
443
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700444 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 final int mnc = configuration.mnc;
446
Romain Guy84f296c2009-11-04 15:00:44 -0800447 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448
Romain Guy84f296c2009-11-04 15:00:44 -0800449 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700450 sLocaleConfiguration.locale = locale;
451 sLocaleConfiguration.mcc = mcc;
452 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700453
Joe Onorato0589f0f2010-02-08 13:44:00 -0800454 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400455 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456
457 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
458 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700459 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700460 public void run() {
461 writeConfiguration(Launcher.this, localeConfiguration);
462 }
463 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700464 }
465 }
466
467 private static class LocaleConfiguration {
468 public String locale;
469 public int mcc = -1;
470 public int mnc = -1;
471 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700472
Romain Guy98d01652009-06-30 16:21:04 -0700473 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
474 DataInputStream in = null;
475 try {
476 in = new DataInputStream(context.openFileInput(PREFERENCES));
477 configuration.locale = in.readUTF();
478 configuration.mcc = in.readInt();
479 configuration.mnc = in.readInt();
480 } catch (FileNotFoundException e) {
481 // Ignore
482 } catch (IOException e) {
483 // Ignore
484 } finally {
485 if (in != null) {
486 try {
487 in.close();
488 } catch (IOException e) {
489 // Ignore
490 }
491 }
492 }
493 }
494
495 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
496 DataOutputStream out = null;
497 try {
498 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
499 out.writeUTF(configuration.locale);
500 out.writeInt(configuration.mcc);
501 out.writeInt(configuration.mnc);
502 out.flush();
503 } catch (FileNotFoundException e) {
504 // Ignore
505 } catch (IOException e) {
506 //noinspection ResultOfMethodCallIgnored
507 context.getFileStreamPath(PREFERENCES).delete();
508 } finally {
509 if (out != null) {
510 try {
511 out.close();
512 } catch (IOException e) {
513 // Ignore
514 }
515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800516 }
517 }
518
519 static int getScreen() {
520 synchronized (sLock) {
521 return sScreen;
522 }
523 }
524
525 static void setScreen(int screen) {
526 synchronized (sLock) {
527 sScreen = screen;
528 }
529 }
530
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700532 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533
534 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700535 // TODO: Put back when we decide about scrolling the wallpaper
536 // boolean isPortrait = display.getWidth() < display.getHeight();
537 // final int width = isPortrait ? display.getWidth() : display.getHeight();
538 // final int height = isPortrait ? display.getHeight() : display.getWidth();
539 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
540 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
542
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 // Note: This doesn't do all the client-id magic that BrowserProvider does
544 // in Browser. (http://b/2425179)
545 private Uri getDefaultBrowserUri() {
546 String url = getString(R.string.default_browser_url);
547 if (url.indexOf("{CID}") != -1) {
548 url = url.replace("{CID}", "android-google");
549 }
550 return Uri.parse(url);
551 }
552
553 // Load the Intent templates from arrays.xml to populate the hotseats. For
554 // each Intent, if it resolves to a single app, use that as the launch
555 // intent & use that app's label as the contentDescription. Otherwise,
556 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400557 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400558 if (mHotseatConfig == null) {
559 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
560 if (mHotseatConfig.length > 0) {
561 mHotseats = new Intent[mHotseatConfig.length];
562 mHotseatLabels = new CharSequence[mHotseatConfig.length];
563 mHotseatIcons = new Drawable[mHotseatConfig.length];
564 } else {
565 mHotseats = null;
566 mHotseatIcons = null;
567 mHotseatLabels = null;
568 }
569
570 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
571 for (int i=0; i<mHotseatConfig.length; i++) {
572 // load icon for this slot; currently unrelated to the actual activity
573 try {
574 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
575 } catch (ArrayIndexOutOfBoundsException ex) {
576 Log.w(TAG, "Missing hotseat_icons array item #" + i);
577 mHotseatIcons[i] = null;
578 }
579 }
580 hotseatIconDrawables.recycle();
581 }
582
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 for (int i=0; i<mHotseatConfig.length; i++) {
585 Intent intent = null;
586 if (mHotseatConfig[i].equals("*BROWSER*")) {
587 // magic value meaning "launch user's default web browser"
588 // replace it with a generic web request so we can see if there is indeed a default
589 String defaultUri = getString(R.string.default_browser_url);
590 intent = new Intent(
591 Intent.ACTION_VIEW,
592 ((defaultUri != null)
593 ? Uri.parse(defaultUri)
594 : getDefaultBrowserUri())
595 ).addCategory(Intent.CATEGORY_BROWSABLE);
596 // note: if the user launches this without a default set, she
597 // will always be taken to the default URL above; this is
598 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 // URL is unavoidably sent to the chosen app.
601 } else {
602 try {
603 intent = Intent.parseUri(mHotseatConfig[i], 0);
604 } catch (java.net.URISyntaxException ex) {
605 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
606 // bogus; leave intent=null
607 }
608 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700609
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 if (intent == null) {
611 mHotseats[i] = null;
612 mHotseatLabels[i] = getText(R.string.activity_not_found);
613 continue;
614 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400615
616 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700617 Log.d(TAG, "loadHotseats: hotseat " + i
618 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400620 + "]");
621 }
622
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400623 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
624 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700625 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 Log.d(TAG, "Best match for intent: " + bestMatch);
627 Log.d(TAG, "All matches: ");
628 for (ResolveInfo ri : allMatches) {
629 Log.d(TAG, " --> " + ri);
630 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400632 // did this resolve to a single app, or the resolver?
633 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 // can't find any activity to handle this. let's leave the
635 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400636 // to launch.
637 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 // set accessibility text to "Not installed"
640 mHotseatLabels[i] = getText(R.string.activity_not_found);
641 } else {
642 boolean found = false;
643 for (ResolveInfo ri : allMatches) {
644 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
645 && bestMatch.activityInfo.applicationInfo.packageName
646 .equals(ri.activityInfo.applicationInfo.packageName)) {
647 found = true;
648 break;
649 }
650 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700651
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400652 if (!found) {
653 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
654 // the bestMatch is probably the ResolveActivity, meaning the
655 // user has not yet selected a default
656 // so: we'll keep the original intent for now
657 mHotseats[i] = intent;
658
659 // set the accessibility text to "Select shortcut"
660 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
661 } else {
662 // we have an app!
663 // now reconstruct the intent to launch it through the front
664 // door
665 ComponentName com = new ComponentName(
666 bestMatch.activityInfo.applicationInfo.packageName,
667 bestMatch.activityInfo.name);
668 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
669
670 // load the app label for accessibility
671 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
672 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400673 }
674
675 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700676 Log.d(TAG, "loadHotseats: hotseat " + i
677 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400678 + ((mHotseats[i] == null)
679 ? "null"
680 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400681 + "] label=[" + mHotseatLabels[i]
682 + "]"
683 );
684 }
685 }
686 }
687
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 @Override
689 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700690 mWaitingForResult = false;
691
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 // The pattern used here is that a user PICKs a specific application,
693 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
696 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700697
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 switch (requestCode) {
700 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700701 completeAddApplication(
702 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 break;
704 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800705 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 break;
707 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 break;
710 case REQUEST_PICK_LIVE_FOLDER:
711 addLiveFolder(data);
712 break;
713 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700714 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700717 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700720 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700721 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700723 case REQUEST_PICK_WALLPAPER:
724 // We just wanted the activity result here so we can clear mWaitingForResult
725 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
Romain Guy18042c82009-11-06 11:44:55 -0800727 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
728 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
729 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 // Clean up the appWidgetId if we canceled
731 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
732 if (appWidgetId != -1) {
733 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 }
735 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737
738 @Override
739 protected void onResume() {
740 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800741 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700742 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400743 mWorkspaceLoading = true;
744 mModel.startLoader(this, true);
745 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700746 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700748 // When we resume Launcher, a different Activity might be responsible for the app
749 // market intent, so refresh the icon
750 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751 }
752
753 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 protected void onPause() {
755 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700756 // Some launcher layouts don't have a previous and next view
757 if (mPreviousView != null) {
758 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700759 }
760 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700761 dismissPreview(mNextView);
762 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700763 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800764 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 }
Romain Guycbb89e42009-06-08 15:52:54 -0700766
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700767 @Override
768 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400769 // Flag the loader to stop early before switching
770 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800771 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800772 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700773 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700774
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800775 // We can't hide the IME if it was forced open. So don't bother
776 /*
777 @Override
778 public void onWindowFocusChanged(boolean hasFocus) {
779 super.onWindowFocusChanged(hasFocus);
780
781 if (hasFocus) {
782 final InputMethodManager inputManager = (InputMethodManager)
783 getSystemService(Context.INPUT_METHOD_SERVICE);
784 WindowManager.LayoutParams lp = getWindow().getAttributes();
785 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
786 android.os.Handler()) {
787 protected void onReceiveResult(int resultCode, Bundle resultData) {
788 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
789 }
790 });
791 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
792 }
793 }
794 */
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 private boolean acceptFilter() {
797 final InputMethodManager inputManager = (InputMethodManager)
798 getSystemService(Context.INPUT_METHOD_SERVICE);
799 return !inputManager.isFullscreenMode();
800 }
801
802 @Override
803 public boolean onKeyDown(int keyCode, KeyEvent event) {
804 boolean handled = super.onKeyDown(keyCode, event);
805 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
806 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
807 keyCode, event);
808 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700809 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700810 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700811 // showSearchDialog()
812 // If there are multiple keystrokes before the search dialog takes focus,
813 // onSearchRequested() will be called for every keystroke,
814 // but it is idempotent, so it's fine.
815 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 }
817 }
818
Joe Onorato8a9625e2010-01-28 15:55:35 -0800819 // Eat the long press event so the keyboard doesn't come up.
820 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
821 return true;
822 }
823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 return handled;
825 }
826
Karl Rosaen138a0412009-04-23 19:00:21 -0700827 private String getTypedText() {
828 return mDefaultKeySsb.toString();
829 }
830
831 private void clearTypedText() {
832 mDefaultKeySsb.clear();
833 mDefaultKeySsb.clearSpans();
834 Selection.setSelection(mDefaultKeySsb, 0);
835 }
836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700838 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
839 * State
840 */
841 private static State intToState(int stateOrdinal) {
842 State state = State.WORKSPACE;
843 final State[] stateValues = State.values();
844 for (int i = 0; i < stateValues.length; i++) {
845 if (stateValues[i].ordinal() == stateOrdinal) {
846 state = stateValues[i];
847 break;
848 }
849 }
850 return state;
851 }
852
853 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800854 * Restores the previous state, if it exists.
855 *
856 * @param savedState The previous state.
857 */
858 private void restoreState(Bundle savedState) {
859 if (savedState == null) {
860 return;
861 }
862
Michael Jurka883f55b2010-10-21 15:47:14 -0700863 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
864
865 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800866 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700867 } else if (state == State.CUSTOMIZE) {
868 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800869 }
870
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
872 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700873 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 }
875
876 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700879 mAddScreen = addScreen;
880 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
881 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 mRestoring = true;
883 }
884
885 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
886 if (renameFolder) {
887 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700888 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 mRestoring = true;
890 }
Winson Chunga12a2502010-12-20 14:41:35 -0800891
892 // Restore the current AllApps drawer tab
893 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
894 String curTab = savedState.getString("allapps_currentTab");
895 if (curTab != null) {
896 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
897 tabhost.setCurrentTabByTag(curTab);
898 }
899 int curPage = savedState.getInt("allapps_currentPage", -1);
900 if (curPage > -1) {
901 mAllAppsPagedView.setRestorePage(curPage);
902 }
903 }
904
905 // Restore the current customization drawer tab
906 if (mHomeCustomizationDrawer != null) {
907 String curTab = savedState.getString("customize_currentTab");
908 if (curTab != null) {
909 // We set this directly so that there is no delay before the tab is set
910 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
911 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
912 }
913
914 // Note: currently we do not restore the page for the customization tray because unlike
915 // AllApps, the page content can change drastically
916 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 }
918
919 /**
920 * Finds all the views we need and configure them properly.
921 */
922 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700923 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400924
Romain Guyb1b69f52009-08-10 15:10:15 -0700925 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400926 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Joe Onorato7c312c12009-08-13 21:36:53 -0700928 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700929 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700930 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800931 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700932 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700933 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700934
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700935 if (LauncherApplication.isScreenXLarge()) {
936 // They need to be INVISIBLE initially so that they will be measured in the layout.
937 // Otherwise the animations are messed up when we show them for the first time.
938 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
939 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
940 }
941
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500945 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700946
Joe Onorato7c312c12009-08-13 21:36:53 -0700947 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
948 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700950 View handleView = findViewById(R.id.all_apps_button);
951 if (handleView != null && handleView instanceof HandleView) {
952 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700953 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700954 mHandleView.setLauncher(this);
955 mHandleView.setOnClickListener(this);
956 mHandleView.setOnLongClickListener(this);
957 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800958
Winson Chung80baf5a2010-08-09 16:03:15 -0700959 if (mCustomizePagedView != null) {
960 mCustomizePagedView.setLauncher(this);
961 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700962 } else {
963 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
964 hotseatLeft.setContentDescription(mHotseatLabels[0]);
965 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
966 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
967 hotseatRight.setContentDescription(mHotseatLabels[1]);
968 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400969
Michael Jurkaaf442092010-06-10 17:01:57 -0700970 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
971 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800972
Michael Jurkaaf442092010-06-10 17:01:57 -0700973 Drawable previous = mPreviousView.getDrawable();
974 Drawable next = mNextView.getDrawable();
975 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976
Michael Jurkaaf442092010-06-10 17:01:57 -0700977 mPreviousView.setHapticFeedbackEnabled(false);
978 mPreviousView.setOnLongClickListener(this);
979 mNextView.setHapticFeedbackEnabled(false);
980 mNextView.setOnLongClickListener(this);
981 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400984 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
987 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400988 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700989 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -0800990
991 final View allAppsButton = findViewById(R.id.all_apps_button);
992 final View divider = findViewById(R.id.divider);
993 final View configureButton = findViewById(R.id.configure_button);
994
Michael Jurka54f7ac32010-08-02 13:56:46 -0700995 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800996 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700997 } else {
Michael Jurkab8e14472010-12-20 16:06:10 -0800998 deleteZone.setOverlappingView(configureButton);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700999 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001000 dragController.addDragListener(deleteZone);
1001
Adam Cohencdc30d52010-12-01 15:09:47 -08001002 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1003 if (allAppsDeleteZone != null) {
1004 allAppsDeleteZone.setLauncher(this);
1005 allAppsDeleteZone.setDragController(dragController);
1006 allAppsDeleteZone.setDragAndDropEnabled(false);
1007 dragController.addDragListener(allAppsDeleteZone);
1008 dragController.addDropTarget(allAppsDeleteZone);
1009 }
1010
1011 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1012 findViewById(R.id.all_apps_info_target);
1013 if (allAppsInfoTarget != null) {
1014 allAppsInfoTarget.setLauncher(this);
1015 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001016 allAppsInfoTarget.setDragAndDropEnabled(false);
1017 View marketButton = findViewById(R.id.market_button);
1018 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001019 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001020 }
1021 }
1022
Joe Onorato00acb122009-08-04 16:04:30 -04001023 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001024 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001025 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001026
Joe Onorato00acb122009-08-04 16:04:30 -04001027 // The order here is bottom to top.
1028 dragController.addDropTarget(workspace);
1029 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001030 if (allAppsInfoTarget != null) {
1031 dragController.addDropTarget(allAppsInfoTarget);
1032 }
1033 if (allAppsDeleteZone != null) {
1034 dragController.addDropTarget(allAppsDeleteZone);
1035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001036 }
1037
Romain Guy8a73c512009-11-09 19:19:59 -08001038 @SuppressWarnings({"UnusedDeclaration"})
1039 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001040 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001041 mWorkspace.scrollLeft();
1042 }
Romain Guy8a73c512009-11-09 19:19:59 -08001043 }
1044
1045 @SuppressWarnings({"UnusedDeclaration"})
1046 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001047 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001048 mWorkspace.scrollRight();
1049 }
Romain Guy8a73c512009-11-09 19:19:59 -08001050 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001051
1052 @SuppressWarnings({"UnusedDeclaration"})
1053 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001054 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001055
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001056 int index = -1;
1057 if (v.getId() == R.id.hotseat_left) {
1058 index = 0;
1059 } else if (v.getId() == R.id.hotseat_right) {
1060 index = 1;
1061 }
1062
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001063 // reload these every tap; you never know when they might change
1064 loadHotseats();
1065 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1066 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001067 startActivitySafely(
1068 mHotseats[index],
1069 "hotseat"
1070 );
1071 }
1072 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 /**
1075 * Creates a view representing a shortcut.
1076 *
1077 * @param info The data structure describing the shortcut.
1078 *
1079 * @return A View inflated from R.layout.application.
1080 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001081 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001083 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085
1086 /**
1087 * Creates a view representing a shortcut inflated from the specified resource.
1088 *
1089 * @param layoutResId The id of the XML layout used to create the shortcut.
1090 * @param parent The group the shortcut belongs to.
1091 * @param info The data structure describing the shortcut.
1092 *
1093 * @return A View inflated from layoutResId.
1094 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001095 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001096 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1097 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099 return favorite;
1100 }
1101
1102 /**
1103 * Add an application shortcut to the workspace.
1104 *
1105 * @param data The intent describing the application.
1106 * @param cellInfo The position on screen where to create the shortcut.
1107 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001108 void completeAddApplication(Context context, Intent data, int screen,
1109 int intersectCellX, int intersectCellY) {
1110 final int[] cellXY = mTmpAddItemCellCoordinates;
1111 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1112
1113 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1114 showOutOfSpaceMessage();
1115 return;
1116 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117
Joe Onorato0589f0f2010-02-08 13:44:00 -08001118 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1119 data, context);
1120
Romain Guy73b979d2009-06-09 12:57:21 -07001121 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1126 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001127 } else {
1128 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 }
1130 }
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 /**
1133 * Add a shortcut to the workspace.
1134 *
1135 * @param data The intent describing the shortcut.
1136 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 private void completeAddShortcut(Intent data, int screen,
1139 int intersectCellX, int intersectCellY) {
1140 final int[] cellXY = mTmpAddItemCellCoordinates;
1141 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
Michael Jurkad3ef3062010-11-23 16:23:58 -08001143 int[] touchXY = null;
1144 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1145 touchXY = mAddDropPosition;
1146 }
1147 boolean foundCellSpan = false;
1148 if (touchXY != null) {
1149 // when dragging and dropping, just find the closest free spot
1150 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1151 int[] result = screenLayout.findNearestVacantArea(
1152 touchXY[0], touchXY[1], 1, 1, cellXY);
1153 foundCellSpan = (result != null);
1154 } else {
1155 foundCellSpan = layout.findCellForSpanThatIntersects(
1156 cellXY, 1, 1, intersectCellX, intersectCellY);
1157 }
1158
1159 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001160 showOutOfSpaceMessage();
1161 return;
1162 }
1163
1164 final ShortcutInfo info = mModel.addShortcut(
1165 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166
1167 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001169 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171 }
1172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001176 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001177 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001179 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1184 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001185
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001187 // We have saved the position to which the widget was dragged-- this really only matters
1188 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001190
1191 // For now, we don't save the coordinate where we dropped the icon because we're not
1192 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1193 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001194 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001195 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001196 touchXY = mAddDropPosition;
1197 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 boolean foundCellSpan = false;
1199 if (touchXY != null) {
1200 // when dragging and dropping, just find the closest free spot
1201 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1202 int[] result = screenLayout.findNearestVacantArea(
1203 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001204 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001205 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001206 // if we long pressed on an empty cell to bring up a menu,
1207 // make sure we intersect the empty cell
1208 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1209 // findCellForSpanThatIntersects will just ignore them
1210 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1211 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001212 }
1213
Michael Jurka0280c3b2010-09-17 15:00:07 -07001214 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001215 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001217 return;
1218 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001220 // Build Launcher-specific widget info and save to database
1221 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 launcherInfo.spanX = spanXY[0];
1223 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001224
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 LauncherModel.addItemToDatabase(this, launcherInfo,
1226 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001227 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
1229 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001233 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001234
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001236 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001239 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001240
1241 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243 }
Romain Guycbb89e42009-06-08 15:52:54 -07001244
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1246 @Override
1247 public void onReceive(Context context, Intent intent) {
1248 final String action = intent.getAction();
1249 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1250 mUserPresent = false;
1251 updateRunning();
1252 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1253 mUserPresent = true;
1254 updateRunning();
1255 }
1256 }
1257 };
1258
1259 @Override
1260 public void onAttachedToWindow() {
1261 super.onAttachedToWindow();
1262
1263 // Listen for broadcasts related to user-presence
1264 final IntentFilter filter = new IntentFilter();
1265 filter.addAction(Intent.ACTION_SCREEN_OFF);
1266 filter.addAction(Intent.ACTION_USER_PRESENT);
1267 registerReceiver(mReceiver, filter);
1268
Adam Cohend113e0c2010-11-11 10:48:05 -08001269 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 mVisible = true;
1271 }
1272
1273 @Override
1274 public void onDetachedFromWindow() {
1275 super.onDetachedFromWindow();
1276 mVisible = false;
1277
Adam Cohend113e0c2010-11-11 10:48:05 -08001278 if (mAttached) {
1279 unregisterReceiver(mReceiver);
1280 mAttached = false;
1281 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001282 updateRunning();
1283 }
1284
1285 public void onWindowVisibilityChanged(int visibility) {
1286 mVisible = visibility == View.VISIBLE;
1287 updateRunning();
1288 }
1289
1290 private void sendAdvanceMessage(long delay) {
1291 mHandler.removeMessages(ADVANCE_MSG);
1292 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1293 mHandler.sendMessageDelayed(msg, delay);
1294 mAutoAdvanceSentTime = System.currentTimeMillis();
1295 }
1296
1297 private void updateRunning() {
1298 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1299 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1300 mAutoAdvanceRunning = autoAdvanceRunning;
1301 if (autoAdvanceRunning) {
1302 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1303 sendAdvanceMessage(delay);
1304 } else {
1305 if (!mWidgetsToAdvance.isEmpty()) {
1306 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1307 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1308 }
1309 mHandler.removeMessages(ADVANCE_MSG);
1310 }
1311 }
1312 }
1313
1314 private final Handler mHandler = new Handler() {
1315 @Override
1316 public void handleMessage(Message msg) {
1317 if (msg.what == ADVANCE_MSG) {
1318 int i = 0;
1319 for (View key: mWidgetsToAdvance.keySet()) {
1320 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1321 final int delay = mAdvanceStagger * i;
1322 if (v instanceof Advanceable) {
1323 postDelayed(new Runnable() {
1324 public void run() {
1325 ((Advanceable) v).advance();
1326 }
1327 }, delay);
1328 }
1329 i++;
1330 }
1331 sendAdvanceMessage(mAdvanceInterval);
1332 }
1333 }
1334 };
1335
1336 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1337 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1338 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1339 if (v instanceof Advanceable) {
1340 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1341 ((Advanceable) v).willBeAdvancedByHost();
1342 updateRunning();
1343 }
1344 }
1345
1346 void removeWidgetToAutoAdvance(View hostView) {
1347 if (mWidgetsToAdvance.containsKey(hostView)) {
1348 mWidgetsToAdvance.remove(hostView);
1349 updateRunning();
1350 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 }
1352
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1354 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 launcherInfo.hostView = null;
1357 }
1358
Adam Cohended9f8d2010-11-03 13:25:16 -07001359 void showOutOfSpaceMessage() {
1360 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1361 }
1362
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001363 public LauncherAppWidgetHost getAppWidgetHost() {
1364 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 }
Romain Guycbb89e42009-06-08 15:52:54 -07001366
Winson Chunga9abd0e2010-10-27 17:18:37 -07001367 public LauncherModel getModel() {
1368 return mModel;
1369 }
1370
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001371 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 getWindow().closeAllPanels();
1373
1374 try {
1375 dismissDialog(DIALOG_CREATE_SHORTCUT);
1376 // Unlock the workspace if the dialog was showing
1377 } catch (Exception e) {
1378 // An exception is thrown if the dialog is not visible, which is fine
1379 }
1380
1381 try {
1382 dismissDialog(DIALOG_RENAME_FOLDER);
1383 // Unlock the workspace if the dialog was showing
1384 } catch (Exception e) {
1385 // An exception is thrown if the dialog is not visible, which is fine
1386 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001387
1388 // Whatever we were doing is hereby canceled.
1389 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
1393 protected void onNewIntent(Intent intent) {
1394 super.onNewIntent(intent);
1395
1396 // Close the menu
1397 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001398 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001399 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001400
Joe Onorato14f122b2009-11-19 14:06:36 -08001401 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1402 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001403
Michael Jurka4cb37242010-08-09 21:05:32 -07001404 // in all these cases, only animate if we're already on home
1405 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001406 mWorkspace.unshrink(alreadyOnHome);
1407 }
1408 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001409 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001410 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1411 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001412 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001413 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001414
Joe Onorato3a8820b2009-11-10 15:06:42 -08001415 final View v = getWindow().peekDecorView();
1416 if (v != null && v.getWindowToken() != null) {
1417 InputMethodManager imm = (InputMethodManager)getSystemService(
1418 INPUT_METHOD_SERVICE);
1419 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421 }
1422 }
1423
1424 @Override
1425 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1426 // Do not call super here
1427 mSavedInstanceState = savedInstanceState;
1428 }
1429
1430 @Override
1431 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001432 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433
1434 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1435 if (folders.size() > 0) {
1436 final int count = folders.size();
1437 long[] ids = new long[count];
1438 for (int i = 0; i < count; i++) {
1439 final FolderInfo info = folders.get(i).getInfo();
1440 ids[i] = info.id;
1441 }
1442 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1443 } else {
1444 super.onSaveInstanceState(outState);
1445 }
1446
Michael Jurka883f55b2010-10-21 15:47:14 -07001447 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448
Michael Jurka0280c3b2010-09-17 15:00:07 -07001449 if (mAddScreen > -1 && mWaitingForResult) {
1450 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1451 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1452 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 }
1454
1455 if (mFolderInfo != null && mWaitingForResult) {
1456 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1457 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1458 }
Michael Jurka946ad472010-07-09 18:05:18 -07001459
Winson Chung43b119d2010-12-06 16:45:05 -08001460 // Save the current AllApps drawer tab
1461 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1462 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1463 String currentTabTag = tabhost.getCurrentTabTag();
1464 if (currentTabTag != null) {
1465 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001466 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001467 }
1468 }
1469
1470 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001471 if (mHomeCustomizationDrawer != null) {
1472 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1473 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001474 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001475 }
1476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 }
1478
1479 @Override
1480 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001484 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001486 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 TextKeyListener.getInstance().release();
1490
Joe Onorato9c1289c2009-08-17 11:03:03 -04001491 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001494
1495 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001496
Patrick Dubroyab962b72010-07-26 12:10:10 -07001497 // Some launcher layouts don't have a previous and next view
1498 if (mPreviousView != null) {
1499 dismissPreview(mPreviousView);
1500 }
1501 if (mNextView != null) {
1502 dismissPreview(mNextView);
1503 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001504
1505 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 }
1507
1508 @Override
1509 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001510 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 super.startActivityForResult(intent, requestCode);
1512 }
1513
1514 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001515 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001517
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001518 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Karl Rosaen138a0412009-04-23 19:00:21 -07001520 if (initialQuery == null) {
1521 // Use any text typed in the launcher as the initial query
1522 initialQuery = getTypedText();
1523 clearTypedText();
1524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 if (appSearchData == null) {
1526 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001527 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 final SearchManager searchManager =
1531 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001533 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
1536 @Override
1537 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001538 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 return false;
1540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
1542 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001543
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1545 .setIcon(android.R.drawable.ic_menu_add)
1546 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001547 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1548 .setIcon(android.R.drawable.ic_menu_manage)
1549 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001550 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 .setIcon(android.R.drawable.ic_menu_gallery)
1552 .setAlphabeticShortcut('W');
1553 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1554 .setIcon(android.R.drawable.ic_search_category_default)
1555 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1556 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1557 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1558 .setAlphabeticShortcut('N');
1559
1560 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001561 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1562 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1565 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1566 .setIntent(settings);
1567
1568 return true;
1569 }
1570
1571 @Override
1572 public boolean onPrepareOptionsMenu(Menu menu) {
1573 super.onPrepareOptionsMenu(menu);
1574
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001575 // If all apps is animating, don't show the menu, because we don't know
1576 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001577 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001578 return false;
1579 }
1580
1581 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001582 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001583 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1584 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1585
1586 // Disable add if the workspace is full.
1587 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001588 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1589 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591
1592 return true;
1593 }
1594
1595 @Override
1596 public boolean onOptionsItemSelected(MenuItem item) {
1597 switch (item.getItemId()) {
1598 case MENU_ADD:
1599 addItems();
1600 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001601 case MENU_MANAGE_APPS:
1602 manageApps();
1603 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 case MENU_WALLPAPER_SETTINGS:
1605 startWallpaper();
1606 return true;
1607 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001608 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 return true;
1610 case MENU_NOTIFICATIONS:
1611 showNotifications();
1612 return true;
1613 }
1614
1615 return super.onOptionsItemSelected(item);
1616 }
Romain Guycbb89e42009-06-08 15:52:54 -07001617
Karl Rosaen138a0412009-04-23 19:00:21 -07001618 /**
1619 * Indicates that we want global search for this activity by setting the globalSearch
1620 * argument for {@link #startSearch} to true.
1621 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001623 @Override
1624 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001625 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001626 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001627 }
1628
Joe Onorato9c1289c2009-08-17 11:03:03 -04001629 public boolean isWorkspaceLocked() {
1630 return mWorkspaceLoading || mWaitingForResult;
1631 }
1632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001634 if (LauncherApplication.isScreenXLarge()) {
1635 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001636 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001637 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001638 }
1639 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001640 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001641 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
1644
Michael Jurka0280c3b2010-09-17 15:00:07 -07001645 private void resetAddInfo() {
1646 mAddScreen = -1;
1647 mAddIntersectCellX = -1;
1648 mAddIntersectCellY = -1;
1649 mAddDropPosition = null;
1650 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001651
Winson Chung55cef262010-10-28 14:14:18 -07001652 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001653 resetAddInfo();
1654 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001655 mAddDropPosition = position;
1656
Michael Jurkaaf442092010-06-10 17:01:57 -07001657 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001658 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1659 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001660 }
1661
Winson Chung7ad01412010-09-27 11:33:03 -07001662 private void manageApps() {
1663 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1664 }
1665
Michael Jurkaaf442092010-06-10 17:01:57 -07001666 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001667 // TODO: catch bad widget exception when sent
1668 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001669 // TODO: Is this log message meaningful?
1670 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001671 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001672 }
1673
Winson Chung55cef262010-10-28 14:14:18 -07001674 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001675 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
Bjorn Bringert7984c942009-12-09 15:38:25 +00001677 if (appWidget.configure != null) {
1678 // Launch over to configure widget, if needed
1679 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1680 intent.setComponent(appWidget.configure);
1681 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001682 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001683 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1684 intent.putExtra(
1685 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1686 info.mimeType);
1687
1688 final String mimeType = info.mimeType;
1689 final ClipData clipData = (ClipData) info.configurationData;
1690 final ClipDescription clipDesc = clipData.getDescription();
1691 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1692 if (clipDesc.getMimeType(i).equals(mimeType)) {
1693 final ClipData.Item item = clipData.getItem(i);
1694 final CharSequence stringData = item.getText();
1695 final Uri uriData = item.getUri();
1696 final Intent intentData = item.getIntent();
1697 final String key =
1698 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1699 if (uriData != null) {
1700 intent.putExtra(key, uriData);
1701 } else if (intentData != null) {
1702 intent.putExtra(key, intentData);
1703 } else if (stringData != null) {
1704 intent.putExtra(key, stringData);
1705 }
1706 break;
1707 }
1708 }
1709 }
Winson Chung55cef262010-10-28 14:14:18 -07001710 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001711
Romain Guy8e633c52010-03-04 12:51:36 -08001712 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001714 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
1717 }
Romain Guycbb89e42009-06-08 15:52:54 -07001718
Michael Jurka0280c3b2010-09-17 15:00:07 -07001719 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1720 resetAddInfo();
1721 mAddScreen = screen;
1722 mAddDropPosition = position;
1723
1724 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1725 createShortcutIntent.setComponent(componentName);
1726 processShortcut(createShortcutIntent);
1727 }
1728
Joe Onoratodeb98af2010-02-19 14:59:39 -08001729 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001730 // Handle case where user selected "Applications"
1731 String applicationName = getResources().getString(R.string.group_applications);
1732 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001733
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001734 if (applicationName != null && applicationName.equals(shortcutName)) {
1735 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1736 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001737
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001738 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1739 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001740 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001741 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001742 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001743 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
1746
Winson Chung24ab2f12010-09-16 14:10:47 -07001747 void processWallpaper(Intent intent) {
1748 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1749 }
1750
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1752 resetAddInfo();
1753 mAddScreen = screen;
1754 mAddDropPosition = position;
1755
1756 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1757 createFolderIntent.setComponent(componentName);
1758
1759 addLiveFolder(createFolderIntent);
1760 }
1761
1762 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001763 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001764 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001766
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001767 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001770 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 UserFolderInfo folderInfo = new UserFolderInfo();
1776 folderInfo.title = getText(R.string.folder_name);
1777
Michael Jurka0280c3b2010-09-17 15:00:07 -07001778 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1779 final int[] cellXY = mTmpAddItemCellCoordinates;
1780 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1781 showOutOfSpaceMessage();
1782 return;
1783 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784
1785 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001786 LauncherModel.addItemToDatabase(this, folderInfo,
1787 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001789 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790
1791 // Create the view
1792 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001793 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1794 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001795 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
Romain Guycbb89e42009-06-08 15:52:54 -07001797
Joe Onorato9c1289c2009-08-17 11:03:03 -04001798 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001799 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 }
1801
Michael Jurka28750fb2010-09-24 17:43:49 -07001802 private void completeAddLiveFolder(
1803 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001804 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1805 final int[] cellXY = mTmpAddItemCellCoordinates;
1806 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1807 showOutOfSpaceMessage();
1808 return;
1809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810
Michael Jurka0280c3b2010-09-17 15:00:07 -07001811 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
1813 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001815 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001816 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818 }
1819
1820 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001821 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822
1823 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1824 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1825
1826 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 Intent.ShortcutIconResource iconResource = null;
1828
1829 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1830 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1831 try {
1832 iconResource = (Intent.ShortcutIconResource) extra;
1833 final PackageManager packageManager = context.getPackageManager();
1834 Resources resources = packageManager.getResourcesForApplication(
1835 iconResource.packageName);
1836 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1837 icon = resources.getDrawable(id);
1838 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001839 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 }
1841 }
1842
1843 if (icon == null) {
1844 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1845 }
1846
1847 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001848 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 info.title = name;
1850 info.iconResource = iconResource;
1851 info.uri = data.getData();
1852 info.baseIntent = baseIntent;
1853 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1854 LiveFolders.DISPLAY_MODE_GRID);
1855
1856 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001857 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001858 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859
1860 return info;
1861 }
1862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 private void showNotifications() {
1864 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1865 if (statusBar != null) {
1866 statusBar.expand();
1867 }
1868 }
1869
1870 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001871 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001873 Intent chooser = Intent.createChooser(pickWallpaper,
1874 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001875 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1876 // Removed in Eclair MR1
1877// WallpaperManager wm = (WallpaperManager)
1878// getSystemService(Context.WALLPAPER_SERVICE);
1879// WallpaperInfo wi = wm.getWallpaperInfo();
1880// if (wi != null && wi.getSettingsActivity() != null) {
1881// LabeledIntent li = new LabeledIntent(getPackageName(),
1882// R.string.configure_wallpaper, 0);
1883// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1884// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1885// }
Mike Clerona0618e42009-10-22 13:55:21 -07001886 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001889 /**
1890 * Registers various content observers. The current implementation registers
1891 * only a favorites observer to keep track of the favorites applications.
1892 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001893 private void registerContentObservers() {
1894 ContentResolver resolver = getContentResolver();
1895 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1896 true, mWidgetObserver);
1897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 @Override
1900 public boolean dispatchKeyEvent(KeyEvent event) {
1901 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1902 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001904 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001905 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001906 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001907 dumpState();
1908 return true;
1909 }
1910 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001911 }
1912 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1913 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001914 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 return true;
1916 }
1917 }
1918
1919 return super.dispatchKeyEvent(event);
1920 }
1921
Joe Onorato88ec0992009-11-19 13:16:06 -08001922 @Override
1923 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001924 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1925 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001926 } else {
1927 closeFolder();
1928 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001929 // Some launcher layouts don't have a previous and next view
1930 if (mPreviousView != null) {
1931 dismissPreview(mPreviousView);
1932 dismissPreview(mNextView);
1933 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001934 }
1935
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 private void closeFolder() {
1937 Folder folder = mWorkspace.getOpenFolder();
1938 if (folder != null) {
1939 closeFolder(folder);
1940 }
1941 }
1942
1943 void closeFolder(Folder folder) {
1944 folder.getInfo().opened = false;
1945 ViewGroup parent = (ViewGroup) folder.getParent();
1946 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001947 CellLayout cl = (CellLayout) parent;
1948 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001949 if (folder instanceof DropTarget) {
1950 // Live folders aren't DropTargets.
1951 mDragController.removeDropTarget((DropTarget)folder);
1952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953 }
1954 folder.onClose();
1955 }
1956
1957 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001958 * Re-listen when widgets are reset.
1959 */
1960 private void onAppWidgetReset() {
1961 mAppWidgetHost.startListening();
1962 }
1963
1964 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001965 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1966 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001968 private void unbindDesktopItems() {
1969 for (ItemInfo item: mDesktopItems) {
1970 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 }
1972 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 /**
1975 * Launches the intent referred by the clicked shortcut.
1976 *
1977 * @param v The view representing the clicked shortcut.
1978 */
1979 public void onClick(View v) {
1980 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001981 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001983 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001984 int[] pos = new int[2];
1985 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001986 intent.setSourceBounds(new Rect(pos[0], pos[1],
1987 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001988 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 } else if (tag instanceof FolderInfo) {
1990 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001991 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001992 if (mState == State.ALL_APPS) {
1993 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001994 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001995 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001996 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998 }
1999
Michael Jurka0e260592010-06-30 17:07:39 -07002000 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002001 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002002 // clicking anywhere on the workspace causes the customization drawer to slide down
2003 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002004 return false;
2005 }
2006
Michael Jurkaaf442092010-06-10 17:01:57 -07002007 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002008 * Event handler for the search button
2009 *
2010 * @param v The view that was clicked.
2011 */
2012 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002013 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002014 // Use a custom animation for launching search
2015 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002016 }
2017
2018 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002019 * Event handler for the voice button
2020 *
2021 * @param v The view that was clicked.
2022 */
2023 public void onClickVoiceButton(View v) {
2024 startVoiceSearch();
2025 }
2026
2027 private void startVoiceSearch() {
2028 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2029 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2030 startActivity(intent);
2031 }
2032
2033 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002034 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002035 * enters home screen customization mode.
2036 *
2037 * @param v The view that was clicked.
2038 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002039 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002040 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002041 }
2042
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002043 /**
2044 * Event handler for the "grid" button that appears on the home screen, which
2045 * enters all apps mode.
2046 *
2047 * @param v The view that was clicked.
2048 */
2049 public void onClickAllAppsButton(View v) {
2050 showAllApps(true);
2051 }
2052
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002053 public void onClickAppMarketButton(View v) {
2054 if (mAppMarketIntent != null) {
2055 startActivitySafely(mAppMarketIntent, "app market");
2056 }
2057 }
2058
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002059 void startApplicationDetailsActivity(ComponentName componentName) {
2060 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002061 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2062 Uri.fromParts("package", packageName, null));
2063 startActivity(intent);
2064 }
2065
Patrick Dubroy5539af72010-09-07 15:22:01 -07002066 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2067 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2068 // System applications cannot be installed. For now, show a toast explaining that.
2069 // We may give them the option of disabling apps this way.
2070 int messageId = R.string.uninstall_system_app_text;
2071 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2072 } else {
2073 String packageName = appInfo.componentName.getPackageName();
2074 String className = appInfo.componentName.getClassName();
2075 Intent intent = new Intent(
2076 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2077 startActivity(intent);
2078 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002079 }
2080
Joe Onoratof984e852010-03-25 09:47:45 -07002081 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2083 try {
2084 startActivity(intent);
2085 } catch (ActivityNotFoundException e) {
2086 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002087 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 } catch (SecurityException e) {
2089 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002090 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002092 "or use the exported attribute for this activity. "
2093 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002096
Romain Guy8e633c52010-03-04 12:51:36 -08002097 void startActivityForResultSafely(Intent intent, int requestCode) {
2098 try {
2099 startActivityForResult(intent, requestCode);
2100 } catch (ActivityNotFoundException e) {
2101 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2102 } catch (SecurityException e) {
2103 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2104 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2105 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2106 "or use the exported attribute for this activity.", e);
2107 }
2108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109
2110 private void handleFolderClick(FolderInfo folderInfo) {
2111 if (!folderInfo.opened) {
2112 // Close any open folder
2113 closeFolder();
2114 // Open the requested folder
2115 openFolder(folderInfo);
2116 } else {
2117 // Find the open folder...
2118 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2119 int folderScreen;
2120 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002121 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 // .. and close it
2123 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002124 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 // Close any folder open on the current screen
2126 closeFolder();
2127 // Pull the folder onto this screen
2128 openFolder(folderInfo);
2129 }
2130 }
2131 }
2132 }
2133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002135 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 * is animated relative to the specified View. If the View is null, no animation
2137 * is played.
2138 *
2139 * @param folderInfo The FolderInfo describing the folder to open.
2140 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002141 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 Folder openFolder;
2143
2144 if (folderInfo instanceof UserFolderInfo) {
2145 openFolder = UserFolder.fromXml(this);
2146 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002147 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 } else {
2149 return;
2150 }
2151
Joe Onorato00acb122009-08-04 16:04:30 -04002152 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 openFolder.setLauncher(this);
2154
2155 openFolder.bind(folderInfo);
2156 folderInfo.opened = true;
2157
Winson Chungaafa03c2010-06-11 17:34:16 -07002158 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 openFolder.onOpen();
2161 }
2162
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002163 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002164 switch (v.getId()) {
2165 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002166 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002167 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2168 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002169 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002170 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002171 return true;
2172 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002173 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002174 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2175 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002176 showPreviews(v);
2177 }
2178 return true;
2179 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002180 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002181 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2182 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2183 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002184 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002185 return true;
2186 }
2187
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 return false;
2190 }
2191
2192 if (!(v instanceof CellLayout)) {
2193 v = (View) v.getParent();
2194 }
2195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002196
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 resetAddInfo();
2198 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 return true;
2202 }
2203
Michael Jurka0280c3b2010-09-17 15:00:07 -07002204 final View itemUnderLongClick = longClickCellInfo.cell;
2205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002207 if (itemUnderLongClick == null) {
2208 // User long pressed on empty space
2209 mWorkspace.setAllowLongPress(false);
2210 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2211 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002212 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002214 }
2215 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002216 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002218 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2219 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002220 mAddIntersectCellX = longClickCellInfo.cellX;
2221 mAddIntersectCellY = longClickCellInfo.cellY;
2222 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 }
2224 }
2225 }
2226 return true;
2227 }
2228
Romain Guye6b8e2f2009-11-10 11:56:55 -08002229 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002230 private void dismissPreview(final View v) {
2231 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002232 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002233 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2234 public void onDismiss() {
2235 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2236 int count = group.getChildCount();
2237 for (int i = 0; i < count; i++) {
2238 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2239 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002240 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2241 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2242
2243 v.setTag(R.id.workspace, null);
2244 v.setTag(R.id.icon, null);
2245 window.setOnDismissListener(null);
2246 }
2247 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002248 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249 }
2250 v.setTag(null);
2251 }
2252
Romain Guyf8e6a802009-12-07 17:48:02 -08002253 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002254 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002255 }
2256
Romain Guya6abce82009-11-10 02:54:41 -08002257 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002258 final Resources resources = getResources();
2259 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002260
Romain Guya6abce82009-11-10 02:54:41 -08002261 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002262
Romain Guy9d31e9f2009-11-11 18:54:28 -08002263 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002264
Romain Guyf8e6a802009-12-07 17:48:02 -08002265 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002266 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002267 int extraW = (int) ((r.left + r.right) * max);
2268 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002269
2270 int aW = cell.getWidth() - extraW;
2271 float w = aW / max;
2272
2273 int width = cell.getWidth();
2274 int height = cell.getHeight();
2275 int x = cell.getLeftPadding();
2276 int y = cell.getTopPadding();
2277 width -= (x + cell.getRightPadding());
2278 height -= (y + cell.getBottomPadding());
2279
2280 float scale = w / width;
2281
2282 int count = end - start;
2283
2284 final float sWidth = width * scale;
2285 float sHeight = height * scale;
2286
Romain Guye6b8e2f2009-11-10 11:56:55 -08002287 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002288
Romain Guye6b8e2f2009-11-10 11:56:55 -08002289 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2290 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002291
2292 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002293 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002294 cell = (CellLayout) workspace.getChildAt(i);
2295
Romain Guyf8e6a802009-12-07 17:48:02 -08002296 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002297 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002298
2299 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002300 c.scale(scale, scale);
2301 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002302 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002303
Romain Guy9d31e9f2009-11-11 18:54:28 -08002304 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 image.setImageBitmap(bitmap);
2306 image.setTag(i);
2307 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002308 image.setOnFocusChangeListener(handler);
2309 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002310 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311
2312 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002313 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2314
Winson Chungaafa03c2010-06-11 17:34:16 -07002315 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002316 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002317
2318 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 p.setContentView(preview);
2320 p.setWidth((int) (sWidth * count + extraW));
2321 p.setHeight((int) (sHeight + extraH));
2322 p.setAnimationStyle(R.style.AnimationPreview);
2323 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002324 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002325 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002327
Romain Guye6b8e2f2009-11-10 11:56:55 -08002328 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2329 public void onDismiss() {
2330 dismissPreview(anchor);
2331 }
2332 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002333
2334 anchor.setTag(p);
2335 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002336 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002337 }
2338
Romain Guy9d31e9f2009-11-11 18:54:28 -08002339 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002340 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002341
Romain Guye6b8e2f2009-11-10 11:56:55 -08002342 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002343 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002344 }
2345
2346 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002347 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002348 v.post(this);
2349 }
2350
2351 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002352 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002353 }
2354
2355 public void onFocusChange(View v, boolean hasFocus) {
2356 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002357 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002358 }
Romain Guya6abce82009-11-10 02:54:41 -08002359 }
2360 }
2361
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 Workspace getWorkspace() {
2363 return mWorkspace;
2364 }
2365
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 @Override
2367 protected Dialog onCreateDialog(int id) {
2368 switch (id) {
2369 case DIALOG_CREATE_SHORTCUT:
2370 return new CreateShortcut().createDialog();
2371 case DIALOG_RENAME_FOLDER:
2372 return new RenameFolder().createDialog();
2373 }
2374
2375 return super.onCreateDialog(id);
2376 }
2377
2378 @Override
2379 protected void onPrepareDialog(int id, Dialog dialog) {
2380 switch (id) {
2381 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 break;
2383 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002384 if (mFolderInfo != null) {
2385 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2386 final CharSequence text = mFolderInfo.title;
2387 input.setText(text);
2388 input.setSelection(0, text.length());
2389 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 break;
2391 }
2392 }
2393
2394 void showRenameDialog(FolderInfo info) {
2395 mFolderInfo = info;
2396 mWaitingForResult = true;
2397 showDialog(DIALOG_RENAME_FOLDER);
2398 }
2399
Michael Jurka0280c3b2010-09-17 15:00:07 -07002400 private void showAddDialog(int intersectX, int intersectY) {
2401 resetAddInfo();
2402 mAddIntersectCellX = intersectX;
2403 mAddIntersectCellY = intersectY;
2404 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 mWaitingForResult = true;
2406 showDialog(DIALOG_CREATE_SHORTCUT);
2407 }
2408
Joe Onoratodeb98af2010-02-19 14:59:39 -08002409 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002410 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002411 Bundle bundle = new Bundle();
2412
2413 ArrayList<String> shortcutNames = new ArrayList<String>();
2414 shortcutNames.add(getString(R.string.group_applications));
2415 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2416
2417 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2418 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2419 R.drawable.ic_launcher_application));
2420 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2421
2422 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2423 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002424 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002425 pickIntent.putExtras(bundle);
2426
Joe Onoratodeb98af2010-02-19 14:59:39 -08002427 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002428 }
2429
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 private class RenameFolder {
2431 private EditText mInput;
2432
2433 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002434 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2435 mInput = (EditText) layout.findViewById(R.id.folder_name);
2436
2437 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2438 builder.setIcon(0);
2439 builder.setTitle(getString(R.string.rename_folder_title));
2440 builder.setCancelable(true);
2441 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2442 public void onCancel(DialogInterface dialog) {
2443 cleanup();
2444 }
2445 });
2446 builder.setNegativeButton(getString(R.string.cancel_action),
2447 new Dialog.OnClickListener() {
2448 public void onClick(DialogInterface dialog, int which) {
2449 cleanup();
2450 }
2451 }
2452 );
2453 builder.setPositiveButton(getString(R.string.rename_action),
2454 new Dialog.OnClickListener() {
2455 public void onClick(DialogInterface dialog, int which) {
2456 changeFolderName();
2457 }
2458 }
2459 );
2460 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002461
2462 final AlertDialog dialog = builder.create();
2463 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2464 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002465 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002466 mInput.requestFocus();
2467 InputMethodManager inputManager = (InputMethodManager)
2468 getSystemService(Context.INPUT_METHOD_SERVICE);
2469 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002470 }
2471 });
2472
2473 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 }
2475
2476 private void changeFolderName() {
2477 final String name = mInput.getText().toString();
2478 if (!TextUtils.isEmpty(name)) {
2479 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002480 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 mFolderInfo.title = name;
2482 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2483
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002485 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 } else {
2488 final FolderIcon folderIcon = (FolderIcon)
2489 mWorkspace.getViewForTag(mFolderInfo);
2490 if (folderIcon != null) {
2491 folderIcon.setText(name);
2492 getWorkspace().requestLayout();
2493 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002494 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002495 mWorkspaceLoading = true;
2496 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002497 }
2498 }
2499 }
2500 cleanup();
2501 }
2502
2503 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002504 dismissDialog(DIALOG_RENAME_FOLDER);
2505 mWaitingForResult = false;
2506 mFolderInfo = null;
2507 }
2508 }
2509
Daniel Sandler843e8602010-06-07 14:59:01 -04002510 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2511 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002512 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002513 }
2514
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002515 // AllAppsView.Watcher
2516 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002517 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2518 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002519 mWorkspace.setVisibility(View.GONE);
2520 }
2521 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002522
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 private void showToolbarButton(View button) {
2524 button.setAlpha(1.0f);
2525 button.setVisibility(View.VISIBLE);
2526 button.setFocusable(true);
2527 button.setClickable(true);
2528 }
2529
2530 private void hideToolbarButton(View button) {
2531 button.setAlpha(0.0f);
2532 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2533 button.setVisibility(View.INVISIBLE);
2534 button.setFocusable(false);
2535 button.setClickable(false);
2536 }
2537
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 *
2541 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2542 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002543 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002544 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002545 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002546 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002547 final boolean showing = show;
2548 final boolean hiding = !show;
2549
2550 final int duration = show ?
2551 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2552 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2553
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002555 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2556 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002557 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002558 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002559 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002562 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002563 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 });
2567 seq.play(anim);
2568 } else {
2569 if (showing) {
2570 showToolbarButton(view);
2571 } else {
2572 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002574 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 }
2576
2577 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 * Show/hide the appropriate toolbar buttons for newState.
2579 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002580 *
2581 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002582 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2583 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002585 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002586 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002587 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002588 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002589 final View configureButton = findViewById(R.id.configure_button);
2590
2591 switch (newState) {
2592 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 hideOrShowToolbarButton(true, searchButton, showSeq);
2594 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002595 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002597 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 break;
2599 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002600 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002602 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002603 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 break;
2605 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002606 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002608 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002609 hideOrShowToolbarButton(false, configureButton, hideSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002610 //mDeleteZone.setOverlappingView(configureButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 break;
2612 }
2613 }
2614
2615 /**
2616 * Helper method for the cameraZoomIn/cameraZoomOut animations
2617 * @param view The view being animated
2618 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2619 * @param scaleFactor The scale factor used for the zoom
2620 */
2621 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2622 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002623
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002625 // Set pivotY so that at the starting zoom factor, the view is partially
2626 // visible. Modifying initialHeightFactor changes how much of the view is
2627 // initially showing, and hence the perceived angle from which the view enters.
2628 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002630 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002631 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002632 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 }
2634 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002635
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002636 /**
2637 * Zoom the camera out from the workspace to reveal 'toView'.
2638 * Assumes that the view to show is anchored at either the very top or very bottom
2639 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002641 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002642 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002643 final Resources res = getResources();
2644 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2645 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 final boolean toAllApps = (toState == State.ALL_APPS);
2647 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002648
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002649 setPivotsForZoom(toView, toState, scale);
2650
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002651 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002652 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002653 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002654 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002656
2657 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002658 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2659 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2660 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2661 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002662
2663 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002664 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002665 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002666 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667 // Prepare the position
2668 toView.setTranslationX(0.0f);
2669 toView.setTranslationY(0.0f);
2670 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002671 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002673 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002674 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002675 // If we don't set the final scale values here, if this animation is cancelled
2676 // it will have the wrong scale value and subsequent cameraPan animations will
2677 // not fix that
2678 toView.setScaleX(1.0f);
2679 toView.setScaleY(1.0f);
2680 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 });
2682
Chet Haaseb1254a62010-09-07 13:35:00 -07002683 AnimatorSet toolbarHideAnim = new AnimatorSet();
2684 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002685 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2686
2687 // toView should appear right at the end of the workspace shrink animation
2688 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2689
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002690 if (mStateAnimation != null) mStateAnimation.cancel();
2691 mStateAnimation = new AnimatorSet();
2692 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2693 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002694
2695 // Show the new toolbar buttons just as the main animation is ending
2696 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002697 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2698 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002699 } else {
2700 toView.setTranslationX(0.0f);
2701 toView.setTranslationY(0.0f);
2702 toView.setScaleX(1.0f);
2703 toView.setScaleY(1.0f);
2704 toView.setVisibility(View.VISIBLE);
2705 hideAndShowToolbarButtons(toState, null, null);
2706 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002707 }
2708
2709 /**
2710 * Zoom the camera back into the workspace, hiding 'fromView'.
2711 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2713 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002714 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002715 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002716 cameraZoomIn(fromState, animated, false);
2717 }
2718
2719 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002720 Resources res = getResources();
2721 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2722 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002723 final View fromView =
2724 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2725
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002726 mCustomizePagedView.endChoiceMode();
2727 mAllAppsPagedView.endChoiceMode();
2728
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730
Michael Jurkad3ef3062010-11-23 16:23:58 -08002731 if (!springLoaded) {
2732 mWorkspace.unshrink(animated);
2733 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734
2735 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002736 if (mStateAnimation != null) mStateAnimation.cancel();
2737 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002738 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2739 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2740 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2741 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002742 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2743
2744 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2745 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2746 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002747 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002748 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002749 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002750 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002751 }
2752 });
2753
Chet Haaseb1254a62010-09-07 13:35:00 -07002754 AnimatorSet toolbarHideAnim = new AnimatorSet();
2755 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002756 if (!springLoaded) {
2757 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2758 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759
Adam Cohen61033d32010-11-15 18:29:44 -08002760 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002761
2762 // Show the new toolbar buttons at the very end of the whole animation
2763 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2764 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002765 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2766 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002767 } else {
2768 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 if (!springLoaded) {
2770 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2771 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002772 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002773 }
2774
2775 /**
2776 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2777 * This is the transition used on xlarge screens to go between all apps and
2778 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2780 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2781 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002782 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002783 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002784 final Resources res = getResources();
2785 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002786 final int workspaceHeight = mWorkspace.getHeight();
2787
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002788 final boolean fromAllApps = (fromState == State.ALL_APPS);
2789 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2790 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002791
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002792 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2793 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2794 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2795 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002796
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002797 mCustomizePagedView.endChoiceMode();
2798 mAllAppsPagedView.endChoiceMode();
2799
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002800 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002801 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002802 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002803 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002804 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002805
2806 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002807 if (mStateAnimation != null) mStateAnimation.cancel();
2808 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002809 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002810 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002811 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002812 toView.setVisibility(View.VISIBLE);
2813 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002814 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002815 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002816 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002817 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 fromView.setVisibility(View.GONE);
2819 }
2820 });
2821
Chet Haaseb1254a62010-09-07 13:35:00 -07002822 AnimatorSet toolbarHideAnim = new AnimatorSet();
2823 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2825
Chet Haase472b2812010-10-14 07:02:04 -07002826 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2827 fromViewStartY, fromViewEndY);
2828 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002829 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2830 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2831 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2832 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2833 );
Chet Haase472b2812010-10-14 07:02:04 -07002834 fromAnim.setDuration(duration);
2835 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002836
2837 // Show the new toolbar buttons just as the main animation is ending
2838 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002839 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2840 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002841 } else {
2842 fromView.setY(fromViewEndY);
2843 fromView.setVisibility(View.GONE);
2844 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002845 toView.setScaleX(1.0f);
2846 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 toView.setVisibility(View.VISIBLE);
2848 hideAndShowToolbarButtons(toState, null, null);
2849 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002850 }
2851
2852 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002853 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002854 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002855 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002856
Michael Jurka79212d82010-07-30 16:36:20 -07002857 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002858 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002859 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002860 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002861 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002862 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002863 } else {
2864 mAllAppsGrid.zoom(1.0f, animated);
2865 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002866
Romain Guyc16fea72010-03-12 17:17:56 -08002867 ((View) mAllAppsGrid).setFocusable(true);
2868 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002869
Joe Onorato7c312c12009-08-13 21:36:53 -07002870 // TODO: fade these two too
2871 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002872
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002873 // Change the state *after* we've called all the transition code
2874 mState = State.ALL_APPS;
2875 }
2876
2877
2878 void showWorkspace(boolean animated) {
2879 showWorkspace(animated, null);
2880 }
2881
2882 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002883 if (layout != null) {
2884 // always animated, but that's ok since we never specify a layout and
2885 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002886 mWorkspace.unshrink(layout);
2887 } else {
2888 mWorkspace.unshrink(animated);
2889 }
2890 if (mState == State.ALL_APPS) {
2891 closeAllApps(animated);
2892 } else if (mState == State.CUSTOMIZE) {
2893 hideCustomizationDrawer(animated);
2894 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002895
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002896 // Change the state *after* we've called all the transition code
2897 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002898 }
2899
Michael Jurkad3ef3062010-11-23 16:23:58 -08002900 void enterSpringLoadedDragMode(CellLayout layout) {
2901 mWorkspace.enterSpringLoadedDragMode(layout);
2902 if (mState == State.ALL_APPS) {
2903 cameraZoomIn(State.ALL_APPS, true, true);
2904 mState = State.ALL_APPS_SPRING_LOADED;
2905 } else if (mState == State.CUSTOMIZE) {
2906 cameraZoomIn(State.CUSTOMIZE, true, true);
2907 mState = State.CUSTOMIZE_SPRING_LOADED;
2908 }/* else {
2909 // we're already in spring loaded mode; don't do anything
2910 }*/
2911 }
2912
2913 void exitSpringLoadedDragMode() {
2914 if (mState == State.ALL_APPS_SPRING_LOADED) {
2915 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2916 cameraZoomOut(State.ALL_APPS, true);
2917 mState = State.ALL_APPS;
2918 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2919 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2920 cameraZoomOut(State.CUSTOMIZE, true);
2921 mState = State.CUSTOMIZE;
2922 }/* else {
2923 // we're not in spring loaded mode; don't do anything
2924 }*/
2925 }
2926
Joe Onoratob2061212009-11-24 16:13:54 -05002927 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002928 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002929 * - Home from workspace
2930 * - from center screen
2931 * - from other screens
2932 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002933 * - from center screen
2934 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002935 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002936 * - from center screen
2937 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002938 * - Launch app from workspace and quit
2939 * - with back
2940 * - with home
2941 * - Launch app from all apps and quit
2942 * - with back
2943 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002944 * - Go to a screen that's not the default, then all
2945 * apps, and launch and app, and go back
2946 * - with back
2947 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002948 * - On workspace, long press power and go back
2949 * - with back
2950 * - with home
2951 * - On all apps, long press power and go back
2952 * - with back
2953 * - with home
2954 * - On workspace, power off
2955 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002956 * - Launch an app and turn off the screen while in that app
2957 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002958 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002959 * - From all apps
2960 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002961 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2962 * - From all apps
2963 * - From the center workspace
2964 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002965 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002966 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002967 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002968 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002969 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002970 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002971 } else {
2972 mAllAppsGrid.zoom(0.0f, animated);
2973 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002974 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002975 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002976 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002977 }
2978
Joe Onorato7c312c12009-08-13 21:36:53 -07002979 void lockAllApps() {
2980 // TODO
2981 }
2982
2983 void unlockAllApps() {
2984 // TODO
2985 }
2986
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002987 // Show the customization drawer (only exists in x-large configuration)
2988 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002989 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002990 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002991 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002992 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002993 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002994 // Change the state *after* we've called all the transition code
2995 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002996 }
2997
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002998 // Hide the customization drawer (only exists in x-large configuration)
2999 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003000 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003001 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003002 }
3003 }
3004
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003005 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3006 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3007 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003008 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003009 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003010
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003011 void onWorkspaceClick(CellLayout layout) {
3012 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003013 }
3014
Michael Jurka4ef207b2010-11-29 17:05:45 -08003015 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003016 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003017 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3018 ImageView button = (ImageView) findViewById(buttonId);
3019 Drawable toolbarIcon = null;
3020 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003021 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003022 // Look for the toolbar icon specified in the activity meta-data
3023 Bundle metaData = packageManager.getActivityInfo(
3024 activityName, PackageManager.GET_META_DATA).metaData;
3025 if (metaData != null) {
3026 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3027 if (iconResId != 0) {
3028 Resources res = packageManager.getResourcesForActivity(activityName);
3029 toolbarIcon = res.getDrawable(iconResId);
3030 }
3031 }
3032 } catch (NameNotFoundException e) {
3033 // Do nothing
3034 }
3035 // If we were unable to find the icon via the meta-data, use a generic one
3036 if (toolbarIcon == null) {
3037 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003038 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003039 } else {
3040 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003041 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003042 }
3043 }
3044
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003045 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003046 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003047 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003048 }
3049
Michael Jurka0423dcf2010-10-05 14:56:18 -07003050 private void updateGlobalSearchIcon() {
3051 if (LauncherApplication.isScreenXLarge()) {
3052 final SearchManager searchManager =
3053 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3054 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003055 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003056 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003057 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003058 } else {
3059 findViewById(R.id.search_button).setVisibility(View.GONE);
3060 }
3061 }
3062 }
3063
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003064 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003065 updateButtonWithDrawable(R.id.search_button, d);
3066 }
3067
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003068 private void updateVoiceSearchIcon() {
3069 if (LauncherApplication.isScreenXLarge()) {
3070 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3071 ComponentName activityName = intent.resolveActivity(getPackageManager());
3072 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003073 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003074 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3075 } else {
3076 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003077 }
3078 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003079 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003080
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003081 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003082 updateButtonWithDrawable(R.id.voice_button, d);
3083 }
3084
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003085 /**
3086 * Sets the app market icon (shown when all apps is visible on x-large screens)
3087 */
3088 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089 if (LauncherApplication.isScreenXLarge()) {
3090 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3091 // Find the app market activity by resolving an intent.
3092 // (If multiple app markets are installed, it will return the ResolverActivity.)
3093 ComponentName activityName = intent.resolveActivity(getPackageManager());
3094 if (activityName != null) {
3095 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003096 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003097 R.id.market_button, activityName, R.drawable.app_market_generic);
3098 }
3099 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003100 }
3101
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003102 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003103 updateButtonWithDrawable(R.id.market_button, d);
3104 }
3105
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003106 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003107 * Displays the shortcut creation dialog and launches, if necessary, the
3108 * appropriate activity.
3109 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003110 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003111 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3112 DialogInterface.OnShowListener {
3113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3120 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003121 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 builder.setInverseBackgroundForced(true);
3124
3125 AlertDialog dialog = builder.create();
3126 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003127 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003128 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003130 return dialog;
3131 }
3132
3133 public void onCancel(DialogInterface dialog) {
3134 mWaitingForResult = false;
3135 cleanup();
3136 }
3137
Romain Guycbb89e42009-06-08 15:52:54 -07003138 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003139 }
3140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003142 try {
3143 dismissDialog(DIALOG_CREATE_SHORTCUT);
3144 } catch (Exception e) {
3145 // An exception is thrown if the dialog is not visible, which is fine
3146 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 }
3148
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003149 /**
3150 * Handle the action clicked in the "Add to home" dialog.
3151 */
3152 public void onClick(DialogInterface dialog, int which) {
3153 Resources res = getResources();
3154 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003155
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003156 switch (which) {
3157 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003158 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003159 break;
3160 }
Romain Guycbb89e42009-06-08 15:52:54 -07003161
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003162 case AddAdapter.ITEM_APPWIDGET: {
3163 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003164
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003165 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3166 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003167 // start the pick activity
3168 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3169 break;
3170 }
Romain Guycbb89e42009-06-08 15:52:54 -07003171
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003172 case AddAdapter.ITEM_LIVE_FOLDER: {
3173 // Insert extra item to handle inserting folder
3174 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003175
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003176 ArrayList<String> shortcutNames = new ArrayList<String>();
3177 shortcutNames.add(res.getString(R.string.group_folder));
3178 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003179
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003180 ArrayList<ShortcutIconResource> shortcutIcons =
3181 new ArrayList<ShortcutIconResource>();
3182 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3183 R.drawable.ic_launcher_folder));
3184 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3185
3186 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3187 pickIntent.putExtra(Intent.EXTRA_INTENT,
3188 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3189 pickIntent.putExtra(Intent.EXTRA_TITLE,
3190 getText(R.string.title_select_live_folder));
3191 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003192
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003193 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3194 break;
3195 }
3196
3197 case AddAdapter.ITEM_WALLPAPER: {
3198 startWallpaper();
3199 break;
3200 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201 }
3202 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003203
3204 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003205 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 }
3208
3209 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003210 * Receives notifications when applications are added/removed.
3211 */
3212 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3213 @Override
3214 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003215 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003216 String reason = intent.getStringExtra("reason");
3217 if (!"homekey".equals(reason)) {
3218 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003219 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003220 animate = false;
3221 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003222 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003223 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003224 }
3225 }
3226
3227 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003228 * Receives notifications whenever the appwidgets are reset.
3229 */
3230 private class AppWidgetResetObserver extends ContentObserver {
3231 public AppWidgetResetObserver() {
3232 super(new Handler());
3233 }
3234
3235 @Override
3236 public void onChange(boolean selfChange) {
3237 onAppWidgetReset();
3238 }
3239 }
3240
3241 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003242 * If the activity is currently paused, signal that we need to re-run the loader
3243 * in onResume.
3244 *
3245 * This needs to be called from incoming places where resources might have been loaded
3246 * while we are paused. That is becaues the Configuration might be wrong
3247 * when we're not running, and if it comes back to what it was when we
3248 * were paused, we are not restarted.
3249 *
3250 * Implementation of the method from LauncherModel.Callbacks.
3251 *
3252 * @return true if we are currently paused. The caller might be able to
3253 * skip some work in that case since we will come back again.
3254 */
3255 public boolean setLoadOnResume() {
3256 if (mPaused) {
3257 Log.i(TAG, "setLoadOnResume");
3258 mOnResumeNeedsLoad = true;
3259 return true;
3260 } else {
3261 return false;
3262 }
3263 }
3264
3265 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003266 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003269 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003270 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003271 } else {
3272 return SCREEN_COUNT / 2;
3273 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003275
Winson Chunga12a2502010-12-20 14:41:35 -08003276 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003277 mAllAppsPagedView = view;
3278 }
3279
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 /**
3281 * Refreshes the shortcuts shown on the workspace.
3282 *
3283 * Implementation of the method from LauncherModel.Callbacks.
3284 */
3285 public void startBinding() {
3286 final Workspace workspace = mWorkspace;
3287 int count = workspace.getChildCount();
3288 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003289 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003290 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3291 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003292
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 if (DEBUG_USER_INTERFACE) {
3294 android.widget.Button finishButton = new android.widget.Button(this);
3295 finishButton.setText("Finish");
3296 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3297
3298 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3299 public void onClick(View v) {
3300 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003303 }
3304 }
3305
3306 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 * Bind the items start-end from the list.
3308 *
3309 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003310 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3312
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003313 setLoadOnResume();
3314
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 final Workspace workspace = mWorkspace;
3316
3317 for (int i=start; i<end; i++) {
3318 final ItemInfo item = shortcuts.get(i);
3319 mDesktopItems.add(item);
3320 switch (item.itemType) {
3321 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3322 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003323 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3325 false);
3326 break;
3327 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3328 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003329 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003330 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3332 false);
3333 break;
3334 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3335 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3336 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003337 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 (LiveFolderInfo) item);
3339 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3340 false);
3341 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003343 }
3344
Joe Onorato9c1289c2009-08-17 11:03:03 -04003345 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003346 }
3347
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 /**
3349 * Implementation of the method from LauncherModel.Callbacks.
3350 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003351 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003352 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003353 sFolders.clear();
3354 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356
3357 /**
3358 * Add the views for a widget to the workspace.
3359 *
3360 * Implementation of the method from LauncherModel.Callbacks.
3361 */
3362 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003363 setLoadOnResume();
3364
Daniel Sandler843e8602010-06-07 14:59:01 -04003365 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3366 if (DEBUG_WIDGETS) {
3367 Log.d(TAG, "bindAppWidget: " + item);
3368 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 final Workspace workspace = mWorkspace;
3370
3371 final int appWidgetId = item.appWidgetId;
3372 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003373 if (DEBUG_WIDGETS) {
3374 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3375 }
3376
Joe Onorato9c1289c2009-08-17 11:03:03 -04003377 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3378
Joe Onorato9c1289c2009-08-17 11:03:03 -04003379 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3380 item.hostView.setTag(item);
3381
3382 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3383 item.cellY, item.spanX, item.spanY, false);
3384
Adam Cohended9f8d2010-11-03 13:25:16 -07003385 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3386
Joe Onorato9c1289c2009-08-17 11:03:03 -04003387 workspace.requestLayout();
3388
3389 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003390
3391 if (DEBUG_WIDGETS) {
3392 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3393 + (SystemClock.uptimeMillis()-start) + "ms");
3394 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 }
3396
3397 /**
3398 * Callback saying that there aren't any more items to bind.
3399 *
3400 * Implementation of the method from LauncherModel.Callbacks.
3401 */
3402 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003403 setLoadOnResume();
3404
Joe Onorato9c1289c2009-08-17 11:03:03 -04003405 if (mSavedState != null) {
3406 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003407 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003408 }
3409
3410 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3411 if (userFolders != null) {
3412 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003413 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 if (info != null) {
3415 openFolder(info);
3416 }
3417 }
3418 final Folder openFolder = mWorkspace.getOpenFolder();
3419 if (openFolder != null) {
3420 openFolder.requestFocus();
3421 }
3422 }
3423
Joe Onorato9c1289c2009-08-17 11:03:03 -04003424 mSavedState = null;
3425 }
3426
3427 if (mSavedInstanceState != null) {
3428 super.onRestoreInstanceState(mSavedInstanceState);
3429 mSavedInstanceState = null;
3430 }
3431
Joe Onorato9c1289c2009-08-17 11:03:03 -04003432 mWorkspaceLoading = false;
3433 }
3434
3435 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003436 * Updates the icons on the launcher that are affected by changes to the package list
3437 * on the device.
3438 */
3439 private void updateIconsAffectedByPackageManagerChanges() {
3440 updateAppMarketIcon();
3441 updateGlobalSearchIcon();
3442 updateVoiceSearchIcon();
3443 }
3444
3445 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003446 * Add the icons for all apps.
3447 *
3448 * Implementation of the method from LauncherModel.Callbacks.
3449 */
3450 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003451 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003452 if (mCustomizePagedView != null) {
3453 mCustomizePagedView.setApps(apps);
3454 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003455 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 }
3457
3458 /**
3459 * A package was installed.
3460 *
3461 * Implementation of the method from LauncherModel.Callbacks.
3462 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003463 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003464 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003466 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003467 if (mCustomizePagedView != null) {
3468 mCustomizePagedView.addApps(apps);
3469 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003470 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 }
3472
3473 /**
3474 * A package was updated.
3475 *
3476 * Implementation of the method from LauncherModel.Callbacks.
3477 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003478 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003479 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003481 mWorkspace.updateShortcuts(apps);
3482 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003483 if (mCustomizePagedView != null) {
3484 mCustomizePagedView.updateApps(apps);
3485 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003486 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 }
3488
3489 /**
3490 * A package was uninstalled.
3491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 */
Joe Onorato36115782010-06-17 13:28:48 -04003494 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003496 if (permanent) {
3497 mWorkspace.removeItems(apps);
3498 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003499 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003500 if (mCustomizePagedView != null) {
3501 mCustomizePagedView.removeApps(apps);
3502 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003503 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 }
Joe Onoratobe386092009-11-17 17:32:16 -08003505
3506 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003507 * A number of packages were updated.
3508 */
3509 public void bindPackagesUpdated() {
3510 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003511 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003512 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003513 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003514 }
3515
3516 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003517 * Prints out out state for debugging.
3518 */
3519 public void dumpState() {
3520 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003521 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003522 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3523 Log.d(TAG, "mRestoring=" + mRestoring);
3524 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3525 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3526 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003527 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003528 mModel.dumpState();
3529 mAllAppsGrid.dumpState();
3530 Log.d(TAG, "END launcher2 dump state");
3531 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003532}