blob: b0c6b7042684eb8fd17a2ec21f5c61c3212a40bf [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) {
Patrick Dubroy9f7aec82010-09-06 11:03:37 -07001308 if (isWorkspaceLocked() || LauncherApplication.isScreenXLarge()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001309 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 Dubroy5539af72010-09-07 15:22:01 -07001775 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1776 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1777 // System applications cannot be installed. For now, show a toast explaining that.
1778 // We may give them the option of disabling apps this way.
1779 int messageId = R.string.uninstall_system_app_text;
1780 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1781 } else {
1782 String packageName = appInfo.componentName.getPackageName();
1783 String className = appInfo.componentName.getClassName();
1784 Intent intent = new Intent(
1785 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1786 startActivity(intent);
1787 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001788 }
1789
Joe Onoratof984e852010-03-25 09:47:45 -07001790 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1792 try {
1793 startActivity(intent);
1794 } catch (ActivityNotFoundException e) {
1795 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001796 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 } catch (SecurityException e) {
1798 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001799 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001801 "or use the exported attribute for this activity. "
1802 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 }
1804 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001805
Romain Guy8e633c52010-03-04 12:51:36 -08001806 void startActivityForResultSafely(Intent intent, int requestCode) {
1807 try {
1808 startActivityForResult(intent, requestCode);
1809 } catch (ActivityNotFoundException e) {
1810 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1811 } catch (SecurityException e) {
1812 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1813 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1814 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1815 "or use the exported attribute for this activity.", e);
1816 }
1817 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818
1819 private void handleFolderClick(FolderInfo folderInfo) {
1820 if (!folderInfo.opened) {
1821 // Close any open folder
1822 closeFolder();
1823 // Open the requested folder
1824 openFolder(folderInfo);
1825 } else {
1826 // Find the open folder...
1827 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1828 int folderScreen;
1829 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001830 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 // .. and close it
1832 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001833 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 // Close any folder open on the current screen
1835 closeFolder();
1836 // Pull the folder onto this screen
1837 openFolder(folderInfo);
1838 }
1839 }
1840 }
1841 }
1842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 /**
1844 * Opens the user fodler described by the specified tag. The opening of the folder
1845 * is animated relative to the specified View. If the View is null, no animation
1846 * is played.
1847 *
1848 * @param folderInfo The FolderInfo describing the folder to open.
1849 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001850 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851 Folder openFolder;
1852
1853 if (folderInfo instanceof UserFolderInfo) {
1854 openFolder = UserFolder.fromXml(this);
1855 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001856 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 } else {
1858 return;
1859 }
1860
Joe Onorato00acb122009-08-04 16:04:30 -04001861 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 openFolder.setLauncher(this);
1863
1864 openFolder.bind(folderInfo);
1865 folderInfo.opened = true;
1866
Winson Chungaafa03c2010-06-11 17:34:16 -07001867 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 openFolder.onOpen();
1870 }
1871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001873 switch (v.getId()) {
1874 case R.id.previous_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);
Joe Onorato0d44e942009-11-16 18:20:51 -08001879 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001880 return true;
1881 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001882 if (!isAllAppsVisible()) {
1883 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1884 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001885 showPreviews(v);
1886 }
1887 return true;
1888 case R.id.all_apps_button:
1889 if (!isAllAppsVisible()) {
1890 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1891 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1892 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001893 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001894 return true;
1895 }
1896
Joe Onorato9c1289c2009-08-17 11:03:03 -04001897 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 return false;
1899 }
1900
1901 if (!(v instanceof CellLayout)) {
1902 v = (View) v.getParent();
1903 }
1904
1905 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1906
1907 // This happens when long clicking an item with the dpad/trackball
1908 if (cellInfo == null) {
1909 return true;
1910 }
1911
1912 if (mWorkspace.allowLongPress()) {
1913 if (cellInfo.cell == null) {
1914 if (cellInfo.valid) {
1915 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001916 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001917 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1918 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 showAddDialog(cellInfo);
1920 }
1921 } else {
1922 if (!(cellInfo.cell instanceof Folder)) {
1923 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001924 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1925 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 mWorkspace.startDrag(cellInfo);
1927 }
1928 }
1929 }
1930 return true;
1931 }
1932
Romain Guye6b8e2f2009-11-10 11:56:55 -08001933 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001934 private void dismissPreview(final View v) {
1935 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001936 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001937 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1938 public void onDismiss() {
1939 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1940 int count = group.getChildCount();
1941 for (int i = 0; i < count; i++) {
1942 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1943 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001944 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1945 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1946
1947 v.setTag(R.id.workspace, null);
1948 v.setTag(R.id.icon, null);
1949 window.setOnDismissListener(null);
1950 }
1951 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001952 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001953 }
1954 v.setTag(null);
1955 }
1956
Romain Guyf8e6a802009-12-07 17:48:02 -08001957 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001958 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001959 }
1960
Romain Guya6abce82009-11-10 02:54:41 -08001961 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001962 final Resources resources = getResources();
1963 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001964
Romain Guya6abce82009-11-10 02:54:41 -08001965 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001966
Romain Guy9d31e9f2009-11-11 18:54:28 -08001967 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001968
Romain Guyf8e6a802009-12-07 17:48:02 -08001969 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001970 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001971 int extraW = (int) ((r.left + r.right) * max);
1972 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001973
1974 int aW = cell.getWidth() - extraW;
1975 float w = aW / max;
1976
1977 int width = cell.getWidth();
1978 int height = cell.getHeight();
1979 int x = cell.getLeftPadding();
1980 int y = cell.getTopPadding();
1981 width -= (x + cell.getRightPadding());
1982 height -= (y + cell.getBottomPadding());
1983
1984 float scale = w / width;
1985
1986 int count = end - start;
1987
1988 final float sWidth = width * scale;
1989 float sHeight = height * scale;
1990
Romain Guye6b8e2f2009-11-10 11:56:55 -08001991 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001992
Romain Guye6b8e2f2009-11-10 11:56:55 -08001993 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1994 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001995
1996 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001997 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001998 cell = (CellLayout) workspace.getChildAt(i);
1999
Romain Guyf8e6a802009-12-07 17:48:02 -08002000 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002001 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002002
2003 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002004 c.scale(scale, scale);
2005 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
2006 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08002007
Romain Guy9d31e9f2009-11-11 18:54:28 -08002008 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002009 image.setImageBitmap(bitmap);
2010 image.setTag(i);
2011 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002012 image.setOnFocusChangeListener(handler);
2013 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002014 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002015
2016 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002017 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2018
Winson Chungaafa03c2010-06-11 17:34:16 -07002019 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002020 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002021
2022 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002023 p.setContentView(preview);
2024 p.setWidth((int) (sWidth * count + extraW));
2025 p.setHeight((int) (sHeight + extraH));
2026 p.setAnimationStyle(R.style.AnimationPreview);
2027 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002028 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002029 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002030 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002031
Romain Guye6b8e2f2009-11-10 11:56:55 -08002032 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2033 public void onDismiss() {
2034 dismissPreview(anchor);
2035 }
2036 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002037
2038 anchor.setTag(p);
2039 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002040 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002041 }
2042
Romain Guy9d31e9f2009-11-11 18:54:28 -08002043 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002044 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002045
Romain Guye6b8e2f2009-11-10 11:56:55 -08002046 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002047 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002048 }
2049
2050 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002051 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002052 v.post(this);
2053 }
2054
2055 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002056 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002057 }
2058
2059 public void onFocusChange(View v, boolean hasFocus) {
2060 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002061 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002062 }
Romain Guya6abce82009-11-10 02:54:41 -08002063 }
2064 }
2065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 Workspace getWorkspace() {
2067 return mWorkspace;
2068 }
2069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 @Override
2071 protected Dialog onCreateDialog(int id) {
2072 switch (id) {
2073 case DIALOG_CREATE_SHORTCUT:
2074 return new CreateShortcut().createDialog();
2075 case DIALOG_RENAME_FOLDER:
2076 return new RenameFolder().createDialog();
2077 }
2078
2079 return super.onCreateDialog(id);
2080 }
2081
2082 @Override
2083 protected void onPrepareDialog(int id, Dialog dialog) {
2084 switch (id) {
2085 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 break;
2087 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002088 if (mFolderInfo != null) {
2089 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2090 final CharSequence text = mFolderInfo.title;
2091 input.setText(text);
2092 input.setSelection(0, text.length());
2093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 break;
2095 }
2096 }
2097
2098 void showRenameDialog(FolderInfo info) {
2099 mFolderInfo = info;
2100 mWaitingForResult = true;
2101 showDialog(DIALOG_RENAME_FOLDER);
2102 }
2103
2104 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2105 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002106 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 mWaitingForResult = true;
2108 showDialog(DIALOG_CREATE_SHORTCUT);
2109 }
2110
Joe Onoratodeb98af2010-02-19 14:59:39 -08002111 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002112 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002113 Bundle bundle = new Bundle();
2114
2115 ArrayList<String> shortcutNames = new ArrayList<String>();
2116 shortcutNames.add(getString(R.string.group_applications));
2117 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2118
2119 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2120 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2121 R.drawable.ic_launcher_application));
2122 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2123
2124 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2125 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002126 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002127 pickIntent.putExtras(bundle);
2128
Joe Onoratodeb98af2010-02-19 14:59:39 -08002129 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002130 }
2131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 private class RenameFolder {
2133 private EditText mInput;
2134
2135 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002136 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2137 mInput = (EditText) layout.findViewById(R.id.folder_name);
2138
2139 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2140 builder.setIcon(0);
2141 builder.setTitle(getString(R.string.rename_folder_title));
2142 builder.setCancelable(true);
2143 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2144 public void onCancel(DialogInterface dialog) {
2145 cleanup();
2146 }
2147 });
2148 builder.setNegativeButton(getString(R.string.cancel_action),
2149 new Dialog.OnClickListener() {
2150 public void onClick(DialogInterface dialog, int which) {
2151 cleanup();
2152 }
2153 }
2154 );
2155 builder.setPositiveButton(getString(R.string.rename_action),
2156 new Dialog.OnClickListener() {
2157 public void onClick(DialogInterface dialog, int which) {
2158 changeFolderName();
2159 }
2160 }
2161 );
2162 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002163
2164 final AlertDialog dialog = builder.create();
2165 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2166 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002167 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002168 mInput.requestFocus();
2169 InputMethodManager inputManager = (InputMethodManager)
2170 getSystemService(Context.INPUT_METHOD_SERVICE);
2171 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002172 }
2173 });
2174
2175 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 }
2177
2178 private void changeFolderName() {
2179 final String name = mInput.getText().toString();
2180 if (!TextUtils.isEmpty(name)) {
2181 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002182 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 mFolderInfo.title = name;
2184 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2185
Joe Onorato9c1289c2009-08-17 11:03:03 -04002186 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002187 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002188 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002189 } else {
2190 final FolderIcon folderIcon = (FolderIcon)
2191 mWorkspace.getViewForTag(mFolderInfo);
2192 if (folderIcon != null) {
2193 folderIcon.setText(name);
2194 getWorkspace().requestLayout();
2195 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002196 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002197 mWorkspaceLoading = true;
2198 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002199 }
2200 }
2201 }
2202 cleanup();
2203 }
2204
2205 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 dismissDialog(DIALOG_RENAME_FOLDER);
2207 mWaitingForResult = false;
2208 mFolderInfo = null;
2209 }
2210 }
2211
Daniel Sandler843e8602010-06-07 14:59:01 -04002212 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2213 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002214 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002215 }
2216
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002217 // AllAppsView.Watcher
2218 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002219 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2220 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002221 mWorkspace.setVisibility(View.GONE);
2222 }
2223 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002224
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002225 private void showToolbarButton(View button) {
2226 button.setAlpha(1.0f);
2227 button.setVisibility(View.VISIBLE);
2228 button.setFocusable(true);
2229 button.setClickable(true);
2230 }
2231
2232 private void hideToolbarButton(View button) {
2233 button.setAlpha(0.0f);
2234 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2235 button.setVisibility(View.INVISIBLE);
2236 button.setFocusable(false);
2237 button.setClickable(false);
2238 }
2239
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002241 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002242 *
2243 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2244 * @param view The toolbar button to be animated
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002245 * @param seq A Sequencer that will be used to animate the transition. If null, the
2246 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002247 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002248 private void hideOrShowToolbarButton(boolean show, final View view, Sequencer seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002249 final boolean showing = show;
2250 final boolean hiding = !show;
2251
2252 final int duration = show ?
2253 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2254 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2255
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002256 if (seq != null) {
2257 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2258 anim.addListener(new AnimatableListenerAdapter() {
2259 public void onAnimationStart(Animatable animation) {
2260 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002261 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002262 public void onAnimationEnd(Animatable animation) {
2263 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002264 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002265 });
2266 seq.play(anim);
2267 } else {
2268 if (showing) {
2269 showToolbarButton(view);
2270 } else {
2271 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002273 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002274 }
2275
2276 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002277 * Show/hide the appropriate toolbar buttons for newState.
2278 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002279 *
2280 * @param newState The state that is being switched to
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002281 * @param showSeq Sequencer in which to put "show" animations, or null.
2282 * @param hideSeq Sequencer in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002283 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002284 private void hideAndShowToolbarButtons(State newState, Sequencer showSeq, Sequencer hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002285 final View searchButton = findViewById(R.id.search_button);
2286 final View allAppsButton = findViewById(R.id.all_apps_button);
2287 final View marketButton = findViewById(R.id.market_button);
2288 final View configureButton = findViewById(R.id.configure_button);
2289
2290 switch (newState) {
2291 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002292 hideOrShowToolbarButton(true, searchButton, showSeq);
2293 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2294 hideOrShowToolbarButton(true, configureButton, showSeq);
2295 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002296 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002297 break;
2298 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002299 hideOrShowToolbarButton(true, configureButton, showSeq);
2300 hideOrShowToolbarButton(true, marketButton, showSeq);
2301 hideOrShowToolbarButton(false, searchButton, hideSeq);
2302 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002303 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 break;
2305 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002306 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2307 hideOrShowToolbarButton(false, searchButton, hideSeq);
2308 hideOrShowToolbarButton(false, marketButton, hideSeq);
2309 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002310 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311 break;
2312 }
2313 }
2314
2315 /**
2316 * Helper method for the cameraZoomIn/cameraZoomOut animations
2317 * @param view The view being animated
2318 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2319 * @param scaleFactor The scale factor used for the zoom
2320 */
2321 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2322 final int height = view.getHeight();
2323 view.setPivotX(view.getWidth() / 2.0f);
2324 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2325 // Assumes that the view is normally anchored to either the top or bottom of the screen
2326 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2327 if (state == State.ALL_APPS) {
2328 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2329 } else {
2330 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2331 }
2332 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002333
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002334 /**
2335 * Zoom the camera out from the workspace to reveal 'toView'.
2336 * Assumes that the view to show is anchored at either the very top or very bottom
2337 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002339 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002341 final Resources res = getResources();
2342 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2343 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002344 final boolean toAllApps = (toState == State.ALL_APPS);
2345 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002346
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 setPivotsForZoom(toView, toState, scale);
2348
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349 if (toState == State.ALL_APPS) {
2350 mWorkspace.shrinkToBottom(animated);
2351 } else {
2352 mWorkspace.shrinkToTop(animated);
2353 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002354
2355 if (animated) {
2356 Animator scaleAnim = new PropertyAnimator(duration, toView,
2357 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2358 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2359 scaleAnim.setInterpolator(new DecelerateInterpolator());
2360 scaleAnim.addListener(new AnimatableListenerAdapter() {
2361 public void onAnimationStart(Animatable animation) {
2362 // Prepare the position
2363 toView.setTranslationX(0.0f);
2364 toView.setTranslationY(0.0f);
2365 toView.setVisibility(View.VISIBLE);
2366 }
2367 });
2368
2369 Sequencer toolbarHideAnim = new Sequencer();
2370 Sequencer toolbarShowAnim = new Sequencer();
2371 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2372
2373 // toView should appear right at the end of the workspace shrink animation
2374 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2375
2376 Sequencer s = new Sequencer();
2377 s.playTogether(scaleAnim, toolbarHideAnim);
2378 s.play(scaleAnim).after(startDelay);
2379
2380 // Show the new toolbar buttons just as the main animation is ending
2381 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2382 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2383 s.start();
2384 } else {
2385 toView.setTranslationX(0.0f);
2386 toView.setTranslationY(0.0f);
2387 toView.setScaleX(1.0f);
2388 toView.setScaleY(1.0f);
2389 toView.setVisibility(View.VISIBLE);
2390 hideAndShowToolbarButtons(toState, null, null);
2391 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002392 }
2393
2394 /**
2395 * Zoom the camera back into the workspace, hiding 'fromView'.
2396 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002397 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2398 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002399 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002400 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002401 Resources res = getResources();
2402 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2403 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002404 final View fromView =
2405 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2406
2407 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002408
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002409 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410
2411 if (animated) {
2412 Sequencer s = new Sequencer();
2413 Animator scaleAnim = new PropertyAnimator(duration, fromView,
2414 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2415 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2416 scaleAnim.setInterpolator(new AccelerateInterpolator());
2417 s.addListener(new AnimatableListenerAdapter() {
2418 public void onAnimationEnd(Animatable animation) {
2419 fromView.setVisibility(View.GONE);
2420 fromView.setScaleX(1.0f);
2421 fromView.setScaleY(1.0f);
2422 }
2423 });
2424
2425 Sequencer toolbarHideAnim = new Sequencer();
2426 Sequencer toolbarShowAnim = new Sequencer();
2427 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2428
2429 s.playTogether(scaleAnim, toolbarHideAnim);
2430
2431 // Show the new toolbar buttons at the very end of the whole animation
2432 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2433 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2434 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2435 s.start();
2436 } else {
2437 fromView.setVisibility(View.GONE);
2438 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2439 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002440 }
2441
2442 /**
2443 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2444 * This is the transition used on xlarge screens to go between all apps and
2445 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002446 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2447 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2448 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002449 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002450 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002451 final Resources res = getResources();
2452 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453 final int workspaceHeight = mWorkspace.getHeight();
2454
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002455 final boolean fromAllApps = (fromState == State.ALL_APPS);
2456 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2457 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002458
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2460 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2461 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2462 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002463
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 if (toState == State.ALL_APPS) {
2465 mWorkspace.shrinkToBottom(animated);
2466 } else {
2467 mWorkspace.shrinkToTop(animated);
2468 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469
2470 if (animated) {
2471 Sequencer s = new Sequencer();
2472 s.addListener(new AnimatableListenerAdapter() {
2473 public void onAnimationStart(Animatable animation) {
2474 toView.setVisibility(View.VISIBLE);
2475 toView.setY(toViewStartY);
2476 }
2477 public void onAnimationEnd(Animatable animation) {
2478 fromView.setVisibility(View.GONE);
2479 }
2480 });
2481
2482 Sequencer toolbarHideAnim = new Sequencer();
2483 Sequencer toolbarShowAnim = new Sequencer();
2484 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2485
2486 s.playTogether(
2487 toolbarHideAnim,
2488 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2489 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2490
2491 // Show the new toolbar buttons just as the main animation is ending
2492 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2493 s.play(toolbarShowAnim).after(duration - fadeInTime);
2494 s.start();
2495 } else {
2496 fromView.setY(fromViewEndY);
2497 fromView.setVisibility(View.GONE);
2498 toView.setY(toViewEndY);
2499 toView.setVisibility(View.VISIBLE);
2500 hideAndShowToolbarButtons(toState, null, null);
2501 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002502 }
2503
2504 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002505 if (mAllAppsGrid.isVisible())
2506 return;
2507
Michael Jurka79212d82010-07-30 16:36:20 -07002508 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002509 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002511 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002512 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002513 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002514 } else {
2515 mAllAppsGrid.zoom(1.0f, animated);
2516 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002517
Romain Guyc16fea72010-03-12 17:17:56 -08002518 ((View) mAllAppsGrid).setFocusable(true);
2519 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002520
Joe Onorato7c312c12009-08-13 21:36:53 -07002521 // TODO: fade these two too
2522 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002523 }
2524
Joe Onoratob2061212009-11-24 16:13:54 -05002525 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002526 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002527 * - Home from workspace
2528 * - from center screen
2529 * - from other screens
2530 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002531 * - from center screen
2532 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002533 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002534 * - from center screen
2535 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002536 * - Launch app from workspace and quit
2537 * - with back
2538 * - with home
2539 * - Launch app from all apps and quit
2540 * - with back
2541 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002542 * - Go to a screen that's not the default, then all
2543 * apps, and launch and app, and go back
2544 * - with back
2545 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002546 * - On workspace, long press power and go back
2547 * - with back
2548 * - with home
2549 * - On all apps, long press power and go back
2550 * - with back
2551 * - with home
2552 * - On workspace, power off
2553 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002554 * - Launch an app and turn off the screen while in that app
2555 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002556 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002557 * - From all apps
2558 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002559 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2560 * - From all apps
2561 * - From the center workspace
2562 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002563 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002564 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002565 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002566 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002568 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002569 } else {
2570 mAllAppsGrid.zoom(0.0f, animated);
2571 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002572 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002573 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002574 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002575 }
2576
Joe Onorato7c312c12009-08-13 21:36:53 -07002577 void lockAllApps() {
2578 // TODO
2579 }
2580
2581 void unlockAllApps() {
2582 // TODO
2583 }
2584
Patrick Dubroy558654c2010-07-23 16:48:11 -07002585 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002586 return mHomeCustomizationDrawer != null &&
2587 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002588 }
2589
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 // Show the customization drawer (only exists in x-large configuration)
2591 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002592 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002594 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002596 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002597 }
2598
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002599 // Hide the customization drawer (only exists in x-large configuration)
2600 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002601 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002603 }
2604 }
2605
Michael Jurka213d9632010-07-28 11:29:25 -07002606 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002608 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002610 }
2611 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002612 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002613 }
2614 }
2615
Joe Onorato7404ee42009-07-31 11:54:44 -07002616 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002617 * Sets the app market icon (shown when all apps is visible on x-large screens)
2618 */
2619 private void updateAppMarketIcon() {
2620 if (LauncherApplication.isScreenXLarge()) {
2621 // Find the app market activity by resolving an intent.
2622 // (If multiple app markets are installed, it will return the ResolverActivity.)
2623 PackageManager packageManager = getPackageManager();
2624 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2625 ComponentName activityName = intent.resolveActivity(getPackageManager());
2626 if (activityName != null) {
2627 mAppMarketIntent = intent;
2628 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2629 Drawable toolbarIcon = null;
2630 try {
2631 // Look for the toolbar icon specified in the activity meta-data
2632 Bundle metaData = packageManager.getActivityInfo(
2633 activityName, PackageManager.GET_META_DATA).metaData;
2634 if (metaData != null) {
2635 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2636 if (iconResId != 0) {
2637 Resources res = packageManager.getResourcesForActivity(activityName);
2638 toolbarIcon = res.getDrawable(iconResId);
2639 }
2640 }
2641 } catch (NameNotFoundException e) {
2642 // Do nothing
2643 }
2644 // If we were unable to find the icon via the meta-data, use a generic one
2645 if (toolbarIcon == null) {
2646 marketButton.setImageResource(R.drawable.app_market_generic);
2647 } else {
2648 marketButton.setImageDrawable(toolbarIcon);
2649 }
2650 } else {
2651 mAppMarketIntent = null;
2652 }
2653 }
2654 }
2655
2656 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002657 * Displays the shortcut creation dialog and launches, if necessary, the
2658 * appropriate activity.
2659 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002660 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002661 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2662 DialogInterface.OnShowListener {
2663
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002664 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002667 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2670 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002671 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002672
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002673 builder.setInverseBackgroundForced(true);
2674
2675 AlertDialog dialog = builder.create();
2676 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002677 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002678 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 return dialog;
2681 }
2682
2683 public void onCancel(DialogInterface dialog) {
2684 mWaitingForResult = false;
2685 cleanup();
2686 }
2687
Romain Guycbb89e42009-06-08 15:52:54 -07002688 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002689 }
2690
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002692 try {
2693 dismissDialog(DIALOG_CREATE_SHORTCUT);
2694 } catch (Exception e) {
2695 // An exception is thrown if the dialog is not visible, which is fine
2696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 }
2698
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002699 /**
2700 * Handle the action clicked in the "Add to home" dialog.
2701 */
2702 public void onClick(DialogInterface dialog, int which) {
2703 Resources res = getResources();
2704 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002705
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002706 switch (which) {
2707 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002708 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002709 break;
2710 }
Romain Guycbb89e42009-06-08 15:52:54 -07002711
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002712 case AddAdapter.ITEM_APPWIDGET: {
2713 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002714
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002715 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2716 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002717 // start the pick activity
2718 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2719 break;
2720 }
Romain Guycbb89e42009-06-08 15:52:54 -07002721
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002722 case AddAdapter.ITEM_LIVE_FOLDER: {
2723 // Insert extra item to handle inserting folder
2724 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002725
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002726 ArrayList<String> shortcutNames = new ArrayList<String>();
2727 shortcutNames.add(res.getString(R.string.group_folder));
2728 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002729
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002730 ArrayList<ShortcutIconResource> shortcutIcons =
2731 new ArrayList<ShortcutIconResource>();
2732 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2733 R.drawable.ic_launcher_folder));
2734 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2735
2736 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2737 pickIntent.putExtra(Intent.EXTRA_INTENT,
2738 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2739 pickIntent.putExtra(Intent.EXTRA_TITLE,
2740 getText(R.string.title_select_live_folder));
2741 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002742
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002743 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2744 break;
2745 }
2746
2747 case AddAdapter.ITEM_WALLPAPER: {
2748 startWallpaper();
2749 break;
2750 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 }
2752 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002753
2754 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002755 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002756 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 }
2758
2759 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002760 * Receives notifications when applications are added/removed.
2761 */
2762 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2763 @Override
2764 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002765 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002766 String reason = intent.getStringExtra("reason");
2767 if (!"homekey".equals(reason)) {
2768 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002769 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002770 animate = false;
2771 }
Joe Onoratob2061212009-11-24 16:13:54 -05002772 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002773 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002774 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002775 }
2776 }
2777
2778 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002779 * Receives notifications whenever the appwidgets are reset.
2780 */
2781 private class AppWidgetResetObserver extends ContentObserver {
2782 public AppWidgetResetObserver() {
2783 super(new Handler());
2784 }
2785
2786 @Override
2787 public void onChange(boolean selfChange) {
2788 onAppWidgetReset();
2789 }
2790 }
2791
2792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002793 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002796 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002797 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002798 } else {
2799 return SCREEN_COUNT / 2;
2800 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002801 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002802
Joe Onorato9c1289c2009-08-17 11:03:03 -04002803 /**
2804 * Refreshes the shortcuts shown on the workspace.
2805 *
2806 * Implementation of the method from LauncherModel.Callbacks.
2807 */
2808 public void startBinding() {
2809 final Workspace workspace = mWorkspace;
2810 int count = workspace.getChildCount();
2811 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002812 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2814 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002815
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 if (DEBUG_USER_INTERFACE) {
2817 android.widget.Button finishButton = new android.widget.Button(this);
2818 finishButton.setText("Finish");
2819 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2820
2821 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2822 public void onClick(View v) {
2823 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002826 }
2827 }
2828
2829 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 * Bind the items start-end from the list.
2831 *
2832 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2835
2836 final Workspace workspace = mWorkspace;
2837
2838 for (int i=start; i<end; i++) {
2839 final ItemInfo item = shortcuts.get(i);
2840 mDesktopItems.add(item);
2841 switch (item.itemType) {
2842 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2843 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002844 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2846 false);
2847 break;
2848 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2849 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002850 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002851 (UserFolderInfo) item);
2852 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2853 false);
2854 break;
2855 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2856 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2857 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002858 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 (LiveFolderInfo) item);
2860 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2861 false);
2862 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 }
2865
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 }
2868
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 /**
2870 * Implementation of the method from LauncherModel.Callbacks.
2871 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002872 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002873 sFolders.clear();
2874 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876
2877 /**
2878 * Add the views for a widget to the workspace.
2879 *
2880 * Implementation of the method from LauncherModel.Callbacks.
2881 */
2882 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002883 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2884 if (DEBUG_WIDGETS) {
2885 Log.d(TAG, "bindAppWidget: " + item);
2886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 final Workspace workspace = mWorkspace;
2888
2889 final int appWidgetId = item.appWidgetId;
2890 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002891 if (DEBUG_WIDGETS) {
2892 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2893 }
2894
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2896
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2898 item.hostView.setTag(item);
2899
2900 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2901 item.cellY, item.spanX, item.spanY, false);
2902
2903 workspace.requestLayout();
2904
2905 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002906
2907 if (DEBUG_WIDGETS) {
2908 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2909 + (SystemClock.uptimeMillis()-start) + "ms");
2910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 }
2912
2913 /**
2914 * Callback saying that there aren't any more items to bind.
2915 *
2916 * Implementation of the method from LauncherModel.Callbacks.
2917 */
2918 public void finishBindingItems() {
2919 if (mSavedState != null) {
2920 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002921 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002922 }
2923
2924 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2925 if (userFolders != null) {
2926 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002927 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 if (info != null) {
2929 openFolder(info);
2930 }
2931 }
2932 final Folder openFolder = mWorkspace.getOpenFolder();
2933 if (openFolder != null) {
2934 openFolder.requestFocus();
2935 }
2936 }
2937
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 mSavedState = null;
2939 }
2940
2941 if (mSavedInstanceState != null) {
2942 super.onRestoreInstanceState(mSavedInstanceState);
2943 mSavedInstanceState = null;
2944 }
2945
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 mWorkspaceLoading = false;
2947 }
2948
2949 /**
2950 * Add the icons for all apps.
2951 *
2952 * Implementation of the method from LauncherModel.Callbacks.
2953 */
2954 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002955 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002956 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 }
2958
2959 /**
2960 * A package was installed.
2961 *
2962 * Implementation of the method from LauncherModel.Callbacks.
2963 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002964 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002966 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002967 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002968 }
2969
2970 /**
2971 * A package was updated.
2972 *
2973 * Implementation of the method from LauncherModel.Callbacks.
2974 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002975 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002977 mWorkspace.updateShortcuts(apps);
2978 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002979 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 }
2981
2982 /**
2983 * A package was uninstalled.
2984 *
2985 * Implementation of the method from LauncherModel.Callbacks.
2986 */
Joe Onorato36115782010-06-17 13:28:48 -04002987 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002989 if (permanent) {
2990 mWorkspace.removeItems(apps);
2991 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002992 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002993 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 }
Joe Onoratobe386092009-11-17 17:32:16 -08002995
2996 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002997 * A number of packages were updated.
2998 */
2999 public void bindPackagesUpdated() {
3000 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07003001 if (mCustomizePagedView != null)
3002 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07003003 }
3004
3005 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003006 * Prints out out state for debugging.
3007 */
3008 public void dumpState() {
3009 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003010 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003011 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3012 Log.d(TAG, "mRestoring=" + mRestoring);
3013 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3014 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3015 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003016 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003017 mModel.dumpState();
3018 mAllAppsGrid.dumpState();
3019 Log.d(TAG, "END launcher2 dump state");
3020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003021}