blob: bee69b90d21d17a195d6106694a04e1eab0f147f [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
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Patrick Dubroy7247f632010-08-04 16:02:59 -070023import android.animation.Animatable;
24import android.animation.AnimatableListenerAdapter;
25import android.animation.Animator;
26import android.animation.PropertyAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Patrick Dubroy7247f632010-08-04 16:02:59 -070028import android.animation.Sequencer;
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;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070045import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Patrick Dubroyceae05d2010-08-30 10:40:53 -070048import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040052import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080055import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070056import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080066import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070067import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070082import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070084import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070085import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070092import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070093import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070094import android.widget.TabHost.OnTabChangeListener;
95import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070096import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
100import java.io.DataInputStream;
101import java.io.DataOutputStream;
102import java.io.FileNotFoundException;
103import java.io.IOException;
104import java.util.ArrayList;
105import java.util.HashMap;
106import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int WALLPAPER_SCREENS_SPAN = 2;
122
123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
127 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
128 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
129 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700130 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 private static final int REQUEST_CREATE_SHORTCUT = 1;
133 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
137 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
142
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800143 static final int SCREEN_COUNT = 5;
144 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Joe Onorato7c312c12009-08-13 21:36:53 -0700146 static final int DIALOG_CREATE_SHORTCUT = 1;
147 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
153 // Type: boolean
154 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
171 // Type: int[]
172 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
177
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 // tags for the customization tabs
179 private static final String WIDGETS_TAG = "widgets";
180 private static final String FOLDERS_TAG = "folders";
181 private static final String SHORTCUTS_TAG = "shortcuts";
182 private static final String WALLPAPERS_TAG = "wallpapers";
183
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
185
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186 /** The different states that Launcher can be in. */
187 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700207 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private CellLayout.CellInfo mMenuAddInfo;
209 private final int[] mCellCoordinates = new int[2];
210 private FolderInfo mFolderInfo;
211
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700215 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 private CustomizePagedView mCustomizePagedView;
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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static LocaleConfiguration sLocaleConfiguration = null;
234
Romain Guy84f296c2009-11-04 15:00:44 -0800235 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Romain Guy1fbc1c82009-11-09 20:43:08 -0800238 private ImageView mPreviousView;
239 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500240
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400242 private String[] mHotseatConfig = null;
243 private Intent[] mHotseats = null;
244 private Drawable[] mHotseatIcons = null;
245 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700247 private Intent mAppMarketIntent = null;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 @Override
250 protected void onCreate(Bundle savedInstanceState) {
251 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700252
Winson Chungaafa03c2010-06-11 17:34:16 -0700253 if (LauncherApplication.isInPlaceRotationEnabled()) {
254 // hide the status bar (temporary until we get the status bar design figured out)
255 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
256 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
257 }
258
Joe Onorato0589f0f2010-02-08 13:44:00 -0800259 LauncherApplication app = ((LauncherApplication)getApplication());
260 mModel = app.setLauncher(this);
261 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400262 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700266 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
267 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 if (PROFILE_STARTUP) {
270 android.os.Debug.startMethodTracing("/sdcard/launcher");
271 }
272
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400273 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 checkForLocaleChange();
275 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700277 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
278 if (mHomeCustomizationDrawer != null) {
279 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700280
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 // share the same customization workspace across all the tabs
282 mCustomizePagedView = new CustomizePagedView(this);
283 TabContentFactory contentFactory = new TabContentFactory() {
284 public View createTabContent(String tag) {
285 return mCustomizePagedView;
286 }
287 };
288
Michael Jurka946ad472010-07-09 18:05:18 -0700289 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700290 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
291 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700292 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
294 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700295 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700296 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
297 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700298 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
300 .setIndicator(wallpapersLabel).setContent(contentFactory));
301
302 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
303 // new assets
304 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
305 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
306 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
307 TextView text = (TextView) tab.getChildAt(1);
308 text.setTextSize(20.0f);
309 text.setPadding(20, 0, 20, 0);
310 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
311 tab.setBackgroundDrawable(null);
312 }
313
314 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
315 public void onTabChanged(String tabId) {
316 // animate the changing of the tab content by fading pages in and out
317 final int duration = 150;
318 final float alpha = mCustomizePagedView.getAlpha();
319 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
320 "alpha", alpha, 0.0f);
321 alphaAnim.addListener(new AnimatableListenerAdapter() {
322 public void onAnimationEnd(Animatable animation) {
323 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
324 if (tag == WIDGETS_TAG) {
325 mCustomizePagedView.setCustomizationFilter(
326 CustomizePagedView.CustomizationType.WidgetCustomization);
327 } else if (tag == FOLDERS_TAG) {
328 mCustomizePagedView.setCustomizationFilter(
329 CustomizePagedView.CustomizationType.FolderCustomization);
330 } else if (tag == SHORTCUTS_TAG) {
331 mCustomizePagedView.setCustomizationFilter(
332 CustomizePagedView.CustomizationType.ShortcutCustomization);
333 } else if (tag == WALLPAPERS_TAG) {
334 mCustomizePagedView.setCustomizationFilter(
335 CustomizePagedView.CustomizationType.WallpaperCustomization);
336 }
337
338 final float alpha = mCustomizePagedView.getAlpha();
339 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
340 "alpha", alpha, 1.0f);
341 alphaAnim.start();
342 }
343 });
344 alphaAnim.start();
345 }
346 });
Michael Jurka946ad472010-07-09 18:05:18 -0700347
348 mHomeCustomizationDrawer.setCurrentTab(0);
349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 setupViews();
351
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800352 registerContentObservers();
353
Joe Onorato7c312c12009-08-13 21:36:53 -0700354 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 mSavedState = savedInstanceState;
357 restoreState(mSavedState);
358
359 if (PROFILE_STARTUP) {
360 android.os.Debug.stopMethodTracing();
361 }
362
363 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
366
367 // For handling default keys
368 mDefaultKeySsb = new SpannableStringBuilder();
369 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800370
371 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
372 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
Romain Guycbb89e42009-06-08 15:52:54 -0700374
Winson Chungaafa03c2010-06-11 17:34:16 -0700375 @Override
376 public void onConfigurationChanged(Configuration newConfig) {
377 // TODO Auto-generated method stub
378 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 if (sLocaleConfiguration == null) {
383 new AsyncTask<Void, Void, LocaleConfiguration>() {
384 @Override
385 protected LocaleConfiguration doInBackground(Void... unused) {
386 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
387 readConfiguration(Launcher.this, localeConfiguration);
388 return localeConfiguration;
389 }
390
391 @Override
392 protected void onPostExecute(LocaleConfiguration result) {
393 sLocaleConfiguration = result;
394 checkForLocaleChange(); // recursive, but now with a locale configuration
395 }
396 }.execute();
397 return;
398 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final Configuration configuration = getResources().getConfiguration();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final String locale = configuration.locale.toString();
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mcc = configuration.mcc;
407
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 final int mnc = configuration.mnc;
410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414 sLocaleConfiguration.locale = locale;
415 sLocaleConfiguration.mcc = mcc;
416 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700417
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400419 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420
421 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
422 new Thread("WriteLocaleConfiguration") {
423 public void run() {
424 writeConfiguration(Launcher.this, localeConfiguration);
425 }
426 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700427 }
428 }
429
430 private static class LocaleConfiguration {
431 public String locale;
432 public int mcc = -1;
433 public int mnc = -1;
434 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700435
Romain Guy98d01652009-06-30 16:21:04 -0700436 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
437 DataInputStream in = null;
438 try {
439 in = new DataInputStream(context.openFileInput(PREFERENCES));
440 configuration.locale = in.readUTF();
441 configuration.mcc = in.readInt();
442 configuration.mnc = in.readInt();
443 } catch (FileNotFoundException e) {
444 // Ignore
445 } catch (IOException e) {
446 // Ignore
447 } finally {
448 if (in != null) {
449 try {
450 in.close();
451 } catch (IOException e) {
452 // Ignore
453 }
454 }
455 }
456 }
457
458 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
459 DataOutputStream out = null;
460 try {
461 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
462 out.writeUTF(configuration.locale);
463 out.writeInt(configuration.mcc);
464 out.writeInt(configuration.mnc);
465 out.flush();
466 } catch (FileNotFoundException e) {
467 // Ignore
468 } catch (IOException e) {
469 //noinspection ResultOfMethodCallIgnored
470 context.getFileStreamPath(PREFERENCES).delete();
471 } finally {
472 if (out != null) {
473 try {
474 out.close();
475 } catch (IOException e) {
476 // Ignore
477 }
478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480 }
481
482 static int getScreen() {
483 synchronized (sLock) {
484 return sScreen;
485 }
486 }
487
488 static void setScreen(int screen) {
489 synchronized (sLock) {
490 sScreen = screen;
491 }
492 }
493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700495 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496
497 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700498 // TODO: Put back when we decide about scrolling the wallpaper
499 // boolean isPortrait = display.getWidth() < display.getHeight();
500 // final int width = isPortrait ? display.getWidth() : display.getHeight();
501 // final int height = isPortrait ? display.getHeight() : display.getWidth();
502 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
503 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
505
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400506 // Note: This doesn't do all the client-id magic that BrowserProvider does
507 // in Browser. (http://b/2425179)
508 private Uri getDefaultBrowserUri() {
509 String url = getString(R.string.default_browser_url);
510 if (url.indexOf("{CID}") != -1) {
511 url = url.replace("{CID}", "android-google");
512 }
513 return Uri.parse(url);
514 }
515
516 // Load the Intent templates from arrays.xml to populate the hotseats. For
517 // each Intent, if it resolves to a single app, use that as the launch
518 // intent & use that app's label as the contentDescription. Otherwise,
519 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400520 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400521 if (mHotseatConfig == null) {
522 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
523 if (mHotseatConfig.length > 0) {
524 mHotseats = new Intent[mHotseatConfig.length];
525 mHotseatLabels = new CharSequence[mHotseatConfig.length];
526 mHotseatIcons = new Drawable[mHotseatConfig.length];
527 } else {
528 mHotseats = null;
529 mHotseatIcons = null;
530 mHotseatLabels = null;
531 }
532
533 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
534 for (int i=0; i<mHotseatConfig.length; i++) {
535 // load icon for this slot; currently unrelated to the actual activity
536 try {
537 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
538 } catch (ArrayIndexOutOfBoundsException ex) {
539 Log.w(TAG, "Missing hotseat_icons array item #" + i);
540 mHotseatIcons[i] = null;
541 }
542 }
543 hotseatIconDrawables.recycle();
544 }
545
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 for (int i=0; i<mHotseatConfig.length; i++) {
548 Intent intent = null;
549 if (mHotseatConfig[i].equals("*BROWSER*")) {
550 // magic value meaning "launch user's default web browser"
551 // replace it with a generic web request so we can see if there is indeed a default
552 String defaultUri = getString(R.string.default_browser_url);
553 intent = new Intent(
554 Intent.ACTION_VIEW,
555 ((defaultUri != null)
556 ? Uri.parse(defaultUri)
557 : getDefaultBrowserUri())
558 ).addCategory(Intent.CATEGORY_BROWSABLE);
559 // note: if the user launches this without a default set, she
560 // will always be taken to the default URL above; this is
561 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700562 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 // URL is unavoidably sent to the chosen app.
564 } else {
565 try {
566 intent = Intent.parseUri(mHotseatConfig[i], 0);
567 } catch (java.net.URISyntaxException ex) {
568 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
569 // bogus; leave intent=null
570 }
571 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700572
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 if (intent == null) {
574 mHotseats[i] = null;
575 mHotseatLabels[i] = getText(R.string.activity_not_found);
576 continue;
577 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578
579 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700580 Log.d(TAG, "loadHotseats: hotseat " + i
581 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 + "]");
584 }
585
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
587 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 Log.d(TAG, "Best match for intent: " + bestMatch);
590 Log.d(TAG, "All matches: ");
591 for (ResolveInfo ri : allMatches) {
592 Log.d(TAG, " --> " + ri);
593 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400594 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400595 // did this resolve to a single app, or the resolver?
596 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700597 // can't find any activity to handle this. let's leave the
598 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 // to launch.
600 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 // set accessibility text to "Not installed"
603 mHotseatLabels[i] = getText(R.string.activity_not_found);
604 } else {
605 boolean found = false;
606 for (ResolveInfo ri : allMatches) {
607 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
608 && bestMatch.activityInfo.applicationInfo.packageName
609 .equals(ri.activityInfo.applicationInfo.packageName)) {
610 found = true;
611 break;
612 }
613 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700614
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 if (!found) {
616 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
617 // the bestMatch is probably the ResolveActivity, meaning the
618 // user has not yet selected a default
619 // so: we'll keep the original intent for now
620 mHotseats[i] = intent;
621
622 // set the accessibility text to "Select shortcut"
623 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
624 } else {
625 // we have an app!
626 // now reconstruct the intent to launch it through the front
627 // door
628 ComponentName com = new ComponentName(
629 bestMatch.activityInfo.applicationInfo.packageName,
630 bestMatch.activityInfo.name);
631 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
632
633 // load the app label for accessibility
634 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
635 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 }
637
638 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 Log.d(TAG, "loadHotseats: hotseat " + i
640 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 + ((mHotseats[i] == null)
642 ? "null"
643 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400644 + "] label=[" + mHotseatLabels[i]
645 + "]"
646 );
647 }
648 }
649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 @Override
652 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700653 mWaitingForResult = false;
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // The pattern used here is that a user PICKs a specific application,
656 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
659 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700660
Romain Guy94dabf12009-07-21 10:55:43 -0700661 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 switch (requestCode) {
663 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
666 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800667 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
672 case REQUEST_PICK_LIVE_FOLDER:
673 addLiveFolder(data);
674 break;
675 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700685 case REQUEST_PICK_WALLPAPER:
686 // We just wanted the activity result here so we can clear mWaitingForResult
687 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
Romain Guy18042c82009-11-06 11:44:55 -0800689 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
690 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
691 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 // Clean up the appWidgetId if we canceled
693 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
694 if (appWidgetId != -1) {
695 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699
700 @Override
701 protected void onResume() {
702 super.onResume();
703
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800704 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 mWorkspaceLoading = true;
708 mModel.startLoader(this, true);
709 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700711 // When we resume Launcher, a different Activity might be responsible for the app
712 // market intent, so refresh the icon
713 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715
716 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 protected void onPause() {
718 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700719 // Some launcher layouts don't have a previous and next view
720 if (mPreviousView != null) {
721 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700722 }
723 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 dismissPreview(mNextView);
725 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800726 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700729 @Override
730 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 // Flag the loader to stop early before switching
732 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800734 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700736
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800737 // We can't hide the IME if it was forced open. So don't bother
738 /*
739 @Override
740 public void onWindowFocusChanged(boolean hasFocus) {
741 super.onWindowFocusChanged(hasFocus);
742
743 if (hasFocus) {
744 final InputMethodManager inputManager = (InputMethodManager)
745 getSystemService(Context.INPUT_METHOD_SERVICE);
746 WindowManager.LayoutParams lp = getWindow().getAttributes();
747 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
748 android.os.Handler()) {
749 protected void onReceiveResult(int resultCode, Bundle resultData) {
750 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
751 }
752 });
753 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
754 }
755 }
756 */
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 private boolean acceptFilter() {
759 final InputMethodManager inputManager = (InputMethodManager)
760 getSystemService(Context.INPUT_METHOD_SERVICE);
761 return !inputManager.isFullscreenMode();
762 }
763
764 @Override
765 public boolean onKeyDown(int keyCode, KeyEvent event) {
766 boolean handled = super.onKeyDown(keyCode, event);
767 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
768 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
769 keyCode, event);
770 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700772 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700773 // showSearchDialog()
774 // If there are multiple keystrokes before the search dialog takes focus,
775 // onSearchRequested() will be called for every keystroke,
776 // but it is idempotent, so it's fine.
777 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779 }
780
Joe Onorato8a9625e2010-01-28 15:55:35 -0800781 // Eat the long press event so the keyboard doesn't come up.
782 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
783 return true;
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return handled;
787 }
788
Karl Rosaen138a0412009-04-23 19:00:21 -0700789 private String getTypedText() {
790 return mDefaultKeySsb.toString();
791 }
792
793 private void clearTypedText() {
794 mDefaultKeySsb.clear();
795 mDefaultKeySsb.clearSpans();
796 Selection.setSelection(mDefaultKeySsb, 0);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 /**
800 * Restores the previous state, if it exists.
801 *
802 * @param savedState The previous state.
803 */
804 private void restoreState(Bundle savedState) {
805 if (savedState == null) {
806 return;
807 }
808
Joe Onorato3a8820b2009-11-10 15:06:42 -0800809 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
810 if (allApps) {
811 showAllApps(false);
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
815 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700816 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
818
819 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
820 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700821 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 mAddItemCellInfo = new CellLayout.CellInfo();
823 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
824 addItemCellInfo.valid = true;
825 addItemCellInfo.screen = addScreen;
826 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
827 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
828 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
829 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700830 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
832 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
833 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
834 mRestoring = true;
835 }
836
837 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
838 if (renameFolder) {
839 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700840 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 mRestoring = true;
842 }
843 }
844
845 /**
846 * Finds all the views we need and configure them properly.
847 */
848 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700849 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400850
Romain Guyb1b69f52009-08-10 15:10:15 -0700851 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400852 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853
Joe Onorato7c312c12009-08-13 21:36:53 -0700854 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700855 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700856 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800857 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700858 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700859 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700860
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700861 if (LauncherApplication.isScreenXLarge()) {
862 // They need to be INVISIBLE initially so that they will be measured in the layout.
863 // Otherwise the animations are messed up when we show them for the first time.
864 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
865 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
866 }
867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
869 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500870 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700871
Joe Onorato7c312c12009-08-13 21:36:53 -0700872 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
873 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700875 View handleView = findViewById(R.id.all_apps_button);
876 if (handleView != null && handleView instanceof HandleView) {
877 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700878 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700879 mHandleView.setLauncher(this);
880 mHandleView.setOnClickListener(this);
881 mHandleView.setOnLongClickListener(this);
882 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800883
Winson Chung80baf5a2010-08-09 16:03:15 -0700884 if (mCustomizePagedView != null) {
885 mCustomizePagedView.setLauncher(this);
886 mCustomizePagedView.setDragController(dragController);
887 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700888 } else {
889 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
890 hotseatLeft.setContentDescription(mHotseatLabels[0]);
891 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
892 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
893 hotseatRight.setContentDescription(mHotseatLabels[1]);
894 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400895
Michael Jurkaaf442092010-06-10 17:01:57 -0700896 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
897 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800898
Michael Jurkaaf442092010-06-10 17:01:57 -0700899 Drawable previous = mPreviousView.getDrawable();
900 Drawable next = mNextView.getDrawable();
901 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Michael Jurkaaf442092010-06-10 17:01:57 -0700903 mPreviousView.setHapticFeedbackEnabled(false);
904 mPreviousView.setOnLongClickListener(this);
905 mNextView.setHapticFeedbackEnabled(false);
906 mNextView.setOnLongClickListener(this);
907 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400910 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
913 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400914 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700915 int deleteZoneHandleId;
916 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700917 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700918 } else {
919 deleteZoneHandleId = R.id.all_apps_button_cluster;
920 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700921 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700922 dragController.addDragListener(deleteZone);
923
924 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
925 if (infoButton != null) {
926 infoButton.setLauncher(this);
927 infoButton.setHandle(findViewById(R.id.configure_button));
928 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
929 dragController.addDragListener(infoButton);
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
Joe Onorato00acb122009-08-04 16:04:30 -0400932 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400933 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800934 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700935
Joe Onorato00acb122009-08-04 16:04:30 -0400936 // The order here is bottom to top.
937 dragController.addDropTarget(workspace);
938 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700939 if (infoButton != null) {
940 dragController.addDropTarget(infoButton);
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
Romain Guy8a73c512009-11-09 19:19:59 -0800944 @SuppressWarnings({"UnusedDeclaration"})
945 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800946 if (!isAllAppsVisible()) {
947 mWorkspace.scrollLeft();
948 }
Romain Guy8a73c512009-11-09 19:19:59 -0800949 }
950
951 @SuppressWarnings({"UnusedDeclaration"})
952 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800953 if (!isAllAppsVisible()) {
954 mWorkspace.scrollRight();
955 }
Romain Guy8a73c512009-11-09 19:19:59 -0800956 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400957
958 @SuppressWarnings({"UnusedDeclaration"})
959 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400960 if (isAllAppsVisible()) return;
961
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400962 int index = -1;
963 if (v.getId() == R.id.hotseat_left) {
964 index = 0;
965 } else if (v.getId() == R.id.hotseat_right) {
966 index = 1;
967 }
968
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400969 // reload these every tap; you never know when they might change
970 loadHotseats();
971 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
972 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400973 startActivitySafely(
974 mHotseats[index],
975 "hotseat"
976 );
977 }
978 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 /**
981 * Creates a view representing a shortcut.
982 *
983 * @param info The data structure describing the shortcut.
984 *
985 * @return A View inflated from R.layout.application.
986 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700989 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 /**
993 * Creates a view representing a shortcut inflated from the specified resource.
994 *
995 * @param layoutResId The id of the XML layout used to create the shortcut.
996 * @param parent The group the shortcut belongs to.
997 * @param info The data structure describing the shortcut.
998 *
999 * @return A View inflated from layoutResId.
1000 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1003
Joe Onorato0589f0f2010-02-08 13:44:00 -08001004 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1005 new FastBitmapDrawable(info.getIcon(mIconCache)),
1006 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 favorite.setText(info.title);
1008 favorite.setTag(info);
1009 favorite.setOnClickListener(this);
1010
1011 return favorite;
1012 }
1013
1014 /**
1015 * Add an application shortcut to the workspace.
1016 *
1017 * @param data The intent describing the application.
1018 * @param cellInfo The position on screen where to create the shortcut.
1019 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001021 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 if (!findSingleSlot(cellInfo)) return;
1023
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1025 data, context);
1026
Romain Guy73b979d2009-06-09 12:57:21 -07001027 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.container = ItemInfo.NO_ID;
1031 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1032 } else {
1033 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 /**
1038 * Add a shortcut to the workspace.
1039 *
1040 * @param data The intent describing the shortcut.
1041 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001044 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001046
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
1049 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1052 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054 }
1055
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001060 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001063 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001068 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 // We have saved the position to which the widget was dragged-- this really only matters
1072 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074
1075 // For now, we don't save the coordinate where we dropped the icon because we're not
1076 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1077 // a widget on the home screen in case we want to add it in the future
1078 final int[] xyTouch = null;
1079 //final int[] xyTouch = mAddItemCoordinates;
1080 boolean findNearestVacantAreaFailed = false;
1081 if (xyTouch != null) {
1082 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1083 int[] result = screen.findNearestVacantArea(
1084 mAddItemCoordinates[0], mAddItemCoordinates[1],
1085 spans[0], spans[1], cellInfo, xy);
1086 findNearestVacantAreaFailed = (result == null);
1087 }
1088
1089 if (findNearestVacantAreaFailed ||
1090 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001091 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1092 return;
1093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 // Build Launcher-specific widget info and save to database
1096 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 launcherInfo.spanX = spans[0];
1098 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 LauncherModel.addItemToDatabase(this, launcherInfo,
1101 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001102 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103
1104 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001108 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Michael Jurkaa63c4522010-08-19 13:52:27 -07001113 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116 }
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1119 mDesktopItems.remove(launcherInfo);
1120 launcherInfo.hostView = null;
1121 }
1122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 public LauncherAppWidgetHost getAppWidgetHost() {
1124 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001127 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001128 getWindow().closeAllPanels();
1129
1130 try {
1131 dismissDialog(DIALOG_CREATE_SHORTCUT);
1132 // Unlock the workspace if the dialog was showing
1133 } catch (Exception e) {
1134 // An exception is thrown if the dialog is not visible, which is fine
1135 }
1136
1137 try {
1138 dismissDialog(DIALOG_RENAME_FOLDER);
1139 // Unlock the workspace if the dialog was showing
1140 } catch (Exception e) {
1141 // An exception is thrown if the dialog is not visible, which is fine
1142 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001143
1144 // Whatever we were doing is hereby canceled.
1145 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001146 }
1147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 @Override
1149 protected void onNewIntent(Intent intent) {
1150 super.onNewIntent(intent);
1151
1152 // Close the menu
1153 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001154 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001155 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001156
Joe Onorato14f122b2009-11-19 14:06:36 -08001157 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1158 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1159 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001160 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1161
Michael Jurka4cb37242010-08-09 21:05:32 -07001162 // in all these cases, only animate if we're already on home
1163 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001164 if (alreadyOnHome && !mWorkspace.isSmall() &&
1165 !allAppsVisible && !customizationDrawerVisible) {
1166 mWorkspace.shrinkToMiddle();
1167 } else {
1168 mWorkspace.unshrink(alreadyOnHome);
1169 }
Michael Jurka0142d492010-08-25 17:46:15 -07001170 } else if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001171 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001172 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001173 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001174 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001175 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001176
Joe Onorato3a8820b2009-11-10 15:06:42 -08001177 final View v = getWindow().peekDecorView();
1178 if (v != null && v.getWindowToken() != null) {
1179 InputMethodManager imm = (InputMethodManager)getSystemService(
1180 INPUT_METHOD_SERVICE);
1181 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183 }
1184 }
1185
1186 @Override
1187 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1188 // Do not call super here
1189 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001190
1191 if (mHomeCustomizationDrawer != null) {
1192 String cur = savedInstanceState.getString("currentTab");
1193 if (cur != null) {
1194 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1195 }
1196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198
1199 @Override
1200 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001201 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202
1203 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1204 if (folders.size() > 0) {
1205 final int count = folders.size();
1206 long[] ids = new long[count];
1207 for (int i = 0; i < count; i++) {
1208 final FolderInfo info = folders.get(i).getInfo();
1209 ids[i] = info.id;
1210 }
1211 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1212 } else {
1213 super.onSaveInstanceState(outState);
1214 }
1215
Joe Onoratofb0ca672009-09-14 17:55:46 -04001216 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001217 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
1221 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1222 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1223 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1224
1225 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1226 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1227 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1228 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1229 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1230 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1231 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1232 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001233 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
1236 if (mFolderInfo != null && mWaitingForResult) {
1237 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1238 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1239 }
Michael Jurka946ad472010-07-09 18:05:18 -07001240
1241 if (mHomeCustomizationDrawer != null) {
1242 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1243 if (currentTabTag != null) {
1244 outState.putString("currentTab", currentTabTag);
1245 }
1246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248
1249 @Override
1250 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001256 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
1258
1259 TextKeyListener.getInstance().release();
1260
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001264
1265 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001266
Patrick Dubroyab962b72010-07-26 12:10:10 -07001267 // Some launcher layouts don't have a previous and next view
1268 if (mPreviousView != null) {
1269 dismissPreview(mPreviousView);
1270 }
1271 if (mNextView != null) {
1272 dismissPreview(mNextView);
1273 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001274
1275 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
1277
1278 @Override
1279 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001280 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 super.startActivityForResult(intent, requestCode);
1282 }
1283
1284 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001285 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001287
Joe Onorato7bb17492009-09-24 17:51:01 -07001288 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001289
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 if (initialQuery == null) {
1291 // Use any text typed in the launcher as the initial query
1292 initialQuery = getTypedText();
1293 clearTypedText();
1294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 if (appSearchData == null) {
1296 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001297 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
Romain Guycbb89e42009-06-08 15:52:54 -07001299
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 final SearchManager searchManager =
1301 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001303 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
1306 @Override
1307 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 if (isWorkspaceLocked()) {
1309 return false;
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311
1312 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1315 .setIcon(android.R.drawable.ic_menu_add)
1316 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001317 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 .setIcon(android.R.drawable.ic_menu_gallery)
1319 .setAlphabeticShortcut('W');
1320 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1321 .setIcon(android.R.drawable.ic_search_category_default)
1322 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1323 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1324 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1325 .setAlphabeticShortcut('N');
1326
1327 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001328 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1329 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
1331 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1332 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1333 .setIntent(settings);
1334
1335 return true;
1336 }
1337
1338 @Override
1339 public boolean onPrepareOptionsMenu(Menu menu) {
1340 super.onPrepareOptionsMenu(menu);
1341
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001342 // If all apps is animating, don't show the menu, because we don't know
1343 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001344 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001345 return false;
1346 }
1347
1348 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001349 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001350 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1351 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1352
1353 // Disable add if the workspace is full.
1354 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001355 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001356 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
1359 return true;
1360 }
1361
Michael Jurka0e260592010-06-30 17:07:39 -07001362 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1363 // using the settings menu to add an item, something similar happens in showAddDialog
1364 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001365 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001366 mAddItemCellInfo = mMenuAddInfo;
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 @Override
1370 public boolean onOptionsItemSelected(MenuItem item) {
1371 switch (item.getItemId()) {
1372 case MENU_ADD:
1373 addItems();
1374 return true;
1375 case MENU_WALLPAPER_SETTINGS:
1376 startWallpaper();
1377 return true;
1378 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001379 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 return true;
1381 case MENU_NOTIFICATIONS:
1382 showNotifications();
1383 return true;
1384 }
1385
1386 return super.onOptionsItemSelected(item);
1387 }
Romain Guycbb89e42009-06-08 15:52:54 -07001388
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 /**
1390 * Indicates that we want global search for this activity by setting the globalSearch
1391 * argument for {@link #startSearch} to true.
1392 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001394 @Override
1395 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001396 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001398 }
1399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 public boolean isWorkspaceLocked() {
1401 return mWorkspaceLoading || mWaitingForResult;
1402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001405 if (LauncherApplication.isScreenXLarge()) {
1406 // Animate the widget chooser up from the bottom of the screen
1407 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001408 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001409 }
1410 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001411 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001412 showAddDialog(mMenuAddInfo);
1413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
Michael Jurkaa63c4522010-08-19 13:52:27 -07001416 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1417 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001418 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001419
1420 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1421 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001422 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1423 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1424 addAppWidgetImpl(appWidgetId);
1425 }
1426
1427 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001428 // TODO: catch bad widget exception when sent
1429 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001430 // TODO: Is this log message meaningful?
1431 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1432 addAppWidgetImpl(appWidgetId);
1433 }
1434
1435 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001436 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
Bjorn Bringert7984c942009-12-09 15:38:25 +00001438 if (appWidget.configure != null) {
1439 // Launch over to configure widget, if needed
1440 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1441 intent.setComponent(appWidget.configure);
1442 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1443
Romain Guy8e633c52010-03-04 12:51:36 -08001444 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001446 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001447 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449 }
Romain Guycbb89e42009-06-08 15:52:54 -07001450
Joe Onoratodeb98af2010-02-19 14:59:39 -08001451 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001452 // Handle case where user selected "Applications"
1453 String applicationName = getResources().getString(R.string.group_applications);
1454 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001456 if (applicationName != null && applicationName.equals(shortcutName)) {
1457 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1458 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001460 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1461 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001462 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001463 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001464 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001469 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001470 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001471 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001473 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001475 } else {
1476 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 UserFolderInfo folderInfo = new UserFolderInfo();
1482 folderInfo.title = getText(R.string.folder_name);
1483
1484 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001485 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 if (!findSingleSlot(cellInfo)) return;
1487
1488 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 LauncherModel.addItemToDatabase(this, folderInfo,
1490 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001491 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001492 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
1494 // Create the view
1495 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001496 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001502 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503 }
1504
1505 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001506 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 if (!findSingleSlot(cellInfo)) return;
1508
1509 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1510
1511 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001513 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1515 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517 }
1518
1519 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1520 CellLayout.CellInfo cellInfo, boolean notify) {
1521
1522 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1523 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1524
1525 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 Intent.ShortcutIconResource iconResource = null;
1527
1528 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1529 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1530 try {
1531 iconResource = (Intent.ShortcutIconResource) extra;
1532 final PackageManager packageManager = context.getPackageManager();
1533 Resources resources = packageManager.getResourcesForApplication(
1534 iconResource.packageName);
1535 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1536 icon = resources.getDrawable(id);
1537 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001538 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540 }
1541
1542 if (icon == null) {
1543 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1544 }
1545
1546 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001547 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 info.title = name;
1549 info.iconResource = iconResource;
1550 info.uri = data.getData();
1551 info.baseIntent = baseIntent;
1552 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1553 LiveFolders.DISPLAY_MODE_GRID);
1554
1555 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1556 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001557 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 return info;
1560 }
1561
1562 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1563 final int[] xy = new int[2];
1564 if (findSlot(cellInfo, xy, 1, 1)) {
1565 cellInfo.cellX = xy[0];
1566 cellInfo.cellY = xy[1];
1567 return true;
1568 }
1569 return false;
1570 }
1571
1572 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1573 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001574 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1575 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1577 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1578 return false;
1579 }
1580 }
1581 return true;
1582 }
1583
1584 private void showNotifications() {
1585 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1586 if (statusBar != null) {
1587 statusBar.expand();
1588 }
1589 }
1590
1591 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001592 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001594 Intent chooser = Intent.createChooser(pickWallpaper,
1595 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001596 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1597 // Removed in Eclair MR1
1598// WallpaperManager wm = (WallpaperManager)
1599// getSystemService(Context.WALLPAPER_SERVICE);
1600// WallpaperInfo wi = wm.getWallpaperInfo();
1601// if (wi != null && wi.getSettingsActivity() != null) {
1602// LabeledIntent li = new LabeledIntent(getPackageName(),
1603// R.string.configure_wallpaper, 0);
1604// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1605// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1606// }
Mike Clerona0618e42009-10-22 13:55:21 -07001607 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001610 /**
1611 * Registers various content observers. The current implementation registers
1612 * only a favorites observer to keep track of the favorites applications.
1613 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001614 private void registerContentObservers() {
1615 ContentResolver resolver = getContentResolver();
1616 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1617 true, mWidgetObserver);
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 @Override
1621 public boolean dispatchKeyEvent(KeyEvent event) {
1622 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1623 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001625 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001626 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001627 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001628 dumpState();
1629 return true;
1630 }
1631 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001632 }
1633 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1634 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001635 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 return true;
1637 }
1638 }
1639
1640 return super.dispatchKeyEvent(event);
1641 }
1642
Joe Onorato88ec0992009-11-19 13:16:06 -08001643 @Override
1644 public void onBackPressed() {
1645 if (isAllAppsVisible()) {
1646 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001647 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001648 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001649 } else {
1650 closeFolder();
1651 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001652 // Some launcher layouts don't have a previous and next view
1653 if (mPreviousView != null) {
1654 dismissPreview(mPreviousView);
1655 dismissPreview(mNextView);
1656 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001657 }
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 private void closeFolder() {
1660 Folder folder = mWorkspace.getOpenFolder();
1661 if (folder != null) {
1662 closeFolder(folder);
1663 }
1664 }
1665
1666 void closeFolder(Folder folder) {
1667 folder.getInfo().opened = false;
1668 ViewGroup parent = (ViewGroup) folder.getParent();
1669 if (parent != null) {
1670 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001671 if (folder instanceof DropTarget) {
1672 // Live folders aren't DropTargets.
1673 mDragController.removeDropTarget((DropTarget)folder);
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
1676 folder.onClose();
1677 }
1678
1679 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001680 * Re-listen when widgets are reset.
1681 */
1682 private void onAppWidgetReset() {
1683 mAppWidgetHost.startListening();
1684 }
1685
1686 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1688 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 private void unbindDesktopItems() {
1691 for (ItemInfo item: mDesktopItems) {
1692 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 /**
1697 * Launches the intent referred by the clicked shortcut.
1698 *
1699 * @param v The view representing the clicked shortcut.
1700 */
1701 public void onClick(View v) {
1702 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001703 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001705 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001706 int[] pos = new int[2];
1707 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001708 intent.setSourceBounds(new Rect(pos[0], pos[1],
1709 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001710 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 } else if (tag instanceof FolderInfo) {
1712 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001713 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001714 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001715 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001716 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001717 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
1720 }
1721
Michael Jurka0e260592010-06-30 17:07:39 -07001722 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001723 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001724 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001725 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001726 return false;
1727 }
1728
Michael Jurkaaf442092010-06-10 17:01:57 -07001729 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001730 * Event handler for the search button
1731 *
1732 * @param v The view that was clicked.
1733 */
1734 public void onClickSearchButton(View v) {
1735 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1736 View button = findViewById(R.id.search_button);
1737 i.setSourceBounds(
1738 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1739 startActivity(i);
1740 }
1741
1742 /**
1743 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001744 * enters home screen customization mode.
1745 *
1746 * @param v The view that was clicked.
1747 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001748 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001749 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001750 }
1751
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001752 /**
1753 * Event handler for the "grid" button that appears on the home screen, which
1754 * enters all apps mode.
1755 *
1756 * @param v The view that was clicked.
1757 */
1758 public void onClickAllAppsButton(View v) {
1759 showAllApps(true);
1760 }
1761
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001762 public void onClickAppMarketButton(View v) {
1763 if (mAppMarketIntent != null) {
1764 startActivitySafely(mAppMarketIntent, "app market");
1765 }
1766 }
1767
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001768 void startApplicationDetailsActivity(ComponentName componentName) {
1769 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001770 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1771 Uri.fromParts("package", packageName, null));
1772 startActivity(intent);
1773 }
1774
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001775 void startApplicationUninstallActivity(ComponentName componentName) {
1776 String packageName = componentName.getPackageName();
1777 String className = componentName.getClassName();
1778 Intent intent = new Intent(
1779 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1780 startActivity(intent);
1781 }
1782
Joe Onoratof984e852010-03-25 09:47:45 -07001783 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1785 try {
1786 startActivity(intent);
1787 } catch (ActivityNotFoundException e) {
1788 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001789 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 } catch (SecurityException e) {
1791 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001792 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001794 "or use the exported attribute for this activity. "
1795 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001798
Romain Guy8e633c52010-03-04 12:51:36 -08001799 void startActivityForResultSafely(Intent intent, int requestCode) {
1800 try {
1801 startActivityForResult(intent, requestCode);
1802 } catch (ActivityNotFoundException e) {
1803 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1804 } catch (SecurityException e) {
1805 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1806 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1807 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1808 "or use the exported attribute for this activity.", e);
1809 }
1810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811
1812 private void handleFolderClick(FolderInfo folderInfo) {
1813 if (!folderInfo.opened) {
1814 // Close any open folder
1815 closeFolder();
1816 // Open the requested folder
1817 openFolder(folderInfo);
1818 } else {
1819 // Find the open folder...
1820 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1821 int folderScreen;
1822 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001823 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 // .. and close it
1825 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001826 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 // Close any folder open on the current screen
1828 closeFolder();
1829 // Pull the folder onto this screen
1830 openFolder(folderInfo);
1831 }
1832 }
1833 }
1834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 /**
1837 * Opens the user fodler described by the specified tag. The opening of the folder
1838 * is animated relative to the specified View. If the View is null, no animation
1839 * is played.
1840 *
1841 * @param folderInfo The FolderInfo describing the folder to open.
1842 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001843 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 Folder openFolder;
1845
1846 if (folderInfo instanceof UserFolderInfo) {
1847 openFolder = UserFolder.fromXml(this);
1848 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001849 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 } else {
1851 return;
1852 }
1853
Joe Onorato00acb122009-08-04 16:04:30 -04001854 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 openFolder.setLauncher(this);
1856
1857 openFolder.bind(folderInfo);
1858 folderInfo.opened = true;
1859
Winson Chungaafa03c2010-06-11 17:34:16 -07001860 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 openFolder.onOpen();
1863 }
1864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001866 switch (v.getId()) {
1867 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001868 if (!isAllAppsVisible()) {
1869 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1870 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001871 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001872 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001873 return true;
1874 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001875 if (!isAllAppsVisible()) {
1876 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1877 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001878 showPreviews(v);
1879 }
1880 return true;
1881 case R.id.all_apps_button:
1882 if (!isAllAppsVisible()) {
1883 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1884 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1885 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001886 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001887 return true;
1888 }
1889
Joe Onorato9c1289c2009-08-17 11:03:03 -04001890 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 return false;
1892 }
1893
1894 if (!(v instanceof CellLayout)) {
1895 v = (View) v.getParent();
1896 }
1897
1898 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1899
1900 // This happens when long clicking an item with the dpad/trackball
1901 if (cellInfo == null) {
1902 return true;
1903 }
1904
1905 if (mWorkspace.allowLongPress()) {
1906 if (cellInfo.cell == null) {
1907 if (cellInfo.valid) {
1908 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001909 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001910 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1911 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 showAddDialog(cellInfo);
1913 }
1914 } else {
1915 if (!(cellInfo.cell instanceof Folder)) {
1916 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001917 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1918 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 mWorkspace.startDrag(cellInfo);
1920 }
1921 }
1922 }
1923 return true;
1924 }
1925
Romain Guye6b8e2f2009-11-10 11:56:55 -08001926 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001927 private void dismissPreview(final View v) {
1928 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001929 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001930 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1931 public void onDismiss() {
1932 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1933 int count = group.getChildCount();
1934 for (int i = 0; i < count; i++) {
1935 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1936 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001937 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1938 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1939
1940 v.setTag(R.id.workspace, null);
1941 v.setTag(R.id.icon, null);
1942 window.setOnDismissListener(null);
1943 }
1944 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001945 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001946 }
1947 v.setTag(null);
1948 }
1949
Romain Guyf8e6a802009-12-07 17:48:02 -08001950 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001951 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001952 }
1953
Romain Guya6abce82009-11-10 02:54:41 -08001954 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001955 final Resources resources = getResources();
1956 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001957
Romain Guya6abce82009-11-10 02:54:41 -08001958 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001959
Romain Guy9d31e9f2009-11-11 18:54:28 -08001960 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001961
Romain Guyf8e6a802009-12-07 17:48:02 -08001962 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001963 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001964 int extraW = (int) ((r.left + r.right) * max);
1965 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001966
1967 int aW = cell.getWidth() - extraW;
1968 float w = aW / max;
1969
1970 int width = cell.getWidth();
1971 int height = cell.getHeight();
1972 int x = cell.getLeftPadding();
1973 int y = cell.getTopPadding();
1974 width -= (x + cell.getRightPadding());
1975 height -= (y + cell.getBottomPadding());
1976
1977 float scale = w / width;
1978
1979 int count = end - start;
1980
1981 final float sWidth = width * scale;
1982 float sHeight = height * scale;
1983
Romain Guye6b8e2f2009-11-10 11:56:55 -08001984 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001985
Romain Guye6b8e2f2009-11-10 11:56:55 -08001986 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1987 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001988
1989 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001990 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001991 cell = (CellLayout) workspace.getChildAt(i);
1992
Romain Guyf8e6a802009-12-07 17:48:02 -08001993 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001994 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001995
1996 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001997 c.scale(scale, scale);
1998 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1999 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002000
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002002 image.setImageBitmap(bitmap);
2003 image.setTag(i);
2004 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002005 image.setOnFocusChangeListener(handler);
2006 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002007 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002008
2009 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002010 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2011
Winson Chungaafa03c2010-06-11 17:34:16 -07002012 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002013 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002014
2015 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002016 p.setContentView(preview);
2017 p.setWidth((int) (sWidth * count + extraW));
2018 p.setHeight((int) (sHeight + extraH));
2019 p.setAnimationStyle(R.style.AnimationPreview);
2020 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002021 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002022 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002023 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002024
Romain Guye6b8e2f2009-11-10 11:56:55 -08002025 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2026 public void onDismiss() {
2027 dismissPreview(anchor);
2028 }
2029 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002030
2031 anchor.setTag(p);
2032 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002033 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002034 }
2035
Romain Guy9d31e9f2009-11-11 18:54:28 -08002036 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002037 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002038
Romain Guye6b8e2f2009-11-10 11:56:55 -08002039 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002040 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002041 }
2042
2043 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002044 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 v.post(this);
2046 }
2047
2048 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002049 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002050 }
2051
2052 public void onFocusChange(View v, boolean hasFocus) {
2053 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002054 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 }
Romain Guya6abce82009-11-10 02:54:41 -08002056 }
2057 }
2058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 Workspace getWorkspace() {
2060 return mWorkspace;
2061 }
2062
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 @Override
2064 protected Dialog onCreateDialog(int id) {
2065 switch (id) {
2066 case DIALOG_CREATE_SHORTCUT:
2067 return new CreateShortcut().createDialog();
2068 case DIALOG_RENAME_FOLDER:
2069 return new RenameFolder().createDialog();
2070 }
2071
2072 return super.onCreateDialog(id);
2073 }
2074
2075 @Override
2076 protected void onPrepareDialog(int id, Dialog dialog) {
2077 switch (id) {
2078 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 break;
2080 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002081 if (mFolderInfo != null) {
2082 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2083 final CharSequence text = mFolderInfo.title;
2084 input.setText(text);
2085 input.setSelection(0, text.length());
2086 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 break;
2088 }
2089 }
2090
2091 void showRenameDialog(FolderInfo info) {
2092 mFolderInfo = info;
2093 mWaitingForResult = true;
2094 showDialog(DIALOG_RENAME_FOLDER);
2095 }
2096
2097 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2098 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002099 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 mWaitingForResult = true;
2101 showDialog(DIALOG_CREATE_SHORTCUT);
2102 }
2103
Joe Onoratodeb98af2010-02-19 14:59:39 -08002104 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002105 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002106 Bundle bundle = new Bundle();
2107
2108 ArrayList<String> shortcutNames = new ArrayList<String>();
2109 shortcutNames.add(getString(R.string.group_applications));
2110 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2111
2112 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2113 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2114 R.drawable.ic_launcher_application));
2115 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2116
2117 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2118 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002119 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002120 pickIntent.putExtras(bundle);
2121
Joe Onoratodeb98af2010-02-19 14:59:39 -08002122 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002123 }
2124
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 private class RenameFolder {
2126 private EditText mInput;
2127
2128 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2130 mInput = (EditText) layout.findViewById(R.id.folder_name);
2131
2132 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2133 builder.setIcon(0);
2134 builder.setTitle(getString(R.string.rename_folder_title));
2135 builder.setCancelable(true);
2136 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2137 public void onCancel(DialogInterface dialog) {
2138 cleanup();
2139 }
2140 });
2141 builder.setNegativeButton(getString(R.string.cancel_action),
2142 new Dialog.OnClickListener() {
2143 public void onClick(DialogInterface dialog, int which) {
2144 cleanup();
2145 }
2146 }
2147 );
2148 builder.setPositiveButton(getString(R.string.rename_action),
2149 new Dialog.OnClickListener() {
2150 public void onClick(DialogInterface dialog, int which) {
2151 changeFolderName();
2152 }
2153 }
2154 );
2155 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002156
2157 final AlertDialog dialog = builder.create();
2158 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2159 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002160 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002161 mInput.requestFocus();
2162 InputMethodManager inputManager = (InputMethodManager)
2163 getSystemService(Context.INPUT_METHOD_SERVICE);
2164 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002165 }
2166 });
2167
2168 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 }
2170
2171 private void changeFolderName() {
2172 final String name = mInput.getText().toString();
2173 if (!TextUtils.isEmpty(name)) {
2174 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002175 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 mFolderInfo.title = name;
2177 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2178
Joe Onorato9c1289c2009-08-17 11:03:03 -04002179 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002180 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 } else {
2183 final FolderIcon folderIcon = (FolderIcon)
2184 mWorkspace.getViewForTag(mFolderInfo);
2185 if (folderIcon != null) {
2186 folderIcon.setText(name);
2187 getWorkspace().requestLayout();
2188 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002189 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002190 mWorkspaceLoading = true;
2191 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 }
2193 }
2194 }
2195 cleanup();
2196 }
2197
2198 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 dismissDialog(DIALOG_RENAME_FOLDER);
2200 mWaitingForResult = false;
2201 mFolderInfo = null;
2202 }
2203 }
2204
Daniel Sandler843e8602010-06-07 14:59:01 -04002205 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2206 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002207 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002208 }
2209
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002210 // AllAppsView.Watcher
2211 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002212 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2213 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002214 mWorkspace.setVisibility(View.GONE);
2215 }
2216 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002217
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002218 private void showToolbarButton(View button) {
2219 button.setAlpha(1.0f);
2220 button.setVisibility(View.VISIBLE);
2221 button.setFocusable(true);
2222 button.setClickable(true);
2223 }
2224
2225 private void hideToolbarButton(View button) {
2226 button.setAlpha(0.0f);
2227 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2228 button.setVisibility(View.INVISIBLE);
2229 button.setFocusable(false);
2230 button.setClickable(false);
2231 }
2232
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002233 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 *
2236 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2237 * @param view The toolbar button to be animated
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002238 * @param seq A Sequencer that will be used to animate the transition. If null, the
2239 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002241 private void hideOrShowToolbarButton(boolean show, final View view, Sequencer seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002242 final boolean showing = show;
2243 final boolean hiding = !show;
2244
2245 final int duration = show ?
2246 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2247 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2248
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002249 if (seq != null) {
2250 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2251 anim.addListener(new AnimatableListenerAdapter() {
2252 public void onAnimationStart(Animatable animation) {
2253 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002255 public void onAnimationEnd(Animatable animation) {
2256 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002257 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 });
2259 seq.play(anim);
2260 } else {
2261 if (showing) {
2262 showToolbarButton(view);
2263 } else {
2264 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002265 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002266 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002267 }
2268
2269 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002270 * Show/hide the appropriate toolbar buttons for newState.
2271 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 *
2273 * @param newState The state that is being switched to
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002274 * @param showSeq Sequencer in which to put "show" animations, or null.
2275 * @param hideSeq Sequencer in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002276 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002277 private void hideAndShowToolbarButtons(State newState, Sequencer showSeq, Sequencer hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002278 final View searchButton = findViewById(R.id.search_button);
2279 final View allAppsButton = findViewById(R.id.all_apps_button);
2280 final View marketButton = findViewById(R.id.market_button);
2281 final View configureButton = findViewById(R.id.configure_button);
2282
2283 switch (newState) {
2284 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002285 hideOrShowToolbarButton(true, searchButton, showSeq);
2286 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2287 hideOrShowToolbarButton(true, configureButton, showSeq);
2288 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002289 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002290 break;
2291 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002292 hideOrShowToolbarButton(true, configureButton, showSeq);
2293 hideOrShowToolbarButton(true, marketButton, showSeq);
2294 hideOrShowToolbarButton(false, searchButton, hideSeq);
2295 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002296 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002297 break;
2298 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002299 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2300 hideOrShowToolbarButton(false, searchButton, hideSeq);
2301 hideOrShowToolbarButton(false, marketButton, hideSeq);
2302 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002303 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 break;
2305 }
2306 }
2307
2308 /**
2309 * Helper method for the cameraZoomIn/cameraZoomOut animations
2310 * @param view The view being animated
2311 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2312 * @param scaleFactor The scale factor used for the zoom
2313 */
2314 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2315 final int height = view.getHeight();
2316 view.setPivotX(view.getWidth() / 2.0f);
2317 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2318 // Assumes that the view is normally anchored to either the top or bottom of the screen
2319 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2320 if (state == State.ALL_APPS) {
2321 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2322 } else {
2323 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2324 }
2325 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002326
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002327 /**
2328 * Zoom the camera out from the workspace to reveal 'toView'.
2329 * Assumes that the view to show is anchored at either the very top or very bottom
2330 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002332 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002333 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002334 final Resources res = getResources();
2335 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2336 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002337 final boolean toAllApps = (toState == State.ALL_APPS);
2338 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002339
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002340 setPivotsForZoom(toView, toState, scale);
2341
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 if (toState == State.ALL_APPS) {
2343 mWorkspace.shrinkToBottom(animated);
2344 } else {
2345 mWorkspace.shrinkToTop(animated);
2346 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347
2348 if (animated) {
2349 Animator scaleAnim = new PropertyAnimator(duration, toView,
2350 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2351 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2352 scaleAnim.setInterpolator(new DecelerateInterpolator());
2353 scaleAnim.addListener(new AnimatableListenerAdapter() {
2354 public void onAnimationStart(Animatable animation) {
2355 // Prepare the position
2356 toView.setTranslationX(0.0f);
2357 toView.setTranslationY(0.0f);
2358 toView.setVisibility(View.VISIBLE);
2359 }
2360 });
2361
2362 Sequencer toolbarHideAnim = new Sequencer();
2363 Sequencer toolbarShowAnim = new Sequencer();
2364 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2365
2366 // toView should appear right at the end of the workspace shrink animation
2367 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2368
2369 Sequencer s = new Sequencer();
2370 s.playTogether(scaleAnim, toolbarHideAnim);
2371 s.play(scaleAnim).after(startDelay);
2372
2373 // Show the new toolbar buttons just as the main animation is ending
2374 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2375 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2376 s.start();
2377 } else {
2378 toView.setTranslationX(0.0f);
2379 toView.setTranslationY(0.0f);
2380 toView.setScaleX(1.0f);
2381 toView.setScaleY(1.0f);
2382 toView.setVisibility(View.VISIBLE);
2383 hideAndShowToolbarButtons(toState, null, null);
2384 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002385 }
2386
2387 /**
2388 * Zoom the camera back into the workspace, hiding 'fromView'.
2389 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002390 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2391 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002392 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002393 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002394 Resources res = getResources();
2395 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2396 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002397 final View fromView =
2398 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2399
2400 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002401
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002402 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002403
2404 if (animated) {
2405 Sequencer s = new Sequencer();
2406 Animator scaleAnim = new PropertyAnimator(duration, fromView,
2407 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2408 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2409 scaleAnim.setInterpolator(new AccelerateInterpolator());
2410 s.addListener(new AnimatableListenerAdapter() {
2411 public void onAnimationEnd(Animatable animation) {
2412 fromView.setVisibility(View.GONE);
2413 fromView.setScaleX(1.0f);
2414 fromView.setScaleY(1.0f);
2415 }
2416 });
2417
2418 Sequencer toolbarHideAnim = new Sequencer();
2419 Sequencer toolbarShowAnim = new Sequencer();
2420 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2421
2422 s.playTogether(scaleAnim, toolbarHideAnim);
2423
2424 // Show the new toolbar buttons at the very end of the whole animation
2425 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2426 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2427 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2428 s.start();
2429 } else {
2430 fromView.setVisibility(View.GONE);
2431 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2432 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002433 }
2434
2435 /**
2436 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2437 * This is the transition used on xlarge screens to go between all apps and
2438 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002439 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2440 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2441 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002443 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002444 final Resources res = getResources();
2445 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002446 final int workspaceHeight = mWorkspace.getHeight();
2447
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002448 final boolean fromAllApps = (fromState == State.ALL_APPS);
2449 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2450 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2453 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2454 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2455 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002457 if (toState == State.ALL_APPS) {
2458 mWorkspace.shrinkToBottom(animated);
2459 } else {
2460 mWorkspace.shrinkToTop(animated);
2461 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002462
2463 if (animated) {
2464 Sequencer s = new Sequencer();
2465 s.addListener(new AnimatableListenerAdapter() {
2466 public void onAnimationStart(Animatable animation) {
2467 toView.setVisibility(View.VISIBLE);
2468 toView.setY(toViewStartY);
2469 }
2470 public void onAnimationEnd(Animatable animation) {
2471 fromView.setVisibility(View.GONE);
2472 }
2473 });
2474
2475 Sequencer toolbarHideAnim = new Sequencer();
2476 Sequencer toolbarShowAnim = new Sequencer();
2477 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2478
2479 s.playTogether(
2480 toolbarHideAnim,
2481 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2482 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2483
2484 // Show the new toolbar buttons just as the main animation is ending
2485 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2486 s.play(toolbarShowAnim).after(duration - fadeInTime);
2487 s.start();
2488 } else {
2489 fromView.setY(fromViewEndY);
2490 fromView.setVisibility(View.GONE);
2491 toView.setY(toViewEndY);
2492 toView.setVisibility(View.VISIBLE);
2493 hideAndShowToolbarButtons(toState, null, null);
2494 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002495 }
2496
2497 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002498 if (mAllAppsGrid.isVisible())
2499 return;
2500
Michael Jurka79212d82010-07-30 16:36:20 -07002501 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002502 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002504 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002506 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002507 } else {
2508 mAllAppsGrid.zoom(1.0f, animated);
2509 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002510
Romain Guyc16fea72010-03-12 17:17:56 -08002511 ((View) mAllAppsGrid).setFocusable(true);
2512 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002513
Joe Onorato7c312c12009-08-13 21:36:53 -07002514 // TODO: fade these two too
2515 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002516 }
2517
Joe Onoratob2061212009-11-24 16:13:54 -05002518 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002519 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002520 * - Home from workspace
2521 * - from center screen
2522 * - from other screens
2523 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002524 * - from center screen
2525 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002526 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002527 * - from center screen
2528 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002529 * - Launch app from workspace and quit
2530 * - with back
2531 * - with home
2532 * - Launch app from all apps and quit
2533 * - with back
2534 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002535 * - Go to a screen that's not the default, then all
2536 * apps, and launch and app, and go back
2537 * - with back
2538 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002539 * - On workspace, long press power and go back
2540 * - with back
2541 * - with home
2542 * - On all apps, long press power and go back
2543 * - with back
2544 * - with home
2545 * - On workspace, power off
2546 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002547 * - Launch an app and turn off the screen while in that app
2548 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002549 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002550 * - From all apps
2551 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002552 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2553 * - From all apps
2554 * - From the center workspace
2555 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002556 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002557 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002558 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002559 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002560 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002562 } else {
2563 mAllAppsGrid.zoom(0.0f, animated);
2564 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002565 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002566 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002567 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002568 }
2569
Joe Onorato7c312c12009-08-13 21:36:53 -07002570 void lockAllApps() {
2571 // TODO
2572 }
2573
2574 void unlockAllApps() {
2575 // TODO
2576 }
2577
Patrick Dubroy558654c2010-07-23 16:48:11 -07002578 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002579 return mHomeCustomizationDrawer != null &&
2580 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002581 }
2582
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 // Show the customization drawer (only exists in x-large configuration)
2584 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002585 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002586 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002587 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002589 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002590 }
2591
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002592 // Hide the customization drawer (only exists in x-large configuration)
2593 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002594 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 }
2597 }
2598
Michael Jurka213d9632010-07-28 11:29:25 -07002599 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002601 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002603 }
2604 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002606 }
2607 }
2608
Joe Onorato7404ee42009-07-31 11:54:44 -07002609 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002610 * Sets the app market icon (shown when all apps is visible on x-large screens)
2611 */
2612 private void updateAppMarketIcon() {
2613 if (LauncherApplication.isScreenXLarge()) {
2614 // Find the app market activity by resolving an intent.
2615 // (If multiple app markets are installed, it will return the ResolverActivity.)
2616 PackageManager packageManager = getPackageManager();
2617 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2618 ComponentName activityName = intent.resolveActivity(getPackageManager());
2619 if (activityName != null) {
2620 mAppMarketIntent = intent;
2621 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2622 Drawable toolbarIcon = null;
2623 try {
2624 // Look for the toolbar icon specified in the activity meta-data
2625 Bundle metaData = packageManager.getActivityInfo(
2626 activityName, PackageManager.GET_META_DATA).metaData;
2627 if (metaData != null) {
2628 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2629 if (iconResId != 0) {
2630 Resources res = packageManager.getResourcesForActivity(activityName);
2631 toolbarIcon = res.getDrawable(iconResId);
2632 }
2633 }
2634 } catch (NameNotFoundException e) {
2635 // Do nothing
2636 }
2637 // If we were unable to find the icon via the meta-data, use a generic one
2638 if (toolbarIcon == null) {
2639 marketButton.setImageResource(R.drawable.app_market_generic);
2640 } else {
2641 marketButton.setImageDrawable(toolbarIcon);
2642 }
2643 } else {
2644 mAppMarketIntent = null;
2645 }
2646 }
2647 }
2648
2649 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 * Displays the shortcut creation dialog and launches, if necessary, the
2651 * appropriate activity.
2652 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002653 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002654 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2655 DialogInterface.OnShowListener {
2656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002661
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002662 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2663 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002664 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 builder.setInverseBackgroundForced(true);
2667
2668 AlertDialog dialog = builder.create();
2669 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002670 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002671 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 return dialog;
2674 }
2675
2676 public void onCancel(DialogInterface dialog) {
2677 mWaitingForResult = false;
2678 cleanup();
2679 }
2680
Romain Guycbb89e42009-06-08 15:52:54 -07002681 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002682 }
2683
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002684 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002685 try {
2686 dismissDialog(DIALOG_CREATE_SHORTCUT);
2687 } catch (Exception e) {
2688 // An exception is thrown if the dialog is not visible, which is fine
2689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002690 }
2691
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002692 /**
2693 * Handle the action clicked in the "Add to home" dialog.
2694 */
2695 public void onClick(DialogInterface dialog, int which) {
2696 Resources res = getResources();
2697 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002698
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002699 switch (which) {
2700 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002701 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002702 break;
2703 }
Romain Guycbb89e42009-06-08 15:52:54 -07002704
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002705 case AddAdapter.ITEM_APPWIDGET: {
2706 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002707
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002708 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2709 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002710 // start the pick activity
2711 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2712 break;
2713 }
Romain Guycbb89e42009-06-08 15:52:54 -07002714
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002715 case AddAdapter.ITEM_LIVE_FOLDER: {
2716 // Insert extra item to handle inserting folder
2717 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002718
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002719 ArrayList<String> shortcutNames = new ArrayList<String>();
2720 shortcutNames.add(res.getString(R.string.group_folder));
2721 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002722
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002723 ArrayList<ShortcutIconResource> shortcutIcons =
2724 new ArrayList<ShortcutIconResource>();
2725 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2726 R.drawable.ic_launcher_folder));
2727 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2728
2729 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2730 pickIntent.putExtra(Intent.EXTRA_INTENT,
2731 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2732 pickIntent.putExtra(Intent.EXTRA_TITLE,
2733 getText(R.string.title_select_live_folder));
2734 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002735
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002736 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2737 break;
2738 }
2739
2740 case AddAdapter.ITEM_WALLPAPER: {
2741 startWallpaper();
2742 break;
2743 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002744 }
2745 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002746
2747 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002748 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002749 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 }
2751
2752 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002753 * Receives notifications when applications are added/removed.
2754 */
2755 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2756 @Override
2757 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002758 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002759 String reason = intent.getStringExtra("reason");
2760 if (!"homekey".equals(reason)) {
2761 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002762 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002763 animate = false;
2764 }
Joe Onoratob2061212009-11-24 16:13:54 -05002765 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002766 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002767 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002768 }
2769 }
2770
2771 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002772 * Receives notifications whenever the appwidgets are reset.
2773 */
2774 private class AppWidgetResetObserver extends ContentObserver {
2775 public AppWidgetResetObserver() {
2776 super(new Handler());
2777 }
2778
2779 @Override
2780 public void onChange(boolean selfChange) {
2781 onAppWidgetReset();
2782 }
2783 }
2784
2785 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002786 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002788 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002789 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002790 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002791 } else {
2792 return SCREEN_COUNT / 2;
2793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002795
Joe Onorato9c1289c2009-08-17 11:03:03 -04002796 /**
2797 * Refreshes the shortcuts shown on the workspace.
2798 *
2799 * Implementation of the method from LauncherModel.Callbacks.
2800 */
2801 public void startBinding() {
2802 final Workspace workspace = mWorkspace;
2803 int count = workspace.getChildCount();
2804 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002805 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2807 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002808
Joe Onorato9c1289c2009-08-17 11:03:03 -04002809 if (DEBUG_USER_INTERFACE) {
2810 android.widget.Button finishButton = new android.widget.Button(this);
2811 finishButton.setText("Finish");
2812 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2813
2814 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2815 public void onClick(View v) {
2816 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002817 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002819 }
2820 }
2821
2822 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002823 * Bind the items start-end from the list.
2824 *
2825 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2828
2829 final Workspace workspace = mWorkspace;
2830
2831 for (int i=start; i<end; i++) {
2832 final ItemInfo item = shortcuts.get(i);
2833 mDesktopItems.add(item);
2834 switch (item.itemType) {
2835 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2836 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002837 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2839 false);
2840 break;
2841 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2842 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002843 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 (UserFolderInfo) item);
2845 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2846 false);
2847 break;
2848 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2849 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2850 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002851 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 (LiveFolderInfo) item);
2853 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2854 false);
2855 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 }
2858
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
2861
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 /**
2863 * Implementation of the method from LauncherModel.Callbacks.
2864 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002865 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002866 sFolders.clear();
2867 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869
2870 /**
2871 * Add the views for a widget to the workspace.
2872 *
2873 * Implementation of the method from LauncherModel.Callbacks.
2874 */
2875 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002876 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2877 if (DEBUG_WIDGETS) {
2878 Log.d(TAG, "bindAppWidget: " + item);
2879 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 final Workspace workspace = mWorkspace;
2881
2882 final int appWidgetId = item.appWidgetId;
2883 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002884 if (DEBUG_WIDGETS) {
2885 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2886 }
2887
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2889
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2891 item.hostView.setTag(item);
2892
2893 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2894 item.cellY, item.spanX, item.spanY, false);
2895
2896 workspace.requestLayout();
2897
2898 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002899
2900 if (DEBUG_WIDGETS) {
2901 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2902 + (SystemClock.uptimeMillis()-start) + "ms");
2903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 }
2905
2906 /**
2907 * Callback saying that there aren't any more items to bind.
2908 *
2909 * Implementation of the method from LauncherModel.Callbacks.
2910 */
2911 public void finishBindingItems() {
2912 if (mSavedState != null) {
2913 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002914 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 }
2916
2917 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2918 if (userFolders != null) {
2919 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002920 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 if (info != null) {
2922 openFolder(info);
2923 }
2924 }
2925 final Folder openFolder = mWorkspace.getOpenFolder();
2926 if (openFolder != null) {
2927 openFolder.requestFocus();
2928 }
2929 }
2930
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 mSavedState = null;
2932 }
2933
2934 if (mSavedInstanceState != null) {
2935 super.onRestoreInstanceState(mSavedInstanceState);
2936 mSavedInstanceState = null;
2937 }
2938
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 mWorkspaceLoading = false;
2940 }
2941
2942 /**
2943 * Add the icons for all apps.
2944 *
2945 * Implementation of the method from LauncherModel.Callbacks.
2946 */
2947 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002948 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002949 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 }
2951
2952 /**
2953 * A package was installed.
2954 *
2955 * Implementation of the method from LauncherModel.Callbacks.
2956 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002957 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002959 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002960 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 }
2962
2963 /**
2964 * A package was updated.
2965 *
2966 * Implementation of the method from LauncherModel.Callbacks.
2967 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002968 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002970 mWorkspace.updateShortcuts(apps);
2971 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002972 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 }
2974
2975 /**
2976 * A package was uninstalled.
2977 *
2978 * Implementation of the method from LauncherModel.Callbacks.
2979 */
Joe Onorato36115782010-06-17 13:28:48 -04002980 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002982 if (permanent) {
2983 mWorkspace.removeItems(apps);
2984 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002985 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002986 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002987 }
Joe Onoratobe386092009-11-17 17:32:16 -08002988
2989 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002990 * A number of packages were updated.
2991 */
2992 public void bindPackagesUpdated() {
2993 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002994 if (mCustomizePagedView != null)
2995 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002996 }
2997
2998 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002999 * Prints out out state for debugging.
3000 */
3001 public void dumpState() {
3002 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003003 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003004 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3005 Log.d(TAG, "mRestoring=" + mRestoring);
3006 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3007 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3008 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003009 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003010 mModel.dumpState();
3011 mAllAppsGrid.dumpState();
3012 Log.d(TAG, "END launcher2 dump state");
3013 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014}