blob: bf72add66d969b83607aed2348f1e32ecbb59a79 [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);
Adam Cohencdc30d52010-12-01 15:09:47 -0800977 allAppsInfoTarget.setDragAndDropEnabled(false);
978 View marketButton = findViewById(R.id.market_button);
979 if (marketButton != null) {
Adam Cohencdc30d52010-12-01 15:09:47 -0800980 allAppsInfoTarget.setHandle(marketButton);
981 }
982 }
983
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700984 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
985 if (infoButton != null) {
986 infoButton.setLauncher(this);
987 infoButton.setHandle(findViewById(R.id.configure_button));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700988 dragController.addDragListener(infoButton);
989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Joe Onorato00acb122009-08-04 16:04:30 -0400991 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400992 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800993 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700994
Joe Onorato00acb122009-08-04 16:04:30 -0400995 // The order here is bottom to top.
996 dragController.addDropTarget(workspace);
997 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700998 if (infoButton != null) {
999 dragController.addDropTarget(infoButton);
1000 }
Adam Cohencdc30d52010-12-01 15:09:47 -08001001 if (allAppsInfoTarget != null) {
1002 dragController.addDropTarget(allAppsInfoTarget);
1003 }
1004 if (allAppsDeleteZone != null) {
1005 dragController.addDropTarget(allAppsDeleteZone);
1006 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008
Romain Guy8a73c512009-11-09 19:19:59 -08001009 @SuppressWarnings({"UnusedDeclaration"})
1010 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001011 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001012 mWorkspace.scrollLeft();
1013 }
Romain Guy8a73c512009-11-09 19:19:59 -08001014 }
1015
1016 @SuppressWarnings({"UnusedDeclaration"})
1017 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001018 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001019 mWorkspace.scrollRight();
1020 }
Romain Guy8a73c512009-11-09 19:19:59 -08001021 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001022
1023 @SuppressWarnings({"UnusedDeclaration"})
1024 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001025 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001026
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001027 int index = -1;
1028 if (v.getId() == R.id.hotseat_left) {
1029 index = 0;
1030 } else if (v.getId() == R.id.hotseat_right) {
1031 index = 1;
1032 }
1033
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001034 // reload these every tap; you never know when they might change
1035 loadHotseats();
1036 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
1037 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001038 startActivitySafely(
1039 mHotseats[index],
1040 "hotseat"
1041 );
1042 }
1043 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001044
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 /**
1046 * Creates a view representing a shortcut.
1047 *
1048 * @param info The data structure describing the shortcut.
1049 *
1050 * @return A View inflated from R.layout.application.
1051 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001052 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001054 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 }
1056
1057 /**
1058 * Creates a view representing a shortcut inflated from the specified resource.
1059 *
1060 * @param layoutResId The id of the XML layout used to create the shortcut.
1061 * @param parent The group the shortcut belongs to.
1062 * @param info The data structure describing the shortcut.
1063 *
1064 * @return A View inflated from layoutResId.
1065 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001066 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001067 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1068 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 return favorite;
1071 }
1072
1073 /**
1074 * Add an application shortcut to the workspace.
1075 *
1076 * @param data The intent describing the application.
1077 * @param cellInfo The position on screen where to create the shortcut.
1078 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001079 void completeAddApplication(Context context, Intent data, int screen,
1080 int intersectCellX, int intersectCellY) {
1081 final int[] cellXY = mTmpAddItemCellCoordinates;
1082 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1083
1084 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1085 showOutOfSpaceMessage();
1086 return;
1087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
Joe Onorato0589f0f2010-02-08 13:44:00 -08001089 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1090 data, context);
1091
Romain Guy73b979d2009-06-09 12:57:21 -07001092 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001093 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001095 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1097 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001098 } else {
1099 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 }
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 /**
1104 * Add a shortcut to the workspace.
1105 *
1106 * @param data The intent describing the shortcut.
1107 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001109 private void completeAddShortcut(Intent data, int screen,
1110 int intersectCellX, int intersectCellY) {
1111 final int[] cellXY = mTmpAddItemCellCoordinates;
1112 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001113
Michael Jurkad3ef3062010-11-23 16:23:58 -08001114 int[] touchXY = null;
1115 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1116 touchXY = mAddDropPosition;
1117 }
1118 boolean foundCellSpan = false;
1119 if (touchXY != null) {
1120 // when dragging and dropping, just find the closest free spot
1121 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1122 int[] result = screenLayout.findNearestVacantArea(
1123 touchXY[0], touchXY[1], 1, 1, cellXY);
1124 foundCellSpan = (result != null);
1125 } else {
1126 foundCellSpan = layout.findCellForSpanThatIntersects(
1127 cellXY, 1, 1, intersectCellX, intersectCellY);
1128 }
1129
1130 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 showOutOfSpaceMessage();
1132 return;
1133 }
1134
1135 final ShortcutInfo info = mModel.addShortcut(
1136 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137
1138 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 }
1142 }
1143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001145 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001147 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001151 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001152
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001153 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001154 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1155 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001158 // We have saved the position to which the widget was dragged-- this really only matters
1159 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001160 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001161
1162 // For now, we don't save the coordinate where we dropped the icon because we're not
1163 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1164 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001165 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001166 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001167 touchXY = mAddDropPosition;
1168 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001169 boolean foundCellSpan = false;
1170 if (touchXY != null) {
1171 // when dragging and dropping, just find the closest free spot
1172 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1173 int[] result = screenLayout.findNearestVacantArea(
1174 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001175 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001176 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001177 // if we long pressed on an empty cell to bring up a menu,
1178 // make sure we intersect the empty cell
1179 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1180 // findCellForSpanThatIntersects will just ignore them
1181 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1182 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001183 }
1184
Michael Jurka0280c3b2010-09-17 15:00:07 -07001185 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001186 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001187 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001188 return;
1189 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001191 // Build Launcher-specific widget info and save to database
1192 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001193 launcherInfo.spanX = spanXY[0];
1194 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001195
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 LauncherModel.addItemToDatabase(this, launcherInfo,
1197 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199
1200 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001202
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001204 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001205
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001208
Michael Jurka0280c3b2010-09-17 15:00:07 -07001209 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001210 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001211
1212 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 }
1214 }
Romain Guycbb89e42009-06-08 15:52:54 -07001215
Adam Cohended9f8d2010-11-03 13:25:16 -07001216 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1217 @Override
1218 public void onReceive(Context context, Intent intent) {
1219 final String action = intent.getAction();
1220 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1221 mUserPresent = false;
1222 updateRunning();
1223 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1224 mUserPresent = true;
1225 updateRunning();
1226 }
1227 }
1228 };
1229
1230 @Override
1231 public void onAttachedToWindow() {
1232 super.onAttachedToWindow();
1233
1234 // Listen for broadcasts related to user-presence
1235 final IntentFilter filter = new IntentFilter();
1236 filter.addAction(Intent.ACTION_SCREEN_OFF);
1237 filter.addAction(Intent.ACTION_USER_PRESENT);
1238 registerReceiver(mReceiver, filter);
1239
Adam Cohend113e0c2010-11-11 10:48:05 -08001240 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001241 mVisible = true;
1242 }
1243
1244 @Override
1245 public void onDetachedFromWindow() {
1246 super.onDetachedFromWindow();
1247 mVisible = false;
1248
Adam Cohend113e0c2010-11-11 10:48:05 -08001249 if (mAttached) {
1250 unregisterReceiver(mReceiver);
1251 mAttached = false;
1252 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001253 updateRunning();
1254 }
1255
1256 public void onWindowVisibilityChanged(int visibility) {
1257 mVisible = visibility == View.VISIBLE;
1258 updateRunning();
1259 }
1260
1261 private void sendAdvanceMessage(long delay) {
1262 mHandler.removeMessages(ADVANCE_MSG);
1263 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1264 mHandler.sendMessageDelayed(msg, delay);
1265 mAutoAdvanceSentTime = System.currentTimeMillis();
1266 }
1267
1268 private void updateRunning() {
1269 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1270 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1271 mAutoAdvanceRunning = autoAdvanceRunning;
1272 if (autoAdvanceRunning) {
1273 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1274 sendAdvanceMessage(delay);
1275 } else {
1276 if (!mWidgetsToAdvance.isEmpty()) {
1277 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1278 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1279 }
1280 mHandler.removeMessages(ADVANCE_MSG);
1281 }
1282 }
1283 }
1284
1285 private final Handler mHandler = new Handler() {
1286 @Override
1287 public void handleMessage(Message msg) {
1288 if (msg.what == ADVANCE_MSG) {
1289 int i = 0;
1290 for (View key: mWidgetsToAdvance.keySet()) {
1291 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1292 final int delay = mAdvanceStagger * i;
1293 if (v instanceof Advanceable) {
1294 postDelayed(new Runnable() {
1295 public void run() {
1296 ((Advanceable) v).advance();
1297 }
1298 }, delay);
1299 }
1300 i++;
1301 }
1302 sendAdvanceMessage(mAdvanceInterval);
1303 }
1304 }
1305 };
1306
1307 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1308 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1309 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1310 if (v instanceof Advanceable) {
1311 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1312 ((Advanceable) v).willBeAdvancedByHost();
1313 updateRunning();
1314 }
1315 }
1316
1317 void removeWidgetToAutoAdvance(View hostView) {
1318 if (mWidgetsToAdvance.containsKey(hostView)) {
1319 mWidgetsToAdvance.remove(hostView);
1320 updateRunning();
1321 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001322 }
1323
Joe Onorato9c1289c2009-08-17 11:03:03 -04001324 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1325 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001326 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001327 launcherInfo.hostView = null;
1328 }
1329
Adam Cohended9f8d2010-11-03 13:25:16 -07001330 void showOutOfSpaceMessage() {
1331 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1332 }
1333
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001334 public LauncherAppWidgetHost getAppWidgetHost() {
1335 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
Romain Guycbb89e42009-06-08 15:52:54 -07001337
Winson Chunga9abd0e2010-10-27 17:18:37 -07001338 public LauncherModel getModel() {
1339 return mModel;
1340 }
1341
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001342 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001343 getWindow().closeAllPanels();
1344
1345 try {
1346 dismissDialog(DIALOG_CREATE_SHORTCUT);
1347 // Unlock the workspace if the dialog was showing
1348 } catch (Exception e) {
1349 // An exception is thrown if the dialog is not visible, which is fine
1350 }
1351
1352 try {
1353 dismissDialog(DIALOG_RENAME_FOLDER);
1354 // Unlock the workspace if the dialog was showing
1355 } catch (Exception e) {
1356 // An exception is thrown if the dialog is not visible, which is fine
1357 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001358
1359 // Whatever we were doing is hereby canceled.
1360 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 @Override
1364 protected void onNewIntent(Intent intent) {
1365 super.onNewIntent(intent);
1366
1367 // Close the menu
1368 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001369 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001370 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001371
Joe Onorato14f122b2009-11-19 14:06:36 -08001372 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1373 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001374
Michael Jurka4cb37242010-08-09 21:05:32 -07001375 // in all these cases, only animate if we're already on home
1376 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001377 mWorkspace.unshrink(alreadyOnHome);
1378 }
1379 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001380 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001381 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1382 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001383 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001384 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001385
Joe Onorato3a8820b2009-11-10 15:06:42 -08001386 final View v = getWindow().peekDecorView();
1387 if (v != null && v.getWindowToken() != null) {
1388 InputMethodManager imm = (InputMethodManager)getSystemService(
1389 INPUT_METHOD_SERVICE);
1390 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392 }
1393 }
1394
1395 @Override
1396 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1397 // Do not call super here
1398 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001399
Winson Chung43b119d2010-12-06 16:45:05 -08001400 // Restore the current AllApps drawer tab
1401 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1402 String cur = savedInstanceState.getString("allapps_currentTab");
1403 if (cur != null) {
1404 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1405 tabhost.setCurrentTabByTag(cur);
1406 }
1407 }
1408
1409 // Restore the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001410 if (mHomeCustomizationDrawer != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001411 String cur = savedInstanceState.getString("customize_currentTab");
Michael Jurka946ad472010-07-09 18:05:18 -07001412 if (cur != null) {
1413 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1414 }
1415 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
1418 @Override
1419 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001420 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421
1422 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1423 if (folders.size() > 0) {
1424 final int count = folders.size();
1425 long[] ids = new long[count];
1426 for (int i = 0; i < count; i++) {
1427 final FolderInfo info = folders.get(i).getInfo();
1428 ids[i] = info.id;
1429 }
1430 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1431 } else {
1432 super.onSaveInstanceState(outState);
1433 }
1434
Michael Jurka883f55b2010-10-21 15:47:14 -07001435 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436
Michael Jurka0280c3b2010-09-17 15:00:07 -07001437 if (mAddScreen > -1 && mWaitingForResult) {
1438 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1439 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 if (mFolderInfo != null && mWaitingForResult) {
1444 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1445 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1446 }
Michael Jurka946ad472010-07-09 18:05:18 -07001447
Winson Chung43b119d2010-12-06 16:45:05 -08001448 // Save the current AllApps drawer tab
1449 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1450 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1451 String currentTabTag = tabhost.getCurrentTabTag();
1452 if (currentTabTag != null) {
1453 outState.putString("allapps_currentTab", currentTabTag);
1454 }
1455 }
1456
1457 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001458 if (mHomeCustomizationDrawer != null) {
1459 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1460 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001461 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001462 }
1463 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 }
1465
1466 @Override
1467 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001471 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001473 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
1476 TextKeyListener.getInstance().release();
1477
Joe Onorato9c1289c2009-08-17 11:03:03 -04001478 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001481
1482 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001483
Patrick Dubroyab962b72010-07-26 12:10:10 -07001484 // Some launcher layouts don't have a previous and next view
1485 if (mPreviousView != null) {
1486 dismissPreview(mPreviousView);
1487 }
1488 if (mNextView != null) {
1489 dismissPreview(mNextView);
1490 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001491
1492 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
1495 @Override
1496 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001497 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 super.startActivityForResult(intent, requestCode);
1499 }
1500
1501 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001502 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001504
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001505 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Karl Rosaen138a0412009-04-23 19:00:21 -07001507 if (initialQuery == null) {
1508 // Use any text typed in the launcher as the initial query
1509 initialQuery = getTypedText();
1510 clearTypedText();
1511 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 if (appSearchData == null) {
1513 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001514 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Karl Rosaen138a0412009-04-23 19:00:21 -07001517 final SearchManager searchManager =
1518 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001519 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001520 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 }
1522
1523 @Override
1524 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001525 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001526 return false;
1527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528
1529 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001530
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1532 .setIcon(android.R.drawable.ic_menu_add)
1533 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001534 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1535 .setIcon(android.R.drawable.ic_menu_manage)
1536 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001537 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 .setIcon(android.R.drawable.ic_menu_gallery)
1539 .setAlphabeticShortcut('W');
1540 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1541 .setIcon(android.R.drawable.ic_search_category_default)
1542 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1543 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1544 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1545 .setAlphabeticShortcut('N');
1546
1547 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001548 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1549 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550
1551 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1552 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1553 .setIntent(settings);
1554
1555 return true;
1556 }
1557
1558 @Override
1559 public boolean onPrepareOptionsMenu(Menu menu) {
1560 super.onPrepareOptionsMenu(menu);
1561
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001562 // If all apps is animating, don't show the menu, because we don't know
1563 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001564 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001565 return false;
1566 }
1567
1568 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001569 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001570 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1571 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1572
1573 // Disable add if the workspace is full.
1574 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001575 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1576 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 return true;
1580 }
1581
1582 @Override
1583 public boolean onOptionsItemSelected(MenuItem item) {
1584 switch (item.getItemId()) {
1585 case MENU_ADD:
1586 addItems();
1587 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001588 case MENU_MANAGE_APPS:
1589 manageApps();
1590 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 case MENU_WALLPAPER_SETTINGS:
1592 startWallpaper();
1593 return true;
1594 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001595 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 return true;
1597 case MENU_NOTIFICATIONS:
1598 showNotifications();
1599 return true;
1600 }
1601
1602 return super.onOptionsItemSelected(item);
1603 }
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Karl Rosaen138a0412009-04-23 19:00:21 -07001605 /**
1606 * Indicates that we want global search for this activity by setting the globalSearch
1607 * argument for {@link #startSearch} to true.
1608 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001610 @Override
1611 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001612 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001613 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001614 }
1615
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 public boolean isWorkspaceLocked() {
1617 return mWorkspaceLoading || mWaitingForResult;
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001621 if (LauncherApplication.isScreenXLarge()) {
1622 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001623 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001624 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001625 }
1626 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001627 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001628 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
1631
Michael Jurka0280c3b2010-09-17 15:00:07 -07001632 private void resetAddInfo() {
1633 mAddScreen = -1;
1634 mAddIntersectCellX = -1;
1635 mAddIntersectCellY = -1;
1636 mAddDropPosition = null;
1637 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001638
Winson Chung55cef262010-10-28 14:14:18 -07001639 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001640 resetAddInfo();
1641 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001642 mAddDropPosition = position;
1643
Michael Jurkaaf442092010-06-10 17:01:57 -07001644 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001645 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1646 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001647 }
1648
Winson Chung7ad01412010-09-27 11:33:03 -07001649 private void manageApps() {
1650 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1651 }
1652
Michael Jurkaaf442092010-06-10 17:01:57 -07001653 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001654 // TODO: catch bad widget exception when sent
1655 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001656 // TODO: Is this log message meaningful?
1657 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001658 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001659 }
1660
Winson Chung55cef262010-10-28 14:14:18 -07001661 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001662 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663
Bjorn Bringert7984c942009-12-09 15:38:25 +00001664 if (appWidget.configure != null) {
1665 // Launch over to configure widget, if needed
1666 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1667 intent.setComponent(appWidget.configure);
1668 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001669 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001670 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1671 intent.putExtra(
1672 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1673 info.mimeType);
1674
1675 final String mimeType = info.mimeType;
1676 final ClipData clipData = (ClipData) info.configurationData;
1677 final ClipDescription clipDesc = clipData.getDescription();
1678 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1679 if (clipDesc.getMimeType(i).equals(mimeType)) {
1680 final ClipData.Item item = clipData.getItem(i);
1681 final CharSequence stringData = item.getText();
1682 final Uri uriData = item.getUri();
1683 final Intent intentData = item.getIntent();
1684 final String key =
1685 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1686 if (uriData != null) {
1687 intent.putExtra(key, uriData);
1688 } else if (intentData != null) {
1689 intent.putExtra(key, intentData);
1690 } else if (stringData != null) {
1691 intent.putExtra(key, stringData);
1692 }
1693 break;
1694 }
1695 }
1696 }
Winson Chung55cef262010-10-28 14:14:18 -07001697 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001698
Romain Guy8e633c52010-03-04 12:51:36 -08001699 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001701 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 }
1704 }
Romain Guycbb89e42009-06-08 15:52:54 -07001705
Michael Jurka0280c3b2010-09-17 15:00:07 -07001706 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1707 resetAddInfo();
1708 mAddScreen = screen;
1709 mAddDropPosition = position;
1710
1711 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1712 createShortcutIntent.setComponent(componentName);
1713 processShortcut(createShortcutIntent);
1714 }
1715
Joe Onoratodeb98af2010-02-19 14:59:39 -08001716 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001717 // Handle case where user selected "Applications"
1718 String applicationName = getResources().getString(R.string.group_applications);
1719 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001720
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001721 if (applicationName != null && applicationName.equals(shortcutName)) {
1722 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1723 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001724
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001725 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1726 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001727 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001728 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001729 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001730 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 }
1733
Winson Chung24ab2f12010-09-16 14:10:47 -07001734 void processWallpaper(Intent intent) {
1735 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1736 }
1737
Michael Jurka0280c3b2010-09-17 15:00:07 -07001738 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1739 resetAddInfo();
1740 mAddScreen = screen;
1741 mAddDropPosition = position;
1742
1743 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1744 createFolderIntent.setComponent(componentName);
1745
1746 addLiveFolder(createFolderIntent);
1747 }
1748
1749 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001750 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001751 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001752 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001753
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001754 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001755 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001756 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001757 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
1760
Michael Jurka0280c3b2010-09-17 15:00:07 -07001761 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 UserFolderInfo folderInfo = new UserFolderInfo();
1763 folderInfo.title = getText(R.string.folder_name);
1764
Michael Jurka0280c3b2010-09-17 15:00:07 -07001765 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1766 final int[] cellXY = mTmpAddItemCellCoordinates;
1767 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1768 showOutOfSpaceMessage();
1769 return;
1770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771
1772 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001773 LauncherModel.addItemToDatabase(this, folderInfo,
1774 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001775 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001776 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777
1778 // Create the view
1779 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001780 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1781 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001782 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 }
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Joe Onorato9c1289c2009-08-17 11:03:03 -04001785 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001786 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001787 }
1788
Michael Jurka28750fb2010-09-24 17:43:49 -07001789 private void completeAddLiveFolder(
1790 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001791 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1792 final int[] cellXY = mTmpAddItemCellCoordinates;
1793 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1794 showOutOfSpaceMessage();
1795 return;
1796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797
Michael Jurka0280c3b2010-09-17 15:00:07 -07001798 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799
1800 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001802 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001803 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804 }
1805 }
1806
1807 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001808 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809
1810 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1811 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1812
1813 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 Intent.ShortcutIconResource iconResource = null;
1815
1816 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1817 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1818 try {
1819 iconResource = (Intent.ShortcutIconResource) extra;
1820 final PackageManager packageManager = context.getPackageManager();
1821 Resources resources = packageManager.getResourcesForApplication(
1822 iconResource.packageName);
1823 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1824 icon = resources.getDrawable(id);
1825 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001826 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
1828 }
1829
1830 if (icon == null) {
1831 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1832 }
1833
1834 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001835 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 info.title = name;
1837 info.iconResource = iconResource;
1838 info.uri = data.getData();
1839 info.baseIntent = baseIntent;
1840 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1841 LiveFolders.DISPLAY_MODE_GRID);
1842
1843 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001844 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001845 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846
1847 return info;
1848 }
1849
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 private void showNotifications() {
1851 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1852 if (statusBar != null) {
1853 statusBar.expand();
1854 }
1855 }
1856
1857 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001858 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001860 Intent chooser = Intent.createChooser(pickWallpaper,
1861 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001862 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1863 // Removed in Eclair MR1
1864// WallpaperManager wm = (WallpaperManager)
1865// getSystemService(Context.WALLPAPER_SERVICE);
1866// WallpaperInfo wi = wm.getWallpaperInfo();
1867// if (wi != null && wi.getSettingsActivity() != null) {
1868// LabeledIntent li = new LabeledIntent(getPackageName(),
1869// R.string.configure_wallpaper, 0);
1870// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1871// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1872// }
Mike Clerona0618e42009-10-22 13:55:21 -07001873 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 }
1875
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001876 /**
1877 * Registers various content observers. The current implementation registers
1878 * only a favorites observer to keep track of the favorites applications.
1879 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001880 private void registerContentObservers() {
1881 ContentResolver resolver = getContentResolver();
1882 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1883 true, mWidgetObserver);
1884 }
1885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 @Override
1887 public boolean dispatchKeyEvent(KeyEvent event) {
1888 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1889 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001891 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001892 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001893 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001894 dumpState();
1895 return true;
1896 }
1897 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001898 }
1899 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1900 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001901 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 return true;
1903 }
1904 }
1905
1906 return super.dispatchKeyEvent(event);
1907 }
1908
Joe Onorato88ec0992009-11-19 13:16:06 -08001909 @Override
1910 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001911 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1912 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001913 } else {
1914 closeFolder();
1915 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001916 // Some launcher layouts don't have a previous and next view
1917 if (mPreviousView != null) {
1918 dismissPreview(mPreviousView);
1919 dismissPreview(mNextView);
1920 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001921 }
1922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 private void closeFolder() {
1924 Folder folder = mWorkspace.getOpenFolder();
1925 if (folder != null) {
1926 closeFolder(folder);
1927 }
1928 }
1929
1930 void closeFolder(Folder folder) {
1931 folder.getInfo().opened = false;
1932 ViewGroup parent = (ViewGroup) folder.getParent();
1933 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001934 CellLayout cl = (CellLayout) parent;
1935 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001936 if (folder instanceof DropTarget) {
1937 // Live folders aren't DropTargets.
1938 mDragController.removeDropTarget((DropTarget)folder);
1939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 }
1941 folder.onClose();
1942 }
1943
1944 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001945 * Re-listen when widgets are reset.
1946 */
1947 private void onAppWidgetReset() {
1948 mAppWidgetHost.startListening();
1949 }
1950
1951 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001952 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1953 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001955 private void unbindDesktopItems() {
1956 for (ItemInfo item: mDesktopItems) {
1957 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001960
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 /**
1962 * Launches the intent referred by the clicked shortcut.
1963 *
1964 * @param v The view representing the clicked shortcut.
1965 */
1966 public void onClick(View v) {
1967 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001968 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001970 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001971 int[] pos = new int[2];
1972 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001973 intent.setSourceBounds(new Rect(pos[0], pos[1],
1974 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001975 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 } else if (tag instanceof FolderInfo) {
1977 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001978 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001979 if (mState == State.ALL_APPS) {
1980 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001981 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001982 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001983 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 }
1985 }
1986
Michael Jurka0e260592010-06-30 17:07:39 -07001987 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001988 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001989 // clicking anywhere on the workspace causes the customization drawer to slide down
1990 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001991 return false;
1992 }
1993
Michael Jurkaaf442092010-06-10 17:01:57 -07001994 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001995 * Event handler for the search button
1996 *
1997 * @param v The view that was clicked.
1998 */
1999 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002000 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002001 // Use a custom animation for launching search
2002 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002003 }
2004
2005 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002006 * Event handler for the voice button
2007 *
2008 * @param v The view that was clicked.
2009 */
2010 public void onClickVoiceButton(View v) {
2011 startVoiceSearch();
2012 }
2013
2014 private void startVoiceSearch() {
2015 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2016 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2017 startActivity(intent);
2018 }
2019
2020 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002021 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002022 * enters home screen customization mode.
2023 *
2024 * @param v The view that was clicked.
2025 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002026 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002027 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002028 }
2029
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002030 /**
2031 * Event handler for the "grid" button that appears on the home screen, which
2032 * enters all apps mode.
2033 *
2034 * @param v The view that was clicked.
2035 */
2036 public void onClickAllAppsButton(View v) {
2037 showAllApps(true);
2038 }
2039
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002040 public void onClickAppMarketButton(View v) {
2041 if (mAppMarketIntent != null) {
2042 startActivitySafely(mAppMarketIntent, "app market");
2043 }
2044 }
2045
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002046 void startApplicationDetailsActivity(ComponentName componentName) {
2047 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002048 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2049 Uri.fromParts("package", packageName, null));
2050 startActivity(intent);
2051 }
2052
Patrick Dubroy5539af72010-09-07 15:22:01 -07002053 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2054 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2055 // System applications cannot be installed. For now, show a toast explaining that.
2056 // We may give them the option of disabling apps this way.
2057 int messageId = R.string.uninstall_system_app_text;
2058 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2059 } else {
2060 String packageName = appInfo.componentName.getPackageName();
2061 String className = appInfo.componentName.getClassName();
2062 Intent intent = new Intent(
2063 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2064 startActivity(intent);
2065 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002066 }
2067
Joe Onoratof984e852010-03-25 09:47:45 -07002068 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2070 try {
2071 startActivity(intent);
2072 } catch (ActivityNotFoundException e) {
2073 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002074 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 } catch (SecurityException e) {
2076 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002077 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002079 "or use the exported attribute for this activity. "
2080 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 }
2082 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002083
Romain Guy8e633c52010-03-04 12:51:36 -08002084 void startActivityForResultSafely(Intent intent, int requestCode) {
2085 try {
2086 startActivityForResult(intent, requestCode);
2087 } catch (ActivityNotFoundException e) {
2088 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2089 } catch (SecurityException e) {
2090 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2091 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2092 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2093 "or use the exported attribute for this activity.", e);
2094 }
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096
2097 private void handleFolderClick(FolderInfo folderInfo) {
2098 if (!folderInfo.opened) {
2099 // Close any open folder
2100 closeFolder();
2101 // Open the requested folder
2102 openFolder(folderInfo);
2103 } else {
2104 // Find the open folder...
2105 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2106 int folderScreen;
2107 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002108 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // .. and close it
2110 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002111 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 // Close any folder open on the current screen
2113 closeFolder();
2114 // Pull the folder onto this screen
2115 openFolder(folderInfo);
2116 }
2117 }
2118 }
2119 }
2120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002122 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 * is animated relative to the specified View. If the View is null, no animation
2124 * is played.
2125 *
2126 * @param folderInfo The FolderInfo describing the folder to open.
2127 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002128 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 Folder openFolder;
2130
2131 if (folderInfo instanceof UserFolderInfo) {
2132 openFolder = UserFolder.fromXml(this);
2133 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002134 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 } else {
2136 return;
2137 }
2138
Joe Onorato00acb122009-08-04 16:04:30 -04002139 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 openFolder.setLauncher(this);
2141
2142 openFolder.bind(folderInfo);
2143 folderInfo.opened = true;
2144
Winson Chungaafa03c2010-06-11 17:34:16 -07002145 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 openFolder.onOpen();
2148 }
2149
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002151 switch (v.getId()) {
2152 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002153 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002154 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2155 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002156 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002157 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002158 return true;
2159 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002160 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002161 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2162 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002163 showPreviews(v);
2164 }
2165 return true;
2166 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002167 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002168 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2169 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2170 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002171 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002172 return true;
2173 }
2174
Joe Onorato9c1289c2009-08-17 11:03:03 -04002175 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 return false;
2177 }
2178
2179 if (!(v instanceof CellLayout)) {
2180 v = (View) v.getParent();
2181 }
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183
Michael Jurka0280c3b2010-09-17 15:00:07 -07002184 resetAddInfo();
2185 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 return true;
2189 }
2190
Michael Jurka0280c3b2010-09-17 15:00:07 -07002191 final View itemUnderLongClick = longClickCellInfo.cell;
2192
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002194 if (itemUnderLongClick == null) {
2195 // User long pressed on empty space
2196 mWorkspace.setAllowLongPress(false);
2197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002199 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002200 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 }
2202 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002203 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002205 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2206 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002207 mAddIntersectCellX = longClickCellInfo.cellX;
2208 mAddIntersectCellY = longClickCellInfo.cellY;
2209 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002210 }
2211 }
2212 }
2213 return true;
2214 }
2215
Romain Guye6b8e2f2009-11-10 11:56:55 -08002216 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002217 private void dismissPreview(final View v) {
2218 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002219 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002220 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2221 public void onDismiss() {
2222 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2223 int count = group.getChildCount();
2224 for (int i = 0; i < count; i++) {
2225 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2226 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002227 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2228 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2229
2230 v.setTag(R.id.workspace, null);
2231 v.setTag(R.id.icon, null);
2232 window.setOnDismissListener(null);
2233 }
2234 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002235 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002236 }
2237 v.setTag(null);
2238 }
2239
Romain Guyf8e6a802009-12-07 17:48:02 -08002240 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002241 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002242 }
2243
Romain Guya6abce82009-11-10 02:54:41 -08002244 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002245 final Resources resources = getResources();
2246 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002247
Romain Guya6abce82009-11-10 02:54:41 -08002248 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002249
Romain Guy9d31e9f2009-11-11 18:54:28 -08002250 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002251
Romain Guyf8e6a802009-12-07 17:48:02 -08002252 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002253 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002254 int extraW = (int) ((r.left + r.right) * max);
2255 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002256
2257 int aW = cell.getWidth() - extraW;
2258 float w = aW / max;
2259
2260 int width = cell.getWidth();
2261 int height = cell.getHeight();
2262 int x = cell.getLeftPadding();
2263 int y = cell.getTopPadding();
2264 width -= (x + cell.getRightPadding());
2265 height -= (y + cell.getBottomPadding());
2266
2267 float scale = w / width;
2268
2269 int count = end - start;
2270
2271 final float sWidth = width * scale;
2272 float sHeight = height * scale;
2273
Romain Guye6b8e2f2009-11-10 11:56:55 -08002274 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002275
Romain Guye6b8e2f2009-11-10 11:56:55 -08002276 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2277 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002278
2279 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002280 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002281 cell = (CellLayout) workspace.getChildAt(i);
2282
Romain Guyf8e6a802009-12-07 17:48:02 -08002283 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002284 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002285
2286 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002287 c.scale(scale, scale);
2288 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002289 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002290
Romain Guy9d31e9f2009-11-11 18:54:28 -08002291 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002292 image.setImageBitmap(bitmap);
2293 image.setTag(i);
2294 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002295 image.setOnFocusChangeListener(handler);
2296 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002297 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002298
2299 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002300 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2301
Winson Chungaafa03c2010-06-11 17:34:16 -07002302 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002303 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002304
2305 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002306 p.setContentView(preview);
2307 p.setWidth((int) (sWidth * count + extraW));
2308 p.setHeight((int) (sHeight + extraH));
2309 p.setAnimationStyle(R.style.AnimationPreview);
2310 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002311 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002312 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002313 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002314
Romain Guye6b8e2f2009-11-10 11:56:55 -08002315 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2316 public void onDismiss() {
2317 dismissPreview(anchor);
2318 }
2319 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002320
2321 anchor.setTag(p);
2322 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002323 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002324 }
2325
Romain Guy9d31e9f2009-11-11 18:54:28 -08002326 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002327 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002328
Romain Guye6b8e2f2009-11-10 11:56:55 -08002329 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002330 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002331 }
2332
2333 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002334 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002335 v.post(this);
2336 }
2337
2338 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002339 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002340 }
2341
2342 public void onFocusChange(View v, boolean hasFocus) {
2343 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002344 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002345 }
Romain Guya6abce82009-11-10 02:54:41 -08002346 }
2347 }
2348
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 Workspace getWorkspace() {
2350 return mWorkspace;
2351 }
2352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002353 @Override
2354 protected Dialog onCreateDialog(int id) {
2355 switch (id) {
2356 case DIALOG_CREATE_SHORTCUT:
2357 return new CreateShortcut().createDialog();
2358 case DIALOG_RENAME_FOLDER:
2359 return new RenameFolder().createDialog();
2360 }
2361
2362 return super.onCreateDialog(id);
2363 }
2364
2365 @Override
2366 protected void onPrepareDialog(int id, Dialog dialog) {
2367 switch (id) {
2368 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 break;
2370 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002371 if (mFolderInfo != null) {
2372 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2373 final CharSequence text = mFolderInfo.title;
2374 input.setText(text);
2375 input.setSelection(0, text.length());
2376 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 break;
2378 }
2379 }
2380
2381 void showRenameDialog(FolderInfo info) {
2382 mFolderInfo = info;
2383 mWaitingForResult = true;
2384 showDialog(DIALOG_RENAME_FOLDER);
2385 }
2386
Michael Jurka0280c3b2010-09-17 15:00:07 -07002387 private void showAddDialog(int intersectX, int intersectY) {
2388 resetAddInfo();
2389 mAddIntersectCellX = intersectX;
2390 mAddIntersectCellY = intersectY;
2391 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002392 mWaitingForResult = true;
2393 showDialog(DIALOG_CREATE_SHORTCUT);
2394 }
2395
Joe Onoratodeb98af2010-02-19 14:59:39 -08002396 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002397 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002398 Bundle bundle = new Bundle();
2399
2400 ArrayList<String> shortcutNames = new ArrayList<String>();
2401 shortcutNames.add(getString(R.string.group_applications));
2402 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2403
2404 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2405 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2406 R.drawable.ic_launcher_application));
2407 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2408
2409 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2410 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002411 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002412 pickIntent.putExtras(bundle);
2413
Joe Onoratodeb98af2010-02-19 14:59:39 -08002414 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002415 }
2416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 private class RenameFolder {
2418 private EditText mInput;
2419
2420 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2422 mInput = (EditText) layout.findViewById(R.id.folder_name);
2423
2424 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2425 builder.setIcon(0);
2426 builder.setTitle(getString(R.string.rename_folder_title));
2427 builder.setCancelable(true);
2428 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2429 public void onCancel(DialogInterface dialog) {
2430 cleanup();
2431 }
2432 });
2433 builder.setNegativeButton(getString(R.string.cancel_action),
2434 new Dialog.OnClickListener() {
2435 public void onClick(DialogInterface dialog, int which) {
2436 cleanup();
2437 }
2438 }
2439 );
2440 builder.setPositiveButton(getString(R.string.rename_action),
2441 new Dialog.OnClickListener() {
2442 public void onClick(DialogInterface dialog, int which) {
2443 changeFolderName();
2444 }
2445 }
2446 );
2447 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002448
2449 final AlertDialog dialog = builder.create();
2450 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2451 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002452 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002453 mInput.requestFocus();
2454 InputMethodManager inputManager = (InputMethodManager)
2455 getSystemService(Context.INPUT_METHOD_SERVICE);
2456 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002457 }
2458 });
2459
2460 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002461 }
2462
2463 private void changeFolderName() {
2464 final String name = mInput.getText().toString();
2465 if (!TextUtils.isEmpty(name)) {
2466 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002467 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002468 mFolderInfo.title = name;
2469 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2470
Joe Onorato9c1289c2009-08-17 11:03:03 -04002471 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002472 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002473 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 } else {
2475 final FolderIcon folderIcon = (FolderIcon)
2476 mWorkspace.getViewForTag(mFolderInfo);
2477 if (folderIcon != null) {
2478 folderIcon.setText(name);
2479 getWorkspace().requestLayout();
2480 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002481 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 mWorkspaceLoading = true;
2483 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 }
2485 }
2486 }
2487 cleanup();
2488 }
2489
2490 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 dismissDialog(DIALOG_RENAME_FOLDER);
2492 mWaitingForResult = false;
2493 mFolderInfo = null;
2494 }
2495 }
2496
Daniel Sandler843e8602010-06-07 14:59:01 -04002497 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2498 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002499 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002500 }
2501
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002502 // AllAppsView.Watcher
2503 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002504 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2505 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002506 mWorkspace.setVisibility(View.GONE);
2507 }
2508 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002509
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002510 private void showToolbarButton(View button) {
2511 button.setAlpha(1.0f);
2512 button.setVisibility(View.VISIBLE);
2513 button.setFocusable(true);
2514 button.setClickable(true);
2515 }
2516
2517 private void hideToolbarButton(View button) {
2518 button.setAlpha(0.0f);
2519 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2520 button.setVisibility(View.INVISIBLE);
2521 button.setFocusable(false);
2522 button.setClickable(false);
2523 }
2524
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002525 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002526 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002527 *
2528 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2529 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002530 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002531 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002532 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002533 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 final boolean showing = show;
2535 final boolean hiding = !show;
2536
2537 final int duration = show ?
2538 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2539 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2540
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002542 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2543 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002544 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002545 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002546 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002549 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002550 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002551 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002552 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002553 });
2554 seq.play(anim);
2555 } else {
2556 if (showing) {
2557 showToolbarButton(view);
2558 } else {
2559 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002560 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002561 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002562 }
2563
2564 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002565 * Show/hide the appropriate toolbar buttons for newState.
2566 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002567 *
2568 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002569 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2570 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002571 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002572 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002573 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574 final View allAppsButton = findViewById(R.id.all_apps_button);
Michael Jurka800242b2010-12-16 11:39:26 -08002575 final View divider = findViewById(R.id.divider);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 final View configureButton = findViewById(R.id.configure_button);
2577
2578 switch (newState) {
2579 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 hideOrShowToolbarButton(true, searchButton, showSeq);
2581 hideOrShowToolbarButton(true, allAppsButton, showSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002582 hideOrShowToolbarButton(true, divider, showSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002583 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);
Michael Jurka800242b2010-12-16 11:39:26 -08002589 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002591 break;
2592 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002593 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002594 hideOrShowToolbarButton(false, searchButton, hideSeq);
Michael Jurka800242b2010-12-16 11:39:26 -08002595 hideOrShowToolbarButton(false, divider, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002597 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 break;
2599 }
2600 }
2601
2602 /**
2603 * Helper method for the cameraZoomIn/cameraZoomOut animations
2604 * @param view The view being animated
2605 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2606 * @param scaleFactor The scale factor used for the zoom
2607 */
2608 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2609 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002610
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002612 // Set pivotY so that at the starting zoom factor, the view is partially
2613 // visible. Modifying initialHeightFactor changes how much of the view is
2614 // initially showing, and hence the perceived angle from which the view enters.
2615 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002616 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002617 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002618 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002619 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 }
2621 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002622
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002623 /**
2624 * Zoom the camera out from the workspace to reveal 'toView'.
2625 * Assumes that the view to show is anchored at either the very top or very bottom
2626 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002628 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002629 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002630 final Resources res = getResources();
2631 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2632 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 final boolean toAllApps = (toState == State.ALL_APPS);
2634 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002635
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 setPivotsForZoom(toView, toState, scale);
2637
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002638 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002639 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002640 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002641 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002642 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002643
2644 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002645 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2646 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2647 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2648 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002649
2650 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002651 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002652 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002653 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 // Prepare the position
2655 toView.setTranslationX(0.0f);
2656 toView.setTranslationY(0.0f);
2657 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002658 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002660 @Override
2661 public void onAnimationEndOrCancel(Animator animation) {
2662 // If we don't set the final scale values here, if this animation is cancelled
2663 // it will have the wrong scale value and subsequent cameraPan animations will
2664 // not fix that
2665 toView.setScaleX(1.0f);
2666 toView.setScaleY(1.0f);
2667 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002668 });
2669
Chet Haaseb1254a62010-09-07 13:35:00 -07002670 AnimatorSet toolbarHideAnim = new AnimatorSet();
2671 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2673
2674 // toView should appear right at the end of the workspace shrink animation
2675 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2676
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002677 if (mStateAnimation != null) mStateAnimation.cancel();
2678 mStateAnimation = new AnimatorSet();
2679 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2680 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681
2682 // Show the new toolbar buttons just as the main animation is ending
2683 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002684 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2685 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002686 } else {
2687 toView.setTranslationX(0.0f);
2688 toView.setTranslationY(0.0f);
2689 toView.setScaleX(1.0f);
2690 toView.setScaleY(1.0f);
2691 toView.setVisibility(View.VISIBLE);
2692 hideAndShowToolbarButtons(toState, null, null);
2693 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002694 }
2695
2696 /**
2697 * Zoom the camera back into the workspace, hiding 'fromView'.
2698 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002699 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2700 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002701 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002702 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002703 cameraZoomIn(fromState, animated, false);
2704 }
2705
2706 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002707 Resources res = getResources();
2708 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2709 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002710 final View fromView =
2711 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2712
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002713 mCustomizePagedView.endChoiceMode();
2714 mAllAppsPagedView.endChoiceMode();
2715
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002716 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002717
Michael Jurkad3ef3062010-11-23 16:23:58 -08002718 if (!springLoaded) {
2719 mWorkspace.unshrink(animated);
2720 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721
2722 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002723 if (mStateAnimation != null) mStateAnimation.cancel();
2724 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002725 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2726 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2727 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2728 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002729 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2730
2731 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2732 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2733 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2734 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002735 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002736 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002737 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002738 }
2739 });
2740
Chet Haaseb1254a62010-09-07 13:35:00 -07002741 AnimatorSet toolbarHideAnim = new AnimatorSet();
2742 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002743 if (!springLoaded) {
2744 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2745 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746
Adam Cohen61033d32010-11-15 18:29:44 -08002747 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002748
2749 // Show the new toolbar buttons at the very end of the whole animation
2750 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2751 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002752 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2753 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002754 } else {
2755 fromView.setVisibility(View.GONE);
Michael Jurkad3ef3062010-11-23 16:23:58 -08002756 if (!springLoaded) {
2757 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2758 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 }
2761
2762 /**
2763 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2764 * This is the transition used on xlarge screens to go between all apps and
2765 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002766 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2767 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2768 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002769 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002770 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002771 final Resources res = getResources();
2772 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002773 final int workspaceHeight = mWorkspace.getHeight();
2774
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002775 final boolean fromAllApps = (fromState == State.ALL_APPS);
2776 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2777 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002778
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002779 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2780 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2781 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2782 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002783
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784 mCustomizePagedView.endChoiceMode();
2785 mAllAppsPagedView.endChoiceMode();
2786
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002787 if (toState == State.ALL_APPS) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002788 mWorkspace.shrink(Workspace.ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002789 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002790 mWorkspace.shrink(Workspace.ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002791 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002792
2793 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002794 if (mStateAnimation != null) mStateAnimation.cancel();
2795 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002796 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002797 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002798 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799 toView.setVisibility(View.VISIBLE);
2800 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002801 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002802 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002803 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002804 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002805 fromView.setVisibility(View.GONE);
2806 }
2807 });
2808
Chet Haaseb1254a62010-09-07 13:35:00 -07002809 AnimatorSet toolbarHideAnim = new AnimatorSet();
2810 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002811 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2812
Chet Haase472b2812010-10-14 07:02:04 -07002813 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2814 fromViewStartY, fromViewEndY);
2815 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002816 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2817 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2818 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2819 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2820 );
Chet Haase472b2812010-10-14 07:02:04 -07002821 fromAnim.setDuration(duration);
2822 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002823
2824 // Show the new toolbar buttons just as the main animation is ending
2825 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002826 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2827 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002828 } else {
2829 fromView.setY(fromViewEndY);
2830 fromView.setVisibility(View.GONE);
2831 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002832 toView.setScaleX(1.0f);
2833 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002834 toView.setVisibility(View.VISIBLE);
2835 hideAndShowToolbarButtons(toState, null, null);
2836 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002837 }
2838
2839 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002840 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002841 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002842 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002843
Michael Jurka79212d82010-07-30 16:36:20 -07002844 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002847 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002848 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002849 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002850 } else {
2851 mAllAppsGrid.zoom(1.0f, animated);
2852 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002853
Romain Guyc16fea72010-03-12 17:17:56 -08002854 ((View) mAllAppsGrid).setFocusable(true);
2855 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002856
Joe Onorato7c312c12009-08-13 21:36:53 -07002857 // TODO: fade these two too
2858 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002859
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002860 // Change the state *after* we've called all the transition code
2861 mState = State.ALL_APPS;
2862 }
2863
2864
2865 void showWorkspace(boolean animated) {
2866 showWorkspace(animated, null);
2867 }
2868
2869 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002870 if (layout != null) {
2871 // always animated, but that's ok since we never specify a layout and
2872 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002873 mWorkspace.unshrink(layout);
2874 } else {
2875 mWorkspace.unshrink(animated);
2876 }
2877 if (mState == State.ALL_APPS) {
2878 closeAllApps(animated);
2879 } else if (mState == State.CUSTOMIZE) {
2880 hideCustomizationDrawer(animated);
2881 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002882
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002883 // Change the state *after* we've called all the transition code
2884 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002885 }
2886
Michael Jurkad3ef3062010-11-23 16:23:58 -08002887 void enterSpringLoadedDragMode(CellLayout layout) {
2888 mWorkspace.enterSpringLoadedDragMode(layout);
2889 if (mState == State.ALL_APPS) {
2890 cameraZoomIn(State.ALL_APPS, true, true);
2891 mState = State.ALL_APPS_SPRING_LOADED;
2892 } else if (mState == State.CUSTOMIZE) {
2893 cameraZoomIn(State.CUSTOMIZE, true, true);
2894 mState = State.CUSTOMIZE_SPRING_LOADED;
2895 }/* else {
2896 // we're already in spring loaded mode; don't do anything
2897 }*/
2898 }
2899
2900 void exitSpringLoadedDragMode() {
2901 if (mState == State.ALL_APPS_SPRING_LOADED) {
2902 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2903 cameraZoomOut(State.ALL_APPS, true);
2904 mState = State.ALL_APPS;
2905 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2906 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2907 cameraZoomOut(State.CUSTOMIZE, true);
2908 mState = State.CUSTOMIZE;
2909 }/* else {
2910 // we're not in spring loaded mode; don't do anything
2911 }*/
2912 }
2913
Joe Onoratob2061212009-11-24 16:13:54 -05002914 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002915 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002916 * - Home from workspace
2917 * - from center screen
2918 * - from other screens
2919 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002920 * - from center screen
2921 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002922 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002923 * - from center screen
2924 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002925 * - Launch app from workspace and quit
2926 * - with back
2927 * - with home
2928 * - Launch app from all apps and quit
2929 * - with back
2930 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002931 * - Go to a screen that's not the default, then all
2932 * apps, and launch and app, and go back
2933 * - with back
2934 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002935 * - On workspace, long press power and go back
2936 * - with back
2937 * - with home
2938 * - On all apps, long press power and go back
2939 * - with back
2940 * - with home
2941 * - On workspace, power off
2942 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002943 * - Launch an app and turn off the screen while in that app
2944 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002945 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002946 * - From all apps
2947 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002948 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2949 * - From all apps
2950 * - From the center workspace
2951 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002952 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002953 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002954 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002955 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002956 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002957 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002958 } else {
2959 mAllAppsGrid.zoom(0.0f, animated);
2960 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002961 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002962 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002963 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002964 }
2965
Joe Onorato7c312c12009-08-13 21:36:53 -07002966 void lockAllApps() {
2967 // TODO
2968 }
2969
2970 void unlockAllApps() {
2971 // TODO
2972 }
2973
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002974 // Show the customization drawer (only exists in x-large configuration)
2975 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002976 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002977 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002978 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002979 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002980 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002981 // Change the state *after* we've called all the transition code
2982 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002983 }
2984
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002985 // Hide the customization drawer (only exists in x-large configuration)
2986 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002987 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002988 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002989 }
2990 }
2991
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002992 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2993 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2994 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002995 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002996 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002997
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002998 void onWorkspaceClick(CellLayout layout) {
2999 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003000 }
3001
Michael Jurka4ef207b2010-11-29 17:05:45 -08003002 // if successful in getting icon, return it; otherwise, set button to use default drawable
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003003 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003004 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3005 ImageView button = (ImageView) findViewById(buttonId);
3006 Drawable toolbarIcon = null;
3007 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003008 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003009 // Look for the toolbar icon specified in the activity meta-data
3010 Bundle metaData = packageManager.getActivityInfo(
3011 activityName, PackageManager.GET_META_DATA).metaData;
3012 if (metaData != null) {
3013 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3014 if (iconResId != 0) {
3015 Resources res = packageManager.getResourcesForActivity(activityName);
3016 toolbarIcon = res.getDrawable(iconResId);
3017 }
3018 }
3019 } catch (NameNotFoundException e) {
3020 // Do nothing
3021 }
3022 // If we were unable to find the icon via the meta-data, use a generic one
3023 if (toolbarIcon == null) {
3024 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003025 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003026 } else {
3027 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003028 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003029 }
3030 }
3031
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003032 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003033 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003034 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003035 }
3036
Michael Jurka0423dcf2010-10-05 14:56:18 -07003037 private void updateGlobalSearchIcon() {
3038 if (LauncherApplication.isScreenXLarge()) {
3039 final SearchManager searchManager =
3040 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3041 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003042 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003043 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003044 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003045 } else {
3046 findViewById(R.id.search_button).setVisibility(View.GONE);
3047 }
3048 }
3049 }
3050
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003051 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003052 updateButtonWithDrawable(R.id.search_button, d);
3053 }
3054
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003055 private void updateVoiceSearchIcon() {
3056 if (LauncherApplication.isScreenXLarge()) {
3057 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3058 ComponentName activityName = intent.resolveActivity(getPackageManager());
3059 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003060 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003061 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3062 } else {
3063 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003064 }
3065 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003066 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003067
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003068 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003069 updateButtonWithDrawable(R.id.voice_button, d);
3070 }
3071
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003072 /**
3073 * Sets the app market icon (shown when all apps is visible on x-large screens)
3074 */
3075 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003076 if (LauncherApplication.isScreenXLarge()) {
3077 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3078 // Find the app market activity by resolving an intent.
3079 // (If multiple app markets are installed, it will return the ResolverActivity.)
3080 ComponentName activityName = intent.resolveActivity(getPackageManager());
3081 if (activityName != null) {
3082 mAppMarketIntent = intent;
Michael Jurka4ef207b2010-11-29 17:05:45 -08003083 sAppMarketIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003084 R.id.market_button, activityName, R.drawable.app_market_generic);
3085 }
3086 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003087 }
3088
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003089 private void updateAppMarketIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003090 updateButtonWithDrawable(R.id.market_button, d);
3091 }
3092
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003093 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003094 * Displays the shortcut creation dialog and launches, if necessary, the
3095 * appropriate activity.
3096 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003097 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003098 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3099 DialogInterface.OnShowListener {
3100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003105
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3107 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003108 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003110 builder.setInverseBackgroundForced(true);
3111
3112 AlertDialog dialog = builder.create();
3113 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003114 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003115 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 return dialog;
3118 }
3119
3120 public void onCancel(DialogInterface dialog) {
3121 mWaitingForResult = false;
3122 cleanup();
3123 }
3124
Romain Guycbb89e42009-06-08 15:52:54 -07003125 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003126 }
3127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003128 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003129 try {
3130 dismissDialog(DIALOG_CREATE_SHORTCUT);
3131 } catch (Exception e) {
3132 // An exception is thrown if the dialog is not visible, which is fine
3133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134 }
3135
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003136 /**
3137 * Handle the action clicked in the "Add to home" dialog.
3138 */
3139 public void onClick(DialogInterface dialog, int which) {
3140 Resources res = getResources();
3141 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003142
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003143 switch (which) {
3144 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003145 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003146 break;
3147 }
Romain Guycbb89e42009-06-08 15:52:54 -07003148
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003149 case AddAdapter.ITEM_APPWIDGET: {
3150 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003151
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003152 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3153 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003154 // start the pick activity
3155 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3156 break;
3157 }
Romain Guycbb89e42009-06-08 15:52:54 -07003158
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003159 case AddAdapter.ITEM_LIVE_FOLDER: {
3160 // Insert extra item to handle inserting folder
3161 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003162
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003163 ArrayList<String> shortcutNames = new ArrayList<String>();
3164 shortcutNames.add(res.getString(R.string.group_folder));
3165 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003166
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003167 ArrayList<ShortcutIconResource> shortcutIcons =
3168 new ArrayList<ShortcutIconResource>();
3169 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3170 R.drawable.ic_launcher_folder));
3171 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3172
3173 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3174 pickIntent.putExtra(Intent.EXTRA_INTENT,
3175 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3176 pickIntent.putExtra(Intent.EXTRA_TITLE,
3177 getText(R.string.title_select_live_folder));
3178 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003179
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003180 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3181 break;
3182 }
3183
3184 case AddAdapter.ITEM_WALLPAPER: {
3185 startWallpaper();
3186 break;
3187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 }
3189 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003190
3191 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003192 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 }
3195
3196 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003197 * Receives notifications when applications are added/removed.
3198 */
3199 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3200 @Override
3201 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003202 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003203 String reason = intent.getStringExtra("reason");
3204 if (!"homekey".equals(reason)) {
3205 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003206 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003207 animate = false;
3208 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003209 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003210 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003211 }
3212 }
3213
3214 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003215 * Receives notifications whenever the appwidgets are reset.
3216 */
3217 private class AppWidgetResetObserver extends ContentObserver {
3218 public AppWidgetResetObserver() {
3219 super(new Handler());
3220 }
3221
3222 @Override
3223 public void onChange(boolean selfChange) {
3224 onAppWidgetReset();
3225 }
3226 }
3227
3228 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003229 * If the activity is currently paused, signal that we need to re-run the loader
3230 * in onResume.
3231 *
3232 * This needs to be called from incoming places where resources might have been loaded
3233 * while we are paused. That is becaues the Configuration might be wrong
3234 * when we're not running, and if it comes back to what it was when we
3235 * were paused, we are not restarted.
3236 *
3237 * Implementation of the method from LauncherModel.Callbacks.
3238 *
3239 * @return true if we are currently paused. The caller might be able to
3240 * skip some work in that case since we will come back again.
3241 */
3242 public boolean setLoadOnResume() {
3243 if (mPaused) {
3244 Log.i(TAG, "setLoadOnResume");
3245 mOnResumeNeedsLoad = true;
3246 return true;
3247 } else {
3248 return false;
3249 }
3250 }
3251
3252 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003255 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003256 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003257 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003258 } else {
3259 return SCREEN_COUNT / 2;
3260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003262
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003263 void setAllAppsPagedView(PagedView view) {
3264 mAllAppsPagedView = view;
3265 }
3266
Joe Onorato9c1289c2009-08-17 11:03:03 -04003267 /**
3268 * Refreshes the shortcuts shown on the workspace.
3269 *
3270 * Implementation of the method from LauncherModel.Callbacks.
3271 */
3272 public void startBinding() {
3273 final Workspace workspace = mWorkspace;
3274 int count = workspace.getChildCount();
3275 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003276 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3278 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003279
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 if (DEBUG_USER_INTERFACE) {
3281 android.widget.Button finishButton = new android.widget.Button(this);
3282 finishButton.setText("Finish");
3283 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3284
3285 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3286 public void onClick(View v) {
3287 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003288 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 }
3291 }
3292
3293 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 * Bind the items start-end from the list.
3295 *
3296 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003297 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3299
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003300 setLoadOnResume();
3301
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 final Workspace workspace = mWorkspace;
3303
3304 for (int i=start; i<end; i++) {
3305 final ItemInfo item = shortcuts.get(i);
3306 mDesktopItems.add(item);
3307 switch (item.itemType) {
3308 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3309 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003310 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003311 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3312 false);
3313 break;
3314 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3315 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003316 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003317 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003318 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3319 false);
3320 break;
3321 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3322 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3323 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003324 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 (LiveFolderInfo) item);
3326 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3327 false);
3328 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003330 }
3331
Joe Onorato9c1289c2009-08-17 11:03:03 -04003332 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003333 }
3334
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 /**
3336 * Implementation of the method from LauncherModel.Callbacks.
3337 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003338 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003339 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003340 sFolders.clear();
3341 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003342 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343
3344 /**
3345 * Add the views for a widget to the workspace.
3346 *
3347 * Implementation of the method from LauncherModel.Callbacks.
3348 */
3349 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003350 setLoadOnResume();
3351
Daniel Sandler843e8602010-06-07 14:59:01 -04003352 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3353 if (DEBUG_WIDGETS) {
3354 Log.d(TAG, "bindAppWidget: " + item);
3355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003356 final Workspace workspace = mWorkspace;
3357
3358 final int appWidgetId = item.appWidgetId;
3359 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003360 if (DEBUG_WIDGETS) {
3361 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3362 }
3363
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3365
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3367 item.hostView.setTag(item);
3368
3369 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3370 item.cellY, item.spanX, item.spanY, false);
3371
Adam Cohended9f8d2010-11-03 13:25:16 -07003372 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3373
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 workspace.requestLayout();
3375
3376 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003377
3378 if (DEBUG_WIDGETS) {
3379 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3380 + (SystemClock.uptimeMillis()-start) + "ms");
3381 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003382 }
3383
3384 /**
3385 * Callback saying that there aren't any more items to bind.
3386 *
3387 * Implementation of the method from LauncherModel.Callbacks.
3388 */
3389 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003390 setLoadOnResume();
3391
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 if (mSavedState != null) {
3393 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003394 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003395 }
3396
3397 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3398 if (userFolders != null) {
3399 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003400 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401 if (info != null) {
3402 openFolder(info);
3403 }
3404 }
3405 final Folder openFolder = mWorkspace.getOpenFolder();
3406 if (openFolder != null) {
3407 openFolder.requestFocus();
3408 }
3409 }
3410
Joe Onorato9c1289c2009-08-17 11:03:03 -04003411 mSavedState = null;
3412 }
3413
3414 if (mSavedInstanceState != null) {
3415 super.onRestoreInstanceState(mSavedInstanceState);
3416 mSavedInstanceState = null;
3417 }
3418
Joe Onorato9c1289c2009-08-17 11:03:03 -04003419 mWorkspaceLoading = false;
3420 }
3421
3422 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003423 * Updates the icons on the launcher that are affected by changes to the package list
3424 * on the device.
3425 */
3426 private void updateIconsAffectedByPackageManagerChanges() {
3427 updateAppMarketIcon();
3428 updateGlobalSearchIcon();
3429 updateVoiceSearchIcon();
3430 }
3431
3432 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003433 * Add the icons for all apps.
3434 *
3435 * Implementation of the method from LauncherModel.Callbacks.
3436 */
3437 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003438 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003439 if (mCustomizePagedView != null) {
3440 mCustomizePagedView.setApps(apps);
3441 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003442 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003443 }
3444
3445 /**
3446 * A package was installed.
3447 *
3448 * Implementation of the method from LauncherModel.Callbacks.
3449 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003450 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003451 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003453 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003454 if (mCustomizePagedView != null) {
3455 mCustomizePagedView.addApps(apps);
3456 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003457 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 }
3459
3460 /**
3461 * A package was updated.
3462 *
3463 * Implementation of the method from LauncherModel.Callbacks.
3464 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003465 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003466 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003467 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003468 mWorkspace.updateShortcuts(apps);
3469 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003470 if (mCustomizePagedView != null) {
3471 mCustomizePagedView.updateApps(apps);
3472 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003473 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003474 }
3475
3476 /**
3477 * A package was uninstalled.
3478 *
3479 * Implementation of the method from LauncherModel.Callbacks.
3480 */
Joe Onorato36115782010-06-17 13:28:48 -04003481 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003482 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003483 if (permanent) {
3484 mWorkspace.removeItems(apps);
3485 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003486 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003487 if (mCustomizePagedView != null) {
3488 mCustomizePagedView.removeApps(apps);
3489 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003490 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003491 }
Joe Onoratobe386092009-11-17 17:32:16 -08003492
3493 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003494 * A number of packages were updated.
3495 */
3496 public void bindPackagesUpdated() {
3497 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003498 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003499 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003500 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003501 }
3502
3503 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003504 * Prints out out state for debugging.
3505 */
3506 public void dumpState() {
3507 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003508 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003509 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3510 Log.d(TAG, "mRestoring=" + mRestoring);
3511 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3512 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3513 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003514 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003515 mModel.dumpState();
3516 mAllAppsGrid.dumpState();
3517 Log.d(TAG, "END launcher2 dump state");
3518 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003519}