blob: 4e339853338f0c5e3cddc55f44ef9bff86ee5682 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Patrick Dubroy4ed62782010-08-17 15:11:18 -070020import com.android.common.Search;
21import com.android.launcher.R;
22
Patrick Dubroy7247f632010-08-04 16:02:59 -070023import android.animation.Animatable;
24import android.animation.AnimatableListenerAdapter;
25import android.animation.Animator;
26import android.animation.PropertyAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Patrick Dubroy7247f632010-08-04 16:02:59 -070028import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070034import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070035import android.appwidget.AppWidgetManager;
36import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080037import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080038import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chung80baf5a2010-08-09 16:03:15 -070044import android.content.Intent.ShortcutIconResource;
Romain Guy5bbc91b2010-08-18 11:38:46 -070045import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Patrick Dubroyceae05d2010-08-30 10:40:53 -070048import android.content.pm.PackageManager.NameNotFoundException;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040052import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080053import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080055import android.graphics.Canvas;
Winson Chung80baf5a2010-08-09 16:03:15 -070056import android.graphics.Color;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080063import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080066import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070067import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
71import android.text.TextUtils;
72import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080075import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070080import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070082import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070084import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070085import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070092import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070093import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070094import android.widget.TabHost.OnTabChangeListener;
95import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070096import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
100import java.io.DataInputStream;
101import java.io.DataOutputStream;
102import java.io.FileNotFoundException;
103import java.io.IOException;
104import java.util.ArrayList;
105import java.util.HashMap;
106import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800107
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108/**
109 * Default launcher application.
110 */
Michael Jurka946ad472010-07-09 18:05:18 -0700111public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700112 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
113 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800114 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700115 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116
Joe Onorato9c1289c2009-08-17 11:03:03 -0400117 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400118 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700120
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800121 private static final int WALLPAPER_SCREENS_SPAN = 2;
122
123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
127 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
128 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
129 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700130 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 private static final int REQUEST_CREATE_SHORTCUT = 1;
133 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
137 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
142
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800143 static final int SCREEN_COUNT = 5;
144 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145
Joe Onorato7c312c12009-08-13 21:36:53 -0700146 static final int DIALOG_CREATE_SHORTCUT = 1;
147 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
151 // Type: int
152 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
153 // Type: boolean
154 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
165 // Type: int
166 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
167 // Type: int
168 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
169 // Type: int
170 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
171 // Type: int[]
172 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
173 // Type: boolean
174 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
175 // Type: long
176 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
177
Winson Chung80baf5a2010-08-09 16:03:15 -0700178 // tags for the customization tabs
179 private static final String WIDGETS_TAG = "widgets";
180 private static final String FOLDERS_TAG = "folders";
181 private static final String SHORTCUTS_TAG = "shortcuts";
182 private static final String WALLPAPERS_TAG = "wallpapers";
183
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
185
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186 /** The different states that Launcher can be in. */
187 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800192 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800194 private final BroadcastReceiver mCloseSystemDialogsReceiver
195 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800196 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private LayoutInflater mInflater;
199
Joe Onorato00acb122009-08-04 16:04:30 -0400200 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700202
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private AppWidgetManager mAppWidgetManager;
204 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private CellLayout.CellInfo mAddItemCellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -0700207 private int[] mAddItemCoordinates;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private CellLayout.CellInfo mMenuAddInfo;
209 private final int[] mCellCoordinates = new int[2];
210 private FolderInfo mFolderInfo;
211
Joe Onorato7c312c12009-08-13 21:36:53 -0700212 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700214 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700215 private TabHost mHomeCustomizationDrawer;
Winson Chung80baf5a2010-08-09 16:03:15 -0700216 private CustomizePagedView mCustomizePagedView;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218 private Bundle mSavedState;
219
220 private SpannableStringBuilder mDefaultKeySsb = null;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private boolean mWorkspaceLoading = true;
223
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800224 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 private boolean mRestoring;
226 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400232
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static LocaleConfiguration sLocaleConfiguration = null;
234
Romain Guy84f296c2009-11-04 15:00:44 -0800235 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Romain Guy1fbc1c82009-11-09 20:43:08 -0800238 private ImageView mPreviousView;
239 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500240
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400242 private String[] mHotseatConfig = null;
243 private Intent[] mHotseats = null;
244 private Drawable[] mHotseatIcons = null;
245 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700247 private Intent mAppMarketIntent = null;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 @Override
250 protected void onCreate(Bundle savedInstanceState) {
251 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700252
Winson Chungaafa03c2010-06-11 17:34:16 -0700253 if (LauncherApplication.isInPlaceRotationEnabled()) {
254 // hide the status bar (temporary until we get the status bar design figured out)
255 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
256 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
257 }
258
Joe Onorato0589f0f2010-02-08 13:44:00 -0800259 LauncherApplication app = ((LauncherApplication)getApplication());
260 mModel = app.setLauncher(this);
261 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400262 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800263 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700264
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700266 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
267 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 if (PROFILE_STARTUP) {
270 android.os.Debug.startMethodTracing("/sdcard/launcher");
271 }
272
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400273 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 checkForLocaleChange();
275 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700277 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
278 if (mHomeCustomizationDrawer != null) {
279 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700280
Winson Chung80baf5a2010-08-09 16:03:15 -0700281 // share the same customization workspace across all the tabs
282 mCustomizePagedView = new CustomizePagedView(this);
283 TabContentFactory contentFactory = new TabContentFactory() {
284 public View createTabContent(String tag) {
285 return mCustomizePagedView;
286 }
287 };
288
Michael Jurka946ad472010-07-09 18:05:18 -0700289 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700290 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
291 .setIndicator(widgetsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700292 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700293 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
294 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700295 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700296 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
297 .setIndicator(shortcutsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700298 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700299 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
300 .setIndicator(wallpapersLabel).setContent(contentFactory));
301
302 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
303 // new assets
304 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
305 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
306 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
307 TextView text = (TextView) tab.getChildAt(1);
308 text.setTextSize(20.0f);
309 text.setPadding(20, 0, 20, 0);
310 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
311 tab.setBackgroundDrawable(null);
312 }
313
314 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
315 public void onTabChanged(String tabId) {
316 // animate the changing of the tab content by fading pages in and out
317 final int duration = 150;
318 final float alpha = mCustomizePagedView.getAlpha();
319 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
320 "alpha", alpha, 0.0f);
321 alphaAnim.addListener(new AnimatableListenerAdapter() {
322 public void onAnimationEnd(Animatable animation) {
323 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
324 if (tag == WIDGETS_TAG) {
325 mCustomizePagedView.setCustomizationFilter(
326 CustomizePagedView.CustomizationType.WidgetCustomization);
327 } else if (tag == FOLDERS_TAG) {
328 mCustomizePagedView.setCustomizationFilter(
329 CustomizePagedView.CustomizationType.FolderCustomization);
330 } else if (tag == SHORTCUTS_TAG) {
331 mCustomizePagedView.setCustomizationFilter(
332 CustomizePagedView.CustomizationType.ShortcutCustomization);
333 } else if (tag == WALLPAPERS_TAG) {
334 mCustomizePagedView.setCustomizationFilter(
335 CustomizePagedView.CustomizationType.WallpaperCustomization);
336 }
337
338 final float alpha = mCustomizePagedView.getAlpha();
339 Animator alphaAnim = new PropertyAnimator(duration, mCustomizePagedView,
340 "alpha", alpha, 1.0f);
341 alphaAnim.start();
342 }
343 });
344 alphaAnim.start();
345 }
346 });
Michael Jurka946ad472010-07-09 18:05:18 -0700347
348 mHomeCustomizationDrawer.setCurrentTab(0);
349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 setupViews();
351
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800352 registerContentObservers();
353
Joe Onorato7c312c12009-08-13 21:36:53 -0700354 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 mSavedState = savedInstanceState;
357 restoreState(mSavedState);
358
359 if (PROFILE_STARTUP) {
360 android.os.Debug.stopMethodTracing();
361 }
362
363 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
366
367 // For handling default keys
368 mDefaultKeySsb = new SpannableStringBuilder();
369 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800370
371 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
372 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
Romain Guycbb89e42009-06-08 15:52:54 -0700374
Winson Chungaafa03c2010-06-11 17:34:16 -0700375 @Override
376 public void onConfigurationChanged(Configuration newConfig) {
377 // TODO Auto-generated method stub
378 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700379 }
380
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 if (sLocaleConfiguration == null) {
383 new AsyncTask<Void, Void, LocaleConfiguration>() {
384 @Override
385 protected LocaleConfiguration doInBackground(Void... unused) {
386 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
387 readConfiguration(Launcher.this, localeConfiguration);
388 return localeConfiguration;
389 }
390
391 @Override
392 protected void onPostExecute(LocaleConfiguration result) {
393 sLocaleConfiguration = result;
394 checkForLocaleChange(); // recursive, but now with a locale configuration
395 }
396 }.execute();
397 return;
398 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700399
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final Configuration configuration = getResources().getConfiguration();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final String locale = configuration.locale.toString();
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mcc = configuration.mcc;
407
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 final int mnc = configuration.mnc;
410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414 sLocaleConfiguration.locale = locale;
415 sLocaleConfiguration.mcc = mcc;
416 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700417
Joe Onorato0589f0f2010-02-08 13:44:00 -0800418 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400419 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700420
421 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
422 new Thread("WriteLocaleConfiguration") {
423 public void run() {
424 writeConfiguration(Launcher.this, localeConfiguration);
425 }
426 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700427 }
428 }
429
430 private static class LocaleConfiguration {
431 public String locale;
432 public int mcc = -1;
433 public int mnc = -1;
434 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700435
Romain Guy98d01652009-06-30 16:21:04 -0700436 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
437 DataInputStream in = null;
438 try {
439 in = new DataInputStream(context.openFileInput(PREFERENCES));
440 configuration.locale = in.readUTF();
441 configuration.mcc = in.readInt();
442 configuration.mnc = in.readInt();
443 } catch (FileNotFoundException e) {
444 // Ignore
445 } catch (IOException e) {
446 // Ignore
447 } finally {
448 if (in != null) {
449 try {
450 in.close();
451 } catch (IOException e) {
452 // Ignore
453 }
454 }
455 }
456 }
457
458 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
459 DataOutputStream out = null;
460 try {
461 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
462 out.writeUTF(configuration.locale);
463 out.writeInt(configuration.mcc);
464 out.writeInt(configuration.mnc);
465 out.flush();
466 } catch (FileNotFoundException e) {
467 // Ignore
468 } catch (IOException e) {
469 //noinspection ResultOfMethodCallIgnored
470 context.getFileStreamPath(PREFERENCES).delete();
471 } finally {
472 if (out != null) {
473 try {
474 out.close();
475 } catch (IOException e) {
476 // Ignore
477 }
478 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 }
480 }
481
482 static int getScreen() {
483 synchronized (sLock) {
484 return sScreen;
485 }
486 }
487
488 static void setScreen(int screen) {
489 synchronized (sLock) {
490 sScreen = screen;
491 }
492 }
493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700495 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496
497 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700498 // TODO: Put back when we decide about scrolling the wallpaper
499 // boolean isPortrait = display.getWidth() < display.getHeight();
500 // final int width = isPortrait ? display.getWidth() : display.getHeight();
501 // final int height = isPortrait ? display.getHeight() : display.getWidth();
502 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
503 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
505
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400506 // Note: This doesn't do all the client-id magic that BrowserProvider does
507 // in Browser. (http://b/2425179)
508 private Uri getDefaultBrowserUri() {
509 String url = getString(R.string.default_browser_url);
510 if (url.indexOf("{CID}") != -1) {
511 url = url.replace("{CID}", "android-google");
512 }
513 return Uri.parse(url);
514 }
515
516 // Load the Intent templates from arrays.xml to populate the hotseats. For
517 // each Intent, if it resolves to a single app, use that as the launch
518 // intent & use that app's label as the contentDescription. Otherwise,
519 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400520 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400521 if (mHotseatConfig == null) {
522 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
523 if (mHotseatConfig.length > 0) {
524 mHotseats = new Intent[mHotseatConfig.length];
525 mHotseatLabels = new CharSequence[mHotseatConfig.length];
526 mHotseatIcons = new Drawable[mHotseatConfig.length];
527 } else {
528 mHotseats = null;
529 mHotseatIcons = null;
530 mHotseatLabels = null;
531 }
532
533 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
534 for (int i=0; i<mHotseatConfig.length; i++) {
535 // load icon for this slot; currently unrelated to the actual activity
536 try {
537 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
538 } catch (ArrayIndexOutOfBoundsException ex) {
539 Log.w(TAG, "Missing hotseat_icons array item #" + i);
540 mHotseatIcons[i] = null;
541 }
542 }
543 hotseatIconDrawables.recycle();
544 }
545
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 for (int i=0; i<mHotseatConfig.length; i++) {
548 Intent intent = null;
549 if (mHotseatConfig[i].equals("*BROWSER*")) {
550 // magic value meaning "launch user's default web browser"
551 // replace it with a generic web request so we can see if there is indeed a default
552 String defaultUri = getString(R.string.default_browser_url);
553 intent = new Intent(
554 Intent.ACTION_VIEW,
555 ((defaultUri != null)
556 ? Uri.parse(defaultUri)
557 : getDefaultBrowserUri())
558 ).addCategory(Intent.CATEGORY_BROWSABLE);
559 // note: if the user launches this without a default set, she
560 // will always be taken to the default URL above; this is
561 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700562 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400563 // URL is unavoidably sent to the chosen app.
564 } else {
565 try {
566 intent = Intent.parseUri(mHotseatConfig[i], 0);
567 } catch (java.net.URISyntaxException ex) {
568 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
569 // bogus; leave intent=null
570 }
571 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700572
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 if (intent == null) {
574 mHotseats[i] = null;
575 mHotseatLabels[i] = getText(R.string.activity_not_found);
576 continue;
577 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578
579 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700580 Log.d(TAG, "loadHotseats: hotseat " + i
581 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400583 + "]");
584 }
585
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
587 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700588 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 Log.d(TAG, "Best match for intent: " + bestMatch);
590 Log.d(TAG, "All matches: ");
591 for (ResolveInfo ri : allMatches) {
592 Log.d(TAG, " --> " + ri);
593 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400594 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400595 // did this resolve to a single app, or the resolver?
596 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700597 // can't find any activity to handle this. let's leave the
598 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 // to launch.
600 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400601
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 // set accessibility text to "Not installed"
603 mHotseatLabels[i] = getText(R.string.activity_not_found);
604 } else {
605 boolean found = false;
606 for (ResolveInfo ri : allMatches) {
607 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
608 && bestMatch.activityInfo.applicationInfo.packageName
609 .equals(ri.activityInfo.applicationInfo.packageName)) {
610 found = true;
611 break;
612 }
613 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700614
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400615 if (!found) {
616 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
617 // the bestMatch is probably the ResolveActivity, meaning the
618 // user has not yet selected a default
619 // so: we'll keep the original intent for now
620 mHotseats[i] = intent;
621
622 // set the accessibility text to "Select shortcut"
623 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
624 } else {
625 // we have an app!
626 // now reconstruct the intent to launch it through the front
627 // door
628 ComponentName com = new ComponentName(
629 bestMatch.activityInfo.applicationInfo.packageName,
630 bestMatch.activityInfo.name);
631 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
632
633 // load the app label for accessibility
634 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
635 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400636 }
637
638 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700639 Log.d(TAG, "loadHotseats: hotseat " + i
640 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400641 + ((mHotseats[i] == null)
642 ? "null"
643 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400644 + "] label=[" + mHotseatLabels[i]
645 + "]"
646 );
647 }
648 }
649 }
650
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 @Override
652 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700653 mWaitingForResult = false;
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 // The pattern used here is that a user PICKs a specific application,
656 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
659 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700660
Romain Guy94dabf12009-07-21 10:55:43 -0700661 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 switch (requestCode) {
663 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400664 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
666 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800667 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400670 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
672 case REQUEST_PICK_LIVE_FOLDER:
673 addLiveFolder(data);
674 break;
675 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400676 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700683 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700685 case REQUEST_PICK_WALLPAPER:
686 // We just wanted the activity result here so we can clear mWaitingForResult
687 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
Romain Guy18042c82009-11-06 11:44:55 -0800689 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
690 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
691 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 // Clean up the appWidgetId if we canceled
693 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
694 if (appWidgetId != -1) {
695 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699
700 @Override
701 protected void onResume() {
702 super.onResume();
703
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800704 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800705
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400707 mWorkspaceLoading = true;
708 mModel.startLoader(this, true);
709 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700711 // When we resume Launcher, a different Activity might be responsible for the app
712 // market intent, so refresh the icon
713 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715
716 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700717 protected void onPause() {
718 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700719 // Some launcher layouts don't have a previous and next view
720 if (mPreviousView != null) {
721 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700722 }
723 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 dismissPreview(mNextView);
725 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800726 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700729 @Override
730 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 // Flag the loader to stop early before switching
732 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800734 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700736
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800737 // We can't hide the IME if it was forced open. So don't bother
738 /*
739 @Override
740 public void onWindowFocusChanged(boolean hasFocus) {
741 super.onWindowFocusChanged(hasFocus);
742
743 if (hasFocus) {
744 final InputMethodManager inputManager = (InputMethodManager)
745 getSystemService(Context.INPUT_METHOD_SERVICE);
746 WindowManager.LayoutParams lp = getWindow().getAttributes();
747 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
748 android.os.Handler()) {
749 protected void onReceiveResult(int resultCode, Bundle resultData) {
750 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
751 }
752 });
753 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
754 }
755 }
756 */
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 private boolean acceptFilter() {
759 final InputMethodManager inputManager = (InputMethodManager)
760 getSystemService(Context.INPUT_METHOD_SERVICE);
761 return !inputManager.isFullscreenMode();
762 }
763
764 @Override
765 public boolean onKeyDown(int keyCode, KeyEvent event) {
766 boolean handled = super.onKeyDown(keyCode, event);
767 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
768 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
769 keyCode, event);
770 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700772 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700773 // showSearchDialog()
774 // If there are multiple keystrokes before the search dialog takes focus,
775 // onSearchRequested() will be called for every keystroke,
776 // but it is idempotent, so it's fine.
777 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779 }
780
Joe Onorato8a9625e2010-01-28 15:55:35 -0800781 // Eat the long press event so the keyboard doesn't come up.
782 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
783 return true;
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return handled;
787 }
788
Karl Rosaen138a0412009-04-23 19:00:21 -0700789 private String getTypedText() {
790 return mDefaultKeySsb.toString();
791 }
792
793 private void clearTypedText() {
794 mDefaultKeySsb.clear();
795 mDefaultKeySsb.clearSpans();
796 Selection.setSelection(mDefaultKeySsb, 0);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 /**
800 * Restores the previous state, if it exists.
801 *
802 * @param savedState The previous state.
803 */
804 private void restoreState(Bundle savedState) {
805 if (savedState == null) {
806 return;
807 }
808
Joe Onorato3a8820b2009-11-10 15:06:42 -0800809 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
810 if (allApps) {
811 showAllApps(false);
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
815 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700816 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 }
818
819 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
820 if (addScreen > -1) {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700821 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 mAddItemCellInfo = new CellLayout.CellInfo();
823 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
824 addItemCellInfo.valid = true;
825 addItemCellInfo.screen = addScreen;
826 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
827 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
828 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
829 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Michael Jurkac28de512010-08-13 11:27:44 -0700830 addItemCellInfo.updateOccupiedCells(
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
832 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
833 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
834 mRestoring = true;
835 }
836
837 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
838 if (renameFolder) {
839 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700840 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 mRestoring = true;
842 }
843 }
844
845 /**
846 * Finds all the views we need and configure them properly.
847 */
848 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700849 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400850
Romain Guyb1b69f52009-08-10 15:10:15 -0700851 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400852 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853
Joe Onorato7c312c12009-08-13 21:36:53 -0700854 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700855 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700856 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800857 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700858 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700859 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700860
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700861 if (LauncherApplication.isScreenXLarge()) {
862 // They need to be INVISIBLE initially so that they will be measured in the layout.
863 // Otherwise the animations are messed up when we show them for the first time.
864 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
865 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
866 }
867
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
869 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500870 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700871
Joe Onorato7c312c12009-08-13 21:36:53 -0700872 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
873 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700875 View handleView = findViewById(R.id.all_apps_button);
876 if (handleView != null && handleView instanceof HandleView) {
877 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700878 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700879 mHandleView.setLauncher(this);
880 mHandleView.setOnClickListener(this);
881 mHandleView.setOnLongClickListener(this);
882 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800883
Winson Chung80baf5a2010-08-09 16:03:15 -0700884 if (mCustomizePagedView != null) {
885 mCustomizePagedView.setLauncher(this);
886 mCustomizePagedView.setDragController(dragController);
887 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700888 } else {
889 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
890 hotseatLeft.setContentDescription(mHotseatLabels[0]);
891 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
892 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
893 hotseatRight.setContentDescription(mHotseatLabels[1]);
894 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400895
Michael Jurkaaf442092010-06-10 17:01:57 -0700896 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
897 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800898
Michael Jurkaaf442092010-06-10 17:01:57 -0700899 Drawable previous = mPreviousView.getDrawable();
900 Drawable next = mNextView.getDrawable();
901 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902
Michael Jurkaaf442092010-06-10 17:01:57 -0700903 mPreviousView.setHapticFeedbackEnabled(false);
904 mPreviousView.setOnLongClickListener(this);
905 mNextView.setHapticFeedbackEnabled(false);
906 mNextView.setOnLongClickListener(this);
907 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400910 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
913 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400914 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700915 int deleteZoneHandleId;
916 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700917 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700918 } else {
919 deleteZoneHandleId = R.id.all_apps_button_cluster;
920 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700921 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700922 dragController.addDragListener(deleteZone);
923
924 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
925 if (infoButton != null) {
926 infoButton.setLauncher(this);
927 infoButton.setHandle(findViewById(R.id.configure_button));
928 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
929 dragController.addDragListener(infoButton);
930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931
Joe Onorato00acb122009-08-04 16:04:30 -0400932 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400933 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800934 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700935
Joe Onorato00acb122009-08-04 16:04:30 -0400936 // The order here is bottom to top.
937 dragController.addDropTarget(workspace);
938 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700939 if (infoButton != null) {
940 dragController.addDropTarget(infoButton);
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
943
Romain Guy8a73c512009-11-09 19:19:59 -0800944 @SuppressWarnings({"UnusedDeclaration"})
945 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800946 if (!isAllAppsVisible()) {
947 mWorkspace.scrollLeft();
948 }
Romain Guy8a73c512009-11-09 19:19:59 -0800949 }
950
951 @SuppressWarnings({"UnusedDeclaration"})
952 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800953 if (!isAllAppsVisible()) {
954 mWorkspace.scrollRight();
955 }
Romain Guy8a73c512009-11-09 19:19:59 -0800956 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400957
958 @SuppressWarnings({"UnusedDeclaration"})
959 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400960 if (isAllAppsVisible()) return;
961
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400962 int index = -1;
963 if (v.getId() == R.id.hotseat_left) {
964 index = 0;
965 } else if (v.getId() == R.id.hotseat_right) {
966 index = 1;
967 }
968
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400969 // reload these every tap; you never know when they might change
970 loadHotseats();
971 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
972 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400973 startActivitySafely(
974 mHotseats[index],
975 "hotseat"
976 );
977 }
978 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 /**
981 * Creates a view representing a shortcut.
982 *
983 * @param info The data structure describing the shortcut.
984 *
985 * @return A View inflated from R.layout.application.
986 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700989 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 }
991
992 /**
993 * Creates a view representing a shortcut inflated from the specified resource.
994 *
995 * @param layoutResId The id of the XML layout used to create the shortcut.
996 * @param parent The group the shortcut belongs to.
997 * @param info The data structure describing the shortcut.
998 *
999 * @return A View inflated from layoutResId.
1000 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001001 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1003
Joe Onorato0589f0f2010-02-08 13:44:00 -08001004 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1005 new FastBitmapDrawable(info.getIcon(mIconCache)),
1006 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 favorite.setText(info.title);
1008 favorite.setTag(info);
1009 favorite.setOnClickListener(this);
1010
1011 return favorite;
1012 }
1013
1014 /**
1015 * Add an application shortcut to the workspace.
1016 *
1017 * @param data The intent describing the application.
1018 * @param cellInfo The position on screen where to create the shortcut.
1019 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001020 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001021 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 if (!findSingleSlot(cellInfo)) return;
1023
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1025 data, context);
1026
Romain Guy73b979d2009-06-09 12:57:21 -07001027 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001028 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.container = ItemInfo.NO_ID;
1031 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
1032 } else {
1033 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
1035 }
Romain Guycbb89e42009-06-08 15:52:54 -07001036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 /**
1038 * Add a shortcut to the workspace.
1039 *
1040 * @param data The intent describing the shortcut.
1041 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001043 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001044 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -07001046
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048
1049 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001051 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1052 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054 }
1055
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001060 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001061 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 */
Michael Jurkaaf442092010-06-10 17:01:57 -07001063 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001064 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001066 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001067 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -07001068 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 // We have saved the position to which the widget was dragged-- this really only matters
1072 // if we are placing widgets on a "spring-loaded" screen
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073 final int[] xy = mCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074
1075 // For now, we don't save the coordinate where we dropped the icon because we're not
1076 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1077 // a widget on the home screen in case we want to add it in the future
1078 final int[] xyTouch = null;
1079 //final int[] xyTouch = mAddItemCoordinates;
1080 boolean findNearestVacantAreaFailed = false;
1081 if (xyTouch != null) {
1082 CellLayout screen = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1083 int[] result = screen.findNearestVacantArea(
1084 mAddItemCoordinates[0], mAddItemCoordinates[1],
1085 spans[0], spans[1], cellInfo, xy);
1086 findNearestVacantAreaFailed = (result == null);
1087 }
1088
1089 if (findNearestVacantAreaFailed ||
1090 (xyTouch == null && !findSlot(cellInfo, xy, spans[0], spans[1]))) {
Romain Guy18042c82009-11-06 11:44:55 -08001091 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1092 return;
1093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001095 // Build Launcher-specific widget info and save to database
1096 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 launcherInfo.spanX = spans[0];
1098 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 LauncherModel.addItemToDatabase(this, launcherInfo,
1101 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurkaa63c4522010-08-19 13:52:27 -07001102 cellInfo.screen, xy[0], xy[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103
1104 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001105 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001108 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Michael Jurkaa63c4522010-08-19 13:52:27 -07001113 mWorkspace.addInScreen(launcherInfo.hostView, cellInfo.screen, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116 }
Romain Guycbb89e42009-06-08 15:52:54 -07001117
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1119 mDesktopItems.remove(launcherInfo);
1120 launcherInfo.hostView = null;
1121 }
1122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 public LauncherAppWidgetHost getAppWidgetHost() {
1124 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001127 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001128 getWindow().closeAllPanels();
1129
1130 try {
1131 dismissDialog(DIALOG_CREATE_SHORTCUT);
1132 // Unlock the workspace if the dialog was showing
1133 } catch (Exception e) {
1134 // An exception is thrown if the dialog is not visible, which is fine
1135 }
1136
1137 try {
1138 dismissDialog(DIALOG_RENAME_FOLDER);
1139 // Unlock the workspace if the dialog was showing
1140 } catch (Exception e) {
1141 // An exception is thrown if the dialog is not visible, which is fine
1142 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001143
1144 // Whatever we were doing is hereby canceled.
1145 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001146 }
1147
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 @Override
1149 protected void onNewIntent(Intent intent) {
1150 super.onNewIntent(intent);
1151
1152 // Close the menu
1153 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001154 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001155 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001156
Joe Onorato14f122b2009-11-19 14:06:36 -08001157 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1158 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1159 boolean allAppsVisible = isAllAppsVisible();
Michael Jurka01f0ed42010-08-20 00:41:17 -07001160 boolean customizationDrawerVisible = isCustomizationDrawerVisible();
1161
Michael Jurka4cb37242010-08-09 21:05:32 -07001162 // in all these cases, only animate if we're already on home
1163 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka01f0ed42010-08-20 00:41:17 -07001164 if (alreadyOnHome && !mWorkspace.isSmall() &&
1165 !allAppsVisible && !customizationDrawerVisible) {
1166 mWorkspace.shrinkToMiddle();
1167 } else {
1168 mWorkspace.unshrink(alreadyOnHome);
1169 }
Michael Jurka0142d492010-08-25 17:46:15 -07001170 } else if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001171 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurka01f0ed42010-08-20 00:41:17 -07001172 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001173 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001174 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001175 hideCustomizationDrawer(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001176
Joe Onorato3a8820b2009-11-10 15:06:42 -08001177 final View v = getWindow().peekDecorView();
1178 if (v != null && v.getWindowToken() != null) {
1179 InputMethodManager imm = (InputMethodManager)getSystemService(
1180 INPUT_METHOD_SERVICE);
1181 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183 }
1184 }
1185
1186 @Override
1187 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1188 // Do not call super here
1189 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001190
1191 if (mHomeCustomizationDrawer != null) {
1192 String cur = savedInstanceState.getString("currentTab");
1193 if (cur != null) {
1194 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1195 }
1196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
1198
1199 @Override
1200 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001201 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202
1203 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1204 if (folders.size() > 0) {
1205 final int count = folders.size();
1206 long[] ids = new long[count];
1207 for (int i = 0; i < count; i++) {
1208 final FolderInfo info = folders.get(i).getInfo();
1209 ids[i] = info.id;
1210 }
1211 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1212 } else {
1213 super.onSaveInstanceState(outState);
1214 }
1215
Joe Onoratofb0ca672009-09-14 17:55:46 -04001216 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001217 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001219 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
1221 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1222 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1223 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1224
1225 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1226 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1227 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1228 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1229 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1230 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1231 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1232 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001233 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
1235
1236 if (mFolderInfo != null && mWaitingForResult) {
1237 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1238 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1239 }
Michael Jurka946ad472010-07-09 18:05:18 -07001240
1241 if (mHomeCustomizationDrawer != null) {
1242 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1243 if (currentTabTag != null) {
1244 outState.putString("currentTab", currentTabTag);
1245 }
1246 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 }
1248
1249 @Override
1250 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001251 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001254 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001256 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
1258
1259 TextKeyListener.getInstance().release();
1260
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262
Joe Onorato9c1289c2009-08-17 11:03:03 -04001263 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001264
1265 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001266
Patrick Dubroyab962b72010-07-26 12:10:10 -07001267 // Some launcher layouts don't have a previous and next view
1268 if (mPreviousView != null) {
1269 dismissPreview(mPreviousView);
1270 }
1271 if (mNextView != null) {
1272 dismissPreview(mNextView);
1273 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001274
1275 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
1277
1278 @Override
1279 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001280 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 super.startActivityForResult(intent, requestCode);
1282 }
1283
1284 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001285 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001287
Joe Onorato7bb17492009-09-24 17:51:01 -07001288 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001289
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 if (initialQuery == null) {
1291 // Use any text typed in the launcher as the initial query
1292 initialQuery = getTypedText();
1293 clearTypedText();
1294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 if (appSearchData == null) {
1296 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001297 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
Romain Guycbb89e42009-06-08 15:52:54 -07001299
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 final SearchManager searchManager =
1301 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001302 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001303 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 }
1305
1306 @Override
1307 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001308 if (isWorkspaceLocked()) {
1309 return false;
1310 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311
1312 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1315 .setIcon(android.R.drawable.ic_menu_add)
1316 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001317 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 .setIcon(android.R.drawable.ic_menu_gallery)
1319 .setAlphabeticShortcut('W');
1320 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1321 .setIcon(android.R.drawable.ic_search_category_default)
1322 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1323 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1324 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1325 .setAlphabeticShortcut('N');
1326
1327 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001328 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1329 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
1331 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1332 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1333 .setIntent(settings);
1334
1335 return true;
1336 }
1337
1338 @Override
1339 public boolean onPrepareOptionsMenu(Menu menu) {
1340 super.onPrepareOptionsMenu(menu);
1341
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001342 // If all apps is animating, don't show the menu, because we don't know
1343 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001344 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001345 return false;
1346 }
1347
1348 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001349 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001350 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1351 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1352
1353 // Disable add if the workspace is full.
1354 if (visible) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001355 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001356 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
1359 return true;
1360 }
1361
Michael Jurka0e260592010-06-30 17:07:39 -07001362 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1363 // using the settings menu to add an item, something similar happens in showAddDialog
1364 public void prepareAddItemFromHomeCustomizationDrawer() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001365 mMenuAddInfo = mWorkspace.updateOccupiedCellsForCurrentScreen(null);
Michael Jurka0e260592010-06-30 17:07:39 -07001366 mAddItemCellInfo = mMenuAddInfo;
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 @Override
1370 public boolean onOptionsItemSelected(MenuItem item) {
1371 switch (item.getItemId()) {
1372 case MENU_ADD:
1373 addItems();
1374 return true;
1375 case MENU_WALLPAPER_SETTINGS:
1376 startWallpaper();
1377 return true;
1378 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001379 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 return true;
1381 case MENU_NOTIFICATIONS:
1382 showNotifications();
1383 return true;
1384 }
1385
1386 return super.onOptionsItemSelected(item);
1387 }
Romain Guycbb89e42009-06-08 15:52:54 -07001388
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 /**
1390 * Indicates that we want global search for this activity by setting the globalSearch
1391 * argument for {@link #startSearch} to true.
1392 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001394 @Override
1395 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001396 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001398 }
1399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 public boolean isWorkspaceLocked() {
1401 return mWorkspaceLoading || mWaitingForResult;
1402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001405 if (LauncherApplication.isScreenXLarge()) {
1406 // Animate the widget chooser up from the bottom of the screen
1407 if (!isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001408 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001409 }
1410 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001411 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001412 showAddDialog(mMenuAddInfo);
1413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
Michael Jurkaa63c4522010-08-19 13:52:27 -07001416 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo,
1417 int[] position) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001418 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001419
1420 // only set mAddItemCoordinates if we dropped on home screen in "spring-loaded" manner
1421 mAddItemCoordinates = position;
Michael Jurkaaf442092010-06-10 17:01:57 -07001422 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1423 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1424 addAppWidgetImpl(appWidgetId);
1425 }
1426
1427 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001428 // TODO: catch bad widget exception when sent
1429 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001430 // TODO: Is this log message meaningful?
1431 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1432 addAppWidgetImpl(appWidgetId);
1433 }
1434
1435 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001436 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
Bjorn Bringert7984c942009-12-09 15:38:25 +00001438 if (appWidget.configure != null) {
1439 // Launch over to configure widget, if needed
1440 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1441 intent.setComponent(appWidget.configure);
1442 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1443
Romain Guy8e633c52010-03-04 12:51:36 -08001444 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001446 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001447 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 }
1449 }
Romain Guycbb89e42009-06-08 15:52:54 -07001450
Joe Onoratodeb98af2010-02-19 14:59:39 -08001451 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001452 // Handle case where user selected "Applications"
1453 String applicationName = getResources().getString(R.string.group_applications);
1454 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001455
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001456 if (applicationName != null && applicationName.equals(shortcutName)) {
1457 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1458 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001459
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001460 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1461 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001462 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001463 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001464 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 }
1467
1468 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001469 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001470 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001471 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001473 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001474 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001475 } else {
1476 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
1479
Joe Onorato9c1289c2009-08-17 11:03:03 -04001480 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 UserFolderInfo folderInfo = new UserFolderInfo();
1482 folderInfo.title = getText(R.string.folder_name);
1483
1484 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
Michael Jurka0142d492010-08-25 17:46:15 -07001485 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 if (!findSingleSlot(cellInfo)) return;
1487
1488 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001489 LauncherModel.addItemToDatabase(this, folderInfo,
1490 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0142d492010-08-25 17:46:15 -07001491 mWorkspace.getCurrentPage(), cellInfo.cellX, cellInfo.cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001492 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
1494 // Create the view
1495 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001496 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001498 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Joe Onorato9c1289c2009-08-17 11:03:03 -04001501 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001502 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001503 }
1504
1505 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
Michael Jurka0142d492010-08-25 17:46:15 -07001506 cellInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 if (!findSingleSlot(cellInfo)) return;
1508
1509 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1510
1511 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001513 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1515 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 }
1517 }
1518
1519 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1520 CellLayout.CellInfo cellInfo, boolean notify) {
1521
1522 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1523 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1524
1525 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 Intent.ShortcutIconResource iconResource = null;
1527
1528 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1529 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1530 try {
1531 iconResource = (Intent.ShortcutIconResource) extra;
1532 final PackageManager packageManager = context.getPackageManager();
1533 Resources resources = packageManager.getResourcesForApplication(
1534 iconResource.packageName);
1535 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1536 icon = resources.getDrawable(id);
1537 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001538 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
1540 }
1541
1542 if (icon == null) {
1543 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1544 }
1545
1546 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001547 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 info.title = name;
1549 info.iconResource = iconResource;
1550 info.uri = data.getData();
1551 info.baseIntent = baseIntent;
1552 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1553 LiveFolders.DISPLAY_MODE_GRID);
1554
1555 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1556 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001557 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558
1559 return info;
1560 }
1561
1562 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1563 final int[] xy = new int[2];
1564 if (findSlot(cellInfo, xy, 1, 1)) {
1565 cellInfo.cellX = xy[0];
1566 cellInfo.cellY = xy[1];
1567 return true;
1568 }
1569 return false;
1570 }
1571
1572 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1573 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001574 CellLayout targetLayout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
1575 cellInfo = targetLayout.updateOccupiedCells(null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1577 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1578 return false;
1579 }
1580 }
1581 return true;
1582 }
1583
1584 private void showNotifications() {
1585 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1586 if (statusBar != null) {
1587 statusBar.expand();
1588 }
1589 }
1590
1591 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001592 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001594 Intent chooser = Intent.createChooser(pickWallpaper,
1595 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001596 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1597 // Removed in Eclair MR1
1598// WallpaperManager wm = (WallpaperManager)
1599// getSystemService(Context.WALLPAPER_SERVICE);
1600// WallpaperInfo wi = wm.getWallpaperInfo();
1601// if (wi != null && wi.getSettingsActivity() != null) {
1602// LabeledIntent li = new LabeledIntent(getPackageName(),
1603// R.string.configure_wallpaper, 0);
1604// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1605// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1606// }
Mike Clerona0618e42009-10-22 13:55:21 -07001607 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001610 /**
1611 * Registers various content observers. The current implementation registers
1612 * only a favorites observer to keep track of the favorites applications.
1613 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001614 private void registerContentObservers() {
1615 ContentResolver resolver = getContentResolver();
1616 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1617 true, mWidgetObserver);
1618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 @Override
1621 public boolean dispatchKeyEvent(KeyEvent event) {
1622 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1623 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001625 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001626 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001627 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001628 dumpState();
1629 return true;
1630 }
1631 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001632 }
1633 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1634 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001635 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 return true;
1637 }
1638 }
1639
1640 return super.dispatchKeyEvent(event);
1641 }
1642
Joe Onorato88ec0992009-11-19 13:16:06 -08001643 @Override
1644 public void onBackPressed() {
1645 if (isAllAppsVisible()) {
1646 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001647 } else if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001648 hideCustomizationDrawer(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001649 } else {
1650 closeFolder();
1651 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001652 // Some launcher layouts don't have a previous and next view
1653 if (mPreviousView != null) {
1654 dismissPreview(mPreviousView);
1655 dismissPreview(mNextView);
1656 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001657 }
1658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 private void closeFolder() {
1660 Folder folder = mWorkspace.getOpenFolder();
1661 if (folder != null) {
1662 closeFolder(folder);
1663 }
1664 }
1665
1666 void closeFolder(Folder folder) {
1667 folder.getInfo().opened = false;
1668 ViewGroup parent = (ViewGroup) folder.getParent();
1669 if (parent != null) {
1670 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001671 if (folder instanceof DropTarget) {
1672 // Live folders aren't DropTargets.
1673 mDragController.removeDropTarget((DropTarget)folder);
1674 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 }
1676 folder.onClose();
1677 }
1678
1679 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001680 * Re-listen when widgets are reset.
1681 */
1682 private void onAppWidgetReset() {
1683 mAppWidgetHost.startListening();
1684 }
1685
1686 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001687 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1688 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001690 private void unbindDesktopItems() {
1691 for (ItemInfo item: mDesktopItems) {
1692 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 }
1694 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 /**
1697 * Launches the intent referred by the clicked shortcut.
1698 *
1699 * @param v The view representing the clicked shortcut.
1700 */
1701 public void onClick(View v) {
1702 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001703 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001705 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001706 int[] pos = new int[2];
1707 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001708 intent.setSourceBounds(new Rect(pos[0], pos[1],
1709 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001710 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 } else if (tag instanceof FolderInfo) {
1712 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001713 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001714 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001715 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001716 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001717 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 }
1720 }
1721
Michael Jurka0e260592010-06-30 17:07:39 -07001722 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001723 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001724 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001725 hideCustomizationDrawer(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001726 return false;
1727 }
1728
Michael Jurkaaf442092010-06-10 17:01:57 -07001729 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001730 * Event handler for the search button
1731 *
1732 * @param v The view that was clicked.
1733 */
1734 public void onClickSearchButton(View v) {
1735 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1736 View button = findViewById(R.id.search_button);
1737 i.setSourceBounds(
1738 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1739 startActivity(i);
1740 }
1741
1742 /**
1743 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001744 * enters home screen customization mode.
1745 *
1746 * @param v The view that was clicked.
1747 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001748 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001749 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001750 }
1751
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001752 /**
1753 * Event handler for the "grid" button that appears on the home screen, which
1754 * enters all apps mode.
1755 *
1756 * @param v The view that was clicked.
1757 */
1758 public void onClickAllAppsButton(View v) {
1759 showAllApps(true);
1760 }
1761
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001762 public void onClickAppMarketButton(View v) {
1763 if (mAppMarketIntent != null) {
1764 startActivitySafely(mAppMarketIntent, "app market");
1765 }
1766 }
1767
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001768 void startApplicationDetailsActivity(String packageName) {
1769 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1770 Uri.fromParts("package", packageName, null));
1771 startActivity(intent);
1772 }
1773
Joe Onoratof984e852010-03-25 09:47:45 -07001774 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1776 try {
1777 startActivity(intent);
1778 } catch (ActivityNotFoundException e) {
1779 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001780 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 } catch (SecurityException e) {
1782 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001783 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001785 "or use the exported attribute for this activity. "
1786 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001789
Romain Guy8e633c52010-03-04 12:51:36 -08001790 void startActivityForResultSafely(Intent intent, int requestCode) {
1791 try {
1792 startActivityForResult(intent, requestCode);
1793 } catch (ActivityNotFoundException e) {
1794 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1795 } catch (SecurityException e) {
1796 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1797 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1798 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1799 "or use the exported attribute for this activity.", e);
1800 }
1801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802
1803 private void handleFolderClick(FolderInfo folderInfo) {
1804 if (!folderInfo.opened) {
1805 // Close any open folder
1806 closeFolder();
1807 // Open the requested folder
1808 openFolder(folderInfo);
1809 } else {
1810 // Find the open folder...
1811 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1812 int folderScreen;
1813 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001814 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 // .. and close it
1816 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001817 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001818 // Close any folder open on the current screen
1819 closeFolder();
1820 // Pull the folder onto this screen
1821 openFolder(folderInfo);
1822 }
1823 }
1824 }
1825 }
1826
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 /**
1828 * Opens the user fodler described by the specified tag. The opening of the folder
1829 * is animated relative to the specified View. If the View is null, no animation
1830 * is played.
1831 *
1832 * @param folderInfo The FolderInfo describing the folder to open.
1833 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001834 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 Folder openFolder;
1836
1837 if (folderInfo instanceof UserFolderInfo) {
1838 openFolder = UserFolder.fromXml(this);
1839 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001840 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 } else {
1842 return;
1843 }
1844
Joe Onorato00acb122009-08-04 16:04:30 -04001845 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 openFolder.setLauncher(this);
1847
1848 openFolder.bind(folderInfo);
1849 folderInfo.opened = true;
1850
Winson Chungaafa03c2010-06-11 17:34:16 -07001851 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 openFolder.onOpen();
1854 }
1855
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001857 switch (v.getId()) {
1858 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001859 if (!isAllAppsVisible()) {
1860 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1861 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001862 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001863 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001864 return true;
1865 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001866 if (!isAllAppsVisible()) {
1867 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1868 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001869 showPreviews(v);
1870 }
1871 return true;
1872 case R.id.all_apps_button:
1873 if (!isAllAppsVisible()) {
1874 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1875 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1876 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001877 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001878 return true;
1879 }
1880
Joe Onorato9c1289c2009-08-17 11:03:03 -04001881 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 return false;
1883 }
1884
1885 if (!(v instanceof CellLayout)) {
1886 v = (View) v.getParent();
1887 }
1888
1889 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1890
1891 // This happens when long clicking an item with the dpad/trackball
1892 if (cellInfo == null) {
1893 return true;
1894 }
1895
1896 if (mWorkspace.allowLongPress()) {
1897 if (cellInfo.cell == null) {
1898 if (cellInfo.valid) {
1899 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001900 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001901 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1902 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 showAddDialog(cellInfo);
1904 }
1905 } else {
1906 if (!(cellInfo.cell instanceof Folder)) {
1907 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001908 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1909 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 mWorkspace.startDrag(cellInfo);
1911 }
1912 }
1913 }
1914 return true;
1915 }
1916
Romain Guye6b8e2f2009-11-10 11:56:55 -08001917 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001918 private void dismissPreview(final View v) {
1919 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001920 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001921 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1922 public void onDismiss() {
1923 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1924 int count = group.getChildCount();
1925 for (int i = 0; i < count; i++) {
1926 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1927 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001928 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1929 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1930
1931 v.setTag(R.id.workspace, null);
1932 v.setTag(R.id.icon, null);
1933 window.setOnDismissListener(null);
1934 }
1935 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001936 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001937 }
1938 v.setTag(null);
1939 }
1940
Romain Guyf8e6a802009-12-07 17:48:02 -08001941 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001942 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001943 }
1944
Romain Guya6abce82009-11-10 02:54:41 -08001945 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001946 final Resources resources = getResources();
1947 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001948
Romain Guya6abce82009-11-10 02:54:41 -08001949 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001950
Romain Guy9d31e9f2009-11-11 18:54:28 -08001951 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001952
Romain Guyf8e6a802009-12-07 17:48:02 -08001953 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001954 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001955 int extraW = (int) ((r.left + r.right) * max);
1956 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001957
1958 int aW = cell.getWidth() - extraW;
1959 float w = aW / max;
1960
1961 int width = cell.getWidth();
1962 int height = cell.getHeight();
1963 int x = cell.getLeftPadding();
1964 int y = cell.getTopPadding();
1965 width -= (x + cell.getRightPadding());
1966 height -= (y + cell.getBottomPadding());
1967
1968 float scale = w / width;
1969
1970 int count = end - start;
1971
1972 final float sWidth = width * scale;
1973 float sHeight = height * scale;
1974
Romain Guye6b8e2f2009-11-10 11:56:55 -08001975 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001976
Romain Guye6b8e2f2009-11-10 11:56:55 -08001977 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1978 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001979
1980 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001981 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001982 cell = (CellLayout) workspace.getChildAt(i);
1983
Romain Guyf8e6a802009-12-07 17:48:02 -08001984 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001985 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001986
1987 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001988 c.scale(scale, scale);
1989 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1990 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001991
Romain Guy9d31e9f2009-11-11 18:54:28 -08001992 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001993 image.setImageBitmap(bitmap);
1994 image.setTag(i);
1995 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001996 image.setOnFocusChangeListener(handler);
1997 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07001998 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001999
2000 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2002
Winson Chungaafa03c2010-06-11 17:34:16 -07002003 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002004 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002005
2006 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002007 p.setContentView(preview);
2008 p.setWidth((int) (sWidth * count + extraW));
2009 p.setHeight((int) (sHeight + extraH));
2010 p.setAnimationStyle(R.style.AnimationPreview);
2011 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002012 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002013 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002014 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002015
Romain Guye6b8e2f2009-11-10 11:56:55 -08002016 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2017 public void onDismiss() {
2018 dismissPreview(anchor);
2019 }
2020 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002021
2022 anchor.setTag(p);
2023 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002024 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002025 }
2026
Romain Guy9d31e9f2009-11-11 18:54:28 -08002027 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002028 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002029
Romain Guye6b8e2f2009-11-10 11:56:55 -08002030 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002031 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002032 }
2033
2034 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002035 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002036 v.post(this);
2037 }
2038
2039 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002040 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002041 }
2042
2043 public void onFocusChange(View v, boolean hasFocus) {
2044 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002045 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002046 }
Romain Guya6abce82009-11-10 02:54:41 -08002047 }
2048 }
2049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 Workspace getWorkspace() {
2051 return mWorkspace;
2052 }
2053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 @Override
2055 protected Dialog onCreateDialog(int id) {
2056 switch (id) {
2057 case DIALOG_CREATE_SHORTCUT:
2058 return new CreateShortcut().createDialog();
2059 case DIALOG_RENAME_FOLDER:
2060 return new RenameFolder().createDialog();
2061 }
2062
2063 return super.onCreateDialog(id);
2064 }
2065
2066 @Override
2067 protected void onPrepareDialog(int id, Dialog dialog) {
2068 switch (id) {
2069 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 break;
2071 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002072 if (mFolderInfo != null) {
2073 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2074 final CharSequence text = mFolderInfo.title;
2075 input.setText(text);
2076 input.setSelection(0, text.length());
2077 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 break;
2079 }
2080 }
2081
2082 void showRenameDialog(FolderInfo info) {
2083 mFolderInfo = info;
2084 mWaitingForResult = true;
2085 showDialog(DIALOG_RENAME_FOLDER);
2086 }
2087
2088 private void showAddDialog(CellLayout.CellInfo cellInfo) {
2089 mAddItemCellInfo = cellInfo;
Michael Jurkaa63c4522010-08-19 13:52:27 -07002090 mAddItemCoordinates = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 mWaitingForResult = true;
2092 showDialog(DIALOG_CREATE_SHORTCUT);
2093 }
2094
Joe Onoratodeb98af2010-02-19 14:59:39 -08002095 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002096 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002097 Bundle bundle = new Bundle();
2098
2099 ArrayList<String> shortcutNames = new ArrayList<String>();
2100 shortcutNames.add(getString(R.string.group_applications));
2101 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2102
2103 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2104 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2105 R.drawable.ic_launcher_application));
2106 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2107
2108 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2109 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002110 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002111 pickIntent.putExtras(bundle);
2112
Joe Onoratodeb98af2010-02-19 14:59:39 -08002113 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002114 }
2115
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 private class RenameFolder {
2117 private EditText mInput;
2118
2119 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002120 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2121 mInput = (EditText) layout.findViewById(R.id.folder_name);
2122
2123 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2124 builder.setIcon(0);
2125 builder.setTitle(getString(R.string.rename_folder_title));
2126 builder.setCancelable(true);
2127 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2128 public void onCancel(DialogInterface dialog) {
2129 cleanup();
2130 }
2131 });
2132 builder.setNegativeButton(getString(R.string.cancel_action),
2133 new Dialog.OnClickListener() {
2134 public void onClick(DialogInterface dialog, int which) {
2135 cleanup();
2136 }
2137 }
2138 );
2139 builder.setPositiveButton(getString(R.string.rename_action),
2140 new Dialog.OnClickListener() {
2141 public void onClick(DialogInterface dialog, int which) {
2142 changeFolderName();
2143 }
2144 }
2145 );
2146 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002147
2148 final AlertDialog dialog = builder.create();
2149 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2150 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002151 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002152 mInput.requestFocus();
2153 InputMethodManager inputManager = (InputMethodManager)
2154 getSystemService(Context.INPUT_METHOD_SERVICE);
2155 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002156 }
2157 });
2158
2159 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 }
2161
2162 private void changeFolderName() {
2163 final String name = mInput.getText().toString();
2164 if (!TextUtils.isEmpty(name)) {
2165 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002166 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 mFolderInfo.title = name;
2168 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2169
Joe Onorato9c1289c2009-08-17 11:03:03 -04002170 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002171 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002172 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 } else {
2174 final FolderIcon folderIcon = (FolderIcon)
2175 mWorkspace.getViewForTag(mFolderInfo);
2176 if (folderIcon != null) {
2177 folderIcon.setText(name);
2178 getWorkspace().requestLayout();
2179 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002180 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002181 mWorkspaceLoading = true;
2182 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 }
2184 }
2185 }
2186 cleanup();
2187 }
2188
2189 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 dismissDialog(DIALOG_RENAME_FOLDER);
2191 mWaitingForResult = false;
2192 mFolderInfo = null;
2193 }
2194 }
2195
Daniel Sandler843e8602010-06-07 14:59:01 -04002196 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2197 public boolean isAllAppsVisible() {
Romain Guy5bbc91b2010-08-18 11:38:46 -07002198 return mAllAppsGrid != null && mAllAppsGrid.isVisible();
Joe Onoratofb0ca672009-09-14 17:55:46 -04002199 }
2200
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002201 // AllAppsView.Watcher
2202 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002203 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2204 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002205 mWorkspace.setVisibility(View.GONE);
2206 }
2207 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002208
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002209 private void showToolbarButton(View button) {
2210 button.setAlpha(1.0f);
2211 button.setVisibility(View.VISIBLE);
2212 button.setFocusable(true);
2213 button.setClickable(true);
2214 }
2215
2216 private void hideToolbarButton(View button) {
2217 button.setAlpha(0.0f);
2218 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2219 button.setVisibility(View.INVISIBLE);
2220 button.setFocusable(false);
2221 button.setClickable(false);
2222 }
2223
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002224 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002225 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002226 *
2227 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2228 * @param view The toolbar button to be animated
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002229 * @param seq A Sequencer that will be used to animate the transition. If null, the
2230 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002231 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002232 private void hideOrShowToolbarButton(boolean show, final View view, Sequencer seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002233 final boolean showing = show;
2234 final boolean hiding = !show;
2235
2236 final int duration = show ?
2237 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2238 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2239
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002240 if (seq != null) {
2241 Animatable anim = new PropertyAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2242 anim.addListener(new AnimatableListenerAdapter() {
2243 public void onAnimationStart(Animatable animation) {
2244 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002245 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002246 public void onAnimationEnd(Animatable animation) {
2247 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002248 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002249 });
2250 seq.play(anim);
2251 } else {
2252 if (showing) {
2253 showToolbarButton(view);
2254 } else {
2255 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002256 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002257 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002258 }
2259
2260 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002261 * Show/hide the appropriate toolbar buttons for newState.
2262 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002263 *
2264 * @param newState The state that is being switched to
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002265 * @param showSeq Sequencer in which to put "show" animations, or null.
2266 * @param hideSeq Sequencer in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002267 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002268 private void hideAndShowToolbarButtons(State newState, Sequencer showSeq, Sequencer hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002269 final View searchButton = findViewById(R.id.search_button);
2270 final View allAppsButton = findViewById(R.id.all_apps_button);
2271 final View marketButton = findViewById(R.id.market_button);
2272 final View configureButton = findViewById(R.id.configure_button);
2273
2274 switch (newState) {
2275 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002276 hideOrShowToolbarButton(true, searchButton, showSeq);
2277 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2278 hideOrShowToolbarButton(true, configureButton, showSeq);
2279 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002280 break;
2281 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002282 hideOrShowToolbarButton(true, configureButton, showSeq);
2283 hideOrShowToolbarButton(true, marketButton, showSeq);
2284 hideOrShowToolbarButton(false, searchButton, hideSeq);
2285 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002286 break;
2287 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002288 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2289 hideOrShowToolbarButton(false, searchButton, hideSeq);
2290 hideOrShowToolbarButton(false, marketButton, hideSeq);
2291 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002292 break;
2293 }
2294 }
2295
2296 /**
2297 * Helper method for the cameraZoomIn/cameraZoomOut animations
2298 * @param view The view being animated
2299 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2300 * @param scaleFactor The scale factor used for the zoom
2301 */
2302 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2303 final int height = view.getHeight();
2304 view.setPivotX(view.getWidth() / 2.0f);
2305 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2306 // Assumes that the view is normally anchored to either the top or bottom of the screen
2307 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2308 if (state == State.ALL_APPS) {
2309 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2310 } else {
2311 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2312 }
2313 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002314
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002315 /**
2316 * Zoom the camera out from the workspace to reveal 'toView'.
2317 * Assumes that the view to show is anchored at either the very top or very bottom
2318 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002319 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002320 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002321 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002322 final Resources res = getResources();
2323 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2324 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002325 final boolean toAllApps = (toState == State.ALL_APPS);
2326 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002327
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002328 setPivotsForZoom(toView, toState, scale);
2329
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002330 if (toState == State.ALL_APPS) {
2331 mWorkspace.shrinkToBottom(animated);
2332 } else {
2333 mWorkspace.shrinkToTop(animated);
2334 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002335
2336 if (animated) {
2337 Animator scaleAnim = new PropertyAnimator(duration, toView,
2338 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2339 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2340 scaleAnim.setInterpolator(new DecelerateInterpolator());
2341 scaleAnim.addListener(new AnimatableListenerAdapter() {
2342 public void onAnimationStart(Animatable animation) {
2343 // Prepare the position
2344 toView.setTranslationX(0.0f);
2345 toView.setTranslationY(0.0f);
2346 toView.setVisibility(View.VISIBLE);
2347 }
2348 });
2349
2350 Sequencer toolbarHideAnim = new Sequencer();
2351 Sequencer toolbarShowAnim = new Sequencer();
2352 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2353
2354 // toView should appear right at the end of the workspace shrink animation
2355 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2356
2357 Sequencer s = new Sequencer();
2358 s.playTogether(scaleAnim, toolbarHideAnim);
2359 s.play(scaleAnim).after(startDelay);
2360
2361 // Show the new toolbar buttons just as the main animation is ending
2362 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2363 s.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2364 s.start();
2365 } else {
2366 toView.setTranslationX(0.0f);
2367 toView.setTranslationY(0.0f);
2368 toView.setScaleX(1.0f);
2369 toView.setScaleY(1.0f);
2370 toView.setVisibility(View.VISIBLE);
2371 hideAndShowToolbarButtons(toState, null, null);
2372 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002373 }
2374
2375 /**
2376 * Zoom the camera back into the workspace, hiding 'fromView'.
2377 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002378 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2379 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002380 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002381 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002382 Resources res = getResources();
2383 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2384 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002385 final View fromView =
2386 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2387
2388 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002389
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002390 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002391
2392 if (animated) {
2393 Sequencer s = new Sequencer();
2394 Animator scaleAnim = new PropertyAnimator(duration, fromView,
2395 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2396 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2397 scaleAnim.setInterpolator(new AccelerateInterpolator());
2398 s.addListener(new AnimatableListenerAdapter() {
2399 public void onAnimationEnd(Animatable animation) {
2400 fromView.setVisibility(View.GONE);
2401 fromView.setScaleX(1.0f);
2402 fromView.setScaleY(1.0f);
2403 }
2404 });
2405
2406 Sequencer toolbarHideAnim = new Sequencer();
2407 Sequencer toolbarShowAnim = new Sequencer();
2408 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2409
2410 s.playTogether(scaleAnim, toolbarHideAnim);
2411
2412 // Show the new toolbar buttons at the very end of the whole animation
2413 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2414 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
2415 s.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2416 s.start();
2417 } else {
2418 fromView.setVisibility(View.GONE);
2419 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2420 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002421 }
2422
2423 /**
2424 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2425 * This is the transition used on xlarge screens to go between all apps and
2426 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002427 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2428 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2429 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002430 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002432 final Resources res = getResources();
2433 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002434 final int workspaceHeight = mWorkspace.getHeight();
2435
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002436 final boolean fromAllApps = (fromState == State.ALL_APPS);
2437 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2438 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002439
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002440 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2441 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2442 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2443 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002444
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002445 if (toState == State.ALL_APPS) {
2446 mWorkspace.shrinkToBottom(animated);
2447 } else {
2448 mWorkspace.shrinkToTop(animated);
2449 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002450
2451 if (animated) {
2452 Sequencer s = new Sequencer();
2453 s.addListener(new AnimatableListenerAdapter() {
2454 public void onAnimationStart(Animatable animation) {
2455 toView.setVisibility(View.VISIBLE);
2456 toView.setY(toViewStartY);
2457 }
2458 public void onAnimationEnd(Animatable animation) {
2459 fromView.setVisibility(View.GONE);
2460 }
2461 });
2462
2463 Sequencer toolbarHideAnim = new Sequencer();
2464 Sequencer toolbarShowAnim = new Sequencer();
2465 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2466
2467 s.playTogether(
2468 toolbarHideAnim,
2469 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2470 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2471
2472 // Show the new toolbar buttons just as the main animation is ending
2473 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2474 s.play(toolbarShowAnim).after(duration - fadeInTime);
2475 s.start();
2476 } else {
2477 fromView.setY(fromViewEndY);
2478 fromView.setVisibility(View.GONE);
2479 toView.setY(toViewEndY);
2480 toView.setVisibility(View.VISIBLE);
2481 hideAndShowToolbarButtons(toState, null, null);
2482 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002483 }
2484
2485 void showAllApps(boolean animated) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002486 if (mAllAppsGrid.isVisible())
2487 return;
2488
Michael Jurka79212d82010-07-30 16:36:20 -07002489 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002490 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002491 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002492 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002493 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002494 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002495 } else {
2496 mAllAppsGrid.zoom(1.0f, animated);
2497 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002498
Romain Guyc16fea72010-03-12 17:17:56 -08002499 ((View) mAllAppsGrid).setFocusable(true);
2500 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002501
Joe Onorato7c312c12009-08-13 21:36:53 -07002502 // TODO: fade these two too
2503 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002504 }
2505
Joe Onoratob2061212009-11-24 16:13:54 -05002506 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002507 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002508 * - Home from workspace
2509 * - from center screen
2510 * - from other screens
2511 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002512 * - from center screen
2513 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002514 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002515 * - from center screen
2516 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002517 * - Launch app from workspace and quit
2518 * - with back
2519 * - with home
2520 * - Launch app from all apps and quit
2521 * - with back
2522 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002523 * - Go to a screen that's not the default, then all
2524 * apps, and launch and app, and go back
2525 * - with back
2526 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002527 * - On workspace, long press power and go back
2528 * - with back
2529 * - with home
2530 * - On all apps, long press power and go back
2531 * - with back
2532 * - with home
2533 * - On workspace, power off
2534 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002535 * - Launch an app and turn off the screen while in that app
2536 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002537 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002538 * - From all apps
2539 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002540 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2541 * - From all apps
2542 * - From the center workspace
2543 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002544 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002545 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002546 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002547 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002548 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002550 } else {
2551 mAllAppsGrid.zoom(0.0f, animated);
2552 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002553 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002554 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002555 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002556 }
2557
Joe Onorato7c312c12009-08-13 21:36:53 -07002558 void lockAllApps() {
2559 // TODO
2560 }
2561
2562 void unlockAllApps() {
2563 // TODO
2564 }
2565
Patrick Dubroy558654c2010-07-23 16:48:11 -07002566 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002567 return mHomeCustomizationDrawer != null &&
2568 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002569 }
2570
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002571 // Show the customization drawer (only exists in x-large configuration)
2572 private void showCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002573 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002575 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002577 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002578 }
2579
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002580 // Hide the customization drawer (only exists in x-large configuration)
2581 void hideCustomizationDrawer(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002582 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002583 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002584 }
2585 }
2586
Michael Jurka213d9632010-07-28 11:29:25 -07002587 void onWorkspaceUnshrink() {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002588 final boolean animated = true;
Michael Jurka213d9632010-07-28 11:29:25 -07002589 if (isAllAppsVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002590 closeAllApps(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002591 }
2592 if (isCustomizationDrawerVisible()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 hideCustomizationDrawer(animated);
Michael Jurka213d9632010-07-28 11:29:25 -07002594 }
2595 }
2596
Joe Onorato7404ee42009-07-31 11:54:44 -07002597 /**
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002598 * Sets the app market icon (shown when all apps is visible on x-large screens)
2599 */
2600 private void updateAppMarketIcon() {
2601 if (LauncherApplication.isScreenXLarge()) {
2602 // Find the app market activity by resolving an intent.
2603 // (If multiple app markets are installed, it will return the ResolverActivity.)
2604 PackageManager packageManager = getPackageManager();
2605 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2606 ComponentName activityName = intent.resolveActivity(getPackageManager());
2607 if (activityName != null) {
2608 mAppMarketIntent = intent;
2609 ImageView marketButton = (ImageView) findViewById(R.id.market_button);
2610 Drawable toolbarIcon = null;
2611 try {
2612 // Look for the toolbar icon specified in the activity meta-data
2613 Bundle metaData = packageManager.getActivityInfo(
2614 activityName, PackageManager.GET_META_DATA).metaData;
2615 if (metaData != null) {
2616 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2617 if (iconResId != 0) {
2618 Resources res = packageManager.getResourcesForActivity(activityName);
2619 toolbarIcon = res.getDrawable(iconResId);
2620 }
2621 }
2622 } catch (NameNotFoundException e) {
2623 // Do nothing
2624 }
2625 // If we were unable to find the icon via the meta-data, use a generic one
2626 if (toolbarIcon == null) {
2627 marketButton.setImageResource(R.drawable.app_market_generic);
2628 } else {
2629 marketButton.setImageDrawable(toolbarIcon);
2630 }
2631 } else {
2632 mAppMarketIntent = null;
2633 }
2634 }
2635 }
2636
2637 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002638 * Displays the shortcut creation dialog and launches, if necessary, the
2639 * appropriate activity.
2640 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002641 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002642 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2643 DialogInterface.OnShowListener {
2644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002648 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2651 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002652 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 builder.setInverseBackgroundForced(true);
2655
2656 AlertDialog dialog = builder.create();
2657 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002658 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002659 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 return dialog;
2662 }
2663
2664 public void onCancel(DialogInterface dialog) {
2665 mWaitingForResult = false;
2666 cleanup();
2667 }
2668
Romain Guycbb89e42009-06-08 15:52:54 -07002669 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002670 }
2671
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002673 try {
2674 dismissDialog(DIALOG_CREATE_SHORTCUT);
2675 } catch (Exception e) {
2676 // An exception is thrown if the dialog is not visible, which is fine
2677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002678 }
2679
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002680 /**
2681 * Handle the action clicked in the "Add to home" dialog.
2682 */
2683 public void onClick(DialogInterface dialog, int which) {
2684 Resources res = getResources();
2685 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002687 switch (which) {
2688 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002689 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002690 break;
2691 }
Romain Guycbb89e42009-06-08 15:52:54 -07002692
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002693 case AddAdapter.ITEM_APPWIDGET: {
2694 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002695
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002696 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2697 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002698 // start the pick activity
2699 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2700 break;
2701 }
Romain Guycbb89e42009-06-08 15:52:54 -07002702
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002703 case AddAdapter.ITEM_LIVE_FOLDER: {
2704 // Insert extra item to handle inserting folder
2705 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002706
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002707 ArrayList<String> shortcutNames = new ArrayList<String>();
2708 shortcutNames.add(res.getString(R.string.group_folder));
2709 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002710
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002711 ArrayList<ShortcutIconResource> shortcutIcons =
2712 new ArrayList<ShortcutIconResource>();
2713 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2714 R.drawable.ic_launcher_folder));
2715 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2716
2717 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2718 pickIntent.putExtra(Intent.EXTRA_INTENT,
2719 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2720 pickIntent.putExtra(Intent.EXTRA_TITLE,
2721 getText(R.string.title_select_live_folder));
2722 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002723
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002724 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2725 break;
2726 }
2727
2728 case AddAdapter.ITEM_WALLPAPER: {
2729 startWallpaper();
2730 break;
2731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
2733 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002734
2735 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002736 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 }
2739
2740 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002741 * Receives notifications when applications are added/removed.
2742 */
2743 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2744 @Override
2745 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002746 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002747 String reason = intent.getStringExtra("reason");
2748 if (!"homekey".equals(reason)) {
2749 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002750 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002751 animate = false;
2752 }
Joe Onoratob2061212009-11-24 16:13:54 -05002753 closeAllApps(animate);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002754 hideCustomizationDrawer(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002755 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002756 }
2757 }
2758
2759 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002760 * Receives notifications whenever the appwidgets are reset.
2761 */
2762 private class AppWidgetResetObserver extends ContentObserver {
2763 public AppWidgetResetObserver() {
2764 super(new Handler());
2765 }
2766
2767 @Override
2768 public void onChange(boolean selfChange) {
2769 onAppWidgetReset();
2770 }
2771 }
2772
2773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002777 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002778 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002779 } else {
2780 return SCREEN_COUNT / 2;
2781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002782 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002783
Joe Onorato9c1289c2009-08-17 11:03:03 -04002784 /**
2785 * Refreshes the shortcuts shown on the workspace.
2786 *
2787 * Implementation of the method from LauncherModel.Callbacks.
2788 */
2789 public void startBinding() {
2790 final Workspace workspace = mWorkspace;
2791 int count = workspace.getChildCount();
2792 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002793 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2795 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002796
Joe Onorato9c1289c2009-08-17 11:03:03 -04002797 if (DEBUG_USER_INTERFACE) {
2798 android.widget.Button finishButton = new android.widget.Button(this);
2799 finishButton.setText("Finish");
2800 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2801
2802 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2803 public void onClick(View v) {
2804 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002805 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002807 }
2808 }
2809
2810 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002811 * Bind the items start-end from the list.
2812 *
2813 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002814 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002815 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2816
2817 final Workspace workspace = mWorkspace;
2818
2819 for (int i=start; i<end; i++) {
2820 final ItemInfo item = shortcuts.get(i);
2821 mDesktopItems.add(item);
2822 switch (item.itemType) {
2823 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2824 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002825 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002826 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2827 false);
2828 break;
2829 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2830 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002831 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 (UserFolderInfo) item);
2833 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2834 false);
2835 break;
2836 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2837 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2838 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002839 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 (LiveFolderInfo) item);
2841 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2842 false);
2843 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846
Joe Onorato9c1289c2009-08-17 11:03:03 -04002847 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 }
2849
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850 /**
2851 * Implementation of the method from LauncherModel.Callbacks.
2852 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002853 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002854 sFolders.clear();
2855 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002856 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857
2858 /**
2859 * Add the views for a widget to the workspace.
2860 *
2861 * Implementation of the method from LauncherModel.Callbacks.
2862 */
2863 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002864 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2865 if (DEBUG_WIDGETS) {
2866 Log.d(TAG, "bindAppWidget: " + item);
2867 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 final Workspace workspace = mWorkspace;
2869
2870 final int appWidgetId = item.appWidgetId;
2871 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002872 if (DEBUG_WIDGETS) {
2873 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2874 }
2875
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2877
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2879 item.hostView.setTag(item);
2880
2881 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2882 item.cellY, item.spanX, item.spanY, false);
2883
2884 workspace.requestLayout();
2885
2886 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002887
2888 if (DEBUG_WIDGETS) {
2889 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2890 + (SystemClock.uptimeMillis()-start) + "ms");
2891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 }
2893
2894 /**
2895 * Callback saying that there aren't any more items to bind.
2896 *
2897 * Implementation of the method from LauncherModel.Callbacks.
2898 */
2899 public void finishBindingItems() {
2900 if (mSavedState != null) {
2901 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002902 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903 }
2904
2905 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2906 if (userFolders != null) {
2907 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002908 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 if (info != null) {
2910 openFolder(info);
2911 }
2912 }
2913 final Folder openFolder = mWorkspace.getOpenFolder();
2914 if (openFolder != null) {
2915 openFolder.requestFocus();
2916 }
2917 }
2918
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 mSavedState = null;
2920 }
2921
2922 if (mSavedInstanceState != null) {
2923 super.onRestoreInstanceState(mSavedInstanceState);
2924 mSavedInstanceState = null;
2925 }
2926
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 mWorkspaceLoading = false;
2928 }
2929
2930 /**
2931 * Add the icons for all apps.
2932 *
2933 * Implementation of the method from LauncherModel.Callbacks.
2934 */
2935 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002936 mAllAppsGrid.setApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002937 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 }
2939
2940 /**
2941 * A package was installed.
2942 *
2943 * Implementation of the method from LauncherModel.Callbacks.
2944 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002945 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002947 mAllAppsGrid.addApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002948 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 }
2950
2951 /**
2952 * A package was updated.
2953 *
2954 * Implementation of the method from LauncherModel.Callbacks.
2955 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002956 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002958 mWorkspace.updateShortcuts(apps);
2959 mAllAppsGrid.updateApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002960 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 }
2962
2963 /**
2964 * A package was uninstalled.
2965 *
2966 * Implementation of the method from LauncherModel.Callbacks.
2967 */
Joe Onorato36115782010-06-17 13:28:48 -04002968 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002970 if (permanent) {
2971 mWorkspace.removeItems(apps);
2972 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002973 mAllAppsGrid.removeApps(apps);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002974 updateAppMarketIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975 }
Joe Onoratobe386092009-11-17 17:32:16 -08002976
2977 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07002978 * A number of packages were updated.
2979 */
2980 public void bindPackagesUpdated() {
2981 // update the customization drawer contents
Winson Chungb3347bb2010-08-19 14:51:28 -07002982 if (mCustomizePagedView != null)
2983 mCustomizePagedView.update();
Winson Chung80baf5a2010-08-09 16:03:15 -07002984 }
2985
2986 /**
Joe Onoratobe386092009-11-17 17:32:16 -08002987 * Prints out out state for debugging.
2988 */
2989 public void dumpState() {
2990 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002991 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002992 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2993 Log.d(TAG, "mRestoring=" + mRestoring);
2994 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2995 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2996 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002997 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002998 mModel.dumpState();
2999 mAllAppsGrid.dumpState();
3000 Log.d(TAG, "END launcher2 dump state");
3001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003002}