blob: 83f13aa11ec69a7861a7084987585dc021095fa7 [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
tedbo2b0857a2010-12-22 18:36:24 -0800386 if (mCustomizePagedView != null) {
387 mCustomizePagedView.update();
388 }
Winson Chunga12a2502010-12-20 14:41:35 -0800389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 if (PROFILE_STARTUP) {
391 android.os.Debug.stopMethodTracing();
392 }
393
394 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400395 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397
398 // For handling default keys
399 mDefaultKeySsb = new SpannableStringBuilder();
400 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800401
402 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
403 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800404
405 // If we have a saved version of these external icons, we load them up immediately
406 if (LauncherApplication.isScreenXLarge()) {
407 if (sGlobalSearchIcon != null) {
408 updateGlobalSearchIcon(sGlobalSearchIcon);
409 }
410 if (sVoiceSearchIcon != null) {
411 updateVoiceSearchIcon(sVoiceSearchIcon);
412 }
413 if (sAppMarketIcon != null) {
414 updateAppMarketIcon(sAppMarketIcon);
415 }
416 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800417 }
Romain Guycbb89e42009-06-08 15:52:54 -0700418
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420 if (sLocaleConfiguration == null) {
421 new AsyncTask<Void, Void, LocaleConfiguration>() {
422 @Override
423 protected LocaleConfiguration doInBackground(Void... unused) {
424 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
425 readConfiguration(Launcher.this, localeConfiguration);
426 return localeConfiguration;
427 }
428
429 @Override
430 protected void onPostExecute(LocaleConfiguration result) {
431 sLocaleConfiguration = result;
432 checkForLocaleChange(); // recursive, but now with a locale configuration
433 }
434 }.execute();
435 return;
436 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 final Configuration configuration = getResources().getConfiguration();
439
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700440 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 final String locale = configuration.locale.toString();
442
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700443 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 final int mcc = configuration.mcc;
445
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 final int mnc = configuration.mnc;
448
Romain Guy84f296c2009-11-04 15:00:44 -0800449 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450
Romain Guy84f296c2009-11-04 15:00:44 -0800451 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700452 sLocaleConfiguration.locale = locale;
453 sLocaleConfiguration.mcc = mcc;
454 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700455
Joe Onorato0589f0f2010-02-08 13:44:00 -0800456 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400457 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700458
459 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
460 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700461 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 public void run() {
463 writeConfiguration(Launcher.this, localeConfiguration);
464 }
465 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700466 }
467 }
468
469 private static class LocaleConfiguration {
470 public String locale;
471 public int mcc = -1;
472 public int mnc = -1;
473 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700474
Romain Guy98d01652009-06-30 16:21:04 -0700475 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
476 DataInputStream in = null;
477 try {
478 in = new DataInputStream(context.openFileInput(PREFERENCES));
479 configuration.locale = in.readUTF();
480 configuration.mcc = in.readInt();
481 configuration.mnc = in.readInt();
482 } catch (FileNotFoundException e) {
483 // Ignore
484 } catch (IOException e) {
485 // Ignore
486 } finally {
487 if (in != null) {
488 try {
489 in.close();
490 } catch (IOException e) {
491 // Ignore
492 }
493 }
494 }
495 }
496
497 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
498 DataOutputStream out = null;
499 try {
500 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
501 out.writeUTF(configuration.locale);
502 out.writeInt(configuration.mcc);
503 out.writeInt(configuration.mnc);
504 out.flush();
505 } catch (FileNotFoundException e) {
506 // Ignore
507 } catch (IOException e) {
508 //noinspection ResultOfMethodCallIgnored
509 context.getFileStreamPath(PREFERENCES).delete();
510 } finally {
511 if (out != null) {
512 try {
513 out.close();
514 } catch (IOException e) {
515 // Ignore
516 }
517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 }
519 }
520
521 static int getScreen() {
522 synchronized (sLock) {
523 return sScreen;
524 }
525 }
526
527 static void setScreen(int screen) {
528 synchronized (sLock) {
529 sScreen = screen;
530 }
531 }
532
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700534 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535
536 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700537 // TODO: Put back when we decide about scrolling the wallpaper
538 // boolean isPortrait = display.getWidth() < display.getHeight();
539 // final int width = isPortrait ? display.getWidth() : display.getHeight();
540 // final int height = isPortrait ? display.getHeight() : display.getWidth();
541 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
542 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 // Note: This doesn't do all the client-id magic that BrowserProvider does
546 // in Browser. (http://b/2425179)
547 private Uri getDefaultBrowserUri() {
548 String url = getString(R.string.default_browser_url);
549 if (url.indexOf("{CID}") != -1) {
550 url = url.replace("{CID}", "android-google");
551 }
552 return Uri.parse(url);
553 }
554
555 // Load the Intent templates from arrays.xml to populate the hotseats. For
556 // each Intent, if it resolves to a single app, use that as the launch
557 // intent & use that app's label as the contentDescription. Otherwise,
558 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400559 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (mHotseatConfig == null) {
561 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
562 if (mHotseatConfig.length > 0) {
563 mHotseats = new Intent[mHotseatConfig.length];
564 mHotseatLabels = new CharSequence[mHotseatConfig.length];
565 mHotseatIcons = new Drawable[mHotseatConfig.length];
566 } else {
567 mHotseats = null;
568 mHotseatIcons = null;
569 mHotseatLabels = null;
570 }
571
572 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
573 for (int i=0; i<mHotseatConfig.length; i++) {
574 // load icon for this slot; currently unrelated to the actual activity
575 try {
576 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
577 } catch (ArrayIndexOutOfBoundsException ex) {
578 Log.w(TAG, "Missing hotseat_icons array item #" + i);
579 mHotseatIcons[i] = null;
580 }
581 }
582 hotseatIconDrawables.recycle();
583 }
584
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400585 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 for (int i=0; i<mHotseatConfig.length; i++) {
587 Intent intent = null;
588 if (mHotseatConfig[i].equals("*BROWSER*")) {
589 // magic value meaning "launch user's default web browser"
590 // replace it with a generic web request so we can see if there is indeed a default
591 String defaultUri = getString(R.string.default_browser_url);
592 intent = new Intent(
593 Intent.ACTION_VIEW,
594 ((defaultUri != null)
595 ? Uri.parse(defaultUri)
596 : getDefaultBrowserUri())
597 ).addCategory(Intent.CATEGORY_BROWSABLE);
598 // note: if the user launches this without a default set, she
599 // will always be taken to the default URL above; this is
600 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700601 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 // URL is unavoidably sent to the chosen app.
603 } else {
604 try {
605 intent = Intent.parseUri(mHotseatConfig[i], 0);
606 } catch (java.net.URISyntaxException ex) {
607 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
608 // bogus; leave intent=null
609 }
610 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700611
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400612 if (intent == null) {
613 mHotseats[i] = null;
614 mHotseatLabels[i] = getText(R.string.activity_not_found);
615 continue;
616 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400617
618 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700619 Log.d(TAG, "loadHotseats: hotseat " + i
620 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400621 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400622 + "]");
623 }
624
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400625 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
626 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700627 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 Log.d(TAG, "Best match for intent: " + bestMatch);
629 Log.d(TAG, "All matches: ");
630 for (ResolveInfo ri : allMatches) {
631 Log.d(TAG, " --> " + ri);
632 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400633 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400634 // did this resolve to a single app, or the resolver?
635 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700636 // can't find any activity to handle this. let's leave the
637 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400638 // to launch.
639 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 // set accessibility text to "Not installed"
642 mHotseatLabels[i] = getText(R.string.activity_not_found);
643 } else {
644 boolean found = false;
645 for (ResolveInfo ri : allMatches) {
646 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
647 && bestMatch.activityInfo.applicationInfo.packageName
648 .equals(ri.activityInfo.applicationInfo.packageName)) {
649 found = true;
650 break;
651 }
652 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700653
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400654 if (!found) {
655 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
656 // the bestMatch is probably the ResolveActivity, meaning the
657 // user has not yet selected a default
658 // so: we'll keep the original intent for now
659 mHotseats[i] = intent;
660
661 // set the accessibility text to "Select shortcut"
662 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
663 } else {
664 // we have an app!
665 // now reconstruct the intent to launch it through the front
666 // door
667 ComponentName com = new ComponentName(
668 bestMatch.activityInfo.applicationInfo.packageName,
669 bestMatch.activityInfo.name);
670 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
671
672 // load the app label for accessibility
673 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
674 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400675 }
676
677 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700678 Log.d(TAG, "loadHotseats: hotseat " + i
679 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400680 + ((mHotseats[i] == null)
681 ? "null"
682 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400683 + "] label=[" + mHotseatLabels[i]
684 + "]"
685 );
686 }
687 }
688 }
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 @Override
691 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700692 mWaitingForResult = false;
693
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 // The pattern used here is that a user PICKs a specific application,
695 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700696
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
698 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700699
Michael Jurka0280c3b2010-09-17 15:00:07 -0700700 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 switch (requestCode) {
702 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700703 completeAddApplication(
704 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 break;
706 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800707 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 break;
709 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 break;
712 case REQUEST_PICK_LIVE_FOLDER:
713 addLiveFolder(data);
714 break;
715 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700716 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700718 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700719 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700721 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700723 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700725 case REQUEST_PICK_WALLPAPER:
726 // We just wanted the activity result here so we can clear mWaitingForResult
727 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728 }
Romain Guy18042c82009-11-06 11:44:55 -0800729 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
730 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
731 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 // Clean up the appWidgetId if we canceled
733 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
734 if (appWidgetId != -1) {
735 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
739
740 @Override
741 protected void onResume() {
742 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800743 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700744 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400745 mWorkspaceLoading = true;
746 mModel.startLoader(this, true);
747 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700748 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700750 // When we resume Launcher, a different Activity might be responsible for the app
751 // market intent, so refresh the icon
752 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800753 }
754
755 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700756 protected void onPause() {
757 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700758 // Some launcher layouts don't have a previous and next view
759 if (mPreviousView != null) {
760 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700761 }
762 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700763 dismissPreview(mNextView);
764 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700765 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800766 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700767 }
Romain Guycbb89e42009-06-08 15:52:54 -0700768
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700769 @Override
770 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400771 // Flag the loader to stop early before switching
772 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800773 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800774 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700775 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700776
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800777 // We can't hide the IME if it was forced open. So don't bother
778 /*
779 @Override
780 public void onWindowFocusChanged(boolean hasFocus) {
781 super.onWindowFocusChanged(hasFocus);
782
783 if (hasFocus) {
784 final InputMethodManager inputManager = (InputMethodManager)
785 getSystemService(Context.INPUT_METHOD_SERVICE);
786 WindowManager.LayoutParams lp = getWindow().getAttributes();
787 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
788 android.os.Handler()) {
789 protected void onReceiveResult(int resultCode, Bundle resultData) {
790 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
791 }
792 });
793 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
794 }
795 }
796 */
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 private boolean acceptFilter() {
799 final InputMethodManager inputManager = (InputMethodManager)
800 getSystemService(Context.INPUT_METHOD_SERVICE);
801 return !inputManager.isFullscreenMode();
802 }
803
804 @Override
805 public boolean onKeyDown(int keyCode, KeyEvent event) {
806 boolean handled = super.onKeyDown(keyCode, event);
807 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
808 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
809 keyCode, event);
810 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700811 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700812 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700813 // showSearchDialog()
814 // If there are multiple keystrokes before the search dialog takes focus,
815 // onSearchRequested() will be called for every keystroke,
816 // but it is idempotent, so it's fine.
817 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 }
819 }
820
Joe Onorato8a9625e2010-01-28 15:55:35 -0800821 // Eat the long press event so the keyboard doesn't come up.
822 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
823 return true;
824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 return handled;
827 }
828
Karl Rosaen138a0412009-04-23 19:00:21 -0700829 private String getTypedText() {
830 return mDefaultKeySsb.toString();
831 }
832
833 private void clearTypedText() {
834 mDefaultKeySsb.clear();
835 mDefaultKeySsb.clearSpans();
836 Selection.setSelection(mDefaultKeySsb, 0);
837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700840 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
841 * State
842 */
843 private static State intToState(int stateOrdinal) {
844 State state = State.WORKSPACE;
845 final State[] stateValues = State.values();
846 for (int i = 0; i < stateValues.length; i++) {
847 if (stateValues[i].ordinal() == stateOrdinal) {
848 state = stateValues[i];
849 break;
850 }
851 }
852 return state;
853 }
854
855 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 * Restores the previous state, if it exists.
857 *
858 * @param savedState The previous state.
859 */
860 private void restoreState(Bundle savedState) {
861 if (savedState == null) {
862 return;
863 }
864
Michael Jurka883f55b2010-10-21 15:47:14 -0700865 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
866
867 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800868 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700869 } else if (state == State.CUSTOMIZE) {
870 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800871 }
872
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
874 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700875 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800876 }
877
878 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700881 mAddScreen = addScreen;
882 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
883 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 mRestoring = true;
885 }
886
887 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
888 if (renameFolder) {
889 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700890 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 mRestoring = true;
892 }
Winson Chunga12a2502010-12-20 14:41:35 -0800893
894 // Restore the current AllApps drawer tab
895 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
896 String curTab = savedState.getString("allapps_currentTab");
897 if (curTab != null) {
898 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
899 tabhost.setCurrentTabByTag(curTab);
900 }
901 int curPage = savedState.getInt("allapps_currentPage", -1);
902 if (curPage > -1) {
903 mAllAppsPagedView.setRestorePage(curPage);
904 }
905 }
906
907 // Restore the current customization drawer tab
908 if (mHomeCustomizationDrawer != null) {
909 String curTab = savedState.getString("customize_currentTab");
910 if (curTab != null) {
911 // We set this directly so that there is no delay before the tab is set
912 mCustomizePagedView.setCustomizationFilter(getCustomizeFilterForTabTag(curTab));
913 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
914 }
915
916 // Note: currently we do not restore the page for the customization tray because unlike
917 // AllApps, the page content can change drastically
918 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 }
920
921 /**
922 * Finds all the views we need and configure them properly.
923 */
924 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700925 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400926
Romain Guyb1b69f52009-08-10 15:10:15 -0700927 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400928 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929
Joe Onorato7c312c12009-08-13 21:36:53 -0700930 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700931 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700932 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800933 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700934 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700935 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700936
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700937 if (LauncherApplication.isScreenXLarge()) {
938 // They need to be INVISIBLE initially so that they will be measured in the layout.
939 // Otherwise the animations are messed up when we show them for the first time.
940 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
941 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
942 }
943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700945
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500947 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700948
Joe Onorato7c312c12009-08-13 21:36:53 -0700949 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
950 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700952 View handleView = findViewById(R.id.all_apps_button);
953 if (handleView != null && handleView instanceof HandleView) {
954 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700955 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700956 mHandleView.setLauncher(this);
957 mHandleView.setOnClickListener(this);
958 mHandleView.setOnLongClickListener(this);
959 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800960
Winson Chung80baf5a2010-08-09 16:03:15 -0700961 if (mCustomizePagedView != null) {
962 mCustomizePagedView.setLauncher(this);
963 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700964 } else {
965 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
966 hotseatLeft.setContentDescription(mHotseatLabels[0]);
967 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
968 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
969 hotseatRight.setContentDescription(mHotseatLabels[1]);
970 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400971
Michael Jurkaaf442092010-06-10 17:01:57 -0700972 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
973 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800974
Michael Jurkaaf442092010-06-10 17:01:57 -0700975 Drawable previous = mPreviousView.getDrawable();
976 Drawable next = mNextView.getDrawable();
977 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978
Michael Jurkaaf442092010-06-10 17:01:57 -0700979 mPreviousView.setHapticFeedbackEnabled(false);
980 mPreviousView.setOnLongClickListener(this);
981 mNextView.setHapticFeedbackEnabled(false);
982 mNextView.setOnLongClickListener(this);
983 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800984
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400986 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988
989 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400990 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700991 int deleteZoneHandleId;
Michael Jurkab8e14472010-12-20 16:06:10 -0800992
993 final View allAppsButton = findViewById(R.id.all_apps_button);
994 final View divider = findViewById(R.id.divider);
995 final View configureButton = findViewById(R.id.configure_button);
996
Michael Jurka54f7ac32010-08-02 13:56:46 -0700997 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800998 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700999 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -08001000 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -07001001 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001002 dragController.addDragListener(deleteZone);
1003
Adam Cohencdc30d52010-12-01 15:09:47 -08001004 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
1005 if (allAppsDeleteZone != null) {
1006 allAppsDeleteZone.setLauncher(this);
1007 allAppsDeleteZone.setDragController(dragController);
1008 allAppsDeleteZone.setDragAndDropEnabled(false);
1009 dragController.addDragListener(allAppsDeleteZone);
1010 dragController.addDropTarget(allAppsDeleteZone);
1011 }
1012
1013 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
1014 findViewById(R.id.all_apps_info_target);
1015 if (allAppsInfoTarget != null) {
1016 allAppsInfoTarget.setLauncher(this);
1017 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -08001018 allAppsInfoTarget.setDragAndDropEnabled(false);
1019 View marketButton = findViewById(R.id.market_button);
1020 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -08001021 allAppsInfoTarget.setOverlappingView(marketButton);
Adam Cohencdc30d52010-12-01 15:09:47 -08001022 }
1023 }
1024
Joe Onorato00acb122009-08-04 16:04:30 -04001025 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -04001026 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -08001027 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001028
Joe Onorato00acb122009-08-04 16:04:30 -04001029 // The order here is bottom to top.
1030 dragController.addDropTarget(workspace);
1031 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001032 if (allAppsInfoTarget != null) {
1033 dragController.addDropTarget(allAppsInfoTarget);
1034 }
1035 if (allAppsDeleteZone != null) {
1036 dragController.addDropTarget(allAppsDeleteZone);
1037 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 }
1039
Romain Guy8a73c512009-11-09 19:19:59 -08001040 @SuppressWarnings({"UnusedDeclaration"})
1041 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001042 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001043 mWorkspace.scrollLeft();
1044 }
Romain Guy8a73c512009-11-09 19:19:59 -08001045 }
1046
1047 @SuppressWarnings({"UnusedDeclaration"})
1048 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001049 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001050 mWorkspace.scrollRight();
1051 }
Romain Guy8a73c512009-11-09 19:19:59 -08001052 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001053
1054 @SuppressWarnings({"UnusedDeclaration"})
1055 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001056 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001057
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001058 int index = -1;
1059 if (v.getId() == R.id.hotseat_left) {
1060 index = 0;
1061 } else if (v.getId() == R.id.hotseat_right) {
1062 index = 1;
1063 }
1064
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001065 // reload these every tap; you never know when they might change
1066 loadHotseats();
1067 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1068 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001069 startActivitySafely(
1070 mHotseats[index],
1071 "hotseat"
1072 );
1073 }
1074 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001075
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 /**
1077 * Creates a view representing a shortcut.
1078 *
1079 * @param info The data structure describing the shortcut.
1080 *
1081 * @return A View inflated from R.layout.application.
1082 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001083 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001085 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 }
1087
1088 /**
1089 * Creates a view representing a shortcut inflated from the specified resource.
1090 *
1091 * @param layoutResId The id of the XML layout used to create the shortcut.
1092 * @param parent The group the shortcut belongs to.
1093 * @param info The data structure describing the shortcut.
1094 *
1095 * @return A View inflated from layoutResId.
1096 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001097 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001098 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1099 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 return favorite;
1102 }
1103
1104 /**
1105 * Add an application shortcut to the workspace.
1106 *
1107 * @param data The intent describing the application.
1108 * @param cellInfo The position on screen where to create the shortcut.
1109 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 void completeAddApplication(Context context, Intent data, int screen,
1111 int intersectCellX, int intersectCellY) {
1112 final int[] cellXY = mTmpAddItemCellCoordinates;
1113 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1114
1115 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1116 showOutOfSpaceMessage();
1117 return;
1118 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119
Joe Onorato0589f0f2010-02-08 13:44:00 -08001120 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1121 data, context);
1122
Romain Guy73b979d2009-06-09 12:57:21 -07001123 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1128 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001129 } else {
1130 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 }
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 /**
1135 * Add a shortcut to the workspace.
1136 *
1137 * @param data The intent describing the shortcut.
1138 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 private void completeAddShortcut(Intent data, int screen,
1141 int intersectCellX, int intersectCellY) {
1142 final int[] cellXY = mTmpAddItemCellCoordinates;
1143 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001144
Michael Jurkad3ef3062010-11-23 16:23:58 -08001145 int[] touchXY = null;
1146 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1147 touchXY = mAddDropPosition;
1148 }
1149 boolean foundCellSpan = false;
1150 if (touchXY != null) {
1151 // when dragging and dropping, just find the closest free spot
1152 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1153 int[] result = screenLayout.findNearestVacantArea(
1154 touchXY[0], touchXY[1], 1, 1, cellXY);
1155 foundCellSpan = (result != null);
1156 } else {
1157 foundCellSpan = layout.findCellForSpanThatIntersects(
1158 cellXY, 1, 1, intersectCellX, intersectCellY);
1159 }
1160
1161 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 showOutOfSpaceMessage();
1163 return;
1164 }
1165
1166 final ShortcutInfo info = mModel.addShortcut(
1167 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168
1169 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001171 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 }
1173 }
1174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001178 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001182 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001183
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001184 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1186 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001189 // We have saved the position to which the widget was dragged-- this really only matters
1190 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001192
1193 // For now, we don't save the coordinate where we dropped the icon because we're not
1194 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1195 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001196 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001197 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001198 touchXY = mAddDropPosition;
1199 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 boolean foundCellSpan = false;
1201 if (touchXY != null) {
1202 // when dragging and dropping, just find the closest free spot
1203 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1204 int[] result = screenLayout.findNearestVacantArea(
1205 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001206 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001208 // if we long pressed on an empty cell to bring up a menu,
1209 // make sure we intersect the empty cell
1210 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1211 // findCellForSpanThatIntersects will just ignore them
1212 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1213 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001214 }
1215
Michael Jurka0280c3b2010-09-17 15:00:07 -07001216 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001217 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001219 return;
1220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 // Build Launcher-specific widget info and save to database
1223 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 launcherInfo.spanX = spanXY[0];
1225 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 LauncherModel.addItemToDatabase(this, launcherInfo,
1228 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001229 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
1231 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001237 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001242
1243 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1248 @Override
1249 public void onReceive(Context context, Intent intent) {
1250 final String action = intent.getAction();
1251 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1252 mUserPresent = false;
1253 updateRunning();
1254 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1255 mUserPresent = true;
1256 updateRunning();
1257 }
1258 }
1259 };
1260
1261 @Override
1262 public void onAttachedToWindow() {
1263 super.onAttachedToWindow();
1264
1265 // Listen for broadcasts related to user-presence
1266 final IntentFilter filter = new IntentFilter();
1267 filter.addAction(Intent.ACTION_SCREEN_OFF);
1268 filter.addAction(Intent.ACTION_USER_PRESENT);
1269 registerReceiver(mReceiver, filter);
1270
Adam Cohend113e0c2010-11-11 10:48:05 -08001271 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001272 mVisible = true;
1273 }
1274
1275 @Override
1276 public void onDetachedFromWindow() {
1277 super.onDetachedFromWindow();
1278 mVisible = false;
1279
Adam Cohend113e0c2010-11-11 10:48:05 -08001280 if (mAttached) {
1281 unregisterReceiver(mReceiver);
1282 mAttached = false;
1283 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001284 updateRunning();
1285 }
1286
1287 public void onWindowVisibilityChanged(int visibility) {
1288 mVisible = visibility == View.VISIBLE;
1289 updateRunning();
1290 }
1291
1292 private void sendAdvanceMessage(long delay) {
1293 mHandler.removeMessages(ADVANCE_MSG);
1294 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1295 mHandler.sendMessageDelayed(msg, delay);
1296 mAutoAdvanceSentTime = System.currentTimeMillis();
1297 }
1298
1299 private void updateRunning() {
1300 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1301 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1302 mAutoAdvanceRunning = autoAdvanceRunning;
1303 if (autoAdvanceRunning) {
1304 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1305 sendAdvanceMessage(delay);
1306 } else {
1307 if (!mWidgetsToAdvance.isEmpty()) {
1308 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1309 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1310 }
1311 mHandler.removeMessages(ADVANCE_MSG);
1312 }
1313 }
1314 }
1315
1316 private final Handler mHandler = new Handler() {
1317 @Override
1318 public void handleMessage(Message msg) {
1319 if (msg.what == ADVANCE_MSG) {
1320 int i = 0;
1321 for (View key: mWidgetsToAdvance.keySet()) {
1322 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1323 final int delay = mAdvanceStagger * i;
1324 if (v instanceof Advanceable) {
1325 postDelayed(new Runnable() {
1326 public void run() {
1327 ((Advanceable) v).advance();
1328 }
1329 }, delay);
1330 }
1331 i++;
1332 }
1333 sendAdvanceMessage(mAdvanceInterval);
1334 }
1335 }
1336 };
1337
1338 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1339 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1340 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1341 if (v instanceof Advanceable) {
1342 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1343 ((Advanceable) v).willBeAdvancedByHost();
1344 updateRunning();
1345 }
1346 }
1347
1348 void removeWidgetToAutoAdvance(View hostView) {
1349 if (mWidgetsToAdvance.containsKey(hostView)) {
1350 mWidgetsToAdvance.remove(hostView);
1351 updateRunning();
1352 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 }
1354
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1356 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001357 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001358 launcherInfo.hostView = null;
1359 }
1360
Adam Cohended9f8d2010-11-03 13:25:16 -07001361 void showOutOfSpaceMessage() {
1362 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1363 }
1364
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001365 public LauncherAppWidgetHost getAppWidgetHost() {
1366 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
Winson Chunga9abd0e2010-10-27 17:18:37 -07001369 public LauncherModel getModel() {
1370 return mModel;
1371 }
1372
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001373 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001374 getWindow().closeAllPanels();
1375
1376 try {
1377 dismissDialog(DIALOG_CREATE_SHORTCUT);
1378 // Unlock the workspace if the dialog was showing
1379 } catch (Exception e) {
1380 // An exception is thrown if the dialog is not visible, which is fine
1381 }
1382
1383 try {
1384 dismissDialog(DIALOG_RENAME_FOLDER);
1385 // Unlock the workspace if the dialog was showing
1386 } catch (Exception e) {
1387 // An exception is thrown if the dialog is not visible, which is fine
1388 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001389
1390 // Whatever we were doing is hereby canceled.
1391 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001392 }
1393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 @Override
1395 protected void onNewIntent(Intent intent) {
1396 super.onNewIntent(intent);
1397
1398 // Close the menu
1399 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001400 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001401 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001402
Joe Onorato14f122b2009-11-19 14:06:36 -08001403 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1404 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001405
Michael Jurka4cb37242010-08-09 21:05:32 -07001406 // in all these cases, only animate if we're already on home
1407 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001408 mWorkspace.unshrink(alreadyOnHome);
1409 }
1410 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001411 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001412 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1413 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001414 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001415 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001416
Joe Onorato3a8820b2009-11-10 15:06:42 -08001417 final View v = getWindow().peekDecorView();
1418 if (v != null && v.getWindowToken() != null) {
1419 InputMethodManager imm = (InputMethodManager)getSystemService(
1420 INPUT_METHOD_SERVICE);
1421 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
1423 }
1424 }
1425
1426 @Override
1427 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1428 // Do not call super here
1429 mSavedInstanceState = savedInstanceState;
1430 }
1431
1432 @Override
1433 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001434 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435
1436 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1437 if (folders.size() > 0) {
1438 final int count = folders.size();
1439 long[] ids = new long[count];
1440 for (int i = 0; i < count; i++) {
1441 final FolderInfo info = folders.get(i).getInfo();
1442 ids[i] = info.id;
1443 }
1444 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1445 } else {
1446 super.onSaveInstanceState(outState);
1447 }
1448
Michael Jurka883f55b2010-10-21 15:47:14 -07001449 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
Michael Jurka0280c3b2010-09-17 15:00:07 -07001451 if (mAddScreen > -1 && mWaitingForResult) {
1452 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1453 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1454 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
1456
1457 if (mFolderInfo != null && mWaitingForResult) {
1458 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1459 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1460 }
Michael Jurka946ad472010-07-09 18:05:18 -07001461
Winson Chung43b119d2010-12-06 16:45:05 -08001462 // Save the current AllApps drawer tab
1463 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1464 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1465 String currentTabTag = tabhost.getCurrentTabTag();
1466 if (currentTabTag != null) {
1467 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001468 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001469 }
1470 }
1471
1472 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001473 if (mHomeCustomizationDrawer != null) {
1474 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1475 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001476 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001477 }
1478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 @Override
1482 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001486 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001488 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 TextKeyListener.getInstance().release();
1492
Joe Onorato9c1289c2009-08-17 11:03:03 -04001493 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
Joe Onorato9c1289c2009-08-17 11:03:03 -04001495 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001496
1497 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001498
Patrick Dubroyab962b72010-07-26 12:10:10 -07001499 // Some launcher layouts don't have a previous and next view
1500 if (mPreviousView != null) {
1501 dismissPreview(mPreviousView);
1502 }
1503 if (mNextView != null) {
1504 dismissPreview(mNextView);
1505 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001506
1507 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
1510 @Override
1511 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001512 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 super.startActivityForResult(intent, requestCode);
1514 }
1515
1516 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001517 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001519
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001520 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Karl Rosaen138a0412009-04-23 19:00:21 -07001522 if (initialQuery == null) {
1523 // Use any text typed in the launcher as the initial query
1524 initialQuery = getTypedText();
1525 clearTypedText();
1526 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 if (appSearchData == null) {
1528 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001529 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 final SearchManager searchManager =
1533 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001535 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537
1538 @Override
1539 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001540 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001541 return false;
1542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1547 .setIcon(android.R.drawable.ic_menu_add)
1548 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001549 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1550 .setIcon(android.R.drawable.ic_menu_manage)
1551 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001552 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 .setIcon(android.R.drawable.ic_menu_gallery)
1554 .setAlphabeticShortcut('W');
1555 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1556 .setIcon(android.R.drawable.ic_search_category_default)
1557 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1558 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1559 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1560 .setAlphabeticShortcut('N');
1561
1562 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001563 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1564 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1567 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1568 .setIntent(settings);
1569
1570 return true;
1571 }
1572
1573 @Override
1574 public boolean onPrepareOptionsMenu(Menu menu) {
1575 super.onPrepareOptionsMenu(menu);
1576
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001577 // If all apps is animating, don't show the menu, because we don't know
1578 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001579 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001580 return false;
1581 }
1582
1583 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001584 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001585 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1586 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1587
1588 // Disable add if the workspace is full.
1589 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001590 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1591 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001592 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593
1594 return true;
1595 }
1596
1597 @Override
1598 public boolean onOptionsItemSelected(MenuItem item) {
1599 switch (item.getItemId()) {
1600 case MENU_ADD:
1601 addItems();
1602 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001603 case MENU_MANAGE_APPS:
1604 manageApps();
1605 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 case MENU_WALLPAPER_SETTINGS:
1607 startWallpaper();
1608 return true;
1609 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001610 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 return true;
1612 case MENU_NOTIFICATIONS:
1613 showNotifications();
1614 return true;
1615 }
1616
1617 return super.onOptionsItemSelected(item);
1618 }
Romain Guycbb89e42009-06-08 15:52:54 -07001619
Karl Rosaen138a0412009-04-23 19:00:21 -07001620 /**
1621 * Indicates that we want global search for this activity by setting the globalSearch
1622 * argument for {@link #startSearch} to true.
1623 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001625 @Override
1626 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001627 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001628 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001629 }
1630
Joe Onorato9c1289c2009-08-17 11:03:03 -04001631 public boolean isWorkspaceLocked() {
1632 return mWorkspaceLoading || mWaitingForResult;
1633 }
1634
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001636 if (LauncherApplication.isScreenXLarge()) {
1637 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001638 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001639 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001640 }
1641 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001642 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001643 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001644 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646
Michael Jurka0280c3b2010-09-17 15:00:07 -07001647 private void resetAddInfo() {
1648 mAddScreen = -1;
1649 mAddIntersectCellX = -1;
1650 mAddIntersectCellY = -1;
1651 mAddDropPosition = null;
1652 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001653
Winson Chung55cef262010-10-28 14:14:18 -07001654 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001655 resetAddInfo();
1656 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 mAddDropPosition = position;
1658
Michael Jurkaaf442092010-06-10 17:01:57 -07001659 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001660 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1661 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001662 }
1663
Winson Chung7ad01412010-09-27 11:33:03 -07001664 private void manageApps() {
1665 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1666 }
1667
Michael Jurkaaf442092010-06-10 17:01:57 -07001668 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001669 // TODO: catch bad widget exception when sent
1670 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001671 // TODO: Is this log message meaningful?
1672 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001673 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001674 }
1675
Winson Chung55cef262010-10-28 14:14:18 -07001676 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001677 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678
Bjorn Bringert7984c942009-12-09 15:38:25 +00001679 if (appWidget.configure != null) {
1680 // Launch over to configure widget, if needed
1681 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1682 intent.setComponent(appWidget.configure);
1683 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001684 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001685 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1686 intent.putExtra(
1687 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1688 info.mimeType);
1689
1690 final String mimeType = info.mimeType;
1691 final ClipData clipData = (ClipData) info.configurationData;
1692 final ClipDescription clipDesc = clipData.getDescription();
1693 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1694 if (clipDesc.getMimeType(i).equals(mimeType)) {
1695 final ClipData.Item item = clipData.getItem(i);
1696 final CharSequence stringData = item.getText();
1697 final Uri uriData = item.getUri();
1698 final Intent intentData = item.getIntent();
1699 final String key =
1700 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1701 if (uriData != null) {
1702 intent.putExtra(key, uriData);
1703 } else if (intentData != null) {
1704 intent.putExtra(key, intentData);
1705 } else if (stringData != null) {
1706 intent.putExtra(key, stringData);
1707 }
1708 break;
1709 }
1710 }
1711 }
Winson Chung55cef262010-10-28 14:14:18 -07001712 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001713
Romain Guy8e633c52010-03-04 12:51:36 -08001714 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001716 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001717 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 }
1719 }
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Michael Jurka0280c3b2010-09-17 15:00:07 -07001721 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1722 resetAddInfo();
1723 mAddScreen = screen;
1724 mAddDropPosition = position;
1725
1726 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1727 createShortcutIntent.setComponent(componentName);
1728 processShortcut(createShortcutIntent);
1729 }
1730
Joe Onoratodeb98af2010-02-19 14:59:39 -08001731 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001732 // Handle case where user selected "Applications"
1733 String applicationName = getResources().getString(R.string.group_applications);
1734 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001735
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001736 if (applicationName != null && applicationName.equals(shortcutName)) {
1737 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1738 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001739
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001740 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1741 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001742 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001743 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001744 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001745 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
1748
Winson Chung24ab2f12010-09-16 14:10:47 -07001749 void processWallpaper(Intent intent) {
1750 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1751 }
1752
Michael Jurka0280c3b2010-09-17 15:00:07 -07001753 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1754 resetAddInfo();
1755 mAddScreen = screen;
1756 mAddDropPosition = position;
1757
1758 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1759 createFolderIntent.setComponent(componentName);
1760
1761 addLiveFolder(createFolderIntent);
1762 }
1763
1764 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001765 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001766 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001767 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001768
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001769 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001770 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001771 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001772 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001773 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775
Michael Jurka0280c3b2010-09-17 15:00:07 -07001776 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 UserFolderInfo folderInfo = new UserFolderInfo();
1778 folderInfo.title = getText(R.string.folder_name);
1779
Michael Jurka0280c3b2010-09-17 15:00:07 -07001780 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1781 final int[] cellXY = mTmpAddItemCellCoordinates;
1782 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1783 showOutOfSpaceMessage();
1784 return;
1785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786
1787 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001788 LauncherModel.addItemToDatabase(this, folderInfo,
1789 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001790 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001791 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792
1793 // Create the view
1794 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001795 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1796 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001797 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
Romain Guycbb89e42009-06-08 15:52:54 -07001799
Joe Onorato9c1289c2009-08-17 11:03:03 -04001800 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001801 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 }
1803
Michael Jurka28750fb2010-09-24 17:43:49 -07001804 private void completeAddLiveFolder(
1805 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001806 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1807 final int[] cellXY = mTmpAddItemCellCoordinates;
1808 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1809 showOutOfSpaceMessage();
1810 return;
1811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814
1815 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001817 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
1821
1822 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001823 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824
1825 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1826 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1827
1828 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 Intent.ShortcutIconResource iconResource = null;
1830
1831 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1832 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1833 try {
1834 iconResource = (Intent.ShortcutIconResource) extra;
1835 final PackageManager packageManager = context.getPackageManager();
1836 Resources resources = packageManager.getResourcesForApplication(
1837 iconResource.packageName);
1838 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1839 icon = resources.getDrawable(id);
1840 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001841 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 }
1843 }
1844
1845 if (icon == null) {
1846 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1847 }
1848
1849 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001850 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 info.title = name;
1852 info.iconResource = iconResource;
1853 info.uri = data.getData();
1854 info.baseIntent = baseIntent;
1855 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1856 LiveFolders.DISPLAY_MODE_GRID);
1857
1858 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001859 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001860 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861
1862 return info;
1863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 private void showNotifications() {
1866 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1867 if (statusBar != null) {
1868 statusBar.expand();
1869 }
1870 }
1871
1872 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001873 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001875 Intent chooser = Intent.createChooser(pickWallpaper,
1876 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001877 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1878 // Removed in Eclair MR1
1879// WallpaperManager wm = (WallpaperManager)
1880// getSystemService(Context.WALLPAPER_SERVICE);
1881// WallpaperInfo wi = wm.getWallpaperInfo();
1882// if (wi != null && wi.getSettingsActivity() != null) {
1883// LabeledIntent li = new LabeledIntent(getPackageName(),
1884// R.string.configure_wallpaper, 0);
1885// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1886// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1887// }
Mike Clerona0618e42009-10-22 13:55:21 -07001888 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001891 /**
1892 * Registers various content observers. The current implementation registers
1893 * only a favorites observer to keep track of the favorites applications.
1894 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001895 private void registerContentObservers() {
1896 ContentResolver resolver = getContentResolver();
1897 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1898 true, mWidgetObserver);
1899 }
1900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 @Override
1902 public boolean dispatchKeyEvent(KeyEvent event) {
1903 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1904 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001906 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001907 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001908 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001909 dumpState();
1910 return true;
1911 }
1912 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001913 }
1914 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1915 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001916 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 return true;
1918 }
1919 }
1920
1921 return super.dispatchKeyEvent(event);
1922 }
1923
Joe Onorato88ec0992009-11-19 13:16:06 -08001924 @Override
1925 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001926 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1927 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001928 } else {
1929 closeFolder();
1930 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001931 // Some launcher layouts don't have a previous and next view
1932 if (mPreviousView != null) {
1933 dismissPreview(mPreviousView);
1934 dismissPreview(mNextView);
1935 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 private void closeFolder() {
1939 Folder folder = mWorkspace.getOpenFolder();
1940 if (folder != null) {
1941 closeFolder(folder);
1942 }
1943 }
1944
1945 void closeFolder(Folder folder) {
1946 folder.getInfo().opened = false;
1947 ViewGroup parent = (ViewGroup) folder.getParent();
1948 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001949 CellLayout cl = (CellLayout) parent;
1950 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001951 if (folder instanceof DropTarget) {
1952 // Live folders aren't DropTargets.
1953 mDragController.removeDropTarget((DropTarget)folder);
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956 folder.onClose();
1957 }
1958
1959 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001960 * Re-listen when widgets are reset.
1961 */
1962 private void onAppWidgetReset() {
1963 mAppWidgetHost.startListening();
1964 }
1965
1966 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001967 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1968 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001970 private void unbindDesktopItems() {
1971 for (ItemInfo item: mDesktopItems) {
1972 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 }
1974 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 /**
1977 * Launches the intent referred by the clicked shortcut.
1978 *
1979 * @param v The view representing the clicked shortcut.
1980 */
1981 public void onClick(View v) {
1982 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001983 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001985 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001986 int[] pos = new int[2];
1987 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001988 intent.setSourceBounds(new Rect(pos[0], pos[1],
1989 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001990 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 } else if (tag instanceof FolderInfo) {
1992 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001993 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001994 if (mState == State.ALL_APPS) {
1995 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001996 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001997 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
2000 }
2001
Michael Jurka0e260592010-06-30 17:07:39 -07002002 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002003 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002004 // clicking anywhere on the workspace causes the customization drawer to slide down
2005 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002006 return false;
2007 }
2008
Michael Jurkaaf442092010-06-10 17:01:57 -07002009 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002010 * Event handler for the search button
2011 *
2012 * @param v The view that was clicked.
2013 */
2014 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002015 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002016 // Use a custom animation for launching search
2017 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002018 }
2019
2020 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002021 * Event handler for the voice button
2022 *
2023 * @param v The view that was clicked.
2024 */
2025 public void onClickVoiceButton(View v) {
2026 startVoiceSearch();
2027 }
2028
2029 private void startVoiceSearch() {
2030 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2031 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2032 startActivity(intent);
2033 }
2034
2035 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002036 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002037 * enters home screen customization mode.
2038 *
2039 * @param v The view that was clicked.
2040 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002041 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002042 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002043 }
2044
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002045 /**
2046 * Event handler for the "grid" button that appears on the home screen, which
2047 * enters all apps mode.
2048 *
2049 * @param v The view that was clicked.
2050 */
2051 public void onClickAllAppsButton(View v) {
2052 showAllApps(true);
2053 }
2054
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002055 public void onClickAppMarketButton(View v) {
2056 if (mAppMarketIntent != null) {
2057 startActivitySafely(mAppMarketIntent, "app market");
2058 }
2059 }
2060
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002061 void startApplicationDetailsActivity(ComponentName componentName) {
2062 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002063 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2064 Uri.fromParts("package", packageName, null));
2065 startActivity(intent);
2066 }
2067
Patrick Dubroy5539af72010-09-07 15:22:01 -07002068 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2069 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2070 // System applications cannot be installed. For now, show a toast explaining that.
2071 // We may give them the option of disabling apps this way.
2072 int messageId = R.string.uninstall_system_app_text;
2073 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2074 } else {
2075 String packageName = appInfo.componentName.getPackageName();
2076 String className = appInfo.componentName.getClassName();
2077 Intent intent = new Intent(
2078 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2079 startActivity(intent);
2080 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002081 }
2082
Joe Onoratof984e852010-03-25 09:47:45 -07002083 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2085 try {
2086 startActivity(intent);
2087 } catch (ActivityNotFoundException e) {
2088 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002089 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 } catch (SecurityException e) {
2091 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002092 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002094 "or use the exported attribute for this activity. "
2095 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002098
Romain Guy8e633c52010-03-04 12:51:36 -08002099 void startActivityForResultSafely(Intent intent, int requestCode) {
2100 try {
2101 startActivityForResult(intent, requestCode);
2102 } catch (ActivityNotFoundException e) {
2103 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2104 } catch (SecurityException e) {
2105 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2106 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2107 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2108 "or use the exported attribute for this activity.", e);
2109 }
2110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111
2112 private void handleFolderClick(FolderInfo folderInfo) {
2113 if (!folderInfo.opened) {
2114 // Close any open folder
2115 closeFolder();
2116 // Open the requested folder
2117 openFolder(folderInfo);
2118 } else {
2119 // Find the open folder...
2120 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2121 int folderScreen;
2122 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002123 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 // .. and close it
2125 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002126 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002127 // Close any folder open on the current screen
2128 closeFolder();
2129 // Pull the folder onto this screen
2130 openFolder(folderInfo);
2131 }
2132 }
2133 }
2134 }
2135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002137 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002138 * is animated relative to the specified View. If the View is null, no animation
2139 * is played.
2140 *
2141 * @param folderInfo The FolderInfo describing the folder to open.
2142 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002143 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 Folder openFolder;
2145
2146 if (folderInfo instanceof UserFolderInfo) {
2147 openFolder = UserFolder.fromXml(this);
2148 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002149 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 } else {
2151 return;
2152 }
2153
Joe Onorato00acb122009-08-04 16:04:30 -04002154 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002155 openFolder.setLauncher(this);
2156
2157 openFolder.bind(folderInfo);
2158 folderInfo.opened = true;
2159
Winson Chungaafa03c2010-06-11 17:34:16 -07002160 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 openFolder.onOpen();
2163 }
2164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002166 switch (v.getId()) {
2167 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002168 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002169 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2170 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002171 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002172 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002173 return true;
2174 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002175 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002176 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2177 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002178 showPreviews(v);
2179 }
2180 return true;
2181 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002182 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002183 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2184 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2185 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002186 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002187 return true;
2188 }
2189
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 return false;
2192 }
2193
2194 if (!(v instanceof CellLayout)) {
2195 v = (View) v.getParent();
2196 }
2197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198
Michael Jurka0280c3b2010-09-17 15:00:07 -07002199 resetAddInfo();
2200 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002202 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 return true;
2204 }
2205
Michael Jurka0280c3b2010-09-17 15:00:07 -07002206 final View itemUnderLongClick = longClickCellInfo.cell;
2207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002209 if (itemUnderLongClick == null) {
2210 // User long pressed on empty space
2211 mWorkspace.setAllowLongPress(false);
2212 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2213 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002214 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002220 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2221 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002222 mAddIntersectCellX = longClickCellInfo.cellX;
2223 mAddIntersectCellY = longClickCellInfo.cellY;
2224 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 }
2226 }
2227 }
2228 return true;
2229 }
2230
Romain Guye6b8e2f2009-11-10 11:56:55 -08002231 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002232 private void dismissPreview(final View v) {
2233 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002234 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002235 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2236 public void onDismiss() {
2237 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2238 int count = group.getChildCount();
2239 for (int i = 0; i < count; i++) {
2240 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2241 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002242 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2243 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2244
2245 v.setTag(R.id.workspace, null);
2246 v.setTag(R.id.icon, null);
2247 window.setOnDismissListener(null);
2248 }
2249 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002250 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002251 }
2252 v.setTag(null);
2253 }
2254
Romain Guyf8e6a802009-12-07 17:48:02 -08002255 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002256 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002257 }
2258
Romain Guya6abce82009-11-10 02:54:41 -08002259 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002260 final Resources resources = getResources();
2261 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002262
Romain Guya6abce82009-11-10 02:54:41 -08002263 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002264
Romain Guy9d31e9f2009-11-11 18:54:28 -08002265 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002266
Romain Guyf8e6a802009-12-07 17:48:02 -08002267 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002268 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002269 int extraW = (int) ((r.left + r.right) * max);
2270 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002271
2272 int aW = cell.getWidth() - extraW;
2273 float w = aW / max;
2274
2275 int width = cell.getWidth();
2276 int height = cell.getHeight();
2277 int x = cell.getLeftPadding();
2278 int y = cell.getTopPadding();
2279 width -= (x + cell.getRightPadding());
2280 height -= (y + cell.getBottomPadding());
2281
2282 float scale = w / width;
2283
2284 int count = end - start;
2285
2286 final float sWidth = width * scale;
2287 float sHeight = height * scale;
2288
Romain Guye6b8e2f2009-11-10 11:56:55 -08002289 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002290
Romain Guye6b8e2f2009-11-10 11:56:55 -08002291 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2292 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002293
2294 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002295 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002296 cell = (CellLayout) workspace.getChildAt(i);
2297
Romain Guyf8e6a802009-12-07 17:48:02 -08002298 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002299 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002300
2301 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002302 c.scale(scale, scale);
2303 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002304 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002305
Romain Guy9d31e9f2009-11-11 18:54:28 -08002306 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002307 image.setImageBitmap(bitmap);
2308 image.setTag(i);
2309 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002310 image.setOnFocusChangeListener(handler);
2311 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002312 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002313
2314 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002315 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2316
Winson Chungaafa03c2010-06-11 17:34:16 -07002317 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002318 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002319
2320 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002321 p.setContentView(preview);
2322 p.setWidth((int) (sWidth * count + extraW));
2323 p.setHeight((int) (sHeight + extraH));
2324 p.setAnimationStyle(R.style.AnimationPreview);
2325 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002327 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002328 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002329
Romain Guye6b8e2f2009-11-10 11:56:55 -08002330 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2331 public void onDismiss() {
2332 dismissPreview(anchor);
2333 }
2334 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002335
2336 anchor.setTag(p);
2337 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002338 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002339 }
2340
Romain Guy9d31e9f2009-11-11 18:54:28 -08002341 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002342 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002343
Romain Guye6b8e2f2009-11-10 11:56:55 -08002344 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002345 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002346 }
2347
2348 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002349 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002350 v.post(this);
2351 }
2352
2353 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002354 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002355 }
2356
2357 public void onFocusChange(View v, boolean hasFocus) {
2358 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002359 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002360 }
Romain Guya6abce82009-11-10 02:54:41 -08002361 }
2362 }
2363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002364 Workspace getWorkspace() {
2365 return mWorkspace;
2366 }
2367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 @Override
2369 protected Dialog onCreateDialog(int id) {
2370 switch (id) {
2371 case DIALOG_CREATE_SHORTCUT:
2372 return new CreateShortcut().createDialog();
2373 case DIALOG_RENAME_FOLDER:
2374 return new RenameFolder().createDialog();
2375 }
2376
2377 return super.onCreateDialog(id);
2378 }
2379
2380 @Override
2381 protected void onPrepareDialog(int id, Dialog dialog) {
2382 switch (id) {
2383 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384 break;
2385 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002386 if (mFolderInfo != null) {
2387 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2388 final CharSequence text = mFolderInfo.title;
2389 input.setText(text);
2390 input.setSelection(0, text.length());
2391 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 break;
2393 }
2394 }
2395
2396 void showRenameDialog(FolderInfo info) {
2397 mFolderInfo = info;
2398 mWaitingForResult = true;
2399 showDialog(DIALOG_RENAME_FOLDER);
2400 }
2401
Michael Jurka0280c3b2010-09-17 15:00:07 -07002402 private void showAddDialog(int intersectX, int intersectY) {
2403 resetAddInfo();
2404 mAddIntersectCellX = intersectX;
2405 mAddIntersectCellY = intersectY;
2406 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 mWaitingForResult = true;
2408 showDialog(DIALOG_CREATE_SHORTCUT);
2409 }
2410
Joe Onoratodeb98af2010-02-19 14:59:39 -08002411 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002412 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002413 Bundle bundle = new Bundle();
2414
2415 ArrayList<String> shortcutNames = new ArrayList<String>();
2416 shortcutNames.add(getString(R.string.group_applications));
2417 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2418
2419 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2420 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2421 R.drawable.ic_launcher_application));
2422 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2423
2424 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2425 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002426 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002427 pickIntent.putExtras(bundle);
2428
Joe Onoratodeb98af2010-02-19 14:59:39 -08002429 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002430 }
2431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 private class RenameFolder {
2433 private EditText mInput;
2434
2435 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2437 mInput = (EditText) layout.findViewById(R.id.folder_name);
2438
2439 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2440 builder.setIcon(0);
2441 builder.setTitle(getString(R.string.rename_folder_title));
2442 builder.setCancelable(true);
2443 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2444 public void onCancel(DialogInterface dialog) {
2445 cleanup();
2446 }
2447 });
2448 builder.setNegativeButton(getString(R.string.cancel_action),
2449 new Dialog.OnClickListener() {
2450 public void onClick(DialogInterface dialog, int which) {
2451 cleanup();
2452 }
2453 }
2454 );
2455 builder.setPositiveButton(getString(R.string.rename_action),
2456 new Dialog.OnClickListener() {
2457 public void onClick(DialogInterface dialog, int which) {
2458 changeFolderName();
2459 }
2460 }
2461 );
2462 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002463
2464 final AlertDialog dialog = builder.create();
2465 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2466 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002467 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002468 mInput.requestFocus();
2469 InputMethodManager inputManager = (InputMethodManager)
2470 getSystemService(Context.INPUT_METHOD_SERVICE);
2471 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002472 }
2473 });
2474
2475 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 }
2477
2478 private void changeFolderName() {
2479 final String name = mInput.getText().toString();
2480 if (!TextUtils.isEmpty(name)) {
2481 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002482 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 mFolderInfo.title = name;
2484 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2485
Joe Onorato9c1289c2009-08-17 11:03:03 -04002486 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002487 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002488 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002489 } else {
2490 final FolderIcon folderIcon = (FolderIcon)
2491 mWorkspace.getViewForTag(mFolderInfo);
2492 if (folderIcon != null) {
2493 folderIcon.setText(name);
2494 getWorkspace().requestLayout();
2495 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002496 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002497 mWorkspaceLoading = true;
2498 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 }
2500 }
2501 }
2502 cleanup();
2503 }
2504
2505 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 dismissDialog(DIALOG_RENAME_FOLDER);
2507 mWaitingForResult = false;
2508 mFolderInfo = null;
2509 }
2510 }
2511
Daniel Sandler843e8602010-06-07 14:59:01 -04002512 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2513 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002514 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002515 }
2516
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002517 // AllAppsView.Watcher
2518 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002519 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2520 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002521 mWorkspace.setVisibility(View.GONE);
2522 }
2523 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002524
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 private void showToolbarButton(View button) {
2526 button.setAlpha(1.0f);
2527 button.setVisibility(View.VISIBLE);
2528 button.setFocusable(true);
2529 button.setClickable(true);
2530 }
2531
2532 private void hideToolbarButton(View button) {
2533 button.setAlpha(0.0f);
2534 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2535 button.setVisibility(View.INVISIBLE);
2536 button.setFocusable(false);
2537 button.setClickable(false);
2538 }
2539
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002542 *
2543 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2544 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002545 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002546 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002547 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002548 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 final boolean showing = show;
2550 final boolean hiding = !show;
2551
2552 final int duration = show ?
2553 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2554 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2555
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002556 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002557 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2558 anim.setDuration(duration);
Michael Jurka8edd75c2010-12-17 20:15:06 -08002559 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002560 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002563 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002564 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002565 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002566 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 });
2569 seq.play(anim);
2570 } else {
2571 if (showing) {
2572 showToolbarButton(view);
2573 } else {
2574 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 }
2578
2579 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 * Show/hide the appropriate toolbar buttons for newState.
2581 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002582 *
2583 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002584 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2585 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002587 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002588 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002589 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002590 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 final View configureButton = findViewById(R.id.configure_button);
2592
2593 switch (newState) {
2594 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002595 hideOrShowToolbarButton(true, searchButton, showSeq);
2596 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002597 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002598 hideOrShowToolbarButton(true, configureButton, showSeq);
Michael Jurkab8e14472010-12-20 16:06:10 -08002599 mDeleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002600 mDeleteZone.setDragAndDropEnabled(true);
2601 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 break;
2603 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002604 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002606 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002608 mDeleteZone.setDragAndDropEnabled(false);
2609 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
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);
Michael Jurka577d0172010-12-17 20:04:50 -08002616 mDeleteZone.setDragAndDropEnabled(false);
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}