blob: 37d1a99896aeeba652c5af574a5d633c04f2fefc [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
Winson Chung68846fd2010-10-29 11:00:27 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070030import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070031import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070033import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.Dialog;
36import android.app.SearchManager;
37import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070038import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070050import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070051import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070054import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070055import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Adam Cohencdc30d52010-12-01 15:09:47 -080062import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070063import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080064import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070065import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040066import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070067import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020069import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080070import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070071import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040073import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080074import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070075import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070076import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080077import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.text.Selection;
79import android.text.SpannableStringBuilder;
80import android.text.TextUtils;
81import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070082import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080084import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.KeyEvent;
86import android.view.LayoutInflater;
87import android.view.Menu;
88import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070089import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.View;
91import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070092import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -0800101import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Winson Chung68846fd2010-10-29 11:00:27 -0700107import com.android.common.Search;
108import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800109
Adam Cohended9f8d2010-11-03 13:25:16 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800125 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700128 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
129 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
131 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700132 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
135 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
139 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Joe Onorato7c312c12009-08-13 21:36:53 -0700148 static final int DIALOG_CREATE_SHORTCUT = 1;
149 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: long
158 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 // tags for the customization tabs
171 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700172 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 private static final String SHORTCUTS_TAG = "shortcuts";
174 private static final String WALLPAPERS_TAG = "wallpapers";
175
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700176 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
177
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178 /** The different states that Launcher can be in. */
179 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700180 private State mState = State.WORKSPACE;
181 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800186 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
Joe Onorato00acb122009-08-04 16:04:30 -0400194 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 private int mAddScreen = -1;
201 private int mAddIntersectCellX = -1;
202 private int mAddIntersectCellY = -1;
203 private int[] mAddDropPosition;
204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Joe Onorato7c312c12009-08-13 21:36:53 -0700208 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700210 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700211 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700212 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700213
214 private PagedView mAllAppsPagedView = null;
215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Michael Jurka4ef207b2010-11-29 17:05:45 -0800262 // External icons saved in case of resource changes, orientation, etc.
263 private static Drawable sGlobalSearchIcon;
264 private static Drawable sVoiceSearchIcon;
265 private static Drawable sAppMarketIcon;
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700270
Winson Chungaafa03c2010-06-11 17:34:16 -0700271 if (LauncherApplication.isInPlaceRotationEnabled()) {
272 // hide the status bar (temporary until we get the status bar design figured out)
273 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
274 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
275 }
276
Joe Onorato0589f0f2010-02-08 13:44:00 -0800277 LauncherApplication app = ((LauncherApplication)getApplication());
278 mModel = app.setLauncher(this);
279 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400280 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700282
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700283 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700284 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
285 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200288 android.os.Debug.startMethodTracing(
289 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 }
291
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400292 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 checkForLocaleChange();
294 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700296 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
297 if (mHomeCustomizationDrawer != null) {
298 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700299
Winson Chung80baf5a2010-08-09 16:03:15 -0700300 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700301 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
302 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700303 TabContentFactory contentFactory = new TabContentFactory() {
304 public View createTabContent(String tag) {
305 return mCustomizePagedView;
306 }
307 };
308
Winson Chung49767ae2010-11-29 14:48:30 -0800309
310 TextView tabView;
311 TabWidget tabWidget = (TabWidget)
312 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
313
314 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
315 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800317 .setIndicator(tabView).setContent(contentFactory));
318 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
319 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700320 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800321 .setIndicator(tabView).setContent(contentFactory));
322 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
323 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800325 .setIndicator(tabView).setContent(contentFactory));
326 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
327 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700328 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800329 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700330 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
331 public void onTabChanged(String tabId) {
332 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800333 final Resources res = getResources();
334 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700336 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700338 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700339 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700340 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700341 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700342 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
343 if (tag == WIDGETS_TAG) {
344 mCustomizePagedView.setCustomizationFilter(
345 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700346 } else if (tag == APPLICATIONS_TAG) {
347 mCustomizePagedView.setCustomizationFilter(
348 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700349 } else if (tag == WALLPAPERS_TAG) {
350 mCustomizePagedView.setCustomizationFilter(
351 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700352 } else if (tag == SHORTCUTS_TAG) {
353 mCustomizePagedView.setCustomizationFilter(
354 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700355 }
356
357 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700358 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700359 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700360 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700361 alphaAnim.start();
362 }
363 });
364 alphaAnim.start();
365 }
366 });
Michael Jurka946ad472010-07-09 18:05:18 -0700367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 setupViews();
369
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800370 registerContentObservers();
371
Joe Onorato7c312c12009-08-13 21:36:53 -0700372 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700373
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 mSavedState = savedInstanceState;
375 restoreState(mSavedState);
376
377 if (PROFILE_STARTUP) {
378 android.os.Debug.stopMethodTracing();
379 }
380
381 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400382 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
384
385 // For handling default keys
386 mDefaultKeySsb = new SpannableStringBuilder();
387 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800388
389 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
390 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800391
392 // If we have a saved version of these external icons, we load them up immediately
393 if (LauncherApplication.isScreenXLarge()) {
394 if (sGlobalSearchIcon != null) {
395 updateGlobalSearchIcon(sGlobalSearchIcon);
396 }
397 if (sVoiceSearchIcon != null) {
398 updateVoiceSearchIcon(sVoiceSearchIcon);
399 }
400 if (sAppMarketIcon != null) {
401 updateAppMarketIcon(sAppMarketIcon);
402 }
403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 }
Romain Guycbb89e42009-06-08 15:52:54 -0700405
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700407 if (sLocaleConfiguration == null) {
408 new AsyncTask<Void, Void, LocaleConfiguration>() {
409 @Override
410 protected LocaleConfiguration doInBackground(Void... unused) {
411 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
412 readConfiguration(Launcher.this, localeConfiguration);
413 return localeConfiguration;
414 }
415
416 @Override
417 protected void onPostExecute(LocaleConfiguration result) {
418 sLocaleConfiguration = result;
419 checkForLocaleChange(); // recursive, but now with a locale configuration
420 }
421 }.execute();
422 return;
423 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 final Configuration configuration = getResources().getConfiguration();
426
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700427 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 final String locale = configuration.locale.toString();
429
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700430 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 final int mcc = configuration.mcc;
432
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700433 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 final int mnc = configuration.mnc;
435
Romain Guy84f296c2009-11-04 15:00:44 -0800436 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437
Romain Guy84f296c2009-11-04 15:00:44 -0800438 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700439 sLocaleConfiguration.locale = locale;
440 sLocaleConfiguration.mcc = mcc;
441 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700442
Joe Onorato0589f0f2010-02-08 13:44:00 -0800443 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400444 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700445
446 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
447 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700448 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700449 public void run() {
450 writeConfiguration(Launcher.this, localeConfiguration);
451 }
452 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700453 }
454 }
455
456 private static class LocaleConfiguration {
457 public String locale;
458 public int mcc = -1;
459 public int mnc = -1;
460 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700461
Romain Guy98d01652009-06-30 16:21:04 -0700462 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
463 DataInputStream in = null;
464 try {
465 in = new DataInputStream(context.openFileInput(PREFERENCES));
466 configuration.locale = in.readUTF();
467 configuration.mcc = in.readInt();
468 configuration.mnc = in.readInt();
469 } catch (FileNotFoundException e) {
470 // Ignore
471 } catch (IOException e) {
472 // Ignore
473 } finally {
474 if (in != null) {
475 try {
476 in.close();
477 } catch (IOException e) {
478 // Ignore
479 }
480 }
481 }
482 }
483
484 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
485 DataOutputStream out = null;
486 try {
487 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
488 out.writeUTF(configuration.locale);
489 out.writeInt(configuration.mcc);
490 out.writeInt(configuration.mnc);
491 out.flush();
492 } catch (FileNotFoundException e) {
493 // Ignore
494 } catch (IOException e) {
495 //noinspection ResultOfMethodCallIgnored
496 context.getFileStreamPath(PREFERENCES).delete();
497 } finally {
498 if (out != null) {
499 try {
500 out.close();
501 } catch (IOException e) {
502 // Ignore
503 }
504 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 }
506 }
507
508 static int getScreen() {
509 synchronized (sLock) {
510 return sScreen;
511 }
512 }
513
514 static void setScreen(int screen) {
515 synchronized (sLock) {
516 sScreen = screen;
517 }
518 }
519
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700521 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522
523 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700524 // TODO: Put back when we decide about scrolling the wallpaper
525 // boolean isPortrait = display.getWidth() < display.getHeight();
526 // final int width = isPortrait ? display.getWidth() : display.getHeight();
527 // final int height = isPortrait ? display.getHeight() : display.getWidth();
528 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
529 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
531
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400532 // Note: This doesn't do all the client-id magic that BrowserProvider does
533 // in Browser. (http://b/2425179)
534 private Uri getDefaultBrowserUri() {
535 String url = getString(R.string.default_browser_url);
536 if (url.indexOf("{CID}") != -1) {
537 url = url.replace("{CID}", "android-google");
538 }
539 return Uri.parse(url);
540 }
541
542 // Load the Intent templates from arrays.xml to populate the hotseats. For
543 // each Intent, if it resolves to a single app, use that as the launch
544 // intent & use that app's label as the contentDescription. Otherwise,
545 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 if (mHotseatConfig == null) {
548 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
549 if (mHotseatConfig.length > 0) {
550 mHotseats = new Intent[mHotseatConfig.length];
551 mHotseatLabels = new CharSequence[mHotseatConfig.length];
552 mHotseatIcons = new Drawable[mHotseatConfig.length];
553 } else {
554 mHotseats = null;
555 mHotseatIcons = null;
556 mHotseatLabels = null;
557 }
558
559 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
560 for (int i=0; i<mHotseatConfig.length; i++) {
561 // load icon for this slot; currently unrelated to the actual activity
562 try {
563 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
564 } catch (ArrayIndexOutOfBoundsException ex) {
565 Log.w(TAG, "Missing hotseat_icons array item #" + i);
566 mHotseatIcons[i] = null;
567 }
568 }
569 hotseatIconDrawables.recycle();
570 }
571
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400572 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 for (int i=0; i<mHotseatConfig.length; i++) {
574 Intent intent = null;
575 if (mHotseatConfig[i].equals("*BROWSER*")) {
576 // magic value meaning "launch user's default web browser"
577 // replace it with a generic web request so we can see if there is indeed a default
578 String defaultUri = getString(R.string.default_browser_url);
579 intent = new Intent(
580 Intent.ACTION_VIEW,
581 ((defaultUri != null)
582 ? Uri.parse(defaultUri)
583 : getDefaultBrowserUri())
584 ).addCategory(Intent.CATEGORY_BROWSABLE);
585 // note: if the user launches this without a default set, she
586 // will always be taken to the default URL above; this is
587 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 // URL is unavoidably sent to the chosen app.
590 } else {
591 try {
592 intent = Intent.parseUri(mHotseatConfig[i], 0);
593 } catch (java.net.URISyntaxException ex) {
594 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
595 // bogus; leave intent=null
596 }
597 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700598
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 if (intent == null) {
600 mHotseats[i] = null;
601 mHotseatLabels[i] = getText(R.string.activity_not_found);
602 continue;
603 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400604
605 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700606 Log.d(TAG, "loadHotseats: hotseat " + i
607 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400608 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400609 + "]");
610 }
611
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400612 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
613 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700614 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 Log.d(TAG, "Best match for intent: " + bestMatch);
616 Log.d(TAG, "All matches: ");
617 for (ResolveInfo ri : allMatches) {
618 Log.d(TAG, " --> " + ri);
619 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400620 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400621 // did this resolve to a single app, or the resolver?
622 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700623 // can't find any activity to handle this. let's leave the
624 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400625 // to launch.
626 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400627
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 // set accessibility text to "Not installed"
629 mHotseatLabels[i] = getText(R.string.activity_not_found);
630 } else {
631 boolean found = false;
632 for (ResolveInfo ri : allMatches) {
633 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
634 && bestMatch.activityInfo.applicationInfo.packageName
635 .equals(ri.activityInfo.applicationInfo.packageName)) {
636 found = true;
637 break;
638 }
639 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700640
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 if (!found) {
642 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
643 // the bestMatch is probably the ResolveActivity, meaning the
644 // user has not yet selected a default
645 // so: we'll keep the original intent for now
646 mHotseats[i] = intent;
647
648 // set the accessibility text to "Select shortcut"
649 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
650 } else {
651 // we have an app!
652 // now reconstruct the intent to launch it through the front
653 // door
654 ComponentName com = new ComponentName(
655 bestMatch.activityInfo.applicationInfo.packageName,
656 bestMatch.activityInfo.name);
657 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
658
659 // load the app label for accessibility
660 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
661 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400662 }
663
664 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700665 Log.d(TAG, "loadHotseats: hotseat " + i
666 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400667 + ((mHotseats[i] == null)
668 ? "null"
669 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400670 + "] label=[" + mHotseatLabels[i]
671 + "]"
672 );
673 }
674 }
675 }
676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 @Override
678 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700679 mWaitingForResult = false;
680
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800681 // The pattern used here is that a user PICKs a specific application,
682 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
685 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700686
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 switch (requestCode) {
689 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700690 completeAddApplication(
691 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 break;
693 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800694 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 break;
696 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700697 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 break;
699 case REQUEST_PICK_LIVE_FOLDER:
700 addLiveFolder(data);
701 break;
702 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700703 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700705 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700706 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800707 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700708 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700709 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700710 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700712 case REQUEST_PICK_WALLPAPER:
713 // We just wanted the activity result here so we can clear mWaitingForResult
714 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
Romain Guy18042c82009-11-06 11:44:55 -0800716 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
717 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
718 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700719 // Clean up the appWidgetId if we canceled
720 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
721 if (appWidgetId != -1) {
722 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800723 }
724 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725 }
726
727 @Override
728 protected void onResume() {
729 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800730 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700731 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400732 mWorkspaceLoading = true;
733 mModel.startLoader(this, true);
734 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700735 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700737 // When we resume Launcher, a different Activity might be responsible for the app
738 // market intent, so refresh the icon
739 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 protected void onPause() {
744 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700745 // Some launcher layouts don't have a previous and next view
746 if (mPreviousView != null) {
747 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700748 }
749 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700750 dismissPreview(mNextView);
751 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700752 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800753 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700754 }
Romain Guycbb89e42009-06-08 15:52:54 -0700755
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700756 @Override
757 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400758 // Flag the loader to stop early before switching
759 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800760 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800761 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700762 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700763
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800764 // We can't hide the IME if it was forced open. So don't bother
765 /*
766 @Override
767 public void onWindowFocusChanged(boolean hasFocus) {
768 super.onWindowFocusChanged(hasFocus);
769
770 if (hasFocus) {
771 final InputMethodManager inputManager = (InputMethodManager)
772 getSystemService(Context.INPUT_METHOD_SERVICE);
773 WindowManager.LayoutParams lp = getWindow().getAttributes();
774 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
775 android.os.Handler()) {
776 protected void onReceiveResult(int resultCode, Bundle resultData) {
777 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
778 }
779 });
780 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
781 }
782 }
783 */
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 private boolean acceptFilter() {
786 final InputMethodManager inputManager = (InputMethodManager)
787 getSystemService(Context.INPUT_METHOD_SERVICE);
788 return !inputManager.isFullscreenMode();
789 }
790
791 @Override
792 public boolean onKeyDown(int keyCode, KeyEvent event) {
793 boolean handled = super.onKeyDown(keyCode, event);
794 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
795 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
796 keyCode, event);
797 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700798 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700799 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700800 // showSearchDialog()
801 // If there are multiple keystrokes before the search dialog takes focus,
802 // onSearchRequested() will be called for every keystroke,
803 // but it is idempotent, so it's fine.
804 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 }
806 }
807
Joe Onorato8a9625e2010-01-28 15:55:35 -0800808 // Eat the long press event so the keyboard doesn't come up.
809 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
810 return true;
811 }
812
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 return handled;
814 }
815
Karl Rosaen138a0412009-04-23 19:00:21 -0700816 private String getTypedText() {
817 return mDefaultKeySsb.toString();
818 }
819
820 private void clearTypedText() {
821 mDefaultKeySsb.clear();
822 mDefaultKeySsb.clearSpans();
823 Selection.setSelection(mDefaultKeySsb, 0);
824 }
825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700827 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
828 * State
829 */
830 private static State intToState(int stateOrdinal) {
831 State state = State.WORKSPACE;
832 final State[] stateValues = State.values();
833 for (int i = 0; i < stateValues.length; i++) {
834 if (stateValues[i].ordinal() == stateOrdinal) {
835 state = stateValues[i];
836 break;
837 }
838 }
839 return state;
840 }
841
842 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 * Restores the previous state, if it exists.
844 *
845 * @param savedState The previous state.
846 */
847 private void restoreState(Bundle savedState) {
848 if (savedState == null) {
849 return;
850 }
851
Michael Jurka883f55b2010-10-21 15:47:14 -0700852 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
853
854 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800855 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700856 } else if (state == State.CUSTOMIZE) {
857 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 }
859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
861 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700862 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864
865 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700866
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700868 mAddScreen = addScreen;
869 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
870 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 mRestoring = true;
872 }
873
874 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
875 if (renameFolder) {
876 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700877 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 mRestoring = true;
879 }
880 }
881
882 /**
883 * Finds all the views we need and configure them properly.
884 */
885 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700886 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400887
Romain Guyb1b69f52009-08-10 15:10:15 -0700888 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400889 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890
Joe Onorato7c312c12009-08-13 21:36:53 -0700891 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700892 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700893 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800894 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700895 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700896 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700897
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700898 if (LauncherApplication.isScreenXLarge()) {
899 // They need to be INVISIBLE initially so that they will be measured in the layout.
900 // Otherwise the animations are messed up when we show them for the first time.
901 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
902 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
903 }
904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700906
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500908 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700909
Joe Onorato7c312c12009-08-13 21:36:53 -0700910 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
911 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700913 View handleView = findViewById(R.id.all_apps_button);
914 if (handleView != null && handleView instanceof HandleView) {
915 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700916 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700917 mHandleView.setLauncher(this);
918 mHandleView.setOnClickListener(this);
919 mHandleView.setOnLongClickListener(this);
920 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800921
Winson Chung80baf5a2010-08-09 16:03:15 -0700922 if (mCustomizePagedView != null) {
923 mCustomizePagedView.setLauncher(this);
924 mCustomizePagedView.setDragController(dragController);
925 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700926 } else {
927 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
928 hotseatLeft.setContentDescription(mHotseatLabels[0]);
929 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
930 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
931 hotseatRight.setContentDescription(mHotseatLabels[1]);
932 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400933
Michael Jurkaaf442092010-06-10 17:01:57 -0700934 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
935 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800936
Michael Jurkaaf442092010-06-10 17:01:57 -0700937 Drawable previous = mPreviousView.getDrawable();
938 Drawable next = mNextView.getDrawable();
939 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940
Michael Jurkaaf442092010-06-10 17:01:57 -0700941 mPreviousView.setHapticFeedbackEnabled(false);
942 mPreviousView.setOnLongClickListener(this);
943 mNextView.setHapticFeedbackEnabled(false);
944 mNextView.setOnLongClickListener(this);
945 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400948 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
951 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400952 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700953 int deleteZoneHandleId;
954 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700955 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700956 } else {
957 deleteZoneHandleId = R.id.all_apps_button_cluster;
958 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700959 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700960 dragController.addDragListener(deleteZone);
961
Adam Cohencdc30d52010-12-01 15:09:47 -0800962 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
963 if (allAppsDeleteZone != null) {
964 allAppsDeleteZone.setLauncher(this);
965 allAppsDeleteZone.setDragController(dragController);
966 allAppsDeleteZone.setDragAndDropEnabled(false);
967 dragController.addDragListener(allAppsDeleteZone);
968 dragController.addDropTarget(allAppsDeleteZone);
969 }
970
971 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
972 findViewById(R.id.all_apps_info_target);
973 if (allAppsInfoTarget != null) {
974 allAppsInfoTarget.setLauncher(this);
975 dragController.addDragListener(allAppsInfoTarget);
976 allAppsInfoTarget.setDragColor(getResources().getColor(R.color.app_info_filter));
977 allAppsInfoTarget.setDragAndDropEnabled(false);
978 View marketButton = findViewById(R.id.market_button);
979 if (marketButton != null) {
980 marketButton.setBackgroundColor(Color.RED);
981 allAppsInfoTarget.setHandle(marketButton);
982 }
983 }
984
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700985 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
986 if (infoButton != null) {
987 infoButton.setLauncher(this);
988 infoButton.setHandle(findViewById(R.id.configure_button));
989 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
990 dragController.addDragListener(infoButton);
991 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992
Joe Onorato00acb122009-08-04 16:04:30 -0400993 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400994 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800995 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700996
Joe Onorato00acb122009-08-04 16:04:30 -0400997 // The order here is bottom to top.
998 dragController.addDropTarget(workspace);
999 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001000 if (infoButton != null) {
1001 dragController.addDropTarget(infoButton);
1002 }
Adam Cohencdc30d52010-12-01 15:09:47 -08001003 if (allAppsInfoTarget != null) {
1004 dragController.addDropTarget(allAppsInfoTarget);
1005 }
1006 if (allAppsDeleteZone != null) {
1007 dragController.addDropTarget(allAppsDeleteZone);
1008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 }
1010
Romain Guy8a73c512009-11-09 19:19:59 -08001011 @SuppressWarnings({"UnusedDeclaration"})
1012 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001013 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001014 mWorkspace.scrollLeft();
1015 }
Romain Guy8a73c512009-11-09 19:19:59 -08001016 }
1017
1018 @SuppressWarnings({"UnusedDeclaration"})
1019 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001020 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001021 mWorkspace.scrollRight();
1022 }
Romain Guy8a73c512009-11-09 19:19:59 -08001023 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001024
1025 @SuppressWarnings({"UnusedDeclaration"})
1026 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001027 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001028
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001029 int index = -1;
1030 if (v.getId() == R.id.hotseat_left) {
1031 index = 0;
1032 } else if (v.getId() == R.id.hotseat_right) {
1033 index = 1;
1034 }
1035
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001036 // reload these every tap; you never know when they might change
1037 loadHotseats();
1038 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1039 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001040 startActivitySafely(
1041 mHotseats[index],
1042 "hotseat"
1043 );
1044 }
1045 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 /**
1048 * Creates a view representing a shortcut.
1049 *
1050 * @param info The data structure describing the shortcut.
1051 *
1052 * @return A View inflated from R.layout.application.
1053 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001054 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001056 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058
1059 /**
1060 * Creates a view representing a shortcut inflated from the specified resource.
1061 *
1062 * @param layoutResId The id of the XML layout used to create the shortcut.
1063 * @param parent The group the shortcut belongs to.
1064 * @param info The data structure describing the shortcut.
1065 *
1066 * @return A View inflated from layoutResId.
1067 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001068 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001069 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1070 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 return favorite;
1073 }
1074
1075 /**
1076 * Add an application shortcut to the workspace.
1077 *
1078 * @param data The intent describing the application.
1079 * @param cellInfo The position on screen where to create the shortcut.
1080 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 void completeAddApplication(Context context, Intent data, int screen,
1082 int intersectCellX, int intersectCellY) {
1083 final int[] cellXY = mTmpAddItemCellCoordinates;
1084 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1085
1086 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1087 showOutOfSpaceMessage();
1088 return;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
Joe Onorato0589f0f2010-02-08 13:44:00 -08001091 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1092 data, context);
1093
Romain Guy73b979d2009-06-09 12:57:21 -07001094 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001095 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001097 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1099 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001100 } else {
1101 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 /**
1106 * Add a shortcut to the workspace.
1107 *
1108 * @param data The intent describing the shortcut.
1109 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001111 private void completeAddShortcut(Intent data, int screen,
1112 int intersectCellX, int intersectCellY) {
1113 final int[] cellXY = mTmpAddItemCellCoordinates;
1114 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1117 showOutOfSpaceMessage();
1118 return;
1119 }
1120
1121 final ShortcutInfo info = mModel.addShortcut(
1122 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123
1124 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001126 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
1128 }
1129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001131 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001133 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001134 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001137 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001139 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1141 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001144 // We have saved the position to which the widget was dragged-- this really only matters
1145 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001147
1148 // For now, we don't save the coordinate where we dropped the icon because we're not
1149 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1150 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001151 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001152 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001153 touchXY = mAddDropPosition;
1154 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001155 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 boolean foundCellSpan = false;
1157 if (touchXY != null) {
1158 // when dragging and dropping, just find the closest free spot
1159 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1160 int[] result = screenLayout.findNearestVacantArea(
1161 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001162 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001163 foundCellSpan = !findNearestVacantAreaFailed;
1164 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001165 // if we long pressed on an empty cell to bring up a menu,
1166 // make sure we intersect the empty cell
1167 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1168 // findCellForSpanThatIntersects will just ignore them
1169 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1170 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001171 }
1172
Michael Jurka0280c3b2010-09-17 15:00:07 -07001173 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001174 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001175 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001176 return;
1177 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 // Build Launcher-specific widget info and save to database
1180 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 launcherInfo.spanX = spanXY[0];
1182 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184 LauncherModel.addItemToDatabase(this, launcherInfo,
1185 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001186 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
1188 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001189 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001192 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001193
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001194 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001196
Michael Jurka0280c3b2010-09-17 15:00:07 -07001197 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001199
1200 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
1202 }
Romain Guycbb89e42009-06-08 15:52:54 -07001203
Adam Cohended9f8d2010-11-03 13:25:16 -07001204 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1205 @Override
1206 public void onReceive(Context context, Intent intent) {
1207 final String action = intent.getAction();
1208 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1209 mUserPresent = false;
1210 updateRunning();
1211 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1212 mUserPresent = true;
1213 updateRunning();
1214 }
1215 }
1216 };
1217
1218 @Override
1219 public void onAttachedToWindow() {
1220 super.onAttachedToWindow();
1221
1222 // Listen for broadcasts related to user-presence
1223 final IntentFilter filter = new IntentFilter();
1224 filter.addAction(Intent.ACTION_SCREEN_OFF);
1225 filter.addAction(Intent.ACTION_USER_PRESENT);
1226 registerReceiver(mReceiver, filter);
1227
Adam Cohend113e0c2010-11-11 10:48:05 -08001228 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001229 mVisible = true;
1230 }
1231
1232 @Override
1233 public void onDetachedFromWindow() {
1234 super.onDetachedFromWindow();
1235 mVisible = false;
1236
Adam Cohend113e0c2010-11-11 10:48:05 -08001237 if (mAttached) {
1238 unregisterReceiver(mReceiver);
1239 mAttached = false;
1240 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001241 updateRunning();
1242 }
1243
1244 public void onWindowVisibilityChanged(int visibility) {
1245 mVisible = visibility == View.VISIBLE;
1246 updateRunning();
1247 }
1248
1249 private void sendAdvanceMessage(long delay) {
1250 mHandler.removeMessages(ADVANCE_MSG);
1251 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1252 mHandler.sendMessageDelayed(msg, delay);
1253 mAutoAdvanceSentTime = System.currentTimeMillis();
1254 }
1255
1256 private void updateRunning() {
1257 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1258 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1259 mAutoAdvanceRunning = autoAdvanceRunning;
1260 if (autoAdvanceRunning) {
1261 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1262 sendAdvanceMessage(delay);
1263 } else {
1264 if (!mWidgetsToAdvance.isEmpty()) {
1265 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1266 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1267 }
1268 mHandler.removeMessages(ADVANCE_MSG);
1269 }
1270 }
1271 }
1272
1273 private final Handler mHandler = new Handler() {
1274 @Override
1275 public void handleMessage(Message msg) {
1276 if (msg.what == ADVANCE_MSG) {
1277 int i = 0;
1278 for (View key: mWidgetsToAdvance.keySet()) {
1279 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1280 final int delay = mAdvanceStagger * i;
1281 if (v instanceof Advanceable) {
1282 postDelayed(new Runnable() {
1283 public void run() {
1284 ((Advanceable) v).advance();
1285 }
1286 }, delay);
1287 }
1288 i++;
1289 }
1290 sendAdvanceMessage(mAdvanceInterval);
1291 }
1292 }
1293 };
1294
1295 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1296 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1297 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1298 if (v instanceof Advanceable) {
1299 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1300 ((Advanceable) v).willBeAdvancedByHost();
1301 updateRunning();
1302 }
1303 }
1304
1305 void removeWidgetToAutoAdvance(View hostView) {
1306 if (mWidgetsToAdvance.containsKey(hostView)) {
1307 mWidgetsToAdvance.remove(hostView);
1308 updateRunning();
1309 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001310 }
1311
Joe Onorato9c1289c2009-08-17 11:03:03 -04001312 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1313 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001314 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001315 launcherInfo.hostView = null;
1316 }
1317
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 void showOutOfSpaceMessage() {
1319 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1320 }
1321
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001322 public LauncherAppWidgetHost getAppWidgetHost() {
1323 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Winson Chunga9abd0e2010-10-27 17:18:37 -07001326 public LauncherModel getModel() {
1327 return mModel;
1328 }
1329
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001330 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001331 getWindow().closeAllPanels();
1332
1333 try {
1334 dismissDialog(DIALOG_CREATE_SHORTCUT);
1335 // Unlock the workspace if the dialog was showing
1336 } catch (Exception e) {
1337 // An exception is thrown if the dialog is not visible, which is fine
1338 }
1339
1340 try {
1341 dismissDialog(DIALOG_RENAME_FOLDER);
1342 // Unlock the workspace if the dialog was showing
1343 } catch (Exception e) {
1344 // An exception is thrown if the dialog is not visible, which is fine
1345 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001346
1347 // Whatever we were doing is hereby canceled.
1348 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001349 }
1350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 @Override
1352 protected void onNewIntent(Intent intent) {
1353 super.onNewIntent(intent);
1354
1355 // Close the menu
1356 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001357 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001358 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001359
Joe Onorato14f122b2009-11-19 14:06:36 -08001360 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1361 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001362
Michael Jurka4cb37242010-08-09 21:05:32 -07001363 // in all these cases, only animate if we're already on home
1364 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001365 mWorkspace.unshrink(alreadyOnHome);
1366 }
1367 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001368 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001369 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1370 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001371 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001372 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001373
Joe Onorato3a8820b2009-11-10 15:06:42 -08001374 final View v = getWindow().peekDecorView();
1375 if (v != null && v.getWindowToken() != null) {
1376 InputMethodManager imm = (InputMethodManager)getSystemService(
1377 INPUT_METHOD_SERVICE);
1378 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
1380 }
1381 }
1382
1383 @Override
1384 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1385 // Do not call super here
1386 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001387
Winson Chung43b119d2010-12-06 16:45:05 -08001388 // Restore the current AllApps drawer tab
1389 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1390 String cur = savedInstanceState.getString("allapps_currentTab");
1391 if (cur != null) {
1392 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1393 tabhost.setCurrentTabByTag(cur);
1394 }
1395 }
1396
1397 // Restore the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001398 if (mHomeCustomizationDrawer != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001399 String cur = savedInstanceState.getString("customize_currentTab");
Michael Jurka946ad472010-07-09 18:05:18 -07001400 if (cur != null) {
1401 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1402 }
1403 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405
1406 @Override
1407 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001408 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409
1410 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1411 if (folders.size() > 0) {
1412 final int count = folders.size();
1413 long[] ids = new long[count];
1414 for (int i = 0; i < count; i++) {
1415 final FolderInfo info = folders.get(i).getInfo();
1416 ids[i] = info.id;
1417 }
1418 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1419 } else {
1420 super.onSaveInstanceState(outState);
1421 }
1422
Michael Jurka883f55b2010-10-21 15:47:14 -07001423 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424
Michael Jurka0280c3b2010-09-17 15:00:07 -07001425 if (mAddScreen > -1 && mWaitingForResult) {
1426 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1427 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1428 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430
1431 if (mFolderInfo != null && mWaitingForResult) {
1432 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1433 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1434 }
Michael Jurka946ad472010-07-09 18:05:18 -07001435
Winson Chung43b119d2010-12-06 16:45:05 -08001436 // Save the current AllApps drawer tab
1437 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1438 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1439 String currentTabTag = tabhost.getCurrentTabTag();
1440 if (currentTabTag != null) {
1441 outState.putString("allapps_currentTab", currentTabTag);
1442 }
1443 }
1444
1445 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001446 if (mHomeCustomizationDrawer != null) {
1447 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1448 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001449 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001450 }
1451 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
1454 @Override
1455 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001457
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001459 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001461 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463
1464 TextKeyListener.getInstance().release();
1465
Joe Onorato9c1289c2009-08-17 11:03:03 -04001466 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
Joe Onorato9c1289c2009-08-17 11:03:03 -04001468 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001469
1470 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001471
Patrick Dubroyab962b72010-07-26 12:10:10 -07001472 // Some launcher layouts don't have a previous and next view
1473 if (mPreviousView != null) {
1474 dismissPreview(mPreviousView);
1475 }
1476 if (mNextView != null) {
1477 dismissPreview(mNextView);
1478 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001479
1480 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 }
1482
1483 @Override
1484 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001485 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 super.startActivityForResult(intent, requestCode);
1487 }
1488
1489 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001490 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001492
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001493 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001494
Karl Rosaen138a0412009-04-23 19:00:21 -07001495 if (initialQuery == null) {
1496 // Use any text typed in the launcher as the initial query
1497 initialQuery = getTypedText();
1498 clearTypedText();
1499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 if (appSearchData == null) {
1501 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001502 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 }
Romain Guycbb89e42009-06-08 15:52:54 -07001504
Karl Rosaen138a0412009-04-23 19:00:21 -07001505 final SearchManager searchManager =
1506 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001507 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001508 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510
1511 @Override
1512 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001513 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 return false;
1515 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516
1517 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1520 .setIcon(android.R.drawable.ic_menu_add)
1521 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001522 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1523 .setIcon(android.R.drawable.ic_menu_manage)
1524 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001525 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 .setIcon(android.R.drawable.ic_menu_gallery)
1527 .setAlphabeticShortcut('W');
1528 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1529 .setIcon(android.R.drawable.ic_search_category_default)
1530 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1531 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1532 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1533 .setAlphabeticShortcut('N');
1534
1535 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001536 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1537 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538
1539 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1540 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1541 .setIntent(settings);
1542
1543 return true;
1544 }
1545
1546 @Override
1547 public boolean onPrepareOptionsMenu(Menu menu) {
1548 super.onPrepareOptionsMenu(menu);
1549
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001550 // If all apps is animating, don't show the menu, because we don't know
1551 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001552 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001553 return false;
1554 }
1555
1556 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001557 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001558 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1559 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1560
1561 // Disable add if the workspace is full.
1562 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1564 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001565 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566
1567 return true;
1568 }
1569
1570 @Override
1571 public boolean onOptionsItemSelected(MenuItem item) {
1572 switch (item.getItemId()) {
1573 case MENU_ADD:
1574 addItems();
1575 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001576 case MENU_MANAGE_APPS:
1577 manageApps();
1578 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 case MENU_WALLPAPER_SETTINGS:
1580 startWallpaper();
1581 return true;
1582 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001583 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 return true;
1585 case MENU_NOTIFICATIONS:
1586 showNotifications();
1587 return true;
1588 }
1589
1590 return super.onOptionsItemSelected(item);
1591 }
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Karl Rosaen138a0412009-04-23 19:00:21 -07001593 /**
1594 * Indicates that we want global search for this activity by setting the globalSearch
1595 * argument for {@link #startSearch} to true.
1596 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001598 @Override
1599 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001600 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001601 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001602 }
1603
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604 public boolean isWorkspaceLocked() {
1605 return mWorkspaceLoading || mWaitingForResult;
1606 }
1607
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001609 if (LauncherApplication.isScreenXLarge()) {
1610 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001611 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001612 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001613 }
1614 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001615 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001616 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001617 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 }
1619
Michael Jurka0280c3b2010-09-17 15:00:07 -07001620 private void resetAddInfo() {
1621 mAddScreen = -1;
1622 mAddIntersectCellX = -1;
1623 mAddIntersectCellY = -1;
1624 mAddDropPosition = null;
1625 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001626
Winson Chung55cef262010-10-28 14:14:18 -07001627 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001628 resetAddInfo();
1629 mAddScreen = screen;
1630
1631 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1632 mAddDropPosition = position;
1633
Michael Jurkaaf442092010-06-10 17:01:57 -07001634 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001635 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1636 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001637 }
1638
Winson Chung7ad01412010-09-27 11:33:03 -07001639 private void manageApps() {
1640 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1641 }
1642
Michael Jurkaaf442092010-06-10 17:01:57 -07001643 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001644 // TODO: catch bad widget exception when sent
1645 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001646 // TODO: Is this log message meaningful?
1647 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001648 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001649 }
1650
Winson Chung55cef262010-10-28 14:14:18 -07001651 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001652 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
Bjorn Bringert7984c942009-12-09 15:38:25 +00001654 if (appWidget.configure != null) {
1655 // Launch over to configure widget, if needed
1656 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1657 intent.setComponent(appWidget.configure);
1658 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001659 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001660 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1661 intent.putExtra(
1662 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1663 info.mimeType);
1664
1665 final String mimeType = info.mimeType;
1666 final ClipData clipData = (ClipData) info.configurationData;
1667 final ClipDescription clipDesc = clipData.getDescription();
1668 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1669 if (clipDesc.getMimeType(i).equals(mimeType)) {
1670 final ClipData.Item item = clipData.getItem(i);
1671 final CharSequence stringData = item.getText();
1672 final Uri uriData = item.getUri();
1673 final Intent intentData = item.getIntent();
1674 final String key =
1675 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1676 if (uriData != null) {
1677 intent.putExtra(key, uriData);
1678 } else if (intentData != null) {
1679 intent.putExtra(key, intentData);
1680 } else if (stringData != null) {
1681 intent.putExtra(key, stringData);
1682 }
1683 break;
1684 }
1685 }
1686 }
Winson Chung55cef262010-10-28 14:14:18 -07001687 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001688
Romain Guy8e633c52010-03-04 12:51:36 -08001689 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001691 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001692 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694 }
Romain Guycbb89e42009-06-08 15:52:54 -07001695
Michael Jurka0280c3b2010-09-17 15:00:07 -07001696 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1697 resetAddInfo();
1698 mAddScreen = screen;
1699 mAddDropPosition = position;
1700
1701 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1702 createShortcutIntent.setComponent(componentName);
1703 processShortcut(createShortcutIntent);
1704 }
1705
Joe Onoratodeb98af2010-02-19 14:59:39 -08001706 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001707 // Handle case where user selected "Applications"
1708 String applicationName = getResources().getString(R.string.group_applications);
1709 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001710
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001711 if (applicationName != null && applicationName.equals(shortcutName)) {
1712 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1713 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001714
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001715 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1716 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001717 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001718 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001719 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001720 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722 }
1723
Winson Chung24ab2f12010-09-16 14:10:47 -07001724 void processWallpaper(Intent intent) {
1725 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1726 }
1727
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1729 resetAddInfo();
1730 mAddScreen = screen;
1731 mAddDropPosition = position;
1732
1733 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1734 createFolderIntent.setComponent(componentName);
1735
1736 addLiveFolder(createFolderIntent);
1737 }
1738
1739 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001740 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001741 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001742 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001743
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001744 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001745 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001746 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001747 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750
Michael Jurka0280c3b2010-09-17 15:00:07 -07001751 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 UserFolderInfo folderInfo = new UserFolderInfo();
1753 folderInfo.title = getText(R.string.folder_name);
1754
Michael Jurka0280c3b2010-09-17 15:00:07 -07001755 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1756 final int[] cellXY = mTmpAddItemCellCoordinates;
1757 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1758 showOutOfSpaceMessage();
1759 return;
1760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761
1762 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 LauncherModel.addItemToDatabase(this, folderInfo,
1764 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001766 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767
1768 // Create the view
1769 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001770 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1771 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001772 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Romain Guycbb89e42009-06-08 15:52:54 -07001774
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001776 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001777 }
1778
Michael Jurka28750fb2010-09-24 17:43:49 -07001779 private void completeAddLiveFolder(
1780 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001781 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1782 final int[] cellXY = mTmpAddItemCellCoordinates;
1783 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1784 showOutOfSpaceMessage();
1785 return;
1786 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787
Michael Jurka0280c3b2010-09-17 15:00:07 -07001788 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789
1790 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001792 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795 }
1796
1797 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001798 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799
1800 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1801 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1802
1803 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 Intent.ShortcutIconResource iconResource = null;
1805
1806 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1807 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1808 try {
1809 iconResource = (Intent.ShortcutIconResource) extra;
1810 final PackageManager packageManager = context.getPackageManager();
1811 Resources resources = packageManager.getResourcesForApplication(
1812 iconResource.packageName);
1813 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1814 icon = resources.getDrawable(id);
1815 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001816 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 }
1818 }
1819
1820 if (icon == null) {
1821 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1822 }
1823
1824 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001825 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 info.title = name;
1827 info.iconResource = iconResource;
1828 info.uri = data.getData();
1829 info.baseIntent = baseIntent;
1830 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1831 LiveFolders.DISPLAY_MODE_GRID);
1832
1833 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001834 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001835 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836
1837 return info;
1838 }
1839
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 private void showNotifications() {
1841 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1842 if (statusBar != null) {
1843 statusBar.expand();
1844 }
1845 }
1846
1847 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001848 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001850 Intent chooser = Intent.createChooser(pickWallpaper,
1851 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001852 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1853 // Removed in Eclair MR1
1854// WallpaperManager wm = (WallpaperManager)
1855// getSystemService(Context.WALLPAPER_SERVICE);
1856// WallpaperInfo wi = wm.getWallpaperInfo();
1857// if (wi != null && wi.getSettingsActivity() != null) {
1858// LabeledIntent li = new LabeledIntent(getPackageName(),
1859// R.string.configure_wallpaper, 0);
1860// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1861// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1862// }
Mike Clerona0618e42009-10-22 13:55:21 -07001863 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 /**
1867 * Registers various content observers. The current implementation registers
1868 * only a favorites observer to keep track of the favorites applications.
1869 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001870 private void registerContentObservers() {
1871 ContentResolver resolver = getContentResolver();
1872 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1873 true, mWidgetObserver);
1874 }
1875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 @Override
1877 public boolean dispatchKeyEvent(KeyEvent event) {
1878 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1879 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001881 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001882 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001883 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001884 dumpState();
1885 return true;
1886 }
1887 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001888 }
1889 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1890 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001891 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 return true;
1893 }
1894 }
1895
1896 return super.dispatchKeyEvent(event);
1897 }
1898
Joe Onorato88ec0992009-11-19 13:16:06 -08001899 @Override
1900 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001901 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1902 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001903 } else {
1904 closeFolder();
1905 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001906 // Some launcher layouts don't have a previous and next view
1907 if (mPreviousView != null) {
1908 dismissPreview(mPreviousView);
1909 dismissPreview(mNextView);
1910 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 private void closeFolder() {
1914 Folder folder = mWorkspace.getOpenFolder();
1915 if (folder != null) {
1916 closeFolder(folder);
1917 }
1918 }
1919
1920 void closeFolder(Folder folder) {
1921 folder.getInfo().opened = false;
1922 ViewGroup parent = (ViewGroup) folder.getParent();
1923 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001924 CellLayout cl = (CellLayout) parent;
1925 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001926 if (folder instanceof DropTarget) {
1927 // Live folders aren't DropTargets.
1928 mDragController.removeDropTarget((DropTarget)folder);
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931 folder.onClose();
1932 }
1933
1934 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001935 * Re-listen when widgets are reset.
1936 */
1937 private void onAppWidgetReset() {
1938 mAppWidgetHost.startListening();
1939 }
1940
1941 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001942 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1943 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001945 private void unbindDesktopItems() {
1946 for (ItemInfo item: mDesktopItems) {
1947 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001950
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951 /**
1952 * Launches the intent referred by the clicked shortcut.
1953 *
1954 * @param v The view representing the clicked shortcut.
1955 */
1956 public void onClick(View v) {
1957 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001958 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001959 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001960 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001961 int[] pos = new int[2];
1962 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001963 intent.setSourceBounds(new Rect(pos[0], pos[1],
1964 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001965 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 } else if (tag instanceof FolderInfo) {
1967 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001968 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001969 if (mState == State.ALL_APPS) {
1970 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001971 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001972 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
1976
Michael Jurka0e260592010-06-30 17:07:39 -07001977 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001978 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001979 // clicking anywhere on the workspace causes the customization drawer to slide down
1980 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001981 return false;
1982 }
1983
Michael Jurkaaf442092010-06-10 17:01:57 -07001984 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001985 * Event handler for the search button
1986 *
1987 * @param v The view that was clicked.
1988 */
1989 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001990 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001991 // Use a custom animation for launching search
1992 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001993 }
1994
1995 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001996 * Event handler for the voice button
1997 *
1998 * @param v The view that was clicked.
1999 */
2000 public void onClickVoiceButton(View v) {
2001 startVoiceSearch();
2002 }
2003
2004 private void startVoiceSearch() {
2005 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2006 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2007 startActivity(intent);
2008 }
2009
2010 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002011 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002012 * enters home screen customization mode.
2013 *
2014 * @param v The view that was clicked.
2015 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002016 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002017 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002018 }
2019
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002020 /**
2021 * Event handler for the "grid" button that appears on the home screen, which
2022 * enters all apps mode.
2023 *
2024 * @param v The view that was clicked.
2025 */
2026 public void onClickAllAppsButton(View v) {
2027 showAllApps(true);
2028 }
2029
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002030 public void onClickAppMarketButton(View v) {
2031 if (mAppMarketIntent != null) {
2032 startActivitySafely(mAppMarketIntent, "app market");
2033 }
2034 }
2035
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002036 void startApplicationDetailsActivity(ComponentName componentName) {
2037 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002038 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2039 Uri.fromParts("package", packageName, null));
2040 startActivity(intent);
2041 }
2042
Patrick Dubroy5539af72010-09-07 15:22:01 -07002043 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2044 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2045 // System applications cannot be installed. For now, show a toast explaining that.
2046 // We may give them the option of disabling apps this way.
2047 int messageId = R.string.uninstall_system_app_text;
2048 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2049 } else {
2050 String packageName = appInfo.componentName.getPackageName();
2051 String className = appInfo.componentName.getClassName();
2052 Intent intent = new Intent(
2053 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2054 startActivity(intent);
2055 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002056 }
2057
Joe Onoratof984e852010-03-25 09:47:45 -07002058 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2060 try {
2061 startActivity(intent);
2062 } catch (ActivityNotFoundException e) {
2063 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002064 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 } catch (SecurityException e) {
2066 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002067 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002069 "or use the exported attribute for this activity. "
2070 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002073
Romain Guy8e633c52010-03-04 12:51:36 -08002074 void startActivityForResultSafely(Intent intent, int requestCode) {
2075 try {
2076 startActivityForResult(intent, requestCode);
2077 } catch (ActivityNotFoundException e) {
2078 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2079 } catch (SecurityException e) {
2080 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2081 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2082 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2083 "or use the exported attribute for this activity.", e);
2084 }
2085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086
2087 private void handleFolderClick(FolderInfo folderInfo) {
2088 if (!folderInfo.opened) {
2089 // Close any open folder
2090 closeFolder();
2091 // Open the requested folder
2092 openFolder(folderInfo);
2093 } else {
2094 // Find the open folder...
2095 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2096 int folderScreen;
2097 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002098 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // .. and close it
2100 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002101 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 // Close any folder open on the current screen
2103 closeFolder();
2104 // Pull the folder onto this screen
2105 openFolder(folderInfo);
2106 }
2107 }
2108 }
2109 }
2110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002112 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 * is animated relative to the specified View. If the View is null, no animation
2114 * is played.
2115 *
2116 * @param folderInfo The FolderInfo describing the folder to open.
2117 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002118 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 Folder openFolder;
2120
2121 if (folderInfo instanceof UserFolderInfo) {
2122 openFolder = UserFolder.fromXml(this);
2123 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002124 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 } else {
2126 return;
2127 }
2128
Joe Onorato00acb122009-08-04 16:04:30 -04002129 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 openFolder.setLauncher(this);
2131
2132 openFolder.bind(folderInfo);
2133 folderInfo.opened = true;
2134
Winson Chungaafa03c2010-06-11 17:34:16 -07002135 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 openFolder.onOpen();
2138 }
2139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002141 switch (v.getId()) {
2142 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002143 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002144 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2145 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002146 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002147 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002148 return true;
2149 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002150 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002151 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2152 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002153 showPreviews(v);
2154 }
2155 return true;
2156 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002157 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002158 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2159 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2160 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002161 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002162 return true;
2163 }
2164
Joe Onorato9c1289c2009-08-17 11:03:03 -04002165 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 return false;
2167 }
2168
2169 if (!(v instanceof CellLayout)) {
2170 v = (View) v.getParent();
2171 }
2172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 resetAddInfo();
2175 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002177 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 return true;
2179 }
2180
Michael Jurka0280c3b2010-09-17 15:00:07 -07002181 final View itemUnderLongClick = longClickCellInfo.cell;
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 if (itemUnderLongClick == null) {
2185 // User long pressed on empty space
2186 mWorkspace.setAllowLongPress(false);
2187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002189 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002190 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002191 }
2192 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002194 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002195 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2196 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002197 mAddIntersectCellX = longClickCellInfo.cellX;
2198 mAddIntersectCellY = longClickCellInfo.cellY;
2199 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200 }
2201 }
2202 }
2203 return true;
2204 }
2205
Romain Guye6b8e2f2009-11-10 11:56:55 -08002206 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002207 private void dismissPreview(final View v) {
2208 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002209 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002210 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2211 public void onDismiss() {
2212 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2213 int count = group.getChildCount();
2214 for (int i = 0; i < count; i++) {
2215 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2216 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002217 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2218 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2219
2220 v.setTag(R.id.workspace, null);
2221 v.setTag(R.id.icon, null);
2222 window.setOnDismissListener(null);
2223 }
2224 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002225 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 }
2227 v.setTag(null);
2228 }
2229
Romain Guyf8e6a802009-12-07 17:48:02 -08002230 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002231 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002232 }
2233
Romain Guya6abce82009-11-10 02:54:41 -08002234 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002235 final Resources resources = getResources();
2236 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002237
Romain Guya6abce82009-11-10 02:54:41 -08002238 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002239
Romain Guy9d31e9f2009-11-11 18:54:28 -08002240 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002241
Romain Guyf8e6a802009-12-07 17:48:02 -08002242 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002243 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002244 int extraW = (int) ((r.left + r.right) * max);
2245 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002246
2247 int aW = cell.getWidth() - extraW;
2248 float w = aW / max;
2249
2250 int width = cell.getWidth();
2251 int height = cell.getHeight();
2252 int x = cell.getLeftPadding();
2253 int y = cell.getTopPadding();
2254 width -= (x + cell.getRightPadding());
2255 height -= (y + cell.getBottomPadding());
2256
2257 float scale = w / width;
2258
2259 int count = end - start;
2260
2261 final float sWidth = width * scale;
2262 float sHeight = height * scale;
2263
Romain Guye6b8e2f2009-11-10 11:56:55 -08002264 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002265
Romain Guye6b8e2f2009-11-10 11:56:55 -08002266 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2267 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002268
2269 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002270 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002271 cell = (CellLayout) workspace.getChildAt(i);
2272
Romain Guyf8e6a802009-12-07 17:48:02 -08002273 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002274 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002275
2276 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002277 c.scale(scale, scale);
2278 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002279 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002280
Romain Guy9d31e9f2009-11-11 18:54:28 -08002281 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002282 image.setImageBitmap(bitmap);
2283 image.setTag(i);
2284 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002285 image.setOnFocusChangeListener(handler);
2286 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002287 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002288
2289 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002290 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2291
Winson Chungaafa03c2010-06-11 17:34:16 -07002292 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002293 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002294
2295 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002296 p.setContentView(preview);
2297 p.setWidth((int) (sWidth * count + extraW));
2298 p.setHeight((int) (sHeight + extraH));
2299 p.setAnimationStyle(R.style.AnimationPreview);
2300 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002301 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002302 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002304
Romain Guye6b8e2f2009-11-10 11:56:55 -08002305 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2306 public void onDismiss() {
2307 dismissPreview(anchor);
2308 }
2309 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002310
2311 anchor.setTag(p);
2312 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002313 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002314 }
2315
Romain Guy9d31e9f2009-11-11 18:54:28 -08002316 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002317 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002318
Romain Guye6b8e2f2009-11-10 11:56:55 -08002319 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002320 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002321 }
2322
2323 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002324 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002325 v.post(this);
2326 }
2327
2328 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002329 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002330 }
2331
2332 public void onFocusChange(View v, boolean hasFocus) {
2333 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002334 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002335 }
Romain Guya6abce82009-11-10 02:54:41 -08002336 }
2337 }
2338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 Workspace getWorkspace() {
2340 return mWorkspace;
2341 }
2342
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 @Override
2344 protected Dialog onCreateDialog(int id) {
2345 switch (id) {
2346 case DIALOG_CREATE_SHORTCUT:
2347 return new CreateShortcut().createDialog();
2348 case DIALOG_RENAME_FOLDER:
2349 return new RenameFolder().createDialog();
2350 }
2351
2352 return super.onCreateDialog(id);
2353 }
2354
2355 @Override
2356 protected void onPrepareDialog(int id, Dialog dialog) {
2357 switch (id) {
2358 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 break;
2360 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002361 if (mFolderInfo != null) {
2362 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2363 final CharSequence text = mFolderInfo.title;
2364 input.setText(text);
2365 input.setSelection(0, text.length());
2366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 break;
2368 }
2369 }
2370
2371 void showRenameDialog(FolderInfo info) {
2372 mFolderInfo = info;
2373 mWaitingForResult = true;
2374 showDialog(DIALOG_RENAME_FOLDER);
2375 }
2376
Michael Jurka0280c3b2010-09-17 15:00:07 -07002377 private void showAddDialog(int intersectX, int intersectY) {
2378 resetAddInfo();
2379 mAddIntersectCellX = intersectX;
2380 mAddIntersectCellY = intersectY;
2381 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002382 mWaitingForResult = true;
2383 showDialog(DIALOG_CREATE_SHORTCUT);
2384 }
2385
Joe Onoratodeb98af2010-02-19 14:59:39 -08002386 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002387 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002388 Bundle bundle = new Bundle();
2389
2390 ArrayList<String> shortcutNames = new ArrayList<String>();
2391 shortcutNames.add(getString(R.string.group_applications));
2392 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2393
2394 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2395 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2396 R.drawable.ic_launcher_application));
2397 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2398
2399 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2400 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002401 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002402 pickIntent.putExtras(bundle);
2403
Joe Onoratodeb98af2010-02-19 14:59:39 -08002404 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002405 }
2406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002407 private class RenameFolder {
2408 private EditText mInput;
2409
2410 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2412 mInput = (EditText) layout.findViewById(R.id.folder_name);
2413
2414 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2415 builder.setIcon(0);
2416 builder.setTitle(getString(R.string.rename_folder_title));
2417 builder.setCancelable(true);
2418 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2419 public void onCancel(DialogInterface dialog) {
2420 cleanup();
2421 }
2422 });
2423 builder.setNegativeButton(getString(R.string.cancel_action),
2424 new Dialog.OnClickListener() {
2425 public void onClick(DialogInterface dialog, int which) {
2426 cleanup();
2427 }
2428 }
2429 );
2430 builder.setPositiveButton(getString(R.string.rename_action),
2431 new Dialog.OnClickListener() {
2432 public void onClick(DialogInterface dialog, int which) {
2433 changeFolderName();
2434 }
2435 }
2436 );
2437 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002438
2439 final AlertDialog dialog = builder.create();
2440 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2441 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002442 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002443 mInput.requestFocus();
2444 InputMethodManager inputManager = (InputMethodManager)
2445 getSystemService(Context.INPUT_METHOD_SERVICE);
2446 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002447 }
2448 });
2449
2450 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 }
2452
2453 private void changeFolderName() {
2454 final String name = mInput.getText().toString();
2455 if (!TextUtils.isEmpty(name)) {
2456 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002457 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 mFolderInfo.title = name;
2459 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2460
Joe Onorato9c1289c2009-08-17 11:03:03 -04002461 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002462 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002463 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 } else {
2465 final FolderIcon folderIcon = (FolderIcon)
2466 mWorkspace.getViewForTag(mFolderInfo);
2467 if (folderIcon != null) {
2468 folderIcon.setText(name);
2469 getWorkspace().requestLayout();
2470 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002471 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002472 mWorkspaceLoading = true;
2473 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 }
2475 }
2476 }
2477 cleanup();
2478 }
2479
2480 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002481 dismissDialog(DIALOG_RENAME_FOLDER);
2482 mWaitingForResult = false;
2483 mFolderInfo = null;
2484 }
2485 }
2486
Daniel Sandler843e8602010-06-07 14:59:01 -04002487 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2488 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002489 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002490 }
2491
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002492 // AllAppsView.Watcher
2493 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002494 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2495 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002496 mWorkspace.setVisibility(View.GONE);
2497 }
2498 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002499
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 private void showToolbarButton(View button) {
2501 button.setAlpha(1.0f);
2502 button.setVisibility(View.VISIBLE);
2503 button.setFocusable(true);
2504 button.setClickable(true);
2505 }
2506
2507 private void hideToolbarButton(View button) {
2508 button.setAlpha(0.0f);
2509 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2510 button.setVisibility(View.INVISIBLE);
2511 button.setFocusable(false);
2512 button.setClickable(false);
2513 }
2514
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002515 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002516 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002517 *
2518 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2519 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002520 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002521 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002522 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002523 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002524 final boolean showing = show;
2525 final boolean hiding = !show;
2526
2527 final int duration = show ?
2528 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2529 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2530
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002531 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002532 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2533 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002534 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002535 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002536 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002539 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002540 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002542 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543 });
2544 seq.play(anim);
2545 } else {
2546 if (showing) {
2547 showToolbarButton(view);
2548 } else {
2549 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002550 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002551 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002552 }
2553
2554 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 * Show/hide the appropriate toolbar buttons for newState.
2556 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 *
2558 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002559 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2560 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002562 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002563 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002565 final View configureButton = findViewById(R.id.configure_button);
2566
2567 switch (newState) {
2568 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002569 hideOrShowToolbarButton(true, searchButton, showSeq);
2570 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2571 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002572 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 break;
2574 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002575 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 hideOrShowToolbarButton(false, searchButton, hideSeq);
2577 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002578 break;
2579 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002580 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002581 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002582 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002583 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 break;
2585 }
2586 }
2587
2588 /**
2589 * Helper method for the cameraZoomIn/cameraZoomOut animations
2590 * @param view The view being animated
2591 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2592 * @param scaleFactor The scale factor used for the zoom
2593 */
2594 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2595 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002596
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002597 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002598 // Set pivotY so that at the starting zoom factor, the view is partially
2599 // visible. Modifying initialHeightFactor changes how much of the view is
2600 // initially showing, and hence the perceived angle from which the view enters.
2601 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002603 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002604 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002605 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002606 }
2607 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002608
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002609 /**
2610 * Zoom the camera out from the workspace to reveal 'toView'.
2611 * Assumes that the view to show is anchored at either the very top or very bottom
2612 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002614 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002615 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002616 final Resources res = getResources();
2617 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2618 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 final boolean toAllApps = (toState == State.ALL_APPS);
2620 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002621
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622 setPivotsForZoom(toView, toState, scale);
2623
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002624 if (toAllApps) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002625 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002626 } else {
2627 mWorkspace.shrinkToTop(animated);
2628 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629
2630 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002631 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2632 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2633 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2634 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002635
2636 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002637 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002638 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002639 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 // Prepare the position
2641 toView.setTranslationX(0.0f);
2642 toView.setTranslationY(0.0f);
2643 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002644 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002645 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002646 @Override
2647 public void onAnimationEndOrCancel(Animator animation) {
2648 // If we don't set the final scale values here, if this animation is cancelled
2649 // it will have the wrong scale value and subsequent cameraPan animations will
2650 // not fix that
2651 toView.setScaleX(1.0f);
2652 toView.setScaleY(1.0f);
2653 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 });
2655
Chet Haaseb1254a62010-09-07 13:35:00 -07002656 AnimatorSet toolbarHideAnim = new AnimatorSet();
2657 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2659
2660 // toView should appear right at the end of the workspace shrink animation
2661 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2662
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002663 if (mStateAnimation != null) mStateAnimation.cancel();
2664 mStateAnimation = new AnimatorSet();
2665 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2666 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667
2668 // Show the new toolbar buttons just as the main animation is ending
2669 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002670 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2671 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 } else {
2673 toView.setTranslationX(0.0f);
2674 toView.setTranslationY(0.0f);
2675 toView.setScaleX(1.0f);
2676 toView.setScaleY(1.0f);
2677 toView.setVisibility(View.VISIBLE);
2678 hideAndShowToolbarButtons(toState, null, null);
2679 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002680 }
2681
2682 /**
2683 * Zoom the camera back into the workspace, hiding 'fromView'.
2684 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2686 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002687 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002689 Resources res = getResources();
2690 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2691 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002692 final View fromView =
2693 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2694
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002695 mCustomizePagedView.endChoiceMode();
2696 mAllAppsPagedView.endChoiceMode();
2697
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002698 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002699
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002701
2702 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002703 if (mStateAnimation != null) mStateAnimation.cancel();
2704 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002705 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2706 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2707 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2708 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002709 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2710
2711 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2712 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2713 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2714 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002715 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002716 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002718 }
2719 });
2720
Chet Haaseb1254a62010-09-07 13:35:00 -07002721 AnimatorSet toolbarHideAnim = new AnimatorSet();
2722 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002723 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2724
Adam Cohen61033d32010-11-15 18:29:44 -08002725 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726
2727 // Show the new toolbar buttons at the very end of the whole animation
2728 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2729 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002730 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2731 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 } else {
2733 fromView.setVisibility(View.GONE);
2734 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2735 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002736 }
2737
2738 /**
2739 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2740 * This is the transition used on xlarge screens to go between all apps and
2741 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002742 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2743 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2744 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002745 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002747 final Resources res = getResources();
2748 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002749 final int workspaceHeight = mWorkspace.getHeight();
2750
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002751 final boolean fromAllApps = (fromState == State.ALL_APPS);
2752 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2753 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002754
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002755 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2756 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2757 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2758 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002759
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002760 mCustomizePagedView.endChoiceMode();
2761 mAllAppsPagedView.endChoiceMode();
2762
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002763 if (toState == State.ALL_APPS) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002764 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002765 } else {
2766 mWorkspace.shrinkToTop(animated);
2767 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002768
2769 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002770 if (mStateAnimation != null) mStateAnimation.cancel();
2771 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002772 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002773 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002774 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775 toView.setVisibility(View.VISIBLE);
2776 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002777 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002778 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002779 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002780 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002781 fromView.setVisibility(View.GONE);
2782 }
2783 });
2784
Chet Haaseb1254a62010-09-07 13:35:00 -07002785 AnimatorSet toolbarHideAnim = new AnimatorSet();
2786 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002787 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2788
Chet Haase472b2812010-10-14 07:02:04 -07002789 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2790 fromViewStartY, fromViewEndY);
2791 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002792 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2793 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2794 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2795 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2796 );
Chet Haase472b2812010-10-14 07:02:04 -07002797 fromAnim.setDuration(duration);
2798 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799
2800 // Show the new toolbar buttons just as the main animation is ending
2801 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002802 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2803 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002804 } else {
2805 fromView.setY(fromViewEndY);
2806 fromView.setVisibility(View.GONE);
2807 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002808 toView.setScaleX(1.0f);
2809 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002810 toView.setVisibility(View.VISIBLE);
2811 hideAndShowToolbarButtons(toState, null, null);
2812 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002813 }
2814
2815 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002816 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002817 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002818 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002819
Michael Jurka79212d82010-07-30 16:36:20 -07002820 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002821 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002822 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002823 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002825 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002826 } else {
2827 mAllAppsGrid.zoom(1.0f, animated);
2828 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002829
Romain Guyc16fea72010-03-12 17:17:56 -08002830 ((View) mAllAppsGrid).setFocusable(true);
2831 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002832
Joe Onorato7c312c12009-08-13 21:36:53 -07002833 // TODO: fade these two too
2834 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002835
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002836 // Change the state *after* we've called all the transition code
2837 mState = State.ALL_APPS;
2838 }
2839
2840
2841 void showWorkspace(boolean animated) {
2842 showWorkspace(animated, null);
2843 }
2844
2845 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002846 if (layout != null) {
2847 // always animated, but that's ok since we never specify a layout and
2848 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002849 mWorkspace.unshrink(layout);
2850 } else {
2851 mWorkspace.unshrink(animated);
2852 }
2853 if (mState == State.ALL_APPS) {
2854 closeAllApps(animated);
2855 } else if (mState == State.CUSTOMIZE) {
2856 hideCustomizationDrawer(animated);
2857 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002858
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002859 // Change the state *after* we've called all the transition code
2860 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002861 }
2862
Joe Onoratob2061212009-11-24 16:13:54 -05002863 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002864 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002865 * - Home from workspace
2866 * - from center screen
2867 * - from other screens
2868 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002869 * - from center screen
2870 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002871 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002872 * - from center screen
2873 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002874 * - Launch app from workspace and quit
2875 * - with back
2876 * - with home
2877 * - Launch app from all apps and quit
2878 * - with back
2879 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002880 * - Go to a screen that's not the default, then all
2881 * apps, and launch and app, and go back
2882 * - with back
2883 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002884 * - On workspace, long press power and go back
2885 * - with back
2886 * - with home
2887 * - On all apps, long press power and go back
2888 * - with back
2889 * - with home
2890 * - On workspace, power off
2891 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002892 * - Launch an app and turn off the screen while in that app
2893 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002894 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002895 * - From all apps
2896 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002897 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2898 * - From all apps
2899 * - From the center workspace
2900 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002901 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002902 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002903 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002904 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002905 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002906 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002907 } else {
2908 mAllAppsGrid.zoom(0.0f, animated);
2909 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002910 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002911 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002912 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002913 }
2914
Joe Onorato7c312c12009-08-13 21:36:53 -07002915 void lockAllApps() {
2916 // TODO
2917 }
2918
2919 void unlockAllApps() {
2920 // TODO
2921 }
2922
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002923 // Show the customization drawer (only exists in x-large configuration)
2924 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002926 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002927 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002928 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002929 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002930 // Change the state *after* we've called all the transition code
2931 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002932 }
2933
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002934 // Hide the customization drawer (only exists in x-large configuration)
2935 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002936 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002937 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002938 }
2939 }
2940
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002941 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2942 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2943 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002944 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002945 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002946
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002947 void onWorkspaceClick(CellLayout layout) {
2948 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002949 }
2950
Michael Jurka4ef207b2010-11-29 17:05:45 -08002951 // if successful in getting icon, return it; otherwise, set button to use default drawable
2952 private Drawable updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07002953 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2954 ImageView button = (ImageView) findViewById(buttonId);
2955 Drawable toolbarIcon = null;
2956 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002957 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002958 // Look for the toolbar icon specified in the activity meta-data
2959 Bundle metaData = packageManager.getActivityInfo(
2960 activityName, PackageManager.GET_META_DATA).metaData;
2961 if (metaData != null) {
2962 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2963 if (iconResId != 0) {
2964 Resources res = packageManager.getResourcesForActivity(activityName);
2965 toolbarIcon = res.getDrawable(iconResId);
2966 }
2967 }
2968 } catch (NameNotFoundException e) {
2969 // Do nothing
2970 }
2971 // If we were unable to find the icon via the meta-data, use a generic one
2972 if (toolbarIcon == null) {
2973 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002974 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002975 } else {
2976 button.setImageDrawable(toolbarIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002977 return toolbarIcon;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002978 }
2979 }
2980
Michael Jurka4ef207b2010-11-29 17:05:45 -08002981 private void updateButtonWithDrawable(int buttonId, Drawable d) {
2982 ImageView button = (ImageView) findViewById(buttonId);
2983 button.setImageDrawable(d);
2984 }
2985
Michael Jurka0423dcf2010-10-05 14:56:18 -07002986 private void updateGlobalSearchIcon() {
2987 if (LauncherApplication.isScreenXLarge()) {
2988 final SearchManager searchManager =
2989 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2990 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002991 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002992 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07002993 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002994 } else {
2995 findViewById(R.id.search_button).setVisibility(View.GONE);
2996 }
2997 }
2998 }
2999
Michael Jurka4ef207b2010-11-29 17:05:45 -08003000 private void updateGlobalSearchIcon(Drawable d) {
3001 updateButtonWithDrawable(R.id.search_button, d);
3002 }
3003
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003004 private void updateVoiceSearchIcon() {
3005 if (LauncherApplication.isScreenXLarge()) {
3006 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3007 ComponentName activityName = intent.resolveActivity(getPackageManager());
3008 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003009 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003010 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3011 } else {
3012 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003013 }
3014 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003015 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003016
Michael Jurka4ef207b2010-11-29 17:05:45 -08003017 private void updateVoiceSearchIcon(Drawable d) {
3018 updateButtonWithDrawable(R.id.voice_button, d);
3019 }
3020
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003021 /**
3022 * Sets the app market icon (shown when all apps is visible on x-large screens)
3023 */
3024 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003025 if (LauncherApplication.isScreenXLarge()) {
3026 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3027 // Find the app market activity by resolving an intent.
3028 // (If multiple app markets are installed, it will return the ResolverActivity.)
3029 ComponentName activityName = intent.resolveActivity(getPackageManager());
3030 if (activityName != null) {
3031 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003032 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003033 R.id.market_button, activityName, R.drawable.app_market_generic);
3034 }
3035 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003036 }
3037
Michael Jurka4ef207b2010-11-29 17:05:45 -08003038 private void updateAppMarketIcon(Drawable d) {
3039 updateButtonWithDrawable(R.id.market_button, d);
3040 }
3041
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003042 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 * Displays the shortcut creation dialog and launches, if necessary, the
3044 * appropriate activity.
3045 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003046 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003047 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3048 DialogInterface.OnShowListener {
3049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003052 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3056 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003057 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 builder.setInverseBackgroundForced(true);
3060
3061 AlertDialog dialog = builder.create();
3062 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003063 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003064 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003066 return dialog;
3067 }
3068
3069 public void onCancel(DialogInterface dialog) {
3070 mWaitingForResult = false;
3071 cleanup();
3072 }
3073
Romain Guycbb89e42009-06-08 15:52:54 -07003074 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003075 }
3076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003077 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003078 try {
3079 dismissDialog(DIALOG_CREATE_SHORTCUT);
3080 } catch (Exception e) {
3081 // An exception is thrown if the dialog is not visible, which is fine
3082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 }
3084
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003085 /**
3086 * Handle the action clicked in the "Add to home" dialog.
3087 */
3088 public void onClick(DialogInterface dialog, int which) {
3089 Resources res = getResources();
3090 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003091
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003092 switch (which) {
3093 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003094 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003095 break;
3096 }
Romain Guycbb89e42009-06-08 15:52:54 -07003097
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003098 case AddAdapter.ITEM_APPWIDGET: {
3099 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003100
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003101 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3102 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003103 // start the pick activity
3104 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3105 break;
3106 }
Romain Guycbb89e42009-06-08 15:52:54 -07003107
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003108 case AddAdapter.ITEM_LIVE_FOLDER: {
3109 // Insert extra item to handle inserting folder
3110 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003111
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003112 ArrayList<String> shortcutNames = new ArrayList<String>();
3113 shortcutNames.add(res.getString(R.string.group_folder));
3114 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003115
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003116 ArrayList<ShortcutIconResource> shortcutIcons =
3117 new ArrayList<ShortcutIconResource>();
3118 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3119 R.drawable.ic_launcher_folder));
3120 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3121
3122 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3123 pickIntent.putExtra(Intent.EXTRA_INTENT,
3124 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3125 pickIntent.putExtra(Intent.EXTRA_TITLE,
3126 getText(R.string.title_select_live_folder));
3127 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003128
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003129 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3130 break;
3131 }
3132
3133 case AddAdapter.ITEM_WALLPAPER: {
3134 startWallpaper();
3135 break;
3136 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003137 }
3138 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003139
3140 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003141 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003142 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 }
3144
3145 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003146 * Receives notifications when applications are added/removed.
3147 */
3148 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3149 @Override
3150 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003151 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003152 String reason = intent.getStringExtra("reason");
3153 if (!"homekey".equals(reason)) {
3154 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003155 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003156 animate = false;
3157 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003158 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003159 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003160 }
3161 }
3162
3163 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003164 * Receives notifications whenever the appwidgets are reset.
3165 */
3166 private class AppWidgetResetObserver extends ContentObserver {
3167 public AppWidgetResetObserver() {
3168 super(new Handler());
3169 }
3170
3171 @Override
3172 public void onChange(boolean selfChange) {
3173 onAppWidgetReset();
3174 }
3175 }
3176
3177 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003178 * If the activity is currently paused, signal that we need to re-run the loader
3179 * in onResume.
3180 *
3181 * This needs to be called from incoming places where resources might have been loaded
3182 * while we are paused. That is becaues the Configuration might be wrong
3183 * when we're not running, and if it comes back to what it was when we
3184 * were paused, we are not restarted.
3185 *
3186 * Implementation of the method from LauncherModel.Callbacks.
3187 *
3188 * @return true if we are currently paused. The caller might be able to
3189 * skip some work in that case since we will come back again.
3190 */
3191 public boolean setLoadOnResume() {
3192 if (mPaused) {
3193 Log.i(TAG, "setLoadOnResume");
3194 mOnResumeNeedsLoad = true;
3195 return true;
3196 } else {
3197 return false;
3198 }
3199 }
3200
3201 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003204 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003205 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003206 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003207 } else {
3208 return SCREEN_COUNT / 2;
3209 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003211
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003212 void setAllAppsPagedView(PagedView view) {
3213 mAllAppsPagedView = view;
3214 }
3215
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 /**
3217 * Refreshes the shortcuts shown on the workspace.
3218 *
3219 * Implementation of the method from LauncherModel.Callbacks.
3220 */
3221 public void startBinding() {
3222 final Workspace workspace = mWorkspace;
3223 int count = workspace.getChildCount();
3224 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003225 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3227 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003228
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 if (DEBUG_USER_INTERFACE) {
3230 android.widget.Button finishButton = new android.widget.Button(this);
3231 finishButton.setText("Finish");
3232 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3233
3234 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3235 public void onClick(View v) {
3236 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003237 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 }
3240 }
3241
3242 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 * Bind the items start-end from the list.
3244 *
3245 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3248
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003249 setLoadOnResume();
3250
Joe Onorato9c1289c2009-08-17 11:03:03 -04003251 final Workspace workspace = mWorkspace;
3252
3253 for (int i=start; i<end; i++) {
3254 final ItemInfo item = shortcuts.get(i);
3255 mDesktopItems.add(item);
3256 switch (item.itemType) {
3257 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3258 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003259 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003260 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3261 false);
3262 break;
3263 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3264 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003265 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003266 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003267 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3268 false);
3269 break;
3270 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3271 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3272 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003273 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274 (LiveFolderInfo) item);
3275 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3276 false);
3277 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279 }
3280
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003282 }
3283
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 /**
3285 * Implementation of the method from LauncherModel.Callbacks.
3286 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003287 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003288 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003289 sFolders.clear();
3290 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003291 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292
3293 /**
3294 * Add the views for a widget to the workspace.
3295 *
3296 * Implementation of the method from LauncherModel.Callbacks.
3297 */
3298 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003299 setLoadOnResume();
3300
Daniel Sandler843e8602010-06-07 14:59:01 -04003301 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3302 if (DEBUG_WIDGETS) {
3303 Log.d(TAG, "bindAppWidget: " + item);
3304 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 final Workspace workspace = mWorkspace;
3306
3307 final int appWidgetId = item.appWidgetId;
3308 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003309 if (DEBUG_WIDGETS) {
3310 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3311 }
3312
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3314
Joe Onorato9c1289c2009-08-17 11:03:03 -04003315 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3316 item.hostView.setTag(item);
3317
3318 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3319 item.cellY, item.spanX, item.spanY, false);
3320
Adam Cohended9f8d2010-11-03 13:25:16 -07003321 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3322
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 workspace.requestLayout();
3324
3325 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003326
3327 if (DEBUG_WIDGETS) {
3328 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3329 + (SystemClock.uptimeMillis()-start) + "ms");
3330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 }
3332
3333 /**
3334 * Callback saying that there aren't any more items to bind.
3335 *
3336 * Implementation of the method from LauncherModel.Callbacks.
3337 */
3338 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003339 setLoadOnResume();
3340
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 if (mSavedState != null) {
3342 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003343 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344 }
3345
3346 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3347 if (userFolders != null) {
3348 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003349 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003350 if (info != null) {
3351 openFolder(info);
3352 }
3353 }
3354 final Folder openFolder = mWorkspace.getOpenFolder();
3355 if (openFolder != null) {
3356 openFolder.requestFocus();
3357 }
3358 }
3359
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 mSavedState = null;
3361 }
3362
3363 if (mSavedInstanceState != null) {
3364 super.onRestoreInstanceState(mSavedInstanceState);
3365 mSavedInstanceState = null;
3366 }
3367
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 mWorkspaceLoading = false;
3369 }
3370
3371 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003372 * Updates the icons on the launcher that are affected by changes to the package list
3373 * on the device.
3374 */
3375 private void updateIconsAffectedByPackageManagerChanges() {
3376 updateAppMarketIcon();
3377 updateGlobalSearchIcon();
3378 updateVoiceSearchIcon();
3379 }
3380
3381 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 * Add the icons for all apps.
3383 *
3384 * Implementation of the method from LauncherModel.Callbacks.
3385 */
3386 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003387 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003388 if (mCustomizePagedView != null) {
3389 mCustomizePagedView.setApps(apps);
3390 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003391 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 }
3393
3394 /**
3395 * A package was installed.
3396 *
3397 * Implementation of the method from LauncherModel.Callbacks.
3398 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003399 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003400 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003402 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003403 if (mCustomizePagedView != null) {
3404 mCustomizePagedView.addApps(apps);
3405 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003406 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003407 }
3408
3409 /**
3410 * A package was updated.
3411 *
3412 * Implementation of the method from LauncherModel.Callbacks.
3413 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003414 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003415 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003416 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003417 mWorkspace.updateShortcuts(apps);
3418 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003419 if (mCustomizePagedView != null) {
3420 mCustomizePagedView.updateApps(apps);
3421 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003422 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 }
3424
3425 /**
3426 * A package was uninstalled.
3427 *
3428 * Implementation of the method from LauncherModel.Callbacks.
3429 */
Joe Onorato36115782010-06-17 13:28:48 -04003430 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003432 if (permanent) {
3433 mWorkspace.removeItems(apps);
3434 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003435 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003436 if (mCustomizePagedView != null) {
3437 mCustomizePagedView.removeApps(apps);
3438 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003439 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003440 }
Joe Onoratobe386092009-11-17 17:32:16 -08003441
3442 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003443 * A number of packages were updated.
3444 */
3445 public void bindPackagesUpdated() {
3446 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003447 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003448 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003449 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003450 }
3451
3452 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003453 * Prints out out state for debugging.
3454 */
3455 public void dumpState() {
3456 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003457 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003458 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3459 Log.d(TAG, "mRestoring=" + mRestoring);
3460 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3461 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3462 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003463 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003464 mModel.dumpState();
3465 mAllAppsGrid.dumpState();
3466 Log.d(TAG, "END launcher2 dump state");
3467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003468}