blob: 696380cd641d45209626cbed40f10dbe4a75544b [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;
990 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700991 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700992 } else {
993 deleteZoneHandleId = R.id.all_apps_button_cluster;
994 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700995 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700996 dragController.addDragListener(deleteZone);
997
Adam Cohencdc30d52010-12-01 15:09:47 -0800998 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
999 if (allAppsDeleteZone != null) {
1000 allAppsDeleteZone.setLauncher(this);
1001 allAppsDeleteZone.setDragController(dragController);
1002 allAppsDeleteZone.setDragAndDropEnabled(false);
1003 dragController.addDragListener(allAppsDeleteZone);
1004 dragController.addDropTarget(allAppsDeleteZone);
1005 }
1006
1007 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1008 findViewById(R.id.all_apps_info_target);
1009 if (allAppsInfoTarget != null) {
1010 allAppsInfoTarget.setLauncher(this);
1011 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001012 allAppsInfoTarget.setDragAndDropEnabled(false);
1013 View marketButton = findViewById(R.id.market_button);
1014 if (marketButton != null) {
Adam Cohencdc30d52010-12-01 15:09:47 -08001015 allAppsInfoTarget.setHandle(marketButton);
1016 }
1017 }
1018
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001019 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
1020 if (infoButton != null) {
1021 infoButton.setLauncher(this);
1022 infoButton.setHandle(findViewById(R.id.configure_button));
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001023 dragController.addDragListener(infoButton);
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
Joe Onorato00acb122009-08-04 16:04:30 -04001026 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001027 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001028 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001029
Joe Onorato00acb122009-08-04 16:04:30 -04001030 // The order here is bottom to top.
1031 dragController.addDropTarget(workspace);
1032 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001033 if (infoButton != null) {
1034 dragController.addDropTarget(infoButton);
1035 }
Adam Cohencdc30d52010-12-01 15:09:47 -08001036 if (allAppsInfoTarget != null) {
1037 dragController.addDropTarget(allAppsInfoTarget);
1038 }
1039 if (allAppsDeleteZone != null) {
1040 dragController.addDropTarget(allAppsDeleteZone);
1041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 }
1043
Romain Guy8a73c512009-11-09 19:19:59 -08001044 @SuppressWarnings({"UnusedDeclaration"})
1045 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001046 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001047 mWorkspace.scrollLeft();
1048 }
Romain Guy8a73c512009-11-09 19:19:59 -08001049 }
1050
1051 @SuppressWarnings({"UnusedDeclaration"})
1052 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001053 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001054 mWorkspace.scrollRight();
1055 }
Romain Guy8a73c512009-11-09 19:19:59 -08001056 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001057
1058 @SuppressWarnings({"UnusedDeclaration"})
1059 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001060 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001061
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001062 int index = -1;
1063 if (v.getId() == R.id.hotseat_left) {
1064 index = 0;
1065 } else if (v.getId() == R.id.hotseat_right) {
1066 index = 1;
1067 }
1068
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001069 // reload these every tap; you never know when they might change
1070 loadHotseats();
1071 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1072 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001073 startActivitySafely(
1074 mHotseats[index],
1075 "hotseat"
1076 );
1077 }
1078 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 /**
1081 * Creates a view representing a shortcut.
1082 *
1083 * @param info The data structure describing the shortcut.
1084 *
1085 * @return A View inflated from R.layout.application.
1086 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001087 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001089 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 }
1091
1092 /**
1093 * Creates a view representing a shortcut inflated from the specified resource.
1094 *
1095 * @param layoutResId The id of the XML layout used to create the shortcut.
1096 * @param parent The group the shortcut belongs to.
1097 * @param info The data structure describing the shortcut.
1098 *
1099 * @return A View inflated from layoutResId.
1100 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001101 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001102 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1103 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 return favorite;
1106 }
1107
1108 /**
1109 * Add an application shortcut to the workspace.
1110 *
1111 * @param data The intent describing the application.
1112 * @param cellInfo The position on screen where to create the shortcut.
1113 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 void completeAddApplication(Context context, Intent data, int screen,
1115 int intersectCellX, int intersectCellY) {
1116 final int[] cellXY = mTmpAddItemCellCoordinates;
1117 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1118
1119 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1120 showOutOfSpaceMessage();
1121 return;
1122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1125 data, context);
1126
Romain Guy73b979d2009-06-09 12:57:21 -07001127 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001128 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001130 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1132 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001133 } else {
1134 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 }
1136 }
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 /**
1139 * Add a shortcut to the workspace.
1140 *
1141 * @param data The intent describing the shortcut.
1142 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001144 private void completeAddShortcut(Intent data, int screen,
1145 int intersectCellX, int intersectCellY) {
1146 final int[] cellXY = mTmpAddItemCellCoordinates;
1147 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 int[] touchXY = null;
1150 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1151 touchXY = mAddDropPosition;
1152 }
1153 boolean foundCellSpan = false;
1154 if (touchXY != null) {
1155 // when dragging and dropping, just find the closest free spot
1156 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1157 int[] result = screenLayout.findNearestVacantArea(
1158 touchXY[0], touchXY[1], 1, 1, cellXY);
1159 foundCellSpan = (result != null);
1160 } else {
1161 foundCellSpan = layout.findCellForSpanThatIntersects(
1162 cellXY, 1, 1, intersectCellX, intersectCellY);
1163 }
1164
1165 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001166 showOutOfSpaceMessage();
1167 return;
1168 }
1169
1170 final ShortcutInfo info = mModel.addShortcut(
1171 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172
1173 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
1177 }
1178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001180 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001182 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001183 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001186 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001188 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1190 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001193 // We have saved the position to which the widget was dragged-- this really only matters
1194 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001196
1197 // For now, we don't save the coordinate where we dropped the icon because we're not
1198 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1199 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001200 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001201 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001202 touchXY = mAddDropPosition;
1203 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001204 boolean foundCellSpan = false;
1205 if (touchXY != null) {
1206 // when dragging and dropping, just find the closest free spot
1207 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1208 int[] result = screenLayout.findNearestVacantArea(
1209 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001210 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001212 // if we long pressed on an empty cell to bring up a menu,
1213 // make sure we intersect the empty cell
1214 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1215 // findCellForSpanThatIntersects will just ignore them
1216 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1217 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001218 }
1219
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001221 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001222 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001223 return;
1224 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001226 // Build Launcher-specific widget info and save to database
1227 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 launcherInfo.spanX = spanXY[0];
1229 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 LauncherModel.addItemToDatabase(this, launcherInfo,
1232 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001233 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234
1235 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001240
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001241 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Michael Jurka0280c3b2010-09-17 15:00:07 -07001244 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001245 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001246
1247 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
1249 }
Romain Guycbb89e42009-06-08 15:52:54 -07001250
Adam Cohended9f8d2010-11-03 13:25:16 -07001251 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1252 @Override
1253 public void onReceive(Context context, Intent intent) {
1254 final String action = intent.getAction();
1255 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1256 mUserPresent = false;
1257 updateRunning();
1258 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1259 mUserPresent = true;
1260 updateRunning();
1261 }
1262 }
1263 };
1264
1265 @Override
1266 public void onAttachedToWindow() {
1267 super.onAttachedToWindow();
1268
1269 // Listen for broadcasts related to user-presence
1270 final IntentFilter filter = new IntentFilter();
1271 filter.addAction(Intent.ACTION_SCREEN_OFF);
1272 filter.addAction(Intent.ACTION_USER_PRESENT);
1273 registerReceiver(mReceiver, filter);
1274
Adam Cohend113e0c2010-11-11 10:48:05 -08001275 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001276 mVisible = true;
1277 }
1278
1279 @Override
1280 public void onDetachedFromWindow() {
1281 super.onDetachedFromWindow();
1282 mVisible = false;
1283
Adam Cohend113e0c2010-11-11 10:48:05 -08001284 if (mAttached) {
1285 unregisterReceiver(mReceiver);
1286 mAttached = false;
1287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 updateRunning();
1289 }
1290
1291 public void onWindowVisibilityChanged(int visibility) {
1292 mVisible = visibility == View.VISIBLE;
1293 updateRunning();
1294 }
1295
1296 private void sendAdvanceMessage(long delay) {
1297 mHandler.removeMessages(ADVANCE_MSG);
1298 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1299 mHandler.sendMessageDelayed(msg, delay);
1300 mAutoAdvanceSentTime = System.currentTimeMillis();
1301 }
1302
1303 private void updateRunning() {
1304 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1305 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1306 mAutoAdvanceRunning = autoAdvanceRunning;
1307 if (autoAdvanceRunning) {
1308 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1309 sendAdvanceMessage(delay);
1310 } else {
1311 if (!mWidgetsToAdvance.isEmpty()) {
1312 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1313 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1314 }
1315 mHandler.removeMessages(ADVANCE_MSG);
1316 }
1317 }
1318 }
1319
1320 private final Handler mHandler = new Handler() {
1321 @Override
1322 public void handleMessage(Message msg) {
1323 if (msg.what == ADVANCE_MSG) {
1324 int i = 0;
1325 for (View key: mWidgetsToAdvance.keySet()) {
1326 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1327 final int delay = mAdvanceStagger * i;
1328 if (v instanceof Advanceable) {
1329 postDelayed(new Runnable() {
1330 public void run() {
1331 ((Advanceable) v).advance();
1332 }
1333 }, delay);
1334 }
1335 i++;
1336 }
1337 sendAdvanceMessage(mAdvanceInterval);
1338 }
1339 }
1340 };
1341
1342 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1343 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1344 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1345 if (v instanceof Advanceable) {
1346 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1347 ((Advanceable) v).willBeAdvancedByHost();
1348 updateRunning();
1349 }
1350 }
1351
1352 void removeWidgetToAutoAdvance(View hostView) {
1353 if (mWidgetsToAdvance.containsKey(hostView)) {
1354 mWidgetsToAdvance.remove(hostView);
1355 updateRunning();
1356 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001357 }
1358
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1360 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001362 launcherInfo.hostView = null;
1363 }
1364
Adam Cohended9f8d2010-11-03 13:25:16 -07001365 void showOutOfSpaceMessage() {
1366 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1367 }
1368
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001369 public LauncherAppWidgetHost getAppWidgetHost() {
1370 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
Romain Guycbb89e42009-06-08 15:52:54 -07001372
Winson Chunga9abd0e2010-10-27 17:18:37 -07001373 public LauncherModel getModel() {
1374 return mModel;
1375 }
1376
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 getWindow().closeAllPanels();
1379
1380 try {
1381 dismissDialog(DIALOG_CREATE_SHORTCUT);
1382 // Unlock the workspace if the dialog was showing
1383 } catch (Exception e) {
1384 // An exception is thrown if the dialog is not visible, which is fine
1385 }
1386
1387 try {
1388 dismissDialog(DIALOG_RENAME_FOLDER);
1389 // Unlock the workspace if the dialog was showing
1390 } catch (Exception e) {
1391 // An exception is thrown if the dialog is not visible, which is fine
1392 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001393
1394 // Whatever we were doing is hereby canceled.
1395 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001396 }
1397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 @Override
1399 protected void onNewIntent(Intent intent) {
1400 super.onNewIntent(intent);
1401
1402 // Close the menu
1403 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001404 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001405 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001406
Joe Onorato14f122b2009-11-19 14:06:36 -08001407 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1408 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001409
Michael Jurka4cb37242010-08-09 21:05:32 -07001410 // in all these cases, only animate if we're already on home
1411 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001412 mWorkspace.unshrink(alreadyOnHome);
1413 }
1414 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001415 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001416 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1417 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001418 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001419 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001420
Joe Onorato3a8820b2009-11-10 15:06:42 -08001421 final View v = getWindow().peekDecorView();
1422 if (v != null && v.getWindowToken() != null) {
1423 InputMethodManager imm = (InputMethodManager)getSystemService(
1424 INPUT_METHOD_SERVICE);
1425 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 }
1427 }
1428 }
1429
1430 @Override
1431 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1432 // Do not call super here
1433 mSavedInstanceState = savedInstanceState;
1434 }
1435
1436 @Override
1437 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001438 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
1440 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1441 if (folders.size() > 0) {
1442 final int count = folders.size();
1443 long[] ids = new long[count];
1444 for (int i = 0; i < count; i++) {
1445 final FolderInfo info = folders.get(i).getInfo();
1446 ids[i] = info.id;
1447 }
1448 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1449 } else {
1450 super.onSaveInstanceState(outState);
1451 }
1452
Michael Jurka883f55b2010-10-21 15:47:14 -07001453 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454
Michael Jurka0280c3b2010-09-17 15:00:07 -07001455 if (mAddScreen > -1 && mWaitingForResult) {
1456 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1457 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1458 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 }
1460
1461 if (mFolderInfo != null && mWaitingForResult) {
1462 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1463 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1464 }
Michael Jurka946ad472010-07-09 18:05:18 -07001465
Winson Chung43b119d2010-12-06 16:45:05 -08001466 // Save the current AllApps drawer tab
1467 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1468 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1469 String currentTabTag = tabhost.getCurrentTabTag();
1470 if (currentTabTag != null) {
1471 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001472 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001473 }
1474 }
1475
1476 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001477 if (mHomeCustomizationDrawer != null) {
1478 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1479 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001480 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001481 }
1482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484
1485 @Override
1486 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001488
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001490 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001492 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
1495 TextKeyListener.getInstance().release();
1496
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
Joe Onorato9c1289c2009-08-17 11:03:03 -04001499 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001500
1501 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001502
Patrick Dubroyab962b72010-07-26 12:10:10 -07001503 // Some launcher layouts don't have a previous and next view
1504 if (mPreviousView != null) {
1505 dismissPreview(mPreviousView);
1506 }
1507 if (mNextView != null) {
1508 dismissPreview(mNextView);
1509 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001510
1511 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513
1514 @Override
1515 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001516 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 super.startActivityForResult(intent, requestCode);
1518 }
1519
1520 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001521 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001523
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001524 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Karl Rosaen138a0412009-04-23 19:00:21 -07001526 if (initialQuery == null) {
1527 // Use any text typed in the launcher as the initial query
1528 initialQuery = getTypedText();
1529 clearTypedText();
1530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 if (appSearchData == null) {
1532 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001533 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 final SearchManager searchManager =
1537 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001538 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001539 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
1542 @Override
1543 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001544 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001545 return false;
1546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547
1548 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001549
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1551 .setIcon(android.R.drawable.ic_menu_add)
1552 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001553 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1554 .setIcon(android.R.drawable.ic_menu_manage)
1555 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001556 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 .setIcon(android.R.drawable.ic_menu_gallery)
1558 .setAlphabeticShortcut('W');
1559 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1560 .setIcon(android.R.drawable.ic_search_category_default)
1561 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1562 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1563 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1564 .setAlphabeticShortcut('N');
1565
1566 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001567 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1568 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569
1570 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1571 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1572 .setIntent(settings);
1573
1574 return true;
1575 }
1576
1577 @Override
1578 public boolean onPrepareOptionsMenu(Menu menu) {
1579 super.onPrepareOptionsMenu(menu);
1580
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001581 // If all apps is animating, don't show the menu, because we don't know
1582 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001583 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001584 return false;
1585 }
1586
1587 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001588 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001589 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1590 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1591
1592 // Disable add if the workspace is full.
1593 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001594 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1595 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
1598 return true;
1599 }
1600
1601 @Override
1602 public boolean onOptionsItemSelected(MenuItem item) {
1603 switch (item.getItemId()) {
1604 case MENU_ADD:
1605 addItems();
1606 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001607 case MENU_MANAGE_APPS:
1608 manageApps();
1609 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 case MENU_WALLPAPER_SETTINGS:
1611 startWallpaper();
1612 return true;
1613 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001614 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 return true;
1616 case MENU_NOTIFICATIONS:
1617 showNotifications();
1618 return true;
1619 }
1620
1621 return super.onOptionsItemSelected(item);
1622 }
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Karl Rosaen138a0412009-04-23 19:00:21 -07001624 /**
1625 * Indicates that we want global search for this activity by setting the globalSearch
1626 * argument for {@link #startSearch} to true.
1627 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001629 @Override
1630 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001631 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001632 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001633 }
1634
Joe Onorato9c1289c2009-08-17 11:03:03 -04001635 public boolean isWorkspaceLocked() {
1636 return mWorkspaceLoading || mWaitingForResult;
1637 }
1638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001640 if (LauncherApplication.isScreenXLarge()) {
1641 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001642 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001643 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001644 }
1645 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001646 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001647 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 }
1650
Michael Jurka0280c3b2010-09-17 15:00:07 -07001651 private void resetAddInfo() {
1652 mAddScreen = -1;
1653 mAddIntersectCellX = -1;
1654 mAddIntersectCellY = -1;
1655 mAddDropPosition = null;
1656 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001657
Winson Chung55cef262010-10-28 14:14:18 -07001658 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001659 resetAddInfo();
1660 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 mAddDropPosition = position;
1662
Michael Jurkaaf442092010-06-10 17:01:57 -07001663 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001664 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1665 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001666 }
1667
Winson Chung7ad01412010-09-27 11:33:03 -07001668 private void manageApps() {
1669 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1670 }
1671
Michael Jurkaaf442092010-06-10 17:01:57 -07001672 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001673 // TODO: catch bad widget exception when sent
1674 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001675 // TODO: Is this log message meaningful?
1676 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001677 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001678 }
1679
Winson Chung55cef262010-10-28 14:14:18 -07001680 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001681 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682
Bjorn Bringert7984c942009-12-09 15:38:25 +00001683 if (appWidget.configure != null) {
1684 // Launch over to configure widget, if needed
1685 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1686 intent.setComponent(appWidget.configure);
1687 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001688 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001689 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1690 intent.putExtra(
1691 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1692 info.mimeType);
1693
1694 final String mimeType = info.mimeType;
1695 final ClipData clipData = (ClipData) info.configurationData;
1696 final ClipDescription clipDesc = clipData.getDescription();
1697 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1698 if (clipDesc.getMimeType(i).equals(mimeType)) {
1699 final ClipData.Item item = clipData.getItem(i);
1700 final CharSequence stringData = item.getText();
1701 final Uri uriData = item.getUri();
1702 final Intent intentData = item.getIntent();
1703 final String key =
1704 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1705 if (uriData != null) {
1706 intent.putExtra(key, uriData);
1707 } else if (intentData != null) {
1708 intent.putExtra(key, intentData);
1709 } else if (stringData != null) {
1710 intent.putExtra(key, stringData);
1711 }
1712 break;
1713 }
1714 }
1715 }
Winson Chung55cef262010-10-28 14:14:18 -07001716 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001717
Romain Guy8e633c52010-03-04 12:51:36 -08001718 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001720 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001721 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723 }
Romain Guycbb89e42009-06-08 15:52:54 -07001724
Michael Jurka0280c3b2010-09-17 15:00:07 -07001725 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1726 resetAddInfo();
1727 mAddScreen = screen;
1728 mAddDropPosition = position;
1729
1730 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1731 createShortcutIntent.setComponent(componentName);
1732 processShortcut(createShortcutIntent);
1733 }
1734
Joe Onoratodeb98af2010-02-19 14:59:39 -08001735 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001736 // Handle case where user selected "Applications"
1737 String applicationName = getResources().getString(R.string.group_applications);
1738 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001740 if (applicationName != null && applicationName.equals(shortcutName)) {
1741 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1742 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001744 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1745 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001746 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001747 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001748 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001749 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
1752
Winson Chung24ab2f12010-09-16 14:10:47 -07001753 void processWallpaper(Intent intent) {
1754 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1755 }
1756
Michael Jurka0280c3b2010-09-17 15:00:07 -07001757 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1758 resetAddInfo();
1759 mAddScreen = screen;
1760 mAddDropPosition = position;
1761
1762 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1763 createFolderIntent.setComponent(componentName);
1764
1765 addLiveFolder(createFolderIntent);
1766 }
1767
1768 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001770 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001772
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001774 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001775 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001776 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 }
1779
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 UserFolderInfo folderInfo = new UserFolderInfo();
1782 folderInfo.title = getText(R.string.folder_name);
1783
Michael Jurka0280c3b2010-09-17 15:00:07 -07001784 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1785 final int[] cellXY = mTmpAddItemCellCoordinates;
1786 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1787 showOutOfSpaceMessage();
1788 return;
1789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790
1791 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001792 LauncherModel.addItemToDatabase(this, folderInfo,
1793 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001794 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001795 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796
1797 // Create the view
1798 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001799 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1800 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001801 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
Romain Guycbb89e42009-06-08 15:52:54 -07001803
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001805 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001806 }
1807
Michael Jurka28750fb2010-09-24 17:43:49 -07001808 private void completeAddLiveFolder(
1809 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1811 final int[] cellXY = mTmpAddItemCellCoordinates;
1812 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1813 showOutOfSpaceMessage();
1814 return;
1815 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816
Michael Jurka0280c3b2010-09-17 15:00:07 -07001817 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818
1819 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001821 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001822 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 }
1824 }
1825
1826 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001827 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828
1829 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1830 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1831
1832 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 Intent.ShortcutIconResource iconResource = null;
1834
1835 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1836 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1837 try {
1838 iconResource = (Intent.ShortcutIconResource) extra;
1839 final PackageManager packageManager = context.getPackageManager();
1840 Resources resources = packageManager.getResourcesForApplication(
1841 iconResource.packageName);
1842 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1843 icon = resources.getDrawable(id);
1844 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001845 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 }
1847 }
1848
1849 if (icon == null) {
1850 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1851 }
1852
1853 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001854 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 info.title = name;
1856 info.iconResource = iconResource;
1857 info.uri = data.getData();
1858 info.baseIntent = baseIntent;
1859 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1860 LiveFolders.DISPLAY_MODE_GRID);
1861
1862 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001863 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001864 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865
1866 return info;
1867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 private void showNotifications() {
1870 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1871 if (statusBar != null) {
1872 statusBar.expand();
1873 }
1874 }
1875
1876 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001877 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001879 Intent chooser = Intent.createChooser(pickWallpaper,
1880 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001881 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1882 // Removed in Eclair MR1
1883// WallpaperManager wm = (WallpaperManager)
1884// getSystemService(Context.WALLPAPER_SERVICE);
1885// WallpaperInfo wi = wm.getWallpaperInfo();
1886// if (wi != null && wi.getSettingsActivity() != null) {
1887// LabeledIntent li = new LabeledIntent(getPackageName(),
1888// R.string.configure_wallpaper, 0);
1889// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1890// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1891// }
Mike Clerona0618e42009-10-22 13:55:21 -07001892 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
1894
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001895 /**
1896 * Registers various content observers. The current implementation registers
1897 * only a favorites observer to keep track of the favorites applications.
1898 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001899 private void registerContentObservers() {
1900 ContentResolver resolver = getContentResolver();
1901 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1902 true, mWidgetObserver);
1903 }
1904
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 @Override
1906 public boolean dispatchKeyEvent(KeyEvent event) {
1907 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1908 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001910 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001911 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001912 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001913 dumpState();
1914 return true;
1915 }
1916 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001917 }
1918 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1919 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001920 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 return true;
1922 }
1923 }
1924
1925 return super.dispatchKeyEvent(event);
1926 }
1927
Joe Onorato88ec0992009-11-19 13:16:06 -08001928 @Override
1929 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001930 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1931 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001932 } else {
1933 closeFolder();
1934 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001935 // Some launcher layouts don't have a previous and next view
1936 if (mPreviousView != null) {
1937 dismissPreview(mPreviousView);
1938 dismissPreview(mNextView);
1939 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001940 }
1941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 private void closeFolder() {
1943 Folder folder = mWorkspace.getOpenFolder();
1944 if (folder != null) {
1945 closeFolder(folder);
1946 }
1947 }
1948
1949 void closeFolder(Folder folder) {
1950 folder.getInfo().opened = false;
1951 ViewGroup parent = (ViewGroup) folder.getParent();
1952 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001953 CellLayout cl = (CellLayout) parent;
1954 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001955 if (folder instanceof DropTarget) {
1956 // Live folders aren't DropTargets.
1957 mDragController.removeDropTarget((DropTarget)folder);
1958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 }
1960 folder.onClose();
1961 }
1962
1963 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001964 * Re-listen when widgets are reset.
1965 */
1966 private void onAppWidgetReset() {
1967 mAppWidgetHost.startListening();
1968 }
1969
1970 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001971 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1972 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001974 private void unbindDesktopItems() {
1975 for (ItemInfo item: mDesktopItems) {
1976 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001979
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 /**
1981 * Launches the intent referred by the clicked shortcut.
1982 *
1983 * @param v The view representing the clicked shortcut.
1984 */
1985 public void onClick(View v) {
1986 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001987 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001989 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001990 int[] pos = new int[2];
1991 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001992 intent.setSourceBounds(new Rect(pos[0], pos[1],
1993 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001994 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 } else if (tag instanceof FolderInfo) {
1996 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001997 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001998 if (mState == State.ALL_APPS) {
1999 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002000 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002001 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004 }
2005
Michael Jurka0e260592010-06-30 17:07:39 -07002006 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002007 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002008 // clicking anywhere on the workspace causes the customization drawer to slide down
2009 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002010 return false;
2011 }
2012
Michael Jurkaaf442092010-06-10 17:01:57 -07002013 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002014 * Event handler for the search button
2015 *
2016 * @param v The view that was clicked.
2017 */
2018 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002019 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002020 // Use a custom animation for launching search
2021 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002022 }
2023
2024 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002025 * Event handler for the voice button
2026 *
2027 * @param v The view that was clicked.
2028 */
2029 public void onClickVoiceButton(View v) {
2030 startVoiceSearch();
2031 }
2032
2033 private void startVoiceSearch() {
2034 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2035 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2036 startActivity(intent);
2037 }
2038
2039 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002040 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002041 * enters home screen customization mode.
2042 *
2043 * @param v The view that was clicked.
2044 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002045 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002046 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002047 }
2048
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002049 /**
2050 * Event handler for the "grid" button that appears on the home screen, which
2051 * enters all apps mode.
2052 *
2053 * @param v The view that was clicked.
2054 */
2055 public void onClickAllAppsButton(View v) {
2056 showAllApps(true);
2057 }
2058
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002059 public void onClickAppMarketButton(View v) {
2060 if (mAppMarketIntent != null) {
2061 startActivitySafely(mAppMarketIntent, "app market");
2062 }
2063 }
2064
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002065 void startApplicationDetailsActivity(ComponentName componentName) {
2066 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002067 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2068 Uri.fromParts("package", packageName, null));
2069 startActivity(intent);
2070 }
2071
Patrick Dubroy5539af72010-09-07 15:22:01 -07002072 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2073 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2074 // System applications cannot be installed. For now, show a toast explaining that.
2075 // We may give them the option of disabling apps this way.
2076 int messageId = R.string.uninstall_system_app_text;
2077 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2078 } else {
2079 String packageName = appInfo.componentName.getPackageName();
2080 String className = appInfo.componentName.getClassName();
2081 Intent intent = new Intent(
2082 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2083 startActivity(intent);
2084 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002085 }
2086
Joe Onoratof984e852010-03-25 09:47:45 -07002087 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2089 try {
2090 startActivity(intent);
2091 } catch (ActivityNotFoundException e) {
2092 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002093 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 } catch (SecurityException e) {
2095 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002096 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002098 "or use the exported attribute for this activity. "
2099 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 }
2101 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002102
Romain Guy8e633c52010-03-04 12:51:36 -08002103 void startActivityForResultSafely(Intent intent, int requestCode) {
2104 try {
2105 startActivityForResult(intent, requestCode);
2106 } catch (ActivityNotFoundException e) {
2107 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2108 } catch (SecurityException e) {
2109 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2110 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2111 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2112 "or use the exported attribute for this activity.", e);
2113 }
2114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115
2116 private void handleFolderClick(FolderInfo folderInfo) {
2117 if (!folderInfo.opened) {
2118 // Close any open folder
2119 closeFolder();
2120 // Open the requested folder
2121 openFolder(folderInfo);
2122 } else {
2123 // Find the open folder...
2124 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2125 int folderScreen;
2126 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002127 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 // .. and close it
2129 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002130 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 // Close any folder open on the current screen
2132 closeFolder();
2133 // Pull the folder onto this screen
2134 openFolder(folderInfo);
2135 }
2136 }
2137 }
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002141 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 * is animated relative to the specified View. If the View is null, no animation
2143 * is played.
2144 *
2145 * @param folderInfo The FolderInfo describing the folder to open.
2146 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002147 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 Folder openFolder;
2149
2150 if (folderInfo instanceof UserFolderInfo) {
2151 openFolder = UserFolder.fromXml(this);
2152 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002153 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 } else {
2155 return;
2156 }
2157
Joe Onorato00acb122009-08-04 16:04:30 -04002158 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 openFolder.setLauncher(this);
2160
2161 openFolder.bind(folderInfo);
2162 folderInfo.opened = true;
2163
Winson Chungaafa03c2010-06-11 17:34:16 -07002164 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2165
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 openFolder.onOpen();
2167 }
2168
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002170 switch (v.getId()) {
2171 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002172 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002173 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2174 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002175 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002176 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002177 return true;
2178 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002179 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002180 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2181 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002182 showPreviews(v);
2183 }
2184 return true;
2185 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002186 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2189 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002190 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002191 return true;
2192 }
2193
Joe Onorato9c1289c2009-08-17 11:03:03 -04002194 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 return false;
2196 }
2197
2198 if (!(v instanceof CellLayout)) {
2199 v = (View) v.getParent();
2200 }
2201
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002202
Michael Jurka0280c3b2010-09-17 15:00:07 -07002203 resetAddInfo();
2204 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002205 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002206 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 return true;
2208 }
2209
Michael Jurka0280c3b2010-09-17 15:00:07 -07002210 final View itemUnderLongClick = longClickCellInfo.cell;
2211
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002213 if (itemUnderLongClick == null) {
2214 // User long pressed on empty space
2215 mWorkspace.setAllowLongPress(false);
2216 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2217 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002218 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002219 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 }
2221 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002222 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002224 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2225 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002226 mAddIntersectCellX = longClickCellInfo.cellX;
2227 mAddIntersectCellY = longClickCellInfo.cellY;
2228 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230 }
2231 }
2232 return true;
2233 }
2234
Romain Guye6b8e2f2009-11-10 11:56:55 -08002235 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002236 private void dismissPreview(final View v) {
2237 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002238 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002239 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2240 public void onDismiss() {
2241 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2242 int count = group.getChildCount();
2243 for (int i = 0; i < count; i++) {
2244 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2245 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002246 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2247 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2248
2249 v.setTag(R.id.workspace, null);
2250 v.setTag(R.id.icon, null);
2251 window.setOnDismissListener(null);
2252 }
2253 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002254 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002255 }
2256 v.setTag(null);
2257 }
2258
Romain Guyf8e6a802009-12-07 17:48:02 -08002259 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002260 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002261 }
2262
Romain Guya6abce82009-11-10 02:54:41 -08002263 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002264 final Resources resources = getResources();
2265 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002266
Romain Guya6abce82009-11-10 02:54:41 -08002267 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002268
Romain Guy9d31e9f2009-11-11 18:54:28 -08002269 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002270
Romain Guyf8e6a802009-12-07 17:48:02 -08002271 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002272 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002273 int extraW = (int) ((r.left + r.right) * max);
2274 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002275
2276 int aW = cell.getWidth() - extraW;
2277 float w = aW / max;
2278
2279 int width = cell.getWidth();
2280 int height = cell.getHeight();
2281 int x = cell.getLeftPadding();
2282 int y = cell.getTopPadding();
2283 width -= (x + cell.getRightPadding());
2284 height -= (y + cell.getBottomPadding());
2285
2286 float scale = w / width;
2287
2288 int count = end - start;
2289
2290 final float sWidth = width * scale;
2291 float sHeight = height * scale;
2292
Romain Guye6b8e2f2009-11-10 11:56:55 -08002293 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002294
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2296 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002297
2298 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002299 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002300 cell = (CellLayout) workspace.getChildAt(i);
2301
Romain Guyf8e6a802009-12-07 17:48:02 -08002302 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002303 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002304
2305 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002306 c.scale(scale, scale);
2307 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002308 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002309
Romain Guy9d31e9f2009-11-11 18:54:28 -08002310 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002311 image.setImageBitmap(bitmap);
2312 image.setTag(i);
2313 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002314 image.setOnFocusChangeListener(handler);
2315 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002316 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002317
2318 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002319 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2320
Winson Chungaafa03c2010-06-11 17:34:16 -07002321 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002322 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002323
2324 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002325 p.setContentView(preview);
2326 p.setWidth((int) (sWidth * count + extraW));
2327 p.setHeight((int) (sHeight + extraH));
2328 p.setAnimationStyle(R.style.AnimationPreview);
2329 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002330 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002331 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002332 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002333
Romain Guye6b8e2f2009-11-10 11:56:55 -08002334 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2335 public void onDismiss() {
2336 dismissPreview(anchor);
2337 }
2338 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002339
2340 anchor.setTag(p);
2341 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002342 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002343 }
2344
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002346 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002347
Romain Guye6b8e2f2009-11-10 11:56:55 -08002348 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002349 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002350 }
2351
2352 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002353 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002354 v.post(this);
2355 }
2356
2357 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002358 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002359 }
2360
2361 public void onFocusChange(View v, boolean hasFocus) {
2362 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002363 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002364 }
Romain Guya6abce82009-11-10 02:54:41 -08002365 }
2366 }
2367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 Workspace getWorkspace() {
2369 return mWorkspace;
2370 }
2371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002372 @Override
2373 protected Dialog onCreateDialog(int id) {
2374 switch (id) {
2375 case DIALOG_CREATE_SHORTCUT:
2376 return new CreateShortcut().createDialog();
2377 case DIALOG_RENAME_FOLDER:
2378 return new RenameFolder().createDialog();
2379 }
2380
2381 return super.onCreateDialog(id);
2382 }
2383
2384 @Override
2385 protected void onPrepareDialog(int id, Dialog dialog) {
2386 switch (id) {
2387 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 break;
2389 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002390 if (mFolderInfo != null) {
2391 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2392 final CharSequence text = mFolderInfo.title;
2393 input.setText(text);
2394 input.setSelection(0, text.length());
2395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 break;
2397 }
2398 }
2399
2400 void showRenameDialog(FolderInfo info) {
2401 mFolderInfo = info;
2402 mWaitingForResult = true;
2403 showDialog(DIALOG_RENAME_FOLDER);
2404 }
2405
Michael Jurka0280c3b2010-09-17 15:00:07 -07002406 private void showAddDialog(int intersectX, int intersectY) {
2407 resetAddInfo();
2408 mAddIntersectCellX = intersectX;
2409 mAddIntersectCellY = intersectY;
2410 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 mWaitingForResult = true;
2412 showDialog(DIALOG_CREATE_SHORTCUT);
2413 }
2414
Joe Onoratodeb98af2010-02-19 14:59:39 -08002415 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002416 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002417 Bundle bundle = new Bundle();
2418
2419 ArrayList<String> shortcutNames = new ArrayList<String>();
2420 shortcutNames.add(getString(R.string.group_applications));
2421 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2422
2423 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2424 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2425 R.drawable.ic_launcher_application));
2426 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2427
2428 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2429 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002430 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002431 pickIntent.putExtras(bundle);
2432
Joe Onoratodeb98af2010-02-19 14:59:39 -08002433 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002434 }
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 private class RenameFolder {
2437 private EditText mInput;
2438
2439 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2441 mInput = (EditText) layout.findViewById(R.id.folder_name);
2442
2443 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2444 builder.setIcon(0);
2445 builder.setTitle(getString(R.string.rename_folder_title));
2446 builder.setCancelable(true);
2447 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2448 public void onCancel(DialogInterface dialog) {
2449 cleanup();
2450 }
2451 });
2452 builder.setNegativeButton(getString(R.string.cancel_action),
2453 new Dialog.OnClickListener() {
2454 public void onClick(DialogInterface dialog, int which) {
2455 cleanup();
2456 }
2457 }
2458 );
2459 builder.setPositiveButton(getString(R.string.rename_action),
2460 new Dialog.OnClickListener() {
2461 public void onClick(DialogInterface dialog, int which) {
2462 changeFolderName();
2463 }
2464 }
2465 );
2466 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002467
2468 final AlertDialog dialog = builder.create();
2469 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2470 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002471 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002472 mInput.requestFocus();
2473 InputMethodManager inputManager = (InputMethodManager)
2474 getSystemService(Context.INPUT_METHOD_SERVICE);
2475 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002476 }
2477 });
2478
2479 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 }
2481
2482 private void changeFolderName() {
2483 final String name = mInput.getText().toString();
2484 if (!TextUtils.isEmpty(name)) {
2485 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002486 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 mFolderInfo.title = name;
2488 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2489
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002491 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002492 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 } else {
2494 final FolderIcon folderIcon = (FolderIcon)
2495 mWorkspace.getViewForTag(mFolderInfo);
2496 if (folderIcon != null) {
2497 folderIcon.setText(name);
2498 getWorkspace().requestLayout();
2499 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002500 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002501 mWorkspaceLoading = true;
2502 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002503 }
2504 }
2505 }
2506 cleanup();
2507 }
2508
2509 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002510 dismissDialog(DIALOG_RENAME_FOLDER);
2511 mWaitingForResult = false;
2512 mFolderInfo = null;
2513 }
2514 }
2515
Daniel Sandler843e8602010-06-07 14:59:01 -04002516 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2517 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002518 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002519 }
2520
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002521 // AllAppsView.Watcher
2522 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002523 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2524 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002525 mWorkspace.setVisibility(View.GONE);
2526 }
2527 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002528
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529 private void showToolbarButton(View button) {
2530 button.setAlpha(1.0f);
2531 button.setVisibility(View.VISIBLE);
2532 button.setFocusable(true);
2533 button.setClickable(true);
2534 }
2535
2536 private void hideToolbarButton(View button) {
2537 button.setAlpha(0.0f);
2538 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2539 button.setVisibility(View.INVISIBLE);
2540 button.setFocusable(false);
2541 button.setClickable(false);
2542 }
2543
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002544 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002545 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002546 *
2547 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2548 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002549 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002552 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002553 final boolean showing = show;
2554 final boolean hiding = !show;
2555
2556 final int duration = show ?
2557 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2558 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2559
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002561 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2562 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002563 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002564 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002565 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002568 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002569 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002570 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002571 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 });
2573 seq.play(anim);
2574 } else {
2575 if (showing) {
2576 showToolbarButton(view);
2577 } else {
2578 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002579 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002581 }
2582
2583 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584 * Show/hide the appropriate toolbar buttons for newState.
2585 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 *
2587 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002588 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2589 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002591 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002592 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002594 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 final View configureButton = findViewById(R.id.configure_button);
2596
2597 switch (newState) {
2598 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599 hideOrShowToolbarButton(true, searchButton, showSeq);
2600 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002601 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002602 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002603 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 break;
2605 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002606 hideOrShowToolbarButton(false, configureButton, 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, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002610 break;
2611 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002612 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002613 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002614 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002615 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002616 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 break;
2618 }
2619 }
2620
2621 /**
2622 * Helper method for the cameraZoomIn/cameraZoomOut animations
2623 * @param view The view being animated
2624 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2625 * @param scaleFactor The scale factor used for the zoom
2626 */
2627 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2628 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002629
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002631 // Set pivotY so that at the starting zoom factor, the view is partially
2632 // visible. Modifying initialHeightFactor changes how much of the view is
2633 // initially showing, and hence the perceived angle from which the view enters.
2634 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002636 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002637 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002638 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002639 }
2640 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002641
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002642 /**
2643 * Zoom the camera out from the workspace to reveal 'toView'.
2644 * Assumes that the view to show is anchored at either the very top or very bottom
2645 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002647 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002648 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002649 final Resources res = getResources();
2650 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2651 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002652 final boolean toAllApps = (toState == State.ALL_APPS);
2653 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002654
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 setPivotsForZoom(toView, toState, scale);
2656
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002657 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002658 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002660 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002662
2663 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002664 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2665 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2666 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2667 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002668
2669 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka8edd75c2010-12-17 20:15:06 -08002670 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002671 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002672 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 // Prepare the position
2674 toView.setTranslationX(0.0f);
2675 toView.setTranslationY(0.0f);
2676 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002677 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002679 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002680 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002681 // If we don't set the final scale values here, if this animation is cancelled
2682 // it will have the wrong scale value and subsequent cameraPan animations will
2683 // not fix that
2684 toView.setScaleX(1.0f);
2685 toView.setScaleY(1.0f);
2686 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002687 });
2688
Chet Haaseb1254a62010-09-07 13:35:00 -07002689 AnimatorSet toolbarHideAnim = new AnimatorSet();
2690 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2692
2693 // toView should appear right at the end of the workspace shrink animation
2694 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2695
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002696 if (mStateAnimation != null) mStateAnimation.cancel();
2697 mStateAnimation = new AnimatorSet();
2698 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2699 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700
2701 // Show the new toolbar buttons just as the main animation is ending
2702 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002703 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2704 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002705 } else {
2706 toView.setTranslationX(0.0f);
2707 toView.setTranslationY(0.0f);
2708 toView.setScaleX(1.0f);
2709 toView.setScaleY(1.0f);
2710 toView.setVisibility(View.VISIBLE);
2711 hideAndShowToolbarButtons(toState, null, null);
2712 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002713 }
2714
2715 /**
2716 * Zoom the camera back into the workspace, hiding 'fromView'.
2717 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2719 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002720 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002722 cameraZoomIn(fromState, animated, false);
2723 }
2724
2725 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002726 Resources res = getResources();
2727 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2728 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002729 final View fromView =
2730 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2731
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002732 mCustomizePagedView.endChoiceMode();
2733 mAllAppsPagedView.endChoiceMode();
2734
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002735 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002736
Michael Jurkad3ef3062010-11-23 16:23:58 -08002737 if (!springLoaded) {
2738 mWorkspace.unshrink(animated);
2739 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002740
2741 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002742 if (mStateAnimation != null) mStateAnimation.cancel();
2743 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002744 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2745 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2746 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2747 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002748 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2749
2750 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2751 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2752 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurka8edd75c2010-12-17 20:15:06 -08002753 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002754 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002755 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002756 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002757 }
2758 });
2759
Chet Haaseb1254a62010-09-07 13:35:00 -07002760 AnimatorSet toolbarHideAnim = new AnimatorSet();
2761 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002762 if (!springLoaded) {
2763 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2764 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002765
Adam Cohen61033d32010-11-15 18:29:44 -08002766 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002767
2768 // Show the new toolbar buttons at the very end of the whole animation
2769 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2770 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002771 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2772 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002773 } else {
2774 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002775 if (!springLoaded) {
2776 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2777 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002778 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002779 }
2780
2781 /**
2782 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2783 * This is the transition used on xlarge screens to go between all apps and
2784 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002785 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2786 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2787 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002788 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002789 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002790 final Resources res = getResources();
2791 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002792 final int workspaceHeight = mWorkspace.getHeight();
2793
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002794 final boolean fromAllApps = (fromState == State.ALL_APPS);
2795 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2796 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002797
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002798 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2799 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2800 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2801 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002802
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002803 mCustomizePagedView.endChoiceMode();
2804 mAllAppsPagedView.endChoiceMode();
2805
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002806 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002807 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002808 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002809 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002810 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002811
2812 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002813 if (mStateAnimation != null) mStateAnimation.cancel();
2814 mStateAnimation = new AnimatorSet();
Michael Jurka8edd75c2010-12-17 20:15:06 -08002815 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002816 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002817 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002818 toView.setVisibility(View.VISIBLE);
2819 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002820 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002821 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002822 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002823 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002824 fromView.setVisibility(View.GONE);
2825 }
2826 });
2827
Chet Haaseb1254a62010-09-07 13:35:00 -07002828 AnimatorSet toolbarHideAnim = new AnimatorSet();
2829 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002830 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2831
Chet Haase472b2812010-10-14 07:02:04 -07002832 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2833 fromViewStartY, fromViewEndY);
2834 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002835 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2836 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2837 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2838 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2839 );
Chet Haase472b2812010-10-14 07:02:04 -07002840 fromAnim.setDuration(duration);
2841 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002842
2843 // Show the new toolbar buttons just as the main animation is ending
2844 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2846 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002847 } else {
2848 fromView.setY(fromViewEndY);
2849 fromView.setVisibility(View.GONE);
2850 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002851 toView.setScaleX(1.0f);
2852 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002853 toView.setVisibility(View.VISIBLE);
2854 hideAndShowToolbarButtons(toState, null, null);
2855 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002856 }
2857
2858 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002859 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002860 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002861 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002862
Michael Jurka79212d82010-07-30 16:36:20 -07002863 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002864 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002865 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002866 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002867 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002868 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002869 } else {
2870 mAllAppsGrid.zoom(1.0f, animated);
2871 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002872
Romain Guyc16fea72010-03-12 17:17:56 -08002873 ((View) mAllAppsGrid).setFocusable(true);
2874 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002875
Joe Onorato7c312c12009-08-13 21:36:53 -07002876 // TODO: fade these two too
2877 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002878
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002879 // Change the state *after* we've called all the transition code
2880 mState = State.ALL_APPS;
2881 }
2882
2883
2884 void showWorkspace(boolean animated) {
2885 showWorkspace(animated, null);
2886 }
2887
2888 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002889 if (layout != null) {
2890 // always animated, but that's ok since we never specify a layout and
2891 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002892 mWorkspace.unshrink(layout);
2893 } else {
2894 mWorkspace.unshrink(animated);
2895 }
2896 if (mState == State.ALL_APPS) {
2897 closeAllApps(animated);
2898 } else if (mState == State.CUSTOMIZE) {
2899 hideCustomizationDrawer(animated);
2900 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002901
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002902 // Change the state *after* we've called all the transition code
2903 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002904 }
2905
Michael Jurkad3ef3062010-11-23 16:23:58 -08002906 void enterSpringLoadedDragMode(CellLayout layout) {
2907 mWorkspace.enterSpringLoadedDragMode(layout);
2908 if (mState == State.ALL_APPS) {
2909 cameraZoomIn(State.ALL_APPS, true, true);
2910 mState = State.ALL_APPS_SPRING_LOADED;
2911 } else if (mState == State.CUSTOMIZE) {
2912 cameraZoomIn(State.CUSTOMIZE, true, true);
2913 mState = State.CUSTOMIZE_SPRING_LOADED;
2914 }/* else {
2915 // we're already in spring loaded mode; don't do anything
2916 }*/
2917 }
2918
2919 void exitSpringLoadedDragMode() {
2920 if (mState == State.ALL_APPS_SPRING_LOADED) {
2921 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2922 cameraZoomOut(State.ALL_APPS, true);
2923 mState = State.ALL_APPS;
2924 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2925 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2926 cameraZoomOut(State.CUSTOMIZE, true);
2927 mState = State.CUSTOMIZE;
2928 }/* else {
2929 // we're not in spring loaded mode; don't do anything
2930 }*/
2931 }
2932
Joe Onoratob2061212009-11-24 16:13:54 -05002933 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002934 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002935 * - Home from workspace
2936 * - from center screen
2937 * - from other screens
2938 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002939 * - from center screen
2940 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002941 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002942 * - from center screen
2943 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002944 * - Launch app from workspace and quit
2945 * - with back
2946 * - with home
2947 * - Launch app from all apps and quit
2948 * - with back
2949 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002950 * - Go to a screen that's not the default, then all
2951 * apps, and launch and app, and go back
2952 * - with back
2953 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002954 * - On workspace, long press power and go back
2955 * - with back
2956 * - with home
2957 * - On all apps, long press power and go back
2958 * - with back
2959 * - with home
2960 * - On workspace, power off
2961 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002962 * - Launch an app and turn off the screen while in that app
2963 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002964 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002965 * - From all apps
2966 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002967 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2968 * - From all apps
2969 * - From the center workspace
2970 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002971 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002972 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002973 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002974 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002975 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002976 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002977 } else {
2978 mAllAppsGrid.zoom(0.0f, animated);
2979 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002980 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002981 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002982 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002983 }
2984
Joe Onorato7c312c12009-08-13 21:36:53 -07002985 void lockAllApps() {
2986 // TODO
2987 }
2988
2989 void unlockAllApps() {
2990 // TODO
2991 }
2992
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002993 // Show the customization drawer (only exists in x-large configuration)
2994 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002995 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002996 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002997 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002998 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002999 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003000 // Change the state *after* we've called all the transition code
3001 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003002 }
3003
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003004 // Hide the customization drawer (only exists in x-large configuration)
3005 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003006 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003007 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003008 }
3009 }
3010
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003011 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
3012 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3013 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07003014 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003015 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003016
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003017 void onWorkspaceClick(CellLayout layout) {
3018 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003019 }
3020
Michael Jurka4ef207b2010-11-29 17:05:45 -08003021 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003022 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003023 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3024 ImageView button = (ImageView) findViewById(buttonId);
3025 Drawable toolbarIcon = null;
3026 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003027 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003028 // Look for the toolbar icon specified in the activity meta-data
3029 Bundle metaData = packageManager.getActivityInfo(
3030 activityName, PackageManager.GET_META_DATA).metaData;
3031 if (metaData != null) {
3032 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3033 if (iconResId != 0) {
3034 Resources res = packageManager.getResourcesForActivity(activityName);
3035 toolbarIcon = res.getDrawable(iconResId);
3036 }
3037 }
3038 } catch (NameNotFoundException e) {
3039 // Do nothing
3040 }
3041 // If we were unable to find the icon via the meta-data, use a generic one
3042 if (toolbarIcon == null) {
3043 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003044 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003045 } else {
3046 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003047 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003048 }
3049 }
3050
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003051 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003052 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003053 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003054 }
3055
Michael Jurka0423dcf2010-10-05 14:56:18 -07003056 private void updateGlobalSearchIcon() {
3057 if (LauncherApplication.isScreenXLarge()) {
3058 final SearchManager searchManager =
3059 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3060 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003061 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003062 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003063 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003064 } else {
3065 findViewById(R.id.search_button).setVisibility(View.GONE);
3066 }
3067 }
3068 }
3069
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003070 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003071 updateButtonWithDrawable(R.id.search_button, d);
3072 }
3073
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003074 private void updateVoiceSearchIcon() {
3075 if (LauncherApplication.isScreenXLarge()) {
3076 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3077 ComponentName activityName = intent.resolveActivity(getPackageManager());
3078 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003079 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003080 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3081 } else {
3082 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003083 }
3084 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003085 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003086
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003087 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003088 updateButtonWithDrawable(R.id.voice_button, d);
3089 }
3090
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003091 /**
3092 * Sets the app market icon (shown when all apps is visible on x-large screens)
3093 */
3094 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003095 if (LauncherApplication.isScreenXLarge()) {
3096 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3097 // Find the app market activity by resolving an intent.
3098 // (If multiple app markets are installed, it will return the ResolverActivity.)
3099 ComponentName activityName = intent.resolveActivity(getPackageManager());
3100 if (activityName != null) {
3101 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003102 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003103 R.id.market_button, activityName, R.drawable.app_market_generic);
3104 }
3105 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003106 }
3107
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003108 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003109 updateButtonWithDrawable(R.id.market_button, d);
3110 }
3111
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003112 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003113 * Displays the shortcut creation dialog and launches, if necessary, the
3114 * appropriate activity.
3115 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003116 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003117 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3118 DialogInterface.OnShowListener {
3119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003121
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003123 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3126 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003127 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 builder.setInverseBackgroundForced(true);
3130
3131 AlertDialog dialog = builder.create();
3132 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003133 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003134 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136 return dialog;
3137 }
3138
3139 public void onCancel(DialogInterface dialog) {
3140 mWaitingForResult = false;
3141 cleanup();
3142 }
3143
Romain Guycbb89e42009-06-08 15:52:54 -07003144 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003145 }
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003148 try {
3149 dismissDialog(DIALOG_CREATE_SHORTCUT);
3150 } catch (Exception e) {
3151 // An exception is thrown if the dialog is not visible, which is fine
3152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
3154
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003155 /**
3156 * Handle the action clicked in the "Add to home" dialog.
3157 */
3158 public void onClick(DialogInterface dialog, int which) {
3159 Resources res = getResources();
3160 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003161
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003162 switch (which) {
3163 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003164 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003165 break;
3166 }
Romain Guycbb89e42009-06-08 15:52:54 -07003167
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003168 case AddAdapter.ITEM_APPWIDGET: {
3169 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003170
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003171 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3172 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003173 // start the pick activity
3174 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3175 break;
3176 }
Romain Guycbb89e42009-06-08 15:52:54 -07003177
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003178 case AddAdapter.ITEM_LIVE_FOLDER: {
3179 // Insert extra item to handle inserting folder
3180 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003181
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003182 ArrayList<String> shortcutNames = new ArrayList<String>();
3183 shortcutNames.add(res.getString(R.string.group_folder));
3184 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003185
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003186 ArrayList<ShortcutIconResource> shortcutIcons =
3187 new ArrayList<ShortcutIconResource>();
3188 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3189 R.drawable.ic_launcher_folder));
3190 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3191
3192 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3193 pickIntent.putExtra(Intent.EXTRA_INTENT,
3194 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3195 pickIntent.putExtra(Intent.EXTRA_TITLE,
3196 getText(R.string.title_select_live_folder));
3197 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003198
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003199 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3200 break;
3201 }
3202
3203 case AddAdapter.ITEM_WALLPAPER: {
3204 startWallpaper();
3205 break;
3206 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003207 }
3208 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003209
3210 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003211 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 }
3214
3215 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003216 * Receives notifications when applications are added/removed.
3217 */
3218 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3219 @Override
3220 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003221 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003222 String reason = intent.getStringExtra("reason");
3223 if (!"homekey".equals(reason)) {
3224 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003225 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003226 animate = false;
3227 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003228 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003229 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003230 }
3231 }
3232
3233 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003234 * Receives notifications whenever the appwidgets are reset.
3235 */
3236 private class AppWidgetResetObserver extends ContentObserver {
3237 public AppWidgetResetObserver() {
3238 super(new Handler());
3239 }
3240
3241 @Override
3242 public void onChange(boolean selfChange) {
3243 onAppWidgetReset();
3244 }
3245 }
3246
3247 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003248 * If the activity is currently paused, signal that we need to re-run the loader
3249 * in onResume.
3250 *
3251 * This needs to be called from incoming places where resources might have been loaded
3252 * while we are paused. That is becaues the Configuration might be wrong
3253 * when we're not running, and if it comes back to what it was when we
3254 * were paused, we are not restarted.
3255 *
3256 * Implementation of the method from LauncherModel.Callbacks.
3257 *
3258 * @return true if we are currently paused. The caller might be able to
3259 * skip some work in that case since we will come back again.
3260 */
3261 public boolean setLoadOnResume() {
3262 if (mPaused) {
3263 Log.i(TAG, "setLoadOnResume");
3264 mOnResumeNeedsLoad = true;
3265 return true;
3266 } else {
3267 return false;
3268 }
3269 }
3270
3271 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003275 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003276 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003277 } else {
3278 return SCREEN_COUNT / 2;
3279 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003281
Winson Chunga12a2502010-12-20 14:41:35 -08003282 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003283 mAllAppsPagedView = view;
3284 }
3285
Joe Onorato9c1289c2009-08-17 11:03:03 -04003286 /**
3287 * Refreshes the shortcuts shown on the workspace.
3288 *
3289 * Implementation of the method from LauncherModel.Callbacks.
3290 */
3291 public void startBinding() {
3292 final Workspace workspace = mWorkspace;
3293 int count = workspace.getChildCount();
3294 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003295 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3297 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003298
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 if (DEBUG_USER_INTERFACE) {
3300 android.widget.Button finishButton = new android.widget.Button(this);
3301 finishButton.setText("Finish");
3302 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3303
3304 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3305 public void onClick(View v) {
3306 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003307 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003308 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003309 }
3310 }
3311
3312 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 * Bind the items start-end from the list.
3314 *
3315 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003316 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3318
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003319 setLoadOnResume();
3320
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321 final Workspace workspace = mWorkspace;
3322
3323 for (int i=start; i<end; i++) {
3324 final ItemInfo item = shortcuts.get(i);
3325 mDesktopItems.add(item);
3326 switch (item.itemType) {
3327 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3328 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003329 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3331 false);
3332 break;
3333 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3334 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003335 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003336 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003337 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3338 false);
3339 break;
3340 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3341 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3342 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003343 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344 (LiveFolderInfo) item);
3345 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3346 false);
3347 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003349 }
3350
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003352 }
3353
Joe Onorato9c1289c2009-08-17 11:03:03 -04003354 /**
3355 * Implementation of the method from LauncherModel.Callbacks.
3356 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003357 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003358 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003359 sFolders.clear();
3360 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003361 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362
3363 /**
3364 * Add the views for a widget to the workspace.
3365 *
3366 * Implementation of the method from LauncherModel.Callbacks.
3367 */
3368 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003369 setLoadOnResume();
3370
Daniel Sandler843e8602010-06-07 14:59:01 -04003371 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3372 if (DEBUG_WIDGETS) {
3373 Log.d(TAG, "bindAppWidget: " + item);
3374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 final Workspace workspace = mWorkspace;
3376
3377 final int appWidgetId = item.appWidgetId;
3378 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003379 if (DEBUG_WIDGETS) {
3380 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3381 }
3382
Joe Onorato9c1289c2009-08-17 11:03:03 -04003383 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3384
Joe Onorato9c1289c2009-08-17 11:03:03 -04003385 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3386 item.hostView.setTag(item);
3387
3388 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3389 item.cellY, item.spanX, item.spanY, false);
3390
Adam Cohended9f8d2010-11-03 13:25:16 -07003391 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3392
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 workspace.requestLayout();
3394
3395 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003396
3397 if (DEBUG_WIDGETS) {
3398 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3399 + (SystemClock.uptimeMillis()-start) + "ms");
3400 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 }
3402
3403 /**
3404 * Callback saying that there aren't any more items to bind.
3405 *
3406 * Implementation of the method from LauncherModel.Callbacks.
3407 */
3408 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003409 setLoadOnResume();
3410
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 if (mSavedState != null) {
3412 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003413 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 }
3415
3416 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3417 if (userFolders != null) {
3418 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003419 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003420 if (info != null) {
3421 openFolder(info);
3422 }
3423 }
3424 final Folder openFolder = mWorkspace.getOpenFolder();
3425 if (openFolder != null) {
3426 openFolder.requestFocus();
3427 }
3428 }
3429
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 mSavedState = null;
3431 }
3432
3433 if (mSavedInstanceState != null) {
3434 super.onRestoreInstanceState(mSavedInstanceState);
3435 mSavedInstanceState = null;
3436 }
3437
Joe Onorato9c1289c2009-08-17 11:03:03 -04003438 mWorkspaceLoading = false;
3439 }
3440
3441 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003442 * Updates the icons on the launcher that are affected by changes to the package list
3443 * on the device.
3444 */
3445 private void updateIconsAffectedByPackageManagerChanges() {
3446 updateAppMarketIcon();
3447 updateGlobalSearchIcon();
3448 updateVoiceSearchIcon();
3449 }
3450
3451 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 * Add the icons for all apps.
3453 *
3454 * Implementation of the method from LauncherModel.Callbacks.
3455 */
3456 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003457 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003458 if (mCustomizePagedView != null) {
3459 mCustomizePagedView.setApps(apps);
3460 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003461 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003462 }
3463
3464 /**
3465 * A package was installed.
3466 *
3467 * Implementation of the method from LauncherModel.Callbacks.
3468 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003469 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003470 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003471 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003472 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003473 if (mCustomizePagedView != null) {
3474 mCustomizePagedView.addApps(apps);
3475 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003476 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003477 }
3478
3479 /**
3480 * A package was updated.
3481 *
3482 * Implementation of the method from LauncherModel.Callbacks.
3483 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003484 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003485 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003487 mWorkspace.updateShortcuts(apps);
3488 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003489 if (mCustomizePagedView != null) {
3490 mCustomizePagedView.updateApps(apps);
3491 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003492 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 }
3494
3495 /**
3496 * A package was uninstalled.
3497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 */
Joe Onorato36115782010-06-17 13:28:48 -04003500 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003502 if (permanent) {
3503 mWorkspace.removeItems(apps);
3504 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003505 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003506 if (mCustomizePagedView != null) {
3507 mCustomizePagedView.removeApps(apps);
3508 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003509 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
Joe Onoratobe386092009-11-17 17:32:16 -08003511
3512 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003513 * A number of packages were updated.
3514 */
3515 public void bindPackagesUpdated() {
3516 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003517 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003518 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003519 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003520 }
3521
3522 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003523 * Prints out out state for debugging.
3524 */
3525 public void dumpState() {
3526 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003527 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003528 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3529 Log.d(TAG, "mRestoring=" + mRestoring);
3530 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3531 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3532 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003533 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003534 mModel.dumpState();
3535 mAllAppsGrid.dumpState();
3536 Log.d(TAG, "END launcher2 dump state");
3537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003538}