blob: 992c506d5e4916b2b6b308c9dec3f0b6c04c6904 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurkaaf91de02010-11-23 16:23:58 -080020import com.android.common.Search;
21import com.android.launcher.R;
22import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070026import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070034import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
36import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070039import android.content.ClipData;
40import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040041import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080042import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Context;
44import android.content.DialogInterface;
45import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070046import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070047import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070048import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070050import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070051import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070053import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040054import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080055import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080057import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080059import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070060import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040061import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070062import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080069import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070070import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070071import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080072import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.Selection;
74import android.text.SpannableStringBuilder;
75import android.text.TextUtils;
76import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070077import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080079import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.KeyEvent;
81import android.view.LayoutInflater;
82import android.view.Menu;
83import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070084import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070088import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
93import android.widget.LinearLayout;
94import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070095import android.widget.TabHost;
Winson Chung49767ae2010-11-29 14:48:30 -080096import android.widget.TabWidget;
Winson Chung68846fd2010-10-29 11:00:27 -070097import android.widget.TextView;
98import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -070099import android.widget.TabHost.OnTabChangeListener;
100import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurkaaf91de02010-11-23 16:23:58 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
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. */
Michael Jurkad3ef3062010-11-23 16:23:58 -0800179 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW,
180 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700181 private State mState = State.WORKSPACE;
182 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183
Joe Onorato9c1289c2009-08-17 11:03:03 -0400184 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800187 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800189 private final BroadcastReceiver mCloseSystemDialogsReceiver
190 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800191 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
192
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 private LayoutInflater mInflater;
194
Joe Onorato00acb122009-08-04 16:04:30 -0400195 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700197
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700198 private AppWidgetManager mAppWidgetManager;
199 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700200
Michael Jurka0280c3b2010-09-17 15:00:07 -0700201 private int mAddScreen = -1;
202 private int mAddIntersectCellX = -1;
203 private int mAddIntersectCellY = -1;
204 private int[] mAddDropPosition;
205 private int[] mTmpAddItemCellCoordinates = new int[2];
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private FolderInfo mFolderInfo;
208
Joe Onorato7c312c12009-08-13 21:36:53 -0700209 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700210 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700211 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700212 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700213 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700214
215 private PagedView mAllAppsPagedView = null;
216 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700227 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800228
229 private Bundle mSavedInstanceState;
230
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800232 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800233 private boolean mUserPresent = true;
234 private boolean mVisible = false;
235 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400236
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700237 private static LocaleConfiguration sLocaleConfiguration = null;
238
Romain Guy84f296c2009-11-04 15:00:44 -0800239 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700240
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700241 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700242
Romain Guy1fbc1c82009-11-09 20:43:08 -0800243 private ImageView mPreviousView;
244 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500245
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400247 private String[] mHotseatConfig = null;
248 private Intent[] mHotseats = null;
249 private Drawable[] mHotseatIcons = null;
250 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400251
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700252 private Intent mAppMarketIntent = null;
253
Adam Cohended9f8d2010-11-03 13:25:16 -0700254 // Related to the auto-advancing of widgets
255 private final int ADVANCE_MSG = 1;
256 private final int mAdvanceInterval = 20000;
257 private final int mAdvanceStagger = 250;
258 private long mAutoAdvanceSentTime;
259 private long mAutoAdvanceTimeLeft = -1;
260 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
261 new HashMap<View, AppWidgetProviderInfo>();
262
Michael Jurka4ef207b2010-11-29 17:05:45 -0800263 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800264 private static Drawable.ConstantState sGlobalSearchIcon;
265 private static Drawable.ConstantState sVoiceSearchIcon;
266 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 @Override
269 protected void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700271
Winson Chungaafa03c2010-06-11 17:34:16 -0700272 if (LauncherApplication.isInPlaceRotationEnabled()) {
273 // hide the status bar (temporary until we get the status bar design figured out)
274 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
275 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
276 }
277
Joe Onorato0589f0f2010-02-08 13:44:00 -0800278 LauncherApplication app = ((LauncherApplication)getApplication());
279 mModel = app.setLauncher(this);
280 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400281 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700283
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700284 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700285 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
286 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200289 android.os.Debug.startMethodTracing(
290 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 }
292
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400293 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 checkForLocaleChange();
295 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 setContentView(R.layout.launcher);
Winson Chung88127032010-12-13 12:11:33 -0800297 mHomeCustomizationDrawer = (TabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700298 if (mHomeCustomizationDrawer != null) {
299 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700300
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700302 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
303 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 TabContentFactory contentFactory = new TabContentFactory() {
305 public View createTabContent(String tag) {
306 return mCustomizePagedView;
307 }
308 };
309
Winson Chung49767ae2010-11-29 14:48:30 -0800310
311 TextView tabView;
312 TabWidget tabWidget = (TabWidget)
313 mHomeCustomizationDrawer.findViewById(com.android.internal.R.id.tabs);
314
315 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
316 tabView.setText(getString(R.string.widgets_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700317 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800318 .setIndicator(tabView).setContent(contentFactory));
319 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
320 tabView.setText(getString(R.string.applications_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700321 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800322 .setIndicator(tabView).setContent(contentFactory));
323 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
324 tabView.setText(getString(R.string.wallpapers_tab_label));
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800326 .setIndicator(tabView).setContent(contentFactory));
327 tabView = (TextView) mInflater.inflate(R.layout.tab_widget_indicator, tabWidget, false);
328 tabView.setText(getString(R.string.shortcuts_tab_label));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700329 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
Winson Chung49767ae2010-11-29 14:48:30 -0800330 .setIndicator(tabView).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
332 public void onTabChanged(String tabId) {
333 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800334 final Resources res = getResources();
335 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700336 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700337 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700338 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700339 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700340 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700341 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700342 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700343 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
344 if (tag == WIDGETS_TAG) {
345 mCustomizePagedView.setCustomizationFilter(
346 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700347 } else if (tag == APPLICATIONS_TAG) {
348 mCustomizePagedView.setCustomizationFilter(
349 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700350 } else if (tag == WALLPAPERS_TAG) {
351 mCustomizePagedView.setCustomizationFilter(
352 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700353 } else if (tag == SHORTCUTS_TAG) {
354 mCustomizePagedView.setCustomizationFilter(
355 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700356 }
357
358 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700359 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700360 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700361 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700362 alphaAnim.start();
363 }
364 });
365 alphaAnim.start();
366 }
367 });
Michael Jurka946ad472010-07-09 18:05:18 -0700368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 setupViews();
370
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800371 registerContentObservers();
372
Joe Onorato7c312c12009-08-13 21:36:53 -0700373 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 mSavedState = savedInstanceState;
376 restoreState(mSavedState);
377
378 if (PROFILE_STARTUP) {
379 android.os.Debug.stopMethodTracing();
380 }
381
382 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400383 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 }
385
386 // For handling default keys
387 mDefaultKeySsb = new SpannableStringBuilder();
388 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800389
390 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
391 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800392
393 // If we have a saved version of these external icons, we load them up immediately
394 if (LauncherApplication.isScreenXLarge()) {
395 if (sGlobalSearchIcon != null) {
396 updateGlobalSearchIcon(sGlobalSearchIcon);
397 }
398 if (sVoiceSearchIcon != null) {
399 updateVoiceSearchIcon(sVoiceSearchIcon);
400 }
401 if (sAppMarketIcon != null) {
402 updateAppMarketIcon(sAppMarketIcon);
403 }
404 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 }
Romain Guycbb89e42009-06-08 15:52:54 -0700406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 if (sLocaleConfiguration == null) {
409 new AsyncTask<Void, Void, LocaleConfiguration>() {
410 @Override
411 protected LocaleConfiguration doInBackground(Void... unused) {
412 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
413 readConfiguration(Launcher.this, localeConfiguration);
414 return localeConfiguration;
415 }
416
417 @Override
418 protected void onPostExecute(LocaleConfiguration result) {
419 sLocaleConfiguration = result;
420 checkForLocaleChange(); // recursive, but now with a locale configuration
421 }
422 }.execute();
423 return;
424 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700425
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800426 final Configuration configuration = getResources().getConfiguration();
427
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 final String locale = configuration.locale.toString();
430
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700431 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 final int mcc = configuration.mcc;
433
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700434 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 final int mnc = configuration.mnc;
436
Romain Guy84f296c2009-11-04 15:00:44 -0800437 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438
Romain Guy84f296c2009-11-04 15:00:44 -0800439 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700440 sLocaleConfiguration.locale = locale;
441 sLocaleConfiguration.mcc = mcc;
442 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700443
Joe Onorato0589f0f2010-02-08 13:44:00 -0800444 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400445 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700446
447 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
448 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700449 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700450 public void run() {
451 writeConfiguration(Launcher.this, localeConfiguration);
452 }
453 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700454 }
455 }
456
457 private static class LocaleConfiguration {
458 public String locale;
459 public int mcc = -1;
460 public int mnc = -1;
461 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700462
Romain Guy98d01652009-06-30 16:21:04 -0700463 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
464 DataInputStream in = null;
465 try {
466 in = new DataInputStream(context.openFileInput(PREFERENCES));
467 configuration.locale = in.readUTF();
468 configuration.mcc = in.readInt();
469 configuration.mnc = in.readInt();
470 } catch (FileNotFoundException e) {
471 // Ignore
472 } catch (IOException e) {
473 // Ignore
474 } finally {
475 if (in != null) {
476 try {
477 in.close();
478 } catch (IOException e) {
479 // Ignore
480 }
481 }
482 }
483 }
484
485 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
486 DataOutputStream out = null;
487 try {
488 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
489 out.writeUTF(configuration.locale);
490 out.writeInt(configuration.mcc);
491 out.writeInt(configuration.mnc);
492 out.flush();
493 } catch (FileNotFoundException e) {
494 // Ignore
495 } catch (IOException e) {
496 //noinspection ResultOfMethodCallIgnored
497 context.getFileStreamPath(PREFERENCES).delete();
498 } finally {
499 if (out != null) {
500 try {
501 out.close();
502 } catch (IOException e) {
503 // Ignore
504 }
505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 }
507 }
508
509 static int getScreen() {
510 synchronized (sLock) {
511 return sScreen;
512 }
513 }
514
515 static void setScreen(int screen) {
516 synchronized (sLock) {
517 sScreen = screen;
518 }
519 }
520
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700522 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523
524 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700525 // TODO: Put back when we decide about scrolling the wallpaper
526 // boolean isPortrait = display.getWidth() < display.getHeight();
527 // final int width = isPortrait ? display.getWidth() : display.getHeight();
528 // final int height = isPortrait ? display.getHeight() : display.getWidth();
529 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
530 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
532
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 // Note: This doesn't do all the client-id magic that BrowserProvider does
534 // in Browser. (http://b/2425179)
535 private Uri getDefaultBrowserUri() {
536 String url = getString(R.string.default_browser_url);
537 if (url.indexOf("{CID}") != -1) {
538 url = url.replace("{CID}", "android-google");
539 }
540 return Uri.parse(url);
541 }
542
543 // Load the Intent templates from arrays.xml to populate the hotseats. For
544 // each Intent, if it resolves to a single app, use that as the launch
545 // intent & use that app's label as the contentDescription. Otherwise,
546 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400547 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400548 if (mHotseatConfig == null) {
549 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
550 if (mHotseatConfig.length > 0) {
551 mHotseats = new Intent[mHotseatConfig.length];
552 mHotseatLabels = new CharSequence[mHotseatConfig.length];
553 mHotseatIcons = new Drawable[mHotseatConfig.length];
554 } else {
555 mHotseats = null;
556 mHotseatIcons = null;
557 mHotseatLabels = null;
558 }
559
560 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
561 for (int i=0; i<mHotseatConfig.length; i++) {
562 // load icon for this slot; currently unrelated to the actual activity
563 try {
564 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
565 } catch (ArrayIndexOutOfBoundsException ex) {
566 Log.w(TAG, "Missing hotseat_icons array item #" + i);
567 mHotseatIcons[i] = null;
568 }
569 }
570 hotseatIconDrawables.recycle();
571 }
572
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400574 for (int i=0; i<mHotseatConfig.length; i++) {
575 Intent intent = null;
576 if (mHotseatConfig[i].equals("*BROWSER*")) {
577 // magic value meaning "launch user's default web browser"
578 // replace it with a generic web request so we can see if there is indeed a default
579 String defaultUri = getString(R.string.default_browser_url);
580 intent = new Intent(
581 Intent.ACTION_VIEW,
582 ((defaultUri != null)
583 ? Uri.parse(defaultUri)
584 : getDefaultBrowserUri())
585 ).addCategory(Intent.CATEGORY_BROWSABLE);
586 // note: if the user launches this without a default set, she
587 // will always be taken to the default URL above; this is
588 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700589 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400590 // URL is unavoidably sent to the chosen app.
591 } else {
592 try {
593 intent = Intent.parseUri(mHotseatConfig[i], 0);
594 } catch (java.net.URISyntaxException ex) {
595 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
596 // bogus; leave intent=null
597 }
598 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700599
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 if (intent == null) {
601 mHotseats[i] = null;
602 mHotseatLabels[i] = getText(R.string.activity_not_found);
603 continue;
604 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400605
606 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700607 Log.d(TAG, "loadHotseats: hotseat " + i
608 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400609 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400610 + "]");
611 }
612
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400613 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
614 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700615 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400616 Log.d(TAG, "Best match for intent: " + bestMatch);
617 Log.d(TAG, "All matches: ");
618 for (ResolveInfo ri : allMatches) {
619 Log.d(TAG, " --> " + ri);
620 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400621 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400622 // did this resolve to a single app, or the resolver?
623 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700624 // can't find any activity to handle this. let's leave the
625 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400626 // to launch.
627 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400628
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400629 // set accessibility text to "Not installed"
630 mHotseatLabels[i] = getText(R.string.activity_not_found);
631 } else {
632 boolean found = false;
633 for (ResolveInfo ri : allMatches) {
634 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
635 && bestMatch.activityInfo.applicationInfo.packageName
636 .equals(ri.activityInfo.applicationInfo.packageName)) {
637 found = true;
638 break;
639 }
640 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700641
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400642 if (!found) {
643 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
644 // the bestMatch is probably the ResolveActivity, meaning the
645 // user has not yet selected a default
646 // so: we'll keep the original intent for now
647 mHotseats[i] = intent;
648
649 // set the accessibility text to "Select shortcut"
650 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
651 } else {
652 // we have an app!
653 // now reconstruct the intent to launch it through the front
654 // door
655 ComponentName com = new ComponentName(
656 bestMatch.activityInfo.applicationInfo.packageName,
657 bestMatch.activityInfo.name);
658 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
659
660 // load the app label for accessibility
661 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
662 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400663 }
664
665 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700666 Log.d(TAG, "loadHotseats: hotseat " + i
667 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400668 + ((mHotseats[i] == null)
669 ? "null"
670 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400671 + "] label=[" + mHotseatLabels[i]
672 + "]"
673 );
674 }
675 }
676 }
677
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800678 @Override
679 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700680 mWaitingForResult = false;
681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 // The pattern used here is that a user PICKs a specific application,
683 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
686 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700687
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 switch (requestCode) {
690 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700691 completeAddApplication(
692 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 break;
694 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800695 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 break;
697 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700698 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 break;
700 case REQUEST_PICK_LIVE_FOLDER:
701 addLiveFolder(data);
702 break;
703 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700704 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700706 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700707 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700709 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700710 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700711 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700713 case REQUEST_PICK_WALLPAPER:
714 // We just wanted the activity result here so we can clear mWaitingForResult
715 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
Romain Guy18042c82009-11-06 11:44:55 -0800717 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
718 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
719 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 // Clean up the appWidgetId if we canceled
721 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
722 if (appWidgetId != -1) {
723 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800724 }
725 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 }
727
728 @Override
729 protected void onResume() {
730 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800731 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700732 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400733 mWorkspaceLoading = true;
734 mModel.startLoader(this, true);
735 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700736 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700738 // When we resume Launcher, a different Activity might be responsible for the app
739 // market intent, so refresh the icon
740 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742
743 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744 protected void onPause() {
745 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700746 // Some launcher layouts don't have a previous and next view
747 if (mPreviousView != null) {
748 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700749 }
750 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700751 dismissPreview(mNextView);
752 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700753 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800754 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700755 }
Romain Guycbb89e42009-06-08 15:52:54 -0700756
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700757 @Override
758 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400759 // Flag the loader to stop early before switching
760 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800761 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800762 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700763 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800765 // We can't hide the IME if it was forced open. So don't bother
766 /*
767 @Override
768 public void onWindowFocusChanged(boolean hasFocus) {
769 super.onWindowFocusChanged(hasFocus);
770
771 if (hasFocus) {
772 final InputMethodManager inputManager = (InputMethodManager)
773 getSystemService(Context.INPUT_METHOD_SERVICE);
774 WindowManager.LayoutParams lp = getWindow().getAttributes();
775 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
776 android.os.Handler()) {
777 protected void onReceiveResult(int resultCode, Bundle resultData) {
778 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
779 }
780 });
781 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
782 }
783 }
784 */
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 private boolean acceptFilter() {
787 final InputMethodManager inputManager = (InputMethodManager)
788 getSystemService(Context.INPUT_METHOD_SERVICE);
789 return !inputManager.isFullscreenMode();
790 }
791
792 @Override
793 public boolean onKeyDown(int keyCode, KeyEvent event) {
794 boolean handled = super.onKeyDown(keyCode, event);
795 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
796 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
797 keyCode, event);
798 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700799 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700800 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700801 // showSearchDialog()
802 // If there are multiple keystrokes before the search dialog takes focus,
803 // onSearchRequested() will be called for every keystroke,
804 // but it is idempotent, so it's fine.
805 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 }
807 }
808
Joe Onorato8a9625e2010-01-28 15:55:35 -0800809 // Eat the long press event so the keyboard doesn't come up.
810 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
811 return true;
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 return handled;
815 }
816
Karl Rosaen138a0412009-04-23 19:00:21 -0700817 private String getTypedText() {
818 return mDefaultKeySsb.toString();
819 }
820
821 private void clearTypedText() {
822 mDefaultKeySsb.clear();
823 mDefaultKeySsb.clearSpans();
824 Selection.setSelection(mDefaultKeySsb, 0);
825 }
826
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700828 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
829 * State
830 */
831 private static State intToState(int stateOrdinal) {
832 State state = State.WORKSPACE;
833 final State[] stateValues = State.values();
834 for (int i = 0; i < stateValues.length; i++) {
835 if (stateValues[i].ordinal() == stateOrdinal) {
836 state = stateValues[i];
837 break;
838 }
839 }
840 return state;
841 }
842
843 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 * Restores the previous state, if it exists.
845 *
846 * @param savedState The previous state.
847 */
848 private void restoreState(Bundle savedState) {
849 if (savedState == null) {
850 return;
851 }
852
Michael Jurka883f55b2010-10-21 15:47:14 -0700853 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
854
855 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800856 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700857 } else if (state == State.CUSTOMIZE) {
858 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800859 }
860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
862 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700863 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
865
866 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700869 mAddScreen = addScreen;
870 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
871 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800872 mRestoring = true;
873 }
874
875 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
876 if (renameFolder) {
877 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700878 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 mRestoring = true;
880 }
881 }
882
883 /**
884 * Finds all the views we need and configure them properly.
885 */
886 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700887 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400888
Romain Guyb1b69f52009-08-10 15:10:15 -0700889 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400890 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891
Joe Onorato7c312c12009-08-13 21:36:53 -0700892 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700893 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700894 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800895 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700896 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700897 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700898
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700899 if (LauncherApplication.isScreenXLarge()) {
900 // They need to be INVISIBLE initially so that they will be measured in the layout.
901 // Otherwise the animations are messed up when we show them for the first time.
902 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
903 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
904 }
905
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700907
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500909 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700910
Joe Onorato7c312c12009-08-13 21:36:53 -0700911 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
912 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700914 View handleView = findViewById(R.id.all_apps_button);
915 if (handleView != null && handleView instanceof HandleView) {
916 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700917 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700918 mHandleView.setLauncher(this);
919 mHandleView.setOnClickListener(this);
920 mHandleView.setOnLongClickListener(this);
921 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800922
Winson Chung80baf5a2010-08-09 16:03:15 -0700923 if (mCustomizePagedView != null) {
924 mCustomizePagedView.setLauncher(this);
925 mCustomizePagedView.setDragController(dragController);
926 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700927 } else {
928 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
929 hotseatLeft.setContentDescription(mHotseatLabels[0]);
930 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
931 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
932 hotseatRight.setContentDescription(mHotseatLabels[1]);
933 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400934
Michael Jurkaaf442092010-06-10 17:01:57 -0700935 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
936 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800937
Michael Jurkaaf442092010-06-10 17:01:57 -0700938 Drawable previous = mPreviousView.getDrawable();
939 Drawable next = mNextView.getDrawable();
940 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941
Michael Jurkaaf442092010-06-10 17:01:57 -0700942 mPreviousView.setHapticFeedbackEnabled(false);
943 mPreviousView.setOnLongClickListener(this);
944 mNextView.setHapticFeedbackEnabled(false);
945 mNextView.setOnLongClickListener(this);
946 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800947
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400949 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951
952 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400953 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700954 int deleteZoneHandleId;
955 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700956 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700957 } else {
958 deleteZoneHandleId = R.id.all_apps_button_cluster;
959 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700960 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700961 dragController.addDragListener(deleteZone);
962
Adam Cohencdc30d52010-12-01 15:09:47 -0800963 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
964 if (allAppsDeleteZone != null) {
965 allAppsDeleteZone.setLauncher(this);
966 allAppsDeleteZone.setDragController(dragController);
967 allAppsDeleteZone.setDragAndDropEnabled(false);
968 dragController.addDragListener(allAppsDeleteZone);
969 dragController.addDropTarget(allAppsDeleteZone);
970 }
971
972 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
973 findViewById(R.id.all_apps_info_target);
974 if (allAppsInfoTarget != null) {
975 allAppsInfoTarget.setLauncher(this);
976 dragController.addDragListener(allAppsInfoTarget);
977 allAppsInfoTarget.setDragColor(getResources().getColor(R.color.app_info_filter));
978 allAppsInfoTarget.setDragAndDropEnabled(false);
979 View marketButton = findViewById(R.id.market_button);
980 if (marketButton != null) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800981 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 Jurkad3ef3062010-11-23 16:23:58 -08001116 int[] touchXY = null;
1117 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1118 touchXY = mAddDropPosition;
1119 }
1120 boolean foundCellSpan = false;
1121 if (touchXY != null) {
1122 // when dragging and dropping, just find the closest free spot
1123 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1124 int[] result = screenLayout.findNearestVacantArea(
1125 touchXY[0], touchXY[1], 1, 1, cellXY);
1126 foundCellSpan = (result != null);
1127 } else {
1128 foundCellSpan = layout.findCellForSpanThatIntersects(
1129 cellXY, 1, 1, intersectCellX, intersectCellY);
1130 }
1131
1132 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 showOutOfSpaceMessage();
1134 return;
1135 }
1136
1137 final ShortcutInfo info = mModel.addShortcut(
1138 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139
1140 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 }
1144 }
1145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001147 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001149 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001150 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001153 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001154
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001155 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1157 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001158
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001160 // We have saved the position to which the widget was dragged-- this really only matters
1161 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001162 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001163
1164 // For now, we don't save the coordinate where we dropped the icon because we're not
1165 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1166 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001167 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001168 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001169 touchXY = mAddDropPosition;
1170 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001171 boolean foundCellSpan = false;
1172 if (touchXY != null) {
1173 // when dragging and dropping, just find the closest free spot
1174 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1175 int[] result = screenLayout.findNearestVacantArea(
1176 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001177 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001178 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001179 // if we long pressed on an empty cell to bring up a menu,
1180 // make sure we intersect the empty cell
1181 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1182 // findCellForSpanThatIntersects will just ignore them
1183 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1184 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001185 }
1186
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001188 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001189 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001190 return;
1191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 // Build Launcher-specific widget info and save to database
1194 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 launcherInfo.spanX = spanXY[0];
1196 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001197
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 LauncherModel.addItemToDatabase(this, launcherInfo,
1199 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001200 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
1202 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001203 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001204
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001207
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001208 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001210
Michael Jurka0280c3b2010-09-17 15:00:07 -07001211 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001212 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001213
1214 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Adam Cohended9f8d2010-11-03 13:25:16 -07001218 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1219 @Override
1220 public void onReceive(Context context, Intent intent) {
1221 final String action = intent.getAction();
1222 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1223 mUserPresent = false;
1224 updateRunning();
1225 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1226 mUserPresent = true;
1227 updateRunning();
1228 }
1229 }
1230 };
1231
1232 @Override
1233 public void onAttachedToWindow() {
1234 super.onAttachedToWindow();
1235
1236 // Listen for broadcasts related to user-presence
1237 final IntentFilter filter = new IntentFilter();
1238 filter.addAction(Intent.ACTION_SCREEN_OFF);
1239 filter.addAction(Intent.ACTION_USER_PRESENT);
1240 registerReceiver(mReceiver, filter);
1241
Adam Cohend113e0c2010-11-11 10:48:05 -08001242 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 mVisible = true;
1244 }
1245
1246 @Override
1247 public void onDetachedFromWindow() {
1248 super.onDetachedFromWindow();
1249 mVisible = false;
1250
Adam Cohend113e0c2010-11-11 10:48:05 -08001251 if (mAttached) {
1252 unregisterReceiver(mReceiver);
1253 mAttached = false;
1254 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001255 updateRunning();
1256 }
1257
1258 public void onWindowVisibilityChanged(int visibility) {
1259 mVisible = visibility == View.VISIBLE;
1260 updateRunning();
1261 }
1262
1263 private void sendAdvanceMessage(long delay) {
1264 mHandler.removeMessages(ADVANCE_MSG);
1265 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1266 mHandler.sendMessageDelayed(msg, delay);
1267 mAutoAdvanceSentTime = System.currentTimeMillis();
1268 }
1269
1270 private void updateRunning() {
1271 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1272 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1273 mAutoAdvanceRunning = autoAdvanceRunning;
1274 if (autoAdvanceRunning) {
1275 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1276 sendAdvanceMessage(delay);
1277 } else {
1278 if (!mWidgetsToAdvance.isEmpty()) {
1279 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1280 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1281 }
1282 mHandler.removeMessages(ADVANCE_MSG);
1283 }
1284 }
1285 }
1286
1287 private final Handler mHandler = new Handler() {
1288 @Override
1289 public void handleMessage(Message msg) {
1290 if (msg.what == ADVANCE_MSG) {
1291 int i = 0;
1292 for (View key: mWidgetsToAdvance.keySet()) {
1293 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1294 final int delay = mAdvanceStagger * i;
1295 if (v instanceof Advanceable) {
1296 postDelayed(new Runnable() {
1297 public void run() {
1298 ((Advanceable) v).advance();
1299 }
1300 }, delay);
1301 }
1302 i++;
1303 }
1304 sendAdvanceMessage(mAdvanceInterval);
1305 }
1306 }
1307 };
1308
1309 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1310 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1311 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1312 if (v instanceof Advanceable) {
1313 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1314 ((Advanceable) v).willBeAdvancedByHost();
1315 updateRunning();
1316 }
1317 }
1318
1319 void removeWidgetToAutoAdvance(View hostView) {
1320 if (mWidgetsToAdvance.containsKey(hostView)) {
1321 mWidgetsToAdvance.remove(hostView);
1322 updateRunning();
1323 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001324 }
1325
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1327 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001328 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001329 launcherInfo.hostView = null;
1330 }
1331
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 void showOutOfSpaceMessage() {
1333 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1334 }
1335
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001336 public LauncherAppWidgetHost getAppWidgetHost() {
1337 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
Romain Guycbb89e42009-06-08 15:52:54 -07001339
Winson Chunga9abd0e2010-10-27 17:18:37 -07001340 public LauncherModel getModel() {
1341 return mModel;
1342 }
1343
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001344 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001345 getWindow().closeAllPanels();
1346
1347 try {
1348 dismissDialog(DIALOG_CREATE_SHORTCUT);
1349 // Unlock the workspace if the dialog was showing
1350 } catch (Exception e) {
1351 // An exception is thrown if the dialog is not visible, which is fine
1352 }
1353
1354 try {
1355 dismissDialog(DIALOG_RENAME_FOLDER);
1356 // Unlock the workspace if the dialog was showing
1357 } catch (Exception e) {
1358 // An exception is thrown if the dialog is not visible, which is fine
1359 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001360
1361 // Whatever we were doing is hereby canceled.
1362 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001363 }
1364
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 @Override
1366 protected void onNewIntent(Intent intent) {
1367 super.onNewIntent(intent);
1368
1369 // Close the menu
1370 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001371 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001373
Joe Onorato14f122b2009-11-19 14:06:36 -08001374 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1375 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001376
Michael Jurka4cb37242010-08-09 21:05:32 -07001377 // in all these cases, only animate if we're already on home
1378 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001379 mWorkspace.unshrink(alreadyOnHome);
1380 }
1381 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001382 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001383 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1384 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001385 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001386 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001387
Joe Onorato3a8820b2009-11-10 15:06:42 -08001388 final View v = getWindow().peekDecorView();
1389 if (v != null && v.getWindowToken() != null) {
1390 InputMethodManager imm = (InputMethodManager)getSystemService(
1391 INPUT_METHOD_SERVICE);
1392 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 }
1394 }
1395 }
1396
1397 @Override
1398 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1399 // Do not call super here
1400 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001401
Winson Chung43b119d2010-12-06 16:45:05 -08001402 // Restore the current AllApps drawer tab
1403 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1404 String cur = savedInstanceState.getString("allapps_currentTab");
1405 if (cur != null) {
1406 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1407 tabhost.setCurrentTabByTag(cur);
1408 }
1409 }
1410
1411 // Restore the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001412 if (mHomeCustomizationDrawer != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001413 String cur = savedInstanceState.getString("customize_currentTab");
Michael Jurka946ad472010-07-09 18:05:18 -07001414 if (cur != null) {
1415 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1416 }
1417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 @Override
1421 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001422 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423
1424 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1425 if (folders.size() > 0) {
1426 final int count = folders.size();
1427 long[] ids = new long[count];
1428 for (int i = 0; i < count; i++) {
1429 final FolderInfo info = folders.get(i).getInfo();
1430 ids[i] = info.id;
1431 }
1432 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1433 } else {
1434 super.onSaveInstanceState(outState);
1435 }
1436
Michael Jurka883f55b2010-10-21 15:47:14 -07001437 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438
Michael Jurka0280c3b2010-09-17 15:00:07 -07001439 if (mAddScreen > -1 && mWaitingForResult) {
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1441 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1442 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
1445 if (mFolderInfo != null && mWaitingForResult) {
1446 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1447 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1448 }
Michael Jurka946ad472010-07-09 18:05:18 -07001449
Winson Chung43b119d2010-12-06 16:45:05 -08001450 // Save the current AllApps drawer tab
1451 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1452 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1453 String currentTabTag = tabhost.getCurrentTabTag();
1454 if (currentTabTag != null) {
1455 outState.putString("allapps_currentTab", currentTabTag);
1456 }
1457 }
1458
1459 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001460 if (mHomeCustomizationDrawer != null) {
1461 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1462 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001463 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001464 }
1465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 @Override
1469 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001471
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001473 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001475 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 TextKeyListener.getInstance().release();
1479
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481
Joe Onorato9c1289c2009-08-17 11:03:03 -04001482 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001483
1484 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001485
Patrick Dubroyab962b72010-07-26 12:10:10 -07001486 // Some launcher layouts don't have a previous and next view
1487 if (mPreviousView != null) {
1488 dismissPreview(mPreviousView);
1489 }
1490 if (mNextView != null) {
1491 dismissPreview(mNextView);
1492 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001493
1494 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 }
1496
1497 @Override
1498 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001499 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 super.startActivityForResult(intent, requestCode);
1501 }
1502
1503 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001504 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001506
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001507 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001508
Karl Rosaen138a0412009-04-23 19:00:21 -07001509 if (initialQuery == null) {
1510 // Use any text typed in the launcher as the initial query
1511 initialQuery = getTypedText();
1512 clearTypedText();
1513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 if (appSearchData == null) {
1515 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001516 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Karl Rosaen138a0412009-04-23 19:00:21 -07001519 final SearchManager searchManager =
1520 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001521 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001522 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
1525 @Override
1526 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001527 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001528 return false;
1529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
1531 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1534 .setIcon(android.R.drawable.ic_menu_add)
1535 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001536 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1537 .setIcon(android.R.drawable.ic_menu_manage)
1538 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001539 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 .setIcon(android.R.drawable.ic_menu_gallery)
1541 .setAlphabeticShortcut('W');
1542 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1543 .setIcon(android.R.drawable.ic_search_category_default)
1544 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1545 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1546 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1547 .setAlphabeticShortcut('N');
1548
1549 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001550 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1551 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
1553 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1554 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1555 .setIntent(settings);
1556
1557 return true;
1558 }
1559
1560 @Override
1561 public boolean onPrepareOptionsMenu(Menu menu) {
1562 super.onPrepareOptionsMenu(menu);
1563
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001564 // If all apps is animating, don't show the menu, because we don't know
1565 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001566 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001567 return false;
1568 }
1569
1570 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001571 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001572 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1573 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1574
1575 // Disable add if the workspace is full.
1576 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001577 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1578 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 return true;
1582 }
1583
1584 @Override
1585 public boolean onOptionsItemSelected(MenuItem item) {
1586 switch (item.getItemId()) {
1587 case MENU_ADD:
1588 addItems();
1589 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001590 case MENU_MANAGE_APPS:
1591 manageApps();
1592 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 case MENU_WALLPAPER_SETTINGS:
1594 startWallpaper();
1595 return true;
1596 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001597 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 return true;
1599 case MENU_NOTIFICATIONS:
1600 showNotifications();
1601 return true;
1602 }
1603
1604 return super.onOptionsItemSelected(item);
1605 }
Romain Guycbb89e42009-06-08 15:52:54 -07001606
Karl Rosaen138a0412009-04-23 19:00:21 -07001607 /**
1608 * Indicates that we want global search for this activity by setting the globalSearch
1609 * argument for {@link #startSearch} to true.
1610 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001612 @Override
1613 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001614 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001615 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001616 }
1617
Joe Onorato9c1289c2009-08-17 11:03:03 -04001618 public boolean isWorkspaceLocked() {
1619 return mWorkspaceLoading || mWaitingForResult;
1620 }
1621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001623 if (LauncherApplication.isScreenXLarge()) {
1624 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001625 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001626 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001627 }
1628 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001629 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001630 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001631 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
1633
Michael Jurka0280c3b2010-09-17 15:00:07 -07001634 private void resetAddInfo() {
1635 mAddScreen = -1;
1636 mAddIntersectCellX = -1;
1637 mAddIntersectCellY = -1;
1638 mAddDropPosition = null;
1639 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001640
Winson Chung55cef262010-10-28 14:14:18 -07001641 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001642 resetAddInfo();
1643 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001644 mAddDropPosition = position;
1645
Michael Jurkaaf442092010-06-10 17:01:57 -07001646 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001647 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1648 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001649 }
1650
Winson Chung7ad01412010-09-27 11:33:03 -07001651 private void manageApps() {
1652 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1653 }
1654
Michael Jurkaaf442092010-06-10 17:01:57 -07001655 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001656 // TODO: catch bad widget exception when sent
1657 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001658 // TODO: Is this log message meaningful?
1659 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001660 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001661 }
1662
Winson Chung55cef262010-10-28 14:14:18 -07001663 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001664 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665
Bjorn Bringert7984c942009-12-09 15:38:25 +00001666 if (appWidget.configure != null) {
1667 // Launch over to configure widget, if needed
1668 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1669 intent.setComponent(appWidget.configure);
1670 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001671 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001672 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1673 intent.putExtra(
1674 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1675 info.mimeType);
1676
1677 final String mimeType = info.mimeType;
1678 final ClipData clipData = (ClipData) info.configurationData;
1679 final ClipDescription clipDesc = clipData.getDescription();
1680 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1681 if (clipDesc.getMimeType(i).equals(mimeType)) {
1682 final ClipData.Item item = clipData.getItem(i);
1683 final CharSequence stringData = item.getText();
1684 final Uri uriData = item.getUri();
1685 final Intent intentData = item.getIntent();
1686 final String key =
1687 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1688 if (uriData != null) {
1689 intent.putExtra(key, uriData);
1690 } else if (intentData != null) {
1691 intent.putExtra(key, intentData);
1692 } else if (stringData != null) {
1693 intent.putExtra(key, stringData);
1694 }
1695 break;
1696 }
1697 }
1698 }
Winson Chung55cef262010-10-28 14:14:18 -07001699 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001700
Romain Guy8e633c52010-03-04 12:51:36 -08001701 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001703 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001704 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
1706 }
Romain Guycbb89e42009-06-08 15:52:54 -07001707
Michael Jurka0280c3b2010-09-17 15:00:07 -07001708 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1709 resetAddInfo();
1710 mAddScreen = screen;
1711 mAddDropPosition = position;
1712
1713 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1714 createShortcutIntent.setComponent(componentName);
1715 processShortcut(createShortcutIntent);
1716 }
1717
Joe Onoratodeb98af2010-02-19 14:59:39 -08001718 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001719 // Handle case where user selected "Applications"
1720 String applicationName = getResources().getString(R.string.group_applications);
1721 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001722
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001723 if (applicationName != null && applicationName.equals(shortcutName)) {
1724 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1725 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001726
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001727 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1728 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001729 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001730 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001732 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001733 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 }
1735
Winson Chung24ab2f12010-09-16 14:10:47 -07001736 void processWallpaper(Intent intent) {
1737 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1738 }
1739
Michael Jurka0280c3b2010-09-17 15:00:07 -07001740 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1741 resetAddInfo();
1742 mAddScreen = screen;
1743 mAddDropPosition = position;
1744
1745 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1746 createFolderIntent.setComponent(componentName);
1747
1748 addLiveFolder(createFolderIntent);
1749 }
1750
1751 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001753 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001754 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001755
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001757 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001758 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001759 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 }
1762
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 UserFolderInfo folderInfo = new UserFolderInfo();
1765 folderInfo.title = getText(R.string.folder_name);
1766
Michael Jurka0280c3b2010-09-17 15:00:07 -07001767 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1768 final int[] cellXY = mTmpAddItemCellCoordinates;
1769 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1770 showOutOfSpaceMessage();
1771 return;
1772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773
1774 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001775 LauncherModel.addItemToDatabase(this, folderInfo,
1776 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001777 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001778 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779
1780 // Create the view
1781 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001782 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1783 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001784 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
Romain Guycbb89e42009-06-08 15:52:54 -07001786
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001788 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001789 }
1790
Michael Jurka28750fb2010-09-24 17:43:49 -07001791 private void completeAddLiveFolder(
1792 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001793 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1794 final int[] cellXY = mTmpAddItemCellCoordinates;
1795 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1796 showOutOfSpaceMessage();
1797 return;
1798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801
1802 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001804 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001805 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 }
1807 }
1808
1809 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001810 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
1812 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1813 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1814
1815 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001816 Intent.ShortcutIconResource iconResource = null;
1817
1818 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1819 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1820 try {
1821 iconResource = (Intent.ShortcutIconResource) extra;
1822 final PackageManager packageManager = context.getPackageManager();
1823 Resources resources = packageManager.getResourcesForApplication(
1824 iconResource.packageName);
1825 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1826 icon = resources.getDrawable(id);
1827 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001828 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830 }
1831
1832 if (icon == null) {
1833 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1834 }
1835
1836 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001837 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 info.title = name;
1839 info.iconResource = iconResource;
1840 info.uri = data.getData();
1841 info.baseIntent = baseIntent;
1842 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1843 LiveFolders.DISPLAY_MODE_GRID);
1844
1845 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001846 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001847 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848
1849 return info;
1850 }
1851
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 private void showNotifications() {
1853 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1854 if (statusBar != null) {
1855 statusBar.expand();
1856 }
1857 }
1858
1859 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001860 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001862 Intent chooser = Intent.createChooser(pickWallpaper,
1863 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001864 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1865 // Removed in Eclair MR1
1866// WallpaperManager wm = (WallpaperManager)
1867// getSystemService(Context.WALLPAPER_SERVICE);
1868// WallpaperInfo wi = wm.getWallpaperInfo();
1869// if (wi != null && wi.getSettingsActivity() != null) {
1870// LabeledIntent li = new LabeledIntent(getPackageName(),
1871// R.string.configure_wallpaper, 0);
1872// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1873// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1874// }
Mike Clerona0618e42009-10-22 13:55:21 -07001875 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 }
1877
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001878 /**
1879 * Registers various content observers. The current implementation registers
1880 * only a favorites observer to keep track of the favorites applications.
1881 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001882 private void registerContentObservers() {
1883 ContentResolver resolver = getContentResolver();
1884 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1885 true, mWidgetObserver);
1886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 public boolean dispatchKeyEvent(KeyEvent event) {
1890 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1891 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001893 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001894 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001895 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001896 dumpState();
1897 return true;
1898 }
1899 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001900 }
1901 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1902 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001903 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 return true;
1905 }
1906 }
1907
1908 return super.dispatchKeyEvent(event);
1909 }
1910
Joe Onorato88ec0992009-11-19 13:16:06 -08001911 @Override
1912 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001913 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1914 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001915 } else {
1916 closeFolder();
1917 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001918 // Some launcher layouts don't have a previous and next view
1919 if (mPreviousView != null) {
1920 dismissPreview(mPreviousView);
1921 dismissPreview(mNextView);
1922 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001923 }
1924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 private void closeFolder() {
1926 Folder folder = mWorkspace.getOpenFolder();
1927 if (folder != null) {
1928 closeFolder(folder);
1929 }
1930 }
1931
1932 void closeFolder(Folder folder) {
1933 folder.getInfo().opened = false;
1934 ViewGroup parent = (ViewGroup) folder.getParent();
1935 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001936 CellLayout cl = (CellLayout) parent;
1937 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001938 if (folder instanceof DropTarget) {
1939 // Live folders aren't DropTargets.
1940 mDragController.removeDropTarget((DropTarget)folder);
1941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943 folder.onClose();
1944 }
1945
1946 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001947 * Re-listen when widgets are reset.
1948 */
1949 private void onAppWidgetReset() {
1950 mAppWidgetHost.startListening();
1951 }
1952
1953 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001954 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1955 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001956 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001957 private void unbindDesktopItems() {
1958 for (ItemInfo item: mDesktopItems) {
1959 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 /**
1964 * Launches the intent referred by the clicked shortcut.
1965 *
1966 * @param v The view representing the clicked shortcut.
1967 */
1968 public void onClick(View v) {
1969 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001970 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001972 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001973 int[] pos = new int[2];
1974 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001975 intent.setSourceBounds(new Rect(pos[0], pos[1],
1976 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001977 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 } else if (tag instanceof FolderInfo) {
1979 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001980 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001981 if (mState == State.ALL_APPS) {
1982 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001983 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001984 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 }
1987 }
1988
Michael Jurka0e260592010-06-30 17:07:39 -07001989 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001990 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001991 // clicking anywhere on the workspace causes the customization drawer to slide down
1992 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001993 return false;
1994 }
1995
Michael Jurkaaf442092010-06-10 17:01:57 -07001996 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001997 * Event handler for the search button
1998 *
1999 * @param v The view that was clicked.
2000 */
2001 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002002 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002003 // Use a custom animation for launching search
2004 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002005 }
2006
2007 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002008 * Event handler for the voice button
2009 *
2010 * @param v The view that was clicked.
2011 */
2012 public void onClickVoiceButton(View v) {
2013 startVoiceSearch();
2014 }
2015
2016 private void startVoiceSearch() {
2017 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2018 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2019 startActivity(intent);
2020 }
2021
2022 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002023 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002024 * enters home screen customization mode.
2025 *
2026 * @param v The view that was clicked.
2027 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002028 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002029 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002030 }
2031
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002032 /**
2033 * Event handler for the "grid" button that appears on the home screen, which
2034 * enters all apps mode.
2035 *
2036 * @param v The view that was clicked.
2037 */
2038 public void onClickAllAppsButton(View v) {
2039 showAllApps(true);
2040 }
2041
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002042 public void onClickAppMarketButton(View v) {
2043 if (mAppMarketIntent != null) {
2044 startActivitySafely(mAppMarketIntent, "app market");
2045 }
2046 }
2047
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002048 void startApplicationDetailsActivity(ComponentName componentName) {
2049 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002050 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2051 Uri.fromParts("package", packageName, null));
2052 startActivity(intent);
2053 }
2054
Patrick Dubroy5539af72010-09-07 15:22:01 -07002055 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2056 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2057 // System applications cannot be installed. For now, show a toast explaining that.
2058 // We may give them the option of disabling apps this way.
2059 int messageId = R.string.uninstall_system_app_text;
2060 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2061 } else {
2062 String packageName = appInfo.componentName.getPackageName();
2063 String className = appInfo.componentName.getClassName();
2064 Intent intent = new Intent(
2065 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2066 startActivity(intent);
2067 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002068 }
2069
Joe Onoratof984e852010-03-25 09:47:45 -07002070 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2072 try {
2073 startActivity(intent);
2074 } catch (ActivityNotFoundException e) {
2075 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002076 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 } catch (SecurityException e) {
2078 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002079 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002081 "or use the exported attribute for this activity. "
2082 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
2084 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002085
Romain Guy8e633c52010-03-04 12:51:36 -08002086 void startActivityForResultSafely(Intent intent, int requestCode) {
2087 try {
2088 startActivityForResult(intent, requestCode);
2089 } catch (ActivityNotFoundException e) {
2090 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2091 } catch (SecurityException e) {
2092 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2093 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2094 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2095 "or use the exported attribute for this activity.", e);
2096 }
2097 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098
2099 private void handleFolderClick(FolderInfo folderInfo) {
2100 if (!folderInfo.opened) {
2101 // Close any open folder
2102 closeFolder();
2103 // Open the requested folder
2104 openFolder(folderInfo);
2105 } else {
2106 // Find the open folder...
2107 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2108 int folderScreen;
2109 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002110 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 // .. and close it
2112 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002113 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 // Close any folder open on the current screen
2115 closeFolder();
2116 // Pull the folder onto this screen
2117 openFolder(folderInfo);
2118 }
2119 }
2120 }
2121 }
2122
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002124 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 * is animated relative to the specified View. If the View is null, no animation
2126 * is played.
2127 *
2128 * @param folderInfo The FolderInfo describing the folder to open.
2129 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002130 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 Folder openFolder;
2132
2133 if (folderInfo instanceof UserFolderInfo) {
2134 openFolder = UserFolder.fromXml(this);
2135 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002136 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 } else {
2138 return;
2139 }
2140
Joe Onorato00acb122009-08-04 16:04:30 -04002141 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 openFolder.setLauncher(this);
2143
2144 openFolder.bind(folderInfo);
2145 folderInfo.opened = true;
2146
Winson Chungaafa03c2010-06-11 17:34:16 -07002147 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 openFolder.onOpen();
2150 }
2151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002153 switch (v.getId()) {
2154 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002155 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002156 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2157 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002158 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002159 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002160 return true;
2161 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002162 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002163 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2164 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002165 showPreviews(v);
2166 }
2167 return true;
2168 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002169 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002170 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2171 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2172 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002173 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002174 return true;
2175 }
2176
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 return false;
2179 }
2180
2181 if (!(v instanceof CellLayout)) {
2182 v = (View) v.getParent();
2183 }
2184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185
Michael Jurka0280c3b2010-09-17 15:00:07 -07002186 resetAddInfo();
2187 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002189 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 return true;
2191 }
2192
Michael Jurka0280c3b2010-09-17 15:00:07 -07002193 final View itemUnderLongClick = longClickCellInfo.cell;
2194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002196 if (itemUnderLongClick == null) {
2197 // User long pressed on empty space
2198 mWorkspace.setAllowLongPress(false);
2199 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2200 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002201 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002202 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
2204 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002205 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002207 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2208 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002209 mAddIntersectCellX = longClickCellInfo.cellX;
2210 mAddIntersectCellY = longClickCellInfo.cellY;
2211 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
2213 }
2214 }
2215 return true;
2216 }
2217
Romain Guye6b8e2f2009-11-10 11:56:55 -08002218 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002219 private void dismissPreview(final View v) {
2220 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002221 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002222 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2223 public void onDismiss() {
2224 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2225 int count = group.getChildCount();
2226 for (int i = 0; i < count; i++) {
2227 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2228 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002229 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2230 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2231
2232 v.setTag(R.id.workspace, null);
2233 v.setTag(R.id.icon, null);
2234 window.setOnDismissListener(null);
2235 }
2236 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002237 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002238 }
2239 v.setTag(null);
2240 }
2241
Romain Guyf8e6a802009-12-07 17:48:02 -08002242 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002243 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002244 }
2245
Romain Guya6abce82009-11-10 02:54:41 -08002246 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002247 final Resources resources = getResources();
2248 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249
Romain Guya6abce82009-11-10 02:54:41 -08002250 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002251
Romain Guy9d31e9f2009-11-11 18:54:28 -08002252 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002253
Romain Guyf8e6a802009-12-07 17:48:02 -08002254 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002255 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002256 int extraW = (int) ((r.left + r.right) * max);
2257 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002258
2259 int aW = cell.getWidth() - extraW;
2260 float w = aW / max;
2261
2262 int width = cell.getWidth();
2263 int height = cell.getHeight();
2264 int x = cell.getLeftPadding();
2265 int y = cell.getTopPadding();
2266 width -= (x + cell.getRightPadding());
2267 height -= (y + cell.getBottomPadding());
2268
2269 float scale = w / width;
2270
2271 int count = end - start;
2272
2273 final float sWidth = width * scale;
2274 float sHeight = height * scale;
2275
Romain Guye6b8e2f2009-11-10 11:56:55 -08002276 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002277
Romain Guye6b8e2f2009-11-10 11:56:55 -08002278 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2279 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002280
2281 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002282 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002283 cell = (CellLayout) workspace.getChildAt(i);
2284
Romain Guyf8e6a802009-12-07 17:48:02 -08002285 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002286 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002287
2288 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002289 c.scale(scale, scale);
2290 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002291 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002292
Romain Guy9d31e9f2009-11-11 18:54:28 -08002293 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002294 image.setImageBitmap(bitmap);
2295 image.setTag(i);
2296 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002297 image.setOnFocusChangeListener(handler);
2298 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002299 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002300
2301 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002302 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2303
Winson Chungaafa03c2010-06-11 17:34:16 -07002304 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002305 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002306
2307 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002308 p.setContentView(preview);
2309 p.setWidth((int) (sWidth * count + extraW));
2310 p.setHeight((int) (sHeight + extraH));
2311 p.setAnimationStyle(R.style.AnimationPreview);
2312 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002313 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002314 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002315 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002316
Romain Guye6b8e2f2009-11-10 11:56:55 -08002317 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2318 public void onDismiss() {
2319 dismissPreview(anchor);
2320 }
2321 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002322
2323 anchor.setTag(p);
2324 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002325 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002326 }
2327
Romain Guy9d31e9f2009-11-11 18:54:28 -08002328 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002329 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002330
Romain Guye6b8e2f2009-11-10 11:56:55 -08002331 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002332 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002333 }
2334
2335 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002336 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002337 v.post(this);
2338 }
2339
2340 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002341 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002342 }
2343
2344 public void onFocusChange(View v, boolean hasFocus) {
2345 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002346 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002347 }
Romain Guya6abce82009-11-10 02:54:41 -08002348 }
2349 }
2350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002351 Workspace getWorkspace() {
2352 return mWorkspace;
2353 }
2354
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002355 @Override
2356 protected Dialog onCreateDialog(int id) {
2357 switch (id) {
2358 case DIALOG_CREATE_SHORTCUT:
2359 return new CreateShortcut().createDialog();
2360 case DIALOG_RENAME_FOLDER:
2361 return new RenameFolder().createDialog();
2362 }
2363
2364 return super.onCreateDialog(id);
2365 }
2366
2367 @Override
2368 protected void onPrepareDialog(int id, Dialog dialog) {
2369 switch (id) {
2370 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002371 break;
2372 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002373 if (mFolderInfo != null) {
2374 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2375 final CharSequence text = mFolderInfo.title;
2376 input.setText(text);
2377 input.setSelection(0, text.length());
2378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002379 break;
2380 }
2381 }
2382
2383 void showRenameDialog(FolderInfo info) {
2384 mFolderInfo = info;
2385 mWaitingForResult = true;
2386 showDialog(DIALOG_RENAME_FOLDER);
2387 }
2388
Michael Jurka0280c3b2010-09-17 15:00:07 -07002389 private void showAddDialog(int intersectX, int intersectY) {
2390 resetAddInfo();
2391 mAddIntersectCellX = intersectX;
2392 mAddIntersectCellY = intersectY;
2393 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 mWaitingForResult = true;
2395 showDialog(DIALOG_CREATE_SHORTCUT);
2396 }
2397
Joe Onoratodeb98af2010-02-19 14:59:39 -08002398 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002399 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002400 Bundle bundle = new Bundle();
2401
2402 ArrayList<String> shortcutNames = new ArrayList<String>();
2403 shortcutNames.add(getString(R.string.group_applications));
2404 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2405
2406 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2407 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2408 R.drawable.ic_launcher_application));
2409 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2410
2411 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2412 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002413 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002414 pickIntent.putExtras(bundle);
2415
Joe Onoratodeb98af2010-02-19 14:59:39 -08002416 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002417 }
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 private class RenameFolder {
2420 private EditText mInput;
2421
2422 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002423 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2424 mInput = (EditText) layout.findViewById(R.id.folder_name);
2425
2426 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2427 builder.setIcon(0);
2428 builder.setTitle(getString(R.string.rename_folder_title));
2429 builder.setCancelable(true);
2430 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2431 public void onCancel(DialogInterface dialog) {
2432 cleanup();
2433 }
2434 });
2435 builder.setNegativeButton(getString(R.string.cancel_action),
2436 new Dialog.OnClickListener() {
2437 public void onClick(DialogInterface dialog, int which) {
2438 cleanup();
2439 }
2440 }
2441 );
2442 builder.setPositiveButton(getString(R.string.rename_action),
2443 new Dialog.OnClickListener() {
2444 public void onClick(DialogInterface dialog, int which) {
2445 changeFolderName();
2446 }
2447 }
2448 );
2449 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002450
2451 final AlertDialog dialog = builder.create();
2452 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2453 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002454 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002455 mInput.requestFocus();
2456 InputMethodManager inputManager = (InputMethodManager)
2457 getSystemService(Context.INPUT_METHOD_SERVICE);
2458 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002459 }
2460 });
2461
2462 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 }
2464
2465 private void changeFolderName() {
2466 final String name = mInput.getText().toString();
2467 if (!TextUtils.isEmpty(name)) {
2468 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002469 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 mFolderInfo.title = name;
2471 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2472
Joe Onorato9c1289c2009-08-17 11:03:03 -04002473 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002474 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002475 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 } else {
2477 final FolderIcon folderIcon = (FolderIcon)
2478 mWorkspace.getViewForTag(mFolderInfo);
2479 if (folderIcon != null) {
2480 folderIcon.setText(name);
2481 getWorkspace().requestLayout();
2482 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002483 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 mWorkspaceLoading = true;
2485 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486 }
2487 }
2488 }
2489 cleanup();
2490 }
2491
2492 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 dismissDialog(DIALOG_RENAME_FOLDER);
2494 mWaitingForResult = false;
2495 mFolderInfo = null;
2496 }
2497 }
2498
Daniel Sandler843e8602010-06-07 14:59:01 -04002499 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2500 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002501 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002502 }
2503
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002504 // AllAppsView.Watcher
2505 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002506 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2507 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002508 mWorkspace.setVisibility(View.GONE);
2509 }
2510 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002511
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002512 private void showToolbarButton(View button) {
2513 button.setAlpha(1.0f);
2514 button.setVisibility(View.VISIBLE);
2515 button.setFocusable(true);
2516 button.setClickable(true);
2517 }
2518
2519 private void hideToolbarButton(View button) {
2520 button.setAlpha(0.0f);
2521 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2522 button.setVisibility(View.INVISIBLE);
2523 button.setFocusable(false);
2524 button.setClickable(false);
2525 }
2526
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002527 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002528 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002529 *
2530 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2531 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002532 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002533 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002535 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 final boolean showing = show;
2537 final boolean hiding = !show;
2538
2539 final int duration = show ?
2540 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2541 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2542
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002544 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2545 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002546 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002547 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002548 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002549 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002550 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002551 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002552 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002553 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 });
2556 seq.play(anim);
2557 } else {
2558 if (showing) {
2559 showToolbarButton(view);
2560 } else {
2561 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002563 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002564 }
2565
2566 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 * Show/hide the appropriate toolbar buttons for newState.
2568 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002569 *
2570 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002571 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2572 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002574 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002575 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 final View configureButton = findViewById(R.id.configure_button);
2578
2579 switch (newState) {
2580 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002581 hideOrShowToolbarButton(true, searchButton, showSeq);
2582 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2583 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002584 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002585 break;
2586 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002587 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002588 hideOrShowToolbarButton(false, searchButton, hideSeq);
2589 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 break;
2591 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002592 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002594 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002595 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002596 break;
2597 }
2598 }
2599
2600 /**
2601 * Helper method for the cameraZoomIn/cameraZoomOut animations
2602 * @param view The view being animated
2603 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2604 * @param scaleFactor The scale factor used for the zoom
2605 */
2606 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2607 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002608
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002610 // Set pivotY so that at the starting zoom factor, the view is partially
2611 // visible. Modifying initialHeightFactor changes how much of the view is
2612 // initially showing, and hence the perceived angle from which the view enters.
2613 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002615 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002617 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 }
2619 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002620
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002621 /**
2622 * Zoom the camera out from the workspace to reveal 'toView'.
2623 * Assumes that the view to show is anchored at either the very top or very bottom
2624 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002625 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002626 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002627 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002628 final Resources res = getResources();
2629 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2630 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002631 final boolean toAllApps = (toState == State.ALL_APPS);
2632 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002633
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002634 setPivotsForZoom(toView, toState, scale);
2635
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002636 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002637 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002639 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002641
2642 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002643 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2644 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2645 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2646 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002647
2648 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002649 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002650 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002651 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 // Prepare the position
2653 toView.setTranslationX(0.0f);
2654 toView.setTranslationY(0.0f);
2655 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002656 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002657 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002658 @Override
2659 public void onAnimationEndOrCancel(Animator animation) {
2660 // If we don't set the final scale values here, if this animation is cancelled
2661 // it will have the wrong scale value and subsequent cameraPan animations will
2662 // not fix that
2663 toView.setScaleX(1.0f);
2664 toView.setScaleY(1.0f);
2665 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002666 });
2667
Chet Haaseb1254a62010-09-07 13:35:00 -07002668 AnimatorSet toolbarHideAnim = new AnimatorSet();
2669 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2671
2672 // toView should appear right at the end of the workspace shrink animation
2673 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2674
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002675 if (mStateAnimation != null) mStateAnimation.cancel();
2676 mStateAnimation = new AnimatorSet();
2677 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2678 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002679
2680 // Show the new toolbar buttons just as the main animation is ending
2681 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002682 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2683 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002684 } else {
2685 toView.setTranslationX(0.0f);
2686 toView.setTranslationY(0.0f);
2687 toView.setScaleX(1.0f);
2688 toView.setScaleY(1.0f);
2689 toView.setVisibility(View.VISIBLE);
2690 hideAndShowToolbarButtons(toState, null, null);
2691 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002692 }
2693
2694 /**
2695 * Zoom the camera back into the workspace, hiding 'fromView'.
2696 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2698 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002699 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002701 cameraZoomIn(fromState, animated, false);
2702 }
2703
2704 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002705 Resources res = getResources();
2706 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2707 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002708 final View fromView =
2709 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2710
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002711 mCustomizePagedView.endChoiceMode();
2712 mAllAppsPagedView.endChoiceMode();
2713
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002714 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002715
Michael Jurkad3ef3062010-11-23 16:23:58 -08002716 if (!springLoaded) {
2717 mWorkspace.unshrink(animated);
2718 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002719
2720 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002721 if (mStateAnimation != null) mStateAnimation.cancel();
2722 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002723 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2724 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2725 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2726 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002727 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2728
2729 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2730 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2731 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2732 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002733 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002734 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002735 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736 }
2737 });
2738
Chet Haaseb1254a62010-09-07 13:35:00 -07002739 AnimatorSet toolbarHideAnim = new AnimatorSet();
2740 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002741 if (!springLoaded) {
2742 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2743 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002744
Adam Cohen61033d32010-11-15 18:29:44 -08002745 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746
2747 // Show the new toolbar buttons at the very end of the whole animation
2748 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2749 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002750 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2751 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752 } else {
2753 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002754 if (!springLoaded) {
2755 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2756 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002757 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002758 }
2759
2760 /**
2761 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2762 * This is the transition used on xlarge screens to go between all apps and
2763 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002764 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2765 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2766 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002767 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002768 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002769 final Resources res = getResources();
2770 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002771 final int workspaceHeight = mWorkspace.getHeight();
2772
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002773 final boolean fromAllApps = (fromState == State.ALL_APPS);
2774 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2775 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002776
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002777 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2778 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2779 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2780 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002781
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002782 mCustomizePagedView.endChoiceMode();
2783 mAllAppsPagedView.endChoiceMode();
2784
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002785 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002786 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002788 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002789 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002790
2791 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002792 if (mStateAnimation != null) mStateAnimation.cancel();
2793 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002794 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002795 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002796 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002797 toView.setVisibility(View.VISIBLE);
2798 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002799 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002800 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002801 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002802 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002803 fromView.setVisibility(View.GONE);
2804 }
2805 });
2806
Chet Haaseb1254a62010-09-07 13:35:00 -07002807 AnimatorSet toolbarHideAnim = new AnimatorSet();
2808 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2810
Chet Haase472b2812010-10-14 07:02:04 -07002811 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2812 fromViewStartY, fromViewEndY);
2813 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002814 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2815 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2816 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2817 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2818 );
Chet Haase472b2812010-10-14 07:02:04 -07002819 fromAnim.setDuration(duration);
2820 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002821
2822 // Show the new toolbar buttons just as the main animation is ending
2823 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002824 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2825 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002826 } else {
2827 fromView.setY(fromViewEndY);
2828 fromView.setVisibility(View.GONE);
2829 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002830 toView.setScaleX(1.0f);
2831 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002832 toView.setVisibility(View.VISIBLE);
2833 hideAndShowToolbarButtons(toState, null, null);
2834 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 }
2836
2837 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002838 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002839 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002840 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002841
Michael Jurka79212d82010-07-30 16:36:20 -07002842 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002843 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002844 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002845 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002847 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002848 } else {
2849 mAllAppsGrid.zoom(1.0f, animated);
2850 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002851
Romain Guyc16fea72010-03-12 17:17:56 -08002852 ((View) mAllAppsGrid).setFocusable(true);
2853 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002854
Joe Onorato7c312c12009-08-13 21:36:53 -07002855 // TODO: fade these two too
2856 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002857
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002858 // Change the state *after* we've called all the transition code
2859 mState = State.ALL_APPS;
2860 }
2861
2862
2863 void showWorkspace(boolean animated) {
2864 showWorkspace(animated, null);
2865 }
2866
2867 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002868 if (layout != null) {
2869 // always animated, but that's ok since we never specify a layout and
2870 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002871 mWorkspace.unshrink(layout);
2872 } else {
2873 mWorkspace.unshrink(animated);
2874 }
2875 if (mState == State.ALL_APPS) {
2876 closeAllApps(animated);
2877 } else if (mState == State.CUSTOMIZE) {
2878 hideCustomizationDrawer(animated);
2879 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002880
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002881 // Change the state *after* we've called all the transition code
2882 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002883 }
2884
Michael Jurkad3ef3062010-11-23 16:23:58 -08002885 void enterSpringLoadedDragMode(CellLayout layout) {
2886 mWorkspace.enterSpringLoadedDragMode(layout);
2887 if (mState == State.ALL_APPS) {
2888 cameraZoomIn(State.ALL_APPS, true, true);
2889 mState = State.ALL_APPS_SPRING_LOADED;
2890 } else if (mState == State.CUSTOMIZE) {
2891 cameraZoomIn(State.CUSTOMIZE, true, true);
2892 mState = State.CUSTOMIZE_SPRING_LOADED;
2893 }/* else {
2894 // we're already in spring loaded mode; don't do anything
2895 }*/
2896 }
2897
2898 void exitSpringLoadedDragMode() {
2899 if (mState == State.ALL_APPS_SPRING_LOADED) {
2900 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2901 cameraZoomOut(State.ALL_APPS, true);
2902 mState = State.ALL_APPS;
2903 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2904 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2905 cameraZoomOut(State.CUSTOMIZE, true);
2906 mState = State.CUSTOMIZE;
2907 }/* else {
2908 // we're not in spring loaded mode; don't do anything
2909 }*/
2910 }
2911
Joe Onoratob2061212009-11-24 16:13:54 -05002912 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002913 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002914 * - Home from workspace
2915 * - from center screen
2916 * - from other screens
2917 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002918 * - from center screen
2919 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002920 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002921 * - from center screen
2922 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002923 * - Launch app from workspace and quit
2924 * - with back
2925 * - with home
2926 * - Launch app from all apps and quit
2927 * - with back
2928 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002929 * - Go to a screen that's not the default, then all
2930 * apps, and launch and app, and go back
2931 * - with back
2932 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002933 * - On workspace, long press power and go back
2934 * - with back
2935 * - with home
2936 * - On all apps, long press power and go back
2937 * - with back
2938 * - with home
2939 * - On workspace, power off
2940 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002941 * - Launch an app and turn off the screen while in that app
2942 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002943 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002944 * - From all apps
2945 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002946 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2947 * - From all apps
2948 * - From the center workspace
2949 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002950 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002951 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002952 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002953 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002954 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002955 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002956 } else {
2957 mAllAppsGrid.zoom(0.0f, animated);
2958 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002959 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002960 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002961 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002962 }
2963
Joe Onorato7c312c12009-08-13 21:36:53 -07002964 void lockAllApps() {
2965 // TODO
2966 }
2967
2968 void unlockAllApps() {
2969 // TODO
2970 }
2971
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002972 // Show the customization drawer (only exists in x-large configuration)
2973 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002974 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002975 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002976 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002977 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002978 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002979 // Change the state *after* we've called all the transition code
2980 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002981 }
2982
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002983 // Hide the customization drawer (only exists in x-large configuration)
2984 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002985 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002986 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002987 }
2988 }
2989
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002990 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2991 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2992 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002993 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002994 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002995
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002996 void onWorkspaceClick(CellLayout layout) {
2997 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002998 }
2999
Michael Jurka4ef207b2010-11-29 17:05:45 -08003000 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003001 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003002 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3003 ImageView button = (ImageView) findViewById(buttonId);
3004 Drawable toolbarIcon = null;
3005 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003006 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003007 // Look for the toolbar icon specified in the activity meta-data
3008 Bundle metaData = packageManager.getActivityInfo(
3009 activityName, PackageManager.GET_META_DATA).metaData;
3010 if (metaData != null) {
3011 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3012 if (iconResId != 0) {
3013 Resources res = packageManager.getResourcesForActivity(activityName);
3014 toolbarIcon = res.getDrawable(iconResId);
3015 }
3016 }
3017 } catch (NameNotFoundException e) {
3018 // Do nothing
3019 }
3020 // If we were unable to find the icon via the meta-data, use a generic one
3021 if (toolbarIcon == null) {
3022 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003023 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003024 } else {
3025 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003026 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003027 }
3028 }
3029
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003030 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003031 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003032 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003033 }
3034
Michael Jurka0423dcf2010-10-05 14:56:18 -07003035 private void updateGlobalSearchIcon() {
3036 if (LauncherApplication.isScreenXLarge()) {
3037 final SearchManager searchManager =
3038 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3039 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003040 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003041 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003042 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003043 } else {
3044 findViewById(R.id.search_button).setVisibility(View.GONE);
3045 }
3046 }
3047 }
3048
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003049 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003050 updateButtonWithDrawable(R.id.search_button, d);
3051 }
3052
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003053 private void updateVoiceSearchIcon() {
3054 if (LauncherApplication.isScreenXLarge()) {
3055 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3056 ComponentName activityName = intent.resolveActivity(getPackageManager());
3057 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003058 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003059 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3060 } else {
3061 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003062 }
3063 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003064 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003065
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003066 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003067 updateButtonWithDrawable(R.id.voice_button, d);
3068 }
3069
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003070 /**
3071 * Sets the app market icon (shown when all apps is visible on x-large screens)
3072 */
3073 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003074 if (LauncherApplication.isScreenXLarge()) {
3075 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3076 // Find the app market activity by resolving an intent.
3077 // (If multiple app markets are installed, it will return the ResolverActivity.)
3078 ComponentName activityName = intent.resolveActivity(getPackageManager());
3079 if (activityName != null) {
3080 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003081 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003082 R.id.market_button, activityName, R.drawable.app_market_generic);
3083 }
3084 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003085 }
3086
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003087 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003088 updateButtonWithDrawable(R.id.market_button, d);
3089 }
3090
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003091 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003092 * Displays the shortcut creation dialog and launches, if necessary, the
3093 * appropriate activity.
3094 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003095 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003096 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3097 DialogInterface.OnShowListener {
3098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003103
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3105 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003106 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 builder.setInverseBackgroundForced(true);
3109
3110 AlertDialog dialog = builder.create();
3111 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003112 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003113 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 return dialog;
3116 }
3117
3118 public void onCancel(DialogInterface dialog) {
3119 mWaitingForResult = false;
3120 cleanup();
3121 }
3122
Romain Guycbb89e42009-06-08 15:52:54 -07003123 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003124 }
3125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003127 try {
3128 dismissDialog(DIALOG_CREATE_SHORTCUT);
3129 } catch (Exception e) {
3130 // An exception is thrown if the dialog is not visible, which is fine
3131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003132 }
3133
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003134 /**
3135 * Handle the action clicked in the "Add to home" dialog.
3136 */
3137 public void onClick(DialogInterface dialog, int which) {
3138 Resources res = getResources();
3139 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003140
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003141 switch (which) {
3142 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003143 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003144 break;
3145 }
Romain Guycbb89e42009-06-08 15:52:54 -07003146
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003147 case AddAdapter.ITEM_APPWIDGET: {
3148 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003149
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003150 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3151 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003152 // start the pick activity
3153 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3154 break;
3155 }
Romain Guycbb89e42009-06-08 15:52:54 -07003156
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003157 case AddAdapter.ITEM_LIVE_FOLDER: {
3158 // Insert extra item to handle inserting folder
3159 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003160
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003161 ArrayList<String> shortcutNames = new ArrayList<String>();
3162 shortcutNames.add(res.getString(R.string.group_folder));
3163 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003164
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003165 ArrayList<ShortcutIconResource> shortcutIcons =
3166 new ArrayList<ShortcutIconResource>();
3167 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3168 R.drawable.ic_launcher_folder));
3169 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3170
3171 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3172 pickIntent.putExtra(Intent.EXTRA_INTENT,
3173 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3174 pickIntent.putExtra(Intent.EXTRA_TITLE,
3175 getText(R.string.title_select_live_folder));
3176 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003177
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003178 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3179 break;
3180 }
3181
3182 case AddAdapter.ITEM_WALLPAPER: {
3183 startWallpaper();
3184 break;
3185 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
3187 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003188
3189 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003190 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
3193
3194 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003195 * Receives notifications when applications are added/removed.
3196 */
3197 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3198 @Override
3199 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003200 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003201 String reason = intent.getStringExtra("reason");
3202 if (!"homekey".equals(reason)) {
3203 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003204 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003205 animate = false;
3206 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003207 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003208 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003209 }
3210 }
3211
3212 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003213 * Receives notifications whenever the appwidgets are reset.
3214 */
3215 private class AppWidgetResetObserver extends ContentObserver {
3216 public AppWidgetResetObserver() {
3217 super(new Handler());
3218 }
3219
3220 @Override
3221 public void onChange(boolean selfChange) {
3222 onAppWidgetReset();
3223 }
3224 }
3225
3226 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003227 * If the activity is currently paused, signal that we need to re-run the loader
3228 * in onResume.
3229 *
3230 * This needs to be called from incoming places where resources might have been loaded
3231 * while we are paused. That is becaues the Configuration might be wrong
3232 * when we're not running, and if it comes back to what it was when we
3233 * were paused, we are not restarted.
3234 *
3235 * Implementation of the method from LauncherModel.Callbacks.
3236 *
3237 * @return true if we are currently paused. The caller might be able to
3238 * skip some work in that case since we will come back again.
3239 */
3240 public boolean setLoadOnResume() {
3241 if (mPaused) {
3242 Log.i(TAG, "setLoadOnResume");
3243 mOnResumeNeedsLoad = true;
3244 return true;
3245 } else {
3246 return false;
3247 }
3248 }
3249
3250 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003251 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003254 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003255 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003256 } else {
3257 return SCREEN_COUNT / 2;
3258 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003259 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003260
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003261 void setAllAppsPagedView(PagedView view) {
3262 mAllAppsPagedView = view;
3263 }
3264
Joe Onorato9c1289c2009-08-17 11:03:03 -04003265 /**
3266 * Refreshes the shortcuts shown on the workspace.
3267 *
3268 * Implementation of the method from LauncherModel.Callbacks.
3269 */
3270 public void startBinding() {
3271 final Workspace workspace = mWorkspace;
3272 int count = workspace.getChildCount();
3273 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003274 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3276 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003277
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 if (DEBUG_USER_INTERFACE) {
3279 android.widget.Button finishButton = new android.widget.Button(this);
3280 finishButton.setText("Finish");
3281 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3282
3283 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3284 public void onClick(View v) {
3285 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003286 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003287 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003288 }
3289 }
3290
3291 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292 * Bind the items start-end from the list.
3293 *
3294 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3297
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003298 setLoadOnResume();
3299
Joe Onorato9c1289c2009-08-17 11:03:03 -04003300 final Workspace workspace = mWorkspace;
3301
3302 for (int i=start; i<end; i++) {
3303 final ItemInfo item = shortcuts.get(i);
3304 mDesktopItems.add(item);
3305 switch (item.itemType) {
3306 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3307 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003308 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003309 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3310 false);
3311 break;
3312 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3313 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003314 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003315 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3317 false);
3318 break;
3319 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3320 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3321 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003322 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 (LiveFolderInfo) item);
3324 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3325 false);
3326 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328 }
3329
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003331 }
3332
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 /**
3334 * Implementation of the method from LauncherModel.Callbacks.
3335 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003336 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003337 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003338 sFolders.clear();
3339 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341
3342 /**
3343 * Add the views for a widget to the workspace.
3344 *
3345 * Implementation of the method from LauncherModel.Callbacks.
3346 */
3347 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003348 setLoadOnResume();
3349
Daniel Sandler843e8602010-06-07 14:59:01 -04003350 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3351 if (DEBUG_WIDGETS) {
3352 Log.d(TAG, "bindAppWidget: " + item);
3353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003354 final Workspace workspace = mWorkspace;
3355
3356 final int appWidgetId = item.appWidgetId;
3357 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003358 if (DEBUG_WIDGETS) {
3359 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3360 }
3361
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3363
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3365 item.hostView.setTag(item);
3366
3367 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3368 item.cellY, item.spanX, item.spanY, false);
3369
Adam Cohended9f8d2010-11-03 13:25:16 -07003370 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3371
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 workspace.requestLayout();
3373
3374 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003375
3376 if (DEBUG_WIDGETS) {
3377 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3378 + (SystemClock.uptimeMillis()-start) + "ms");
3379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 }
3381
3382 /**
3383 * Callback saying that there aren't any more items to bind.
3384 *
3385 * Implementation of the method from LauncherModel.Callbacks.
3386 */
3387 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003388 setLoadOnResume();
3389
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 if (mSavedState != null) {
3391 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003392 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 }
3394
3395 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3396 if (userFolders != null) {
3397 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003398 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003399 if (info != null) {
3400 openFolder(info);
3401 }
3402 }
3403 final Folder openFolder = mWorkspace.getOpenFolder();
3404 if (openFolder != null) {
3405 openFolder.requestFocus();
3406 }
3407 }
3408
Joe Onorato9c1289c2009-08-17 11:03:03 -04003409 mSavedState = null;
3410 }
3411
3412 if (mSavedInstanceState != null) {
3413 super.onRestoreInstanceState(mSavedInstanceState);
3414 mSavedInstanceState = null;
3415 }
3416
Joe Onorato9c1289c2009-08-17 11:03:03 -04003417 mWorkspaceLoading = false;
3418 }
3419
3420 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003421 * Updates the icons on the launcher that are affected by changes to the package list
3422 * on the device.
3423 */
3424 private void updateIconsAffectedByPackageManagerChanges() {
3425 updateAppMarketIcon();
3426 updateGlobalSearchIcon();
3427 updateVoiceSearchIcon();
3428 }
3429
3430 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003431 * Add the icons for all apps.
3432 *
3433 * Implementation of the method from LauncherModel.Callbacks.
3434 */
3435 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003436 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003437 if (mCustomizePagedView != null) {
3438 mCustomizePagedView.setApps(apps);
3439 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003440 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
3442
3443 /**
3444 * A package was installed.
3445 *
3446 * Implementation of the method from LauncherModel.Callbacks.
3447 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003448 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003449 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003450 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003451 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003452 if (mCustomizePagedView != null) {
3453 mCustomizePagedView.addApps(apps);
3454 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003455 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003456 }
3457
3458 /**
3459 * A package was updated.
3460 *
3461 * Implementation of the method from LauncherModel.Callbacks.
3462 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003463 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003464 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003466 mWorkspace.updateShortcuts(apps);
3467 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003468 if (mCustomizePagedView != null) {
3469 mCustomizePagedView.updateApps(apps);
3470 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003471 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003472 }
3473
3474 /**
3475 * A package was uninstalled.
3476 *
3477 * Implementation of the method from LauncherModel.Callbacks.
3478 */
Joe Onorato36115782010-06-17 13:28:48 -04003479 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003481 if (permanent) {
3482 mWorkspace.removeItems(apps);
3483 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003484 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003485 if (mCustomizePagedView != null) {
3486 mCustomizePagedView.removeApps(apps);
3487 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003488 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003489 }
Joe Onoratobe386092009-11-17 17:32:16 -08003490
3491 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003492 * A number of packages were updated.
3493 */
3494 public void bindPackagesUpdated() {
3495 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003496 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003497 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003498 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003499 }
3500
3501 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003502 * Prints out out state for debugging.
3503 */
3504 public void dumpState() {
3505 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003506 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003507 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3508 Log.d(TAG, "mRestoring=" + mRestoring);
3509 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3510 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3511 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003512 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003513 mModel.dumpState();
3514 mAllAppsGrid.dumpState();
3515 Log.d(TAG, "END launcher2 dump state");
3516 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003517}