blob: d7c2f5bb533c574b9911fce8c2a0eebb2c3b50c3 [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
Chet Haaseb1254a62010-09-07 13:35:00 -070020import android.animation.Animator;
21import android.animation.AnimatorListenerAdapter;
22import android.animation.ValueAnimator;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023import com.android.common.Search;
24import com.android.launcher.R;
25
Chet Haaseb1254a62010-09-07 13:35:00 -070026import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070027import android.animation.PropertyValuesHolder;
Chet Haaseb1254a62010-09-07 13:35:00 -070028import android.animation.AnimatorSet;
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;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040066import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080067import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070068import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070069import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
72import android.text.TextUtils;
73import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080076import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
80import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070081import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.View;
Romain Guy5bbc91b2010-08-18 11:38:46 -070083import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070085import android.view.WindowManager;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070086import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070087import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070090import android.widget.ImageView;
91import android.widget.LinearLayout;
92import android.widget.PopupWindow;
Winson Chung80baf5a2010-08-09 16:03:15 -070093import android.widget.RelativeLayout;
Michael Jurka0e260592010-06-30 17:07:39 -070094import android.widget.TabHost;
Romain Guy5bbc91b2010-08-18 11:38:46 -070095import android.widget.TabHost.OnTabChangeListener;
96import android.widget.TabHost.TabContentFactory;
Winson Chung80baf5a2010-08-09 16:03:15 -070097import android.widget.TabWidget;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.widget.TextView;
99import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700100
101import java.io.DataInputStream;
102import java.io.DataOutputStream;
103import java.io.FileNotFoundException;
104import java.io.IOException;
105import java.util.ArrayList;
106import java.util.HashMap;
107import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700121
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800123 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
124
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700126 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
127 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128 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";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 // Type: boolean
164 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
165 // Type: long
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
167
Winson Chung80baf5a2010-08-09 16:03:15 -0700168 // tags for the customization tabs
169 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700170 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700171 private static final String FOLDERS_TAG = "folders";
172 private static final String SHORTCUTS_TAG = "shortcuts";
173 private static final String WALLPAPERS_TAG = "wallpapers";
174
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700175 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
176
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177 /** The different states that Launcher can be in. */
178 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700179 private State mState = State.WORKSPACE;
180 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800187 private final BroadcastReceiver mCloseSystemDialogsReceiver
188 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800189 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private LayoutInflater mInflater;
192
Joe Onorato00acb122009-08-04 16:04:30 -0400193 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 private int mAddScreen = -1;
200 private int mAddIntersectCellX = -1;
201 private int mAddIntersectCellY = -1;
202 private int[] mAddDropPosition;
203 private int[] mTmpAddItemCellCoordinates = new int[2];
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private FolderInfo mFolderInfo;
206
Joe Onorato7c312c12009-08-13 21:36:53 -0700207 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700209 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700210 private TabHost mHomeCustomizationDrawer;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700211
212 private PagedView mAllAppsPagedView = null;
213 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private Bundle mSavedState;
216
217 private SpannableStringBuilder mDefaultKeySsb = null;
218
Joe Onorato9c1289c2009-08-17 11:03:03 -0400219 private boolean mWorkspaceLoading = true;
220
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800221 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private boolean mRestoring;
223 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224
225 private Bundle mSavedInstanceState;
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800228 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static LocaleConfiguration sLocaleConfiguration = null;
231
Romain Guy84f296c2009-11-04 15:00:44 -0800232 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700233 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700234
Romain Guy1fbc1c82009-11-09 20:43:08 -0800235 private ImageView mPreviousView;
236 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500237
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400238 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400239 private String[] mHotseatConfig = null;
240 private Intent[] mHotseats = null;
241 private Drawable[] mHotseatIcons = null;
242 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400243
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700244 private Intent mAppMarketIntent = null;
245
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246 @Override
247 protected void onCreate(Bundle savedInstanceState) {
248 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700249
Winson Chungaafa03c2010-06-11 17:34:16 -0700250 if (LauncherApplication.isInPlaceRotationEnabled()) {
251 // hide the status bar (temporary until we get the status bar design figured out)
252 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
253 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
254 }
255
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 LauncherApplication app = ((LauncherApplication)getApplication());
257 mModel = app.setLauncher(this);
258 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400259 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700263 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
264 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200267 android.os.Debug.startMethodTracing(
268 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
270
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400271 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 checkForLocaleChange();
273 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700275 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
276 if (mHomeCustomizationDrawer != null) {
277 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700278
Winson Chung80baf5a2010-08-09 16:03:15 -0700279 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700280 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
281 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700282 TabContentFactory contentFactory = new TabContentFactory() {
283 public View createTabContent(String tag) {
284 return mCustomizePagedView;
285 }
286 };
287
Michael Jurka946ad472010-07-09 18:05:18 -0700288 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700289 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
290 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700291 String applicationsLabel = getString(R.string.applications_tab_label);
292 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
293 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700294 String foldersLabel = getString(R.string.folders_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(FOLDERS_TAG)
296 .setIndicator(foldersLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700297 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700298 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
299 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700300 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
301 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
302 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700303
304 // TEMP: just styling the tab widget to be a bit nicer until we get the actual
305 // new assets
306 TabWidget tabWidget = mHomeCustomizationDrawer.getTabWidget();
307 for (int i = 0; i < tabWidget.getChildCount(); ++i) {
308 RelativeLayout tab = (RelativeLayout) tabWidget.getChildTabViewAt(i);
309 TextView text = (TextView) tab.getChildAt(1);
310 text.setTextSize(20.0f);
311 text.setPadding(20, 0, 20, 0);
312 text.setShadowLayer(1.0f, 0.0f, 1.0f, Color.BLACK);
313 tab.setBackgroundDrawable(null);
314 }
315
316 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
317 public void onTabChanged(String tabId) {
318 // animate the changing of the tab content by fading pages in and out
319 final int duration = 150;
320 final float alpha = mCustomizePagedView.getAlpha();
Chet Haaseb1254a62010-09-07 13:35:00 -0700321 ValueAnimator alphaAnim = new ObjectAnimator(duration, mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 "alpha", alpha, 0.0f);
Chet Haaseb1254a62010-09-07 13:35:00 -0700323 alphaAnim.addListener(new AnimatorListenerAdapter() {
324 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700325 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
326 if (tag == WIDGETS_TAG) {
327 mCustomizePagedView.setCustomizationFilter(
328 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700329 } else if (tag == APPLICATIONS_TAG) {
330 mCustomizePagedView.setCustomizationFilter(
331 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700332 } else if (tag == FOLDERS_TAG) {
333 mCustomizePagedView.setCustomizationFilter(
334 CustomizePagedView.CustomizationType.FolderCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 } else if (tag == WALLPAPERS_TAG) {
336 mCustomizePagedView.setCustomizationFilter(
337 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700338 } else if (tag == SHORTCUTS_TAG) {
339 mCustomizePagedView.setCustomizationFilter(
340 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 }
342
343 final float alpha = mCustomizePagedView.getAlpha();
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700344 ValueAnimator alphaAnim = new ObjectAnimator(duration,
345 mCustomizePagedView, "alpha", alpha, 1.0f);
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 alphaAnim.start();
347 }
348 });
349 alphaAnim.start();
350 }
351 });
Michael Jurka946ad472010-07-09 18:05:18 -0700352
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700353 // TEMP: Working around a bug in tab host where the current tab does not initially have
354 // a highlight on it by selecting something else, then selecting the actual tab we want..
355 mHomeCustomizationDrawer.setCurrentTab(1);
Michael Jurka946ad472010-07-09 18:05:18 -0700356 mHomeCustomizationDrawer.setCurrentTab(0);
357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 setupViews();
359
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800360 registerContentObservers();
361
Joe Onorato7c312c12009-08-13 21:36:53 -0700362 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700363
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 mSavedState = savedInstanceState;
365 restoreState(mSavedState);
366
367 if (PROFILE_STARTUP) {
368 android.os.Debug.stopMethodTracing();
369 }
370
371 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400372 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
374
375 // For handling default keys
376 mDefaultKeySsb = new SpannableStringBuilder();
377 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800378
379 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
380 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 }
Romain Guycbb89e42009-06-08 15:52:54 -0700382
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 if (sLocaleConfiguration == null) {
385 new AsyncTask<Void, Void, LocaleConfiguration>() {
386 @Override
387 protected LocaleConfiguration doInBackground(Void... unused) {
388 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
389 readConfiguration(Launcher.this, localeConfiguration);
390 return localeConfiguration;
391 }
392
393 @Override
394 protected void onPostExecute(LocaleConfiguration result) {
395 sLocaleConfiguration = result;
396 checkForLocaleChange(); // recursive, but now with a locale configuration
397 }
398 }.execute();
399 return;
400 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700401
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402 final Configuration configuration = getResources().getConfiguration();
403
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700404 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 final String locale = configuration.locale.toString();
406
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700407 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 final int mcc = configuration.mcc;
409
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700410 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 final int mnc = configuration.mnc;
412
Romain Guy84f296c2009-11-04 15:00:44 -0800413 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800414
Romain Guy84f296c2009-11-04 15:00:44 -0800415 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700416 sLocaleConfiguration.locale = locale;
417 sLocaleConfiguration.mcc = mcc;
418 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700419
Joe Onorato0589f0f2010-02-08 13:44:00 -0800420 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400421 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422
423 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
424 new Thread("WriteLocaleConfiguration") {
425 public void run() {
426 writeConfiguration(Launcher.this, localeConfiguration);
427 }
428 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700429 }
430 }
431
432 private static class LocaleConfiguration {
433 public String locale;
434 public int mcc = -1;
435 public int mnc = -1;
436 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700437
Romain Guy98d01652009-06-30 16:21:04 -0700438 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
439 DataInputStream in = null;
440 try {
441 in = new DataInputStream(context.openFileInput(PREFERENCES));
442 configuration.locale = in.readUTF();
443 configuration.mcc = in.readInt();
444 configuration.mnc = in.readInt();
445 } catch (FileNotFoundException e) {
446 // Ignore
447 } catch (IOException e) {
448 // Ignore
449 } finally {
450 if (in != null) {
451 try {
452 in.close();
453 } catch (IOException e) {
454 // Ignore
455 }
456 }
457 }
458 }
459
460 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
461 DataOutputStream out = null;
462 try {
463 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
464 out.writeUTF(configuration.locale);
465 out.writeInt(configuration.mcc);
466 out.writeInt(configuration.mnc);
467 out.flush();
468 } catch (FileNotFoundException e) {
469 // Ignore
470 } catch (IOException e) {
471 //noinspection ResultOfMethodCallIgnored
472 context.getFileStreamPath(PREFERENCES).delete();
473 } finally {
474 if (out != null) {
475 try {
476 out.close();
477 } catch (IOException e) {
478 // Ignore
479 }
480 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 }
482 }
483
484 static int getScreen() {
485 synchronized (sLock) {
486 return sScreen;
487 }
488 }
489
490 static void setScreen(int screen) {
491 synchronized (sLock) {
492 sScreen = screen;
493 }
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700497 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498
499 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700500 // TODO: Put back when we decide about scrolling the wallpaper
501 // boolean isPortrait = display.getWidth() < display.getHeight();
502 // final int width = isPortrait ? display.getWidth() : display.getHeight();
503 // final int height = isPortrait ? display.getHeight() : display.getWidth();
504 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
505 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 }
507
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 // Note: This doesn't do all the client-id magic that BrowserProvider does
509 // in Browser. (http://b/2425179)
510 private Uri getDefaultBrowserUri() {
511 String url = getString(R.string.default_browser_url);
512 if (url.indexOf("{CID}") != -1) {
513 url = url.replace("{CID}", "android-google");
514 }
515 return Uri.parse(url);
516 }
517
518 // Load the Intent templates from arrays.xml to populate the hotseats. For
519 // each Intent, if it resolves to a single app, use that as the launch
520 // intent & use that app's label as the contentDescription. Otherwise,
521 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400523 if (mHotseatConfig == null) {
524 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
525 if (mHotseatConfig.length > 0) {
526 mHotseats = new Intent[mHotseatConfig.length];
527 mHotseatLabels = new CharSequence[mHotseatConfig.length];
528 mHotseatIcons = new Drawable[mHotseatConfig.length];
529 } else {
530 mHotseats = null;
531 mHotseatIcons = null;
532 mHotseatLabels = null;
533 }
534
535 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
536 for (int i=0; i<mHotseatConfig.length; i++) {
537 // load icon for this slot; currently unrelated to the actual activity
538 try {
539 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
540 } catch (ArrayIndexOutOfBoundsException ex) {
541 Log.w(TAG, "Missing hotseat_icons array item #" + i);
542 mHotseatIcons[i] = null;
543 }
544 }
545 hotseatIconDrawables.recycle();
546 }
547
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400548 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400549 for (int i=0; i<mHotseatConfig.length; i++) {
550 Intent intent = null;
551 if (mHotseatConfig[i].equals("*BROWSER*")) {
552 // magic value meaning "launch user's default web browser"
553 // replace it with a generic web request so we can see if there is indeed a default
554 String defaultUri = getString(R.string.default_browser_url);
555 intent = new Intent(
556 Intent.ACTION_VIEW,
557 ((defaultUri != null)
558 ? Uri.parse(defaultUri)
559 : getDefaultBrowserUri())
560 ).addCategory(Intent.CATEGORY_BROWSABLE);
561 // note: if the user launches this without a default set, she
562 // will always be taken to the default URL above; this is
563 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700564 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400565 // URL is unavoidably sent to the chosen app.
566 } else {
567 try {
568 intent = Intent.parseUri(mHotseatConfig[i], 0);
569 } catch (java.net.URISyntaxException ex) {
570 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
571 // bogus; leave intent=null
572 }
573 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700574
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400575 if (intent == null) {
576 mHotseats[i] = null;
577 mHotseatLabels[i] = getText(R.string.activity_not_found);
578 continue;
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580
581 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700582 Log.d(TAG, "loadHotseats: hotseat " + i
583 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400585 + "]");
586 }
587
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
589 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700590 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400591 Log.d(TAG, "Best match for intent: " + bestMatch);
592 Log.d(TAG, "All matches: ");
593 for (ResolveInfo ri : allMatches) {
594 Log.d(TAG, " --> " + ri);
595 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400596 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400597 // did this resolve to a single app, or the resolver?
598 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700599 // can't find any activity to handle this. let's leave the
600 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 // to launch.
602 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400603
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400604 // set accessibility text to "Not installed"
605 mHotseatLabels[i] = getText(R.string.activity_not_found);
606 } else {
607 boolean found = false;
608 for (ResolveInfo ri : allMatches) {
609 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
610 && bestMatch.activityInfo.applicationInfo.packageName
611 .equals(ri.activityInfo.applicationInfo.packageName)) {
612 found = true;
613 break;
614 }
615 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700616
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400617 if (!found) {
618 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
619 // the bestMatch is probably the ResolveActivity, meaning the
620 // user has not yet selected a default
621 // so: we'll keep the original intent for now
622 mHotseats[i] = intent;
623
624 // set the accessibility text to "Select shortcut"
625 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
626 } else {
627 // we have an app!
628 // now reconstruct the intent to launch it through the front
629 // door
630 ComponentName com = new ComponentName(
631 bestMatch.activityInfo.applicationInfo.packageName,
632 bestMatch.activityInfo.name);
633 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
634
635 // load the app label for accessibility
636 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
637 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400638 }
639
640 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700641 Log.d(TAG, "loadHotseats: hotseat " + i
642 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400643 + ((mHotseats[i] == null)
644 ? "null"
645 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400646 + "] label=[" + mHotseatLabels[i]
647 + "]"
648 );
649 }
650 }
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 @Override
654 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700655 mWaitingForResult = false;
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // The pattern used here is that a user PICKs a specific application,
658 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
661 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700662
Michael Jurka0280c3b2010-09-17 15:00:07 -0700663 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 switch (requestCode) {
665 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700666 completeAddApplication(
667 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800670 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
672 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700673 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 break;
675 case REQUEST_PICK_LIVE_FOLDER:
676 addLiveFolder(data);
677 break;
678 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
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_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700684 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700685 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700688 case REQUEST_PICK_WALLPAPER:
689 // We just wanted the activity result here so we can clear mWaitingForResult
690 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Romain Guy18042c82009-11-06 11:44:55 -0800692 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
693 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
694 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 // Clean up the appWidgetId if we canceled
696 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
697 if (appWidgetId != -1) {
698 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702
703 @Override
704 protected void onResume() {
705 super.onResume();
706
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800707 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400710 mWorkspaceLoading = true;
711 mModel.startLoader(this, true);
712 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700714 // When we resume Launcher, a different Activity might be responsible for the app
715 // market intent, so refresh the icon
716 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700720 protected void onPause() {
721 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 // Some launcher layouts don't have a previous and next view
723 if (mPreviousView != null) {
724 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700725 }
726 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700727 dismissPreview(mNextView);
728 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800729 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700730 }
Romain Guycbb89e42009-06-08 15:52:54 -0700731
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700732 @Override
733 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 // Flag the loader to stop early before switching
735 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800736 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800737 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700738 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700739
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800740 // We can't hide the IME if it was forced open. So don't bother
741 /*
742 @Override
743 public void onWindowFocusChanged(boolean hasFocus) {
744 super.onWindowFocusChanged(hasFocus);
745
746 if (hasFocus) {
747 final InputMethodManager inputManager = (InputMethodManager)
748 getSystemService(Context.INPUT_METHOD_SERVICE);
749 WindowManager.LayoutParams lp = getWindow().getAttributes();
750 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
751 android.os.Handler()) {
752 protected void onReceiveResult(int resultCode, Bundle resultData) {
753 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
754 }
755 });
756 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
757 }
758 }
759 */
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 private boolean acceptFilter() {
762 final InputMethodManager inputManager = (InputMethodManager)
763 getSystemService(Context.INPUT_METHOD_SERVICE);
764 return !inputManager.isFullscreenMode();
765 }
766
767 @Override
768 public boolean onKeyDown(int keyCode, KeyEvent event) {
769 boolean handled = super.onKeyDown(keyCode, event);
770 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
771 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
772 keyCode, event);
773 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700774 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700775 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700776 // showSearchDialog()
777 // If there are multiple keystrokes before the search dialog takes focus,
778 // onSearchRequested() will be called for every keystroke,
779 // but it is idempotent, so it's fine.
780 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 }
782 }
783
Joe Onorato8a9625e2010-01-28 15:55:35 -0800784 // Eat the long press event so the keyboard doesn't come up.
785 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
786 return true;
787 }
788
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 return handled;
790 }
791
Karl Rosaen138a0412009-04-23 19:00:21 -0700792 private String getTypedText() {
793 return mDefaultKeySsb.toString();
794 }
795
796 private void clearTypedText() {
797 mDefaultKeySsb.clear();
798 mDefaultKeySsb.clearSpans();
799 Selection.setSelection(mDefaultKeySsb, 0);
800 }
801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 /**
803 * Restores the previous state, if it exists.
804 *
805 * @param savedState The previous state.
806 */
807 private void restoreState(Bundle savedState) {
808 if (savedState == null) {
809 return;
810 }
811
Joe Onorato3a8820b2009-11-10 15:06:42 -0800812 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
813 if (allApps) {
814 showAllApps(false);
815 }
816
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
818 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700819 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 }
821
822 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700825 mAddScreen = addScreen;
826 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
827 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 mRestoring = true;
829 }
830
831 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
832 if (renameFolder) {
833 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700834 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 mRestoring = true;
836 }
837 }
838
839 /**
840 * Finds all the views we need and configure them properly.
841 */
842 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700843 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400844
Romain Guyb1b69f52009-08-10 15:10:15 -0700845 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400846 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
Joe Onorato7c312c12009-08-13 21:36:53 -0700848 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700849 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700850 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800851 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700852 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700853 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700854
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700855 if (LauncherApplication.isScreenXLarge()) {
856 // They need to be INVISIBLE initially so that they will be measured in the layout.
857 // Otherwise the animations are messed up when we show them for the first time.
858 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
859 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
860 }
861
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
863 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500864 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700865
Joe Onorato7c312c12009-08-13 21:36:53 -0700866 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
867 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700869 View handleView = findViewById(R.id.all_apps_button);
870 if (handleView != null && handleView instanceof HandleView) {
871 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700872 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700873 mHandleView.setLauncher(this);
874 mHandleView.setOnClickListener(this);
875 mHandleView.setOnLongClickListener(this);
876 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800877
Winson Chung80baf5a2010-08-09 16:03:15 -0700878 if (mCustomizePagedView != null) {
879 mCustomizePagedView.setLauncher(this);
880 mCustomizePagedView.setDragController(dragController);
881 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700882 } else {
883 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
884 hotseatLeft.setContentDescription(mHotseatLabels[0]);
885 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
886 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
887 hotseatRight.setContentDescription(mHotseatLabels[1]);
888 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400889
Michael Jurkaaf442092010-06-10 17:01:57 -0700890 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
891 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800892
Michael Jurkaaf442092010-06-10 17:01:57 -0700893 Drawable previous = mPreviousView.getDrawable();
894 Drawable next = mNextView.getDrawable();
895 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896
Michael Jurkaaf442092010-06-10 17:01:57 -0700897 mPreviousView.setHapticFeedbackEnabled(false);
898 mPreviousView.setOnLongClickListener(this);
899 mNextView.setHapticFeedbackEnabled(false);
900 mNextView.setOnLongClickListener(this);
901 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400904 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906
907 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400908 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700909 int deleteZoneHandleId;
910 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700911 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700912 } else {
913 deleteZoneHandleId = R.id.all_apps_button_cluster;
914 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700915 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700916 dragController.addDragListener(deleteZone);
917
918 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
919 if (infoButton != null) {
920 infoButton.setLauncher(this);
921 infoButton.setHandle(findViewById(R.id.configure_button));
922 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
923 dragController.addDragListener(infoButton);
924 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925
Joe Onorato00acb122009-08-04 16:04:30 -0400926 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400927 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800928 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700929
Joe Onorato00acb122009-08-04 16:04:30 -0400930 // The order here is bottom to top.
931 dragController.addDropTarget(workspace);
932 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700933 if (infoButton != null) {
934 dragController.addDropTarget(infoButton);
935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 }
937
Romain Guy8a73c512009-11-09 19:19:59 -0800938 @SuppressWarnings({"UnusedDeclaration"})
939 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700940 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800941 mWorkspace.scrollLeft();
942 }
Romain Guy8a73c512009-11-09 19:19:59 -0800943 }
944
945 @SuppressWarnings({"UnusedDeclaration"})
946 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700947 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800948 mWorkspace.scrollRight();
949 }
Romain Guy8a73c512009-11-09 19:19:59 -0800950 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400951
952 @SuppressWarnings({"UnusedDeclaration"})
953 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700954 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400955
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400956 int index = -1;
957 if (v.getId() == R.id.hotseat_left) {
958 index = 0;
959 } else if (v.getId() == R.id.hotseat_right) {
960 index = 1;
961 }
962
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400963 // reload these every tap; you never know when they might change
964 loadHotseats();
965 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
966 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400967 startActivitySafely(
968 mHotseats[index],
969 "hotseat"
970 );
971 }
972 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700973
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 /**
975 * Creates a view representing a shortcut.
976 *
977 * @param info The data structure describing the shortcut.
978 *
979 * @return A View inflated from R.layout.application.
980 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800981 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700983 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 }
985
986 /**
987 * Creates a view representing a shortcut inflated from the specified resource.
988 *
989 * @param layoutResId The id of the XML layout used to create the shortcut.
990 * @param parent The group the shortcut belongs to.
991 * @param info The data structure describing the shortcut.
992 *
993 * @return A View inflated from layoutResId.
994 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
997
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
999 new FastBitmapDrawable(info.getIcon(mIconCache)),
1000 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 favorite.setText(info.title);
1002 favorite.setTag(info);
1003 favorite.setOnClickListener(this);
1004
1005 return favorite;
1006 }
1007
1008 /**
1009 * Add an application shortcut to the workspace.
1010 *
1011 * @param data The intent describing the application.
1012 * @param cellInfo The position on screen where to create the shortcut.
1013 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001014 void completeAddApplication(Context context, Intent data, int screen,
1015 int intersectCellX, int intersectCellY) {
1016 final int[] cellXY = mTmpAddItemCellCoordinates;
1017 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1018
1019 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1020 showOutOfSpaceMessage();
1021 return;
1022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023
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;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001031 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1032 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001033 } else {
1034 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
1036 }
Romain Guycbb89e42009-06-08 15:52:54 -07001037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001038 /**
1039 * Add a shortcut to the workspace.
1040 *
1041 * @param data The intent describing the shortcut.
1042 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001044 private void completeAddShortcut(Intent data, int screen,
1045 int intersectCellX, int intersectCellY) {
1046 final int[] cellXY = mTmpAddItemCellCoordinates;
1047 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001048
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1050 showOutOfSpaceMessage();
1051 return;
1052 }
1053
1054 final ShortcutInfo info = mModel.addShortcut(
1055 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
1057 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001059 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061 }
1062
Romain Guycbb89e42009-06-08 15:52:54 -07001063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001067 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001071 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001072
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001074 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1075 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001078 // We have saved the position to which the widget was dragged-- this really only matters
1079 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001081
1082 // For now, we don't save the coordinate where we dropped the icon because we're not
1083 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1084 // a widget on the home screen in case we want to add it in the future
Michael Jurka0280c3b2010-09-17 15:00:07 -07001085 final int[] touchXY = null;
1086 //final int[] touchXY = mAddDropPosition;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001087 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 boolean foundCellSpan = false;
1089 if (touchXY != null) {
1090 // when dragging and dropping, just find the closest free spot
1091 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1092 int[] result = screenLayout.findNearestVacantArea(
1093 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001094 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 foundCellSpan = !findNearestVacantAreaFailed;
1096 } else {
1097 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1098 // if we long pressed on an empty cell to bring up a menu,
1099 // make sure we intersect the empty cell
1100 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1101 mAddIntersectCellX, mAddIntersectCellY);
1102 } else {
1103 // if we went through the menu -> add, just find any spot
1104 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1105 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001106 }
1107
Michael Jurka0280c3b2010-09-17 15:00:07 -07001108 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001109 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001111 return;
1112 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001114 // Build Launcher-specific widget info and save to database
1115 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 launcherInfo.spanX = spanXY[0];
1117 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001118
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 LauncherModel.addItemToDatabase(this, launcherInfo,
1120 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122
1123 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001124 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001125
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001126 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001127 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
Michael Jurka0280c3b2010-09-17 15:00:07 -07001132 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001133 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
1135 }
Romain Guycbb89e42009-06-08 15:52:54 -07001136
Michael Jurka0280c3b2010-09-17 15:00:07 -07001137 void showOutOfSpaceMessage() {
1138 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1139 }
1140
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1142 mDesktopItems.remove(launcherInfo);
1143 launcherInfo.hostView = null;
1144 }
1145
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 public LauncherAppWidgetHost getAppWidgetHost() {
1147 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 }
Romain Guycbb89e42009-06-08 15:52:54 -07001149
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001150 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001151 getWindow().closeAllPanels();
1152
1153 try {
1154 dismissDialog(DIALOG_CREATE_SHORTCUT);
1155 // Unlock the workspace if the dialog was showing
1156 } catch (Exception e) {
1157 // An exception is thrown if the dialog is not visible, which is fine
1158 }
1159
1160 try {
1161 dismissDialog(DIALOG_RENAME_FOLDER);
1162 // Unlock the workspace if the dialog was showing
1163 } catch (Exception e) {
1164 // An exception is thrown if the dialog is not visible, which is fine
1165 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001166
1167 // Whatever we were doing is hereby canceled.
1168 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001169 }
1170
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 @Override
1172 protected void onNewIntent(Intent intent) {
1173 super.onNewIntent(intent);
1174
1175 // Close the menu
1176 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001177 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001178 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001179
Joe Onorato14f122b2009-11-19 14:06:36 -08001180 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1181 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001182
Michael Jurka4cb37242010-08-09 21:05:32 -07001183 // in all these cases, only animate if we're already on home
1184 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001185 mWorkspace.unshrink(alreadyOnHome);
1186 }
1187 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001188 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001189 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1190 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001191 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001192 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001193
Joe Onorato3a8820b2009-11-10 15:06:42 -08001194 final View v = getWindow().peekDecorView();
1195 if (v != null && v.getWindowToken() != null) {
1196 InputMethodManager imm = (InputMethodManager)getSystemService(
1197 INPUT_METHOD_SERVICE);
1198 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200 }
1201 }
1202
1203 @Override
1204 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1205 // Do not call super here
1206 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001207
1208 if (mHomeCustomizationDrawer != null) {
1209 String cur = savedInstanceState.getString("currentTab");
1210 if (cur != null) {
1211 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1212 }
1213 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001214 }
1215
1216 @Override
1217 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001218 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219
1220 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1221 if (folders.size() > 0) {
1222 final int count = folders.size();
1223 long[] ids = new long[count];
1224 for (int i = 0; i < count; i++) {
1225 final FolderInfo info = folders.get(i).getInfo();
1226 ids[i] = info.id;
1227 }
1228 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1229 } else {
1230 super.onSaveInstanceState(outState);
1231 }
1232
Joe Onoratofb0ca672009-09-14 17:55:46 -04001233 // TODO should not do this if the drawer is currently closing.
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001234 if (mState == State.ALL_APPS) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001236 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
Michael Jurka0280c3b2010-09-17 15:00:07 -07001238 if (mAddScreen > -1 && mWaitingForResult) {
1239 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1240 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1241 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
1243
1244 if (mFolderInfo != null && mWaitingForResult) {
1245 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1246 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1247 }
Michael Jurka946ad472010-07-09 18:05:18 -07001248
1249 if (mHomeCustomizationDrawer != null) {
1250 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1251 if (currentTabTag != null) {
1252 outState.putString("currentTab", currentTabTag);
1253 }
1254 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
1256
1257 @Override
1258 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001264 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 }
1266
1267 TextKeyListener.getInstance().release();
1268
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001272
1273 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001274
Patrick Dubroyab962b72010-07-26 12:10:10 -07001275 // Some launcher layouts don't have a previous and next view
1276 if (mPreviousView != null) {
1277 dismissPreview(mPreviousView);
1278 }
1279 if (mNextView != null) {
1280 dismissPreview(mNextView);
1281 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001282
1283 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001284 }
1285
1286 @Override
1287 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001288 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 super.startActivityForResult(intent, requestCode);
1290 }
1291
1292 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001293 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001295
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001296 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001297
Karl Rosaen138a0412009-04-23 19:00:21 -07001298 if (initialQuery == null) {
1299 // Use any text typed in the launcher as the initial query
1300 initialQuery = getTypedText();
1301 clearTypedText();
1302 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 if (appSearchData == null) {
1304 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001305 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
Romain Guycbb89e42009-06-08 15:52:54 -07001307
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 final SearchManager searchManager =
1309 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001310 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001311 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313
1314 @Override
1315 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001316 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001317 return false;
1318 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319
1320 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001321
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1323 .setIcon(android.R.drawable.ic_menu_add)
1324 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001325 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1326 .setIcon(android.R.drawable.ic_menu_manage)
1327 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001328 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 .setIcon(android.R.drawable.ic_menu_gallery)
1330 .setAlphabeticShortcut('W');
1331 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1332 .setIcon(android.R.drawable.ic_search_category_default)
1333 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1334 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1335 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1336 .setAlphabeticShortcut('N');
1337
1338 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001339 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1340 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
1342 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1343 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1344 .setIntent(settings);
1345
1346 return true;
1347 }
1348
1349 @Override
1350 public boolean onPrepareOptionsMenu(Menu menu) {
1351 super.onPrepareOptionsMenu(menu);
1352
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001353 // If all apps is animating, don't show the menu, because we don't know
1354 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001355 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001356 return false;
1357 }
1358
1359 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001360 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001361 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1362 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1363
1364 // Disable add if the workspace is full.
1365 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001366 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1367 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
1370 return true;
1371 }
1372
1373 @Override
1374 public boolean onOptionsItemSelected(MenuItem item) {
1375 switch (item.getItemId()) {
1376 case MENU_ADD:
1377 addItems();
1378 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001379 case MENU_MANAGE_APPS:
1380 manageApps();
1381 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 case MENU_WALLPAPER_SETTINGS:
1383 startWallpaper();
1384 return true;
1385 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001386 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 return true;
1388 case MENU_NOTIFICATIONS:
1389 showNotifications();
1390 return true;
1391 }
1392
1393 return super.onOptionsItemSelected(item);
1394 }
Romain Guycbb89e42009-06-08 15:52:54 -07001395
Karl Rosaen138a0412009-04-23 19:00:21 -07001396 /**
1397 * Indicates that we want global search for this activity by setting the globalSearch
1398 * argument for {@link #startSearch} to true.
1399 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001401 @Override
1402 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001403 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001404 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001405 }
1406
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 public boolean isWorkspaceLocked() {
1408 return mWorkspaceLoading || mWaitingForResult;
1409 }
1410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001412 if (LauncherApplication.isScreenXLarge()) {
1413 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001414 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001415 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001416 }
1417 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001418 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001419 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
Michael Jurka0280c3b2010-09-17 15:00:07 -07001423 private void resetAddInfo() {
1424 mAddScreen = -1;
1425 mAddIntersectCellX = -1;
1426 mAddIntersectCellY = -1;
1427 mAddDropPosition = null;
1428 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001429
Michael Jurka0280c3b2010-09-17 15:00:07 -07001430 void addAppWidgetFromDrop(ComponentName appWidgetProvider, int screen, int[] position) {
1431 resetAddInfo();
1432 mAddScreen = screen;
1433
1434 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1435 mAddDropPosition = position;
1436
Michael Jurkaaf442092010-06-10 17:01:57 -07001437 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1438 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1439 addAppWidgetImpl(appWidgetId);
1440 }
1441
Winson Chung7ad01412010-09-27 11:33:03 -07001442 private void manageApps() {
1443 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1444 }
1445
Michael Jurkaaf442092010-06-10 17:01:57 -07001446 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 // TODO: catch bad widget exception when sent
1448 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001449 // TODO: Is this log message meaningful?
1450 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1451 addAppWidgetImpl(appWidgetId);
1452 }
1453
1454 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001455 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456
Bjorn Bringert7984c942009-12-09 15:38:25 +00001457 if (appWidget.configure != null) {
1458 // Launch over to configure widget, if needed
1459 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1460 intent.setComponent(appWidget.configure);
1461 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1462
Romain Guy8e633c52010-03-04 12:51:36 -08001463 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001465 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001466 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468 }
Romain Guycbb89e42009-06-08 15:52:54 -07001469
Michael Jurka0280c3b2010-09-17 15:00:07 -07001470 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1471 resetAddInfo();
1472 mAddScreen = screen;
1473 mAddDropPosition = position;
1474
1475 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1476 createShortcutIntent.setComponent(componentName);
1477 processShortcut(createShortcutIntent);
1478 }
1479
Joe Onoratodeb98af2010-02-19 14:59:39 -08001480 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001481 // Handle case where user selected "Applications"
1482 String applicationName = getResources().getString(R.string.group_applications);
1483 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001484
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001485 if (applicationName != null && applicationName.equals(shortcutName)) {
1486 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1487 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001488
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001489 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1490 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001491 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001492 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001493 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001494 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 }
1497
Winson Chung24ab2f12010-09-16 14:10:47 -07001498 void processWallpaper(Intent intent) {
1499 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1500 }
1501
Michael Jurka0280c3b2010-09-17 15:00:07 -07001502 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1503 resetAddInfo();
1504 mAddScreen = screen;
1505 mAddDropPosition = position;
1506
1507 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1508 createFolderIntent.setComponent(componentName);
1509
1510 addLiveFolder(createFolderIntent);
1511 }
1512
1513 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001514 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001515 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001516 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001517
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001518 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001520 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001521 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 }
1524
Michael Jurka0280c3b2010-09-17 15:00:07 -07001525 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 UserFolderInfo folderInfo = new UserFolderInfo();
1527 folderInfo.title = getText(R.string.folder_name);
1528
Michael Jurka0280c3b2010-09-17 15:00:07 -07001529 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1530 final int[] cellXY = mTmpAddItemCellCoordinates;
1531 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1532 showOutOfSpaceMessage();
1533 return;
1534 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535
1536 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 LauncherModel.addItemToDatabase(this, folderInfo,
1538 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001539 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001540 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541
1542 // Create the view
1543 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001544 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 }
Romain Guycbb89e42009-06-08 15:52:54 -07001547
Joe Onorato9c1289c2009-08-17 11:03:03 -04001548 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001549 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 }
1551
Michael Jurka28750fb2010-09-24 17:43:49 -07001552 private void completeAddLiveFolder(
1553 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1555 final int[] cellXY = mTmpAddItemCellCoordinates;
1556 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1557 showOutOfSpaceMessage();
1558 return;
1559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
1563 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001565 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568 }
1569
1570 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001571 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572
1573 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1574 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1575
1576 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 Intent.ShortcutIconResource iconResource = null;
1578
1579 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1580 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1581 try {
1582 iconResource = (Intent.ShortcutIconResource) extra;
1583 final PackageManager packageManager = context.getPackageManager();
1584 Resources resources = packageManager.getResourcesForApplication(
1585 iconResource.packageName);
1586 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1587 icon = resources.getDrawable(id);
1588 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001589 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591 }
1592
1593 if (icon == null) {
1594 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1595 }
1596
1597 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001598 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 info.title = name;
1600 info.iconResource = iconResource;
1601 info.uri = data.getData();
1602 info.baseIntent = baseIntent;
1603 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1604 LiveFolders.DISPLAY_MODE_GRID);
1605
1606 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001608 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609
1610 return info;
1611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 private void showNotifications() {
1614 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1615 if (statusBar != null) {
1616 statusBar.expand();
1617 }
1618 }
1619
1620 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001621 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001623 Intent chooser = Intent.createChooser(pickWallpaper,
1624 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001625 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1626 // Removed in Eclair MR1
1627// WallpaperManager wm = (WallpaperManager)
1628// getSystemService(Context.WALLPAPER_SERVICE);
1629// WallpaperInfo wi = wm.getWallpaperInfo();
1630// if (wi != null && wi.getSettingsActivity() != null) {
1631// LabeledIntent li = new LabeledIntent(getPackageName(),
1632// R.string.configure_wallpaper, 0);
1633// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1634// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1635// }
Mike Clerona0618e42009-10-22 13:55:21 -07001636 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
1638
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001639 /**
1640 * Registers various content observers. The current implementation registers
1641 * only a favorites observer to keep track of the favorites applications.
1642 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001643 private void registerContentObservers() {
1644 ContentResolver resolver = getContentResolver();
1645 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1646 true, mWidgetObserver);
1647 }
1648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 @Override
1650 public boolean dispatchKeyEvent(KeyEvent event) {
1651 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1652 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001654 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001655 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001656 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001657 dumpState();
1658 return true;
1659 }
1660 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001661 }
1662 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1663 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001664 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665 return true;
1666 }
1667 }
1668
1669 return super.dispatchKeyEvent(event);
1670 }
1671
Joe Onorato88ec0992009-11-19 13:16:06 -08001672 @Override
1673 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001674 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1675 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001676 } else {
1677 closeFolder();
1678 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001679 // Some launcher layouts don't have a previous and next view
1680 if (mPreviousView != null) {
1681 dismissPreview(mPreviousView);
1682 dismissPreview(mNextView);
1683 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 private void closeFolder() {
1687 Folder folder = mWorkspace.getOpenFolder();
1688 if (folder != null) {
1689 closeFolder(folder);
1690 }
1691 }
1692
1693 void closeFolder(Folder folder) {
1694 folder.getInfo().opened = false;
1695 ViewGroup parent = (ViewGroup) folder.getParent();
1696 if (parent != null) {
1697 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001698 if (folder instanceof DropTarget) {
1699 // Live folders aren't DropTargets.
1700 mDragController.removeDropTarget((DropTarget)folder);
1701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
1703 folder.onClose();
1704 }
1705
1706 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001707 * Re-listen when widgets are reset.
1708 */
1709 private void onAppWidgetReset() {
1710 mAppWidgetHost.startListening();
1711 }
1712
1713 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001714 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1715 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717 private void unbindDesktopItems() {
1718 for (ItemInfo item: mDesktopItems) {
1719 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 }
1721 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 /**
1724 * Launches the intent referred by the clicked shortcut.
1725 *
1726 * @param v The view representing the clicked shortcut.
1727 */
1728 public void onClick(View v) {
1729 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001730 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001732 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001733 int[] pos = new int[2];
1734 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001735 intent.setSourceBounds(new Rect(pos[0], pos[1],
1736 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001737 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 } else if (tag instanceof FolderInfo) {
1739 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001740 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001741 if (mState == State.ALL_APPS) {
1742 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001743 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001744 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747 }
1748
Michael Jurka0e260592010-06-30 17:07:39 -07001749 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001750 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001751 // clicking anywhere on the workspace causes the customization drawer to slide down
1752 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001753 return false;
1754 }
1755
Michael Jurkaaf442092010-06-10 17:01:57 -07001756 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001757 * Event handler for the search button
1758 *
1759 * @param v The view that was clicked.
1760 */
1761 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001762 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001763 }
1764
1765 /**
1766 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001767 * enters home screen customization mode.
1768 *
1769 * @param v The view that was clicked.
1770 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001771 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001772 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001773 }
1774
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001775 /**
1776 * Event handler for the "grid" button that appears on the home screen, which
1777 * enters all apps mode.
1778 *
1779 * @param v The view that was clicked.
1780 */
1781 public void onClickAllAppsButton(View v) {
1782 showAllApps(true);
1783 }
1784
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001785 public void onClickAppMarketButton(View v) {
1786 if (mAppMarketIntent != null) {
1787 startActivitySafely(mAppMarketIntent, "app market");
1788 }
1789 }
1790
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001791 void startApplicationDetailsActivity(ComponentName componentName) {
1792 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001793 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1794 Uri.fromParts("package", packageName, null));
1795 startActivity(intent);
1796 }
1797
Patrick Dubroy5539af72010-09-07 15:22:01 -07001798 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1799 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1800 // System applications cannot be installed. For now, show a toast explaining that.
1801 // We may give them the option of disabling apps this way.
1802 int messageId = R.string.uninstall_system_app_text;
1803 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1804 } else {
1805 String packageName = appInfo.componentName.getPackageName();
1806 String className = appInfo.componentName.getClassName();
1807 Intent intent = new Intent(
1808 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1809 startActivity(intent);
1810 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001811 }
1812
Joe Onoratof984e852010-03-25 09:47:45 -07001813 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1815 try {
1816 startActivity(intent);
1817 } catch (ActivityNotFoundException e) {
1818 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001819 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 } catch (SecurityException e) {
1821 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001822 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001824 "or use the exported attribute for this activity. "
1825 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 }
1827 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001828
Romain Guy8e633c52010-03-04 12:51:36 -08001829 void startActivityForResultSafely(Intent intent, int requestCode) {
1830 try {
1831 startActivityForResult(intent, requestCode);
1832 } catch (ActivityNotFoundException e) {
1833 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1834 } catch (SecurityException e) {
1835 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1836 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1837 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1838 "or use the exported attribute for this activity.", e);
1839 }
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841
1842 private void handleFolderClick(FolderInfo folderInfo) {
1843 if (!folderInfo.opened) {
1844 // Close any open folder
1845 closeFolder();
1846 // Open the requested folder
1847 openFolder(folderInfo);
1848 } else {
1849 // Find the open folder...
1850 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1851 int folderScreen;
1852 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001853 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001854 // .. and close it
1855 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001856 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 // Close any folder open on the current screen
1858 closeFolder();
1859 // Pull the folder onto this screen
1860 openFolder(folderInfo);
1861 }
1862 }
1863 }
1864 }
1865
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 /**
1867 * Opens the user fodler described by the specified tag. The opening of the folder
1868 * is animated relative to the specified View. If the View is null, no animation
1869 * is played.
1870 *
1871 * @param folderInfo The FolderInfo describing the folder to open.
1872 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001873 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 Folder openFolder;
1875
1876 if (folderInfo instanceof UserFolderInfo) {
1877 openFolder = UserFolder.fromXml(this);
1878 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001879 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 } else {
1881 return;
1882 }
1883
Joe Onorato00acb122009-08-04 16:04:30 -04001884 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 openFolder.setLauncher(this);
1886
1887 openFolder.bind(folderInfo);
1888 folderInfo.opened = true;
1889
Winson Chungaafa03c2010-06-11 17:34:16 -07001890 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1891
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 openFolder.onOpen();
1893 }
1894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001896 switch (v.getId()) {
1897 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001898 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001899 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1900 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001901 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001902 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001903 return true;
1904 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001905 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001906 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1907 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001908 showPreviews(v);
1909 }
1910 return true;
1911 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001912 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001913 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1914 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1915 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001916 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001917 return true;
1918 }
1919
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 return false;
1922 }
1923
1924 if (!(v instanceof CellLayout)) {
1925 v = (View) v.getParent();
1926 }
1927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928
Michael Jurka0280c3b2010-09-17 15:00:07 -07001929 resetAddInfo();
1930 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001932 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 return true;
1934 }
1935
Michael Jurka0280c3b2010-09-17 15:00:07 -07001936 final View itemUnderLongClick = longClickCellInfo.cell;
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939 if (itemUnderLongClick == null) {
1940 // User long pressed on empty space
1941 mWorkspace.setAllowLongPress(false);
1942 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1943 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001944 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001950 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1951 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001952 mAddIntersectCellX = longClickCellInfo.cellX;
1953 mAddIntersectCellY = longClickCellInfo.cellY;
1954 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
1956 }
1957 }
1958 return true;
1959 }
1960
Romain Guye6b8e2f2009-11-10 11:56:55 -08001961 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001962 private void dismissPreview(final View v) {
1963 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001964 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001965 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1966 public void onDismiss() {
1967 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1968 int count = group.getChildCount();
1969 for (int i = 0; i < count; i++) {
1970 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1971 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001972 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1973 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1974
1975 v.setTag(R.id.workspace, null);
1976 v.setTag(R.id.icon, null);
1977 window.setOnDismissListener(null);
1978 }
1979 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001980 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001981 }
1982 v.setTag(null);
1983 }
1984
Romain Guyf8e6a802009-12-07 17:48:02 -08001985 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001986 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001987 }
1988
Romain Guya6abce82009-11-10 02:54:41 -08001989 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001990 final Resources resources = getResources();
1991 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001992
Romain Guya6abce82009-11-10 02:54:41 -08001993 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001994
Romain Guy9d31e9f2009-11-11 18:54:28 -08001995 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001996
Romain Guyf8e6a802009-12-07 17:48:02 -08001997 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001998 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001999 int extraW = (int) ((r.left + r.right) * max);
2000 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002001
2002 int aW = cell.getWidth() - extraW;
2003 float w = aW / max;
2004
2005 int width = cell.getWidth();
2006 int height = cell.getHeight();
2007 int x = cell.getLeftPadding();
2008 int y = cell.getTopPadding();
2009 width -= (x + cell.getRightPadding());
2010 height -= (y + cell.getBottomPadding());
2011
2012 float scale = w / width;
2013
2014 int count = end - start;
2015
2016 final float sWidth = width * scale;
2017 float sHeight = height * scale;
2018
Romain Guye6b8e2f2009-11-10 11:56:55 -08002019 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002020
Romain Guye6b8e2f2009-11-10 11:56:55 -08002021 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2022 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002023
2024 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002025 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002026 cell = (CellLayout) workspace.getChildAt(i);
2027
Romain Guyf8e6a802009-12-07 17:48:02 -08002028 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002029 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002030
2031 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002032 c.scale(scale, scale);
2033 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002034 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002035
Romain Guy9d31e9f2009-11-11 18:54:28 -08002036 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002037 image.setImageBitmap(bitmap);
2038 image.setTag(i);
2039 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002040 image.setOnFocusChangeListener(handler);
2041 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002042 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002043
2044 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2046
Winson Chungaafa03c2010-06-11 17:34:16 -07002047 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002048 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002049
2050 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002051 p.setContentView(preview);
2052 p.setWidth((int) (sWidth * count + extraW));
2053 p.setHeight((int) (sHeight + extraH));
2054 p.setAnimationStyle(R.style.AnimationPreview);
2055 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002056 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002057 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002058 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002059
Romain Guye6b8e2f2009-11-10 11:56:55 -08002060 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2061 public void onDismiss() {
2062 dismissPreview(anchor);
2063 }
2064 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002065
2066 anchor.setTag(p);
2067 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002068 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002069 }
2070
Romain Guy9d31e9f2009-11-11 18:54:28 -08002071 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002072 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002073
Romain Guye6b8e2f2009-11-10 11:56:55 -08002074 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002075 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002076 }
2077
2078 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002079 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002080 v.post(this);
2081 }
2082
2083 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002084 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002085 }
2086
2087 public void onFocusChange(View v, boolean hasFocus) {
2088 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002089 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002090 }
Romain Guya6abce82009-11-10 02:54:41 -08002091 }
2092 }
2093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 Workspace getWorkspace() {
2095 return mWorkspace;
2096 }
2097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 @Override
2099 protected Dialog onCreateDialog(int id) {
2100 switch (id) {
2101 case DIALOG_CREATE_SHORTCUT:
2102 return new CreateShortcut().createDialog();
2103 case DIALOG_RENAME_FOLDER:
2104 return new RenameFolder().createDialog();
2105 }
2106
2107 return super.onCreateDialog(id);
2108 }
2109
2110 @Override
2111 protected void onPrepareDialog(int id, Dialog dialog) {
2112 switch (id) {
2113 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 break;
2115 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002116 if (mFolderInfo != null) {
2117 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2118 final CharSequence text = mFolderInfo.title;
2119 input.setText(text);
2120 input.setSelection(0, text.length());
2121 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002122 break;
2123 }
2124 }
2125
2126 void showRenameDialog(FolderInfo info) {
2127 mFolderInfo = info;
2128 mWaitingForResult = true;
2129 showDialog(DIALOG_RENAME_FOLDER);
2130 }
2131
Michael Jurka0280c3b2010-09-17 15:00:07 -07002132 private void showAddDialog(int intersectX, int intersectY) {
2133 resetAddInfo();
2134 mAddIntersectCellX = intersectX;
2135 mAddIntersectCellY = intersectY;
2136 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 mWaitingForResult = true;
2138 showDialog(DIALOG_CREATE_SHORTCUT);
2139 }
2140
Joe Onoratodeb98af2010-02-19 14:59:39 -08002141 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002142 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002143 Bundle bundle = new Bundle();
2144
2145 ArrayList<String> shortcutNames = new ArrayList<String>();
2146 shortcutNames.add(getString(R.string.group_applications));
2147 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2148
2149 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2150 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2151 R.drawable.ic_launcher_application));
2152 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2153
2154 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2155 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002156 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002157 pickIntent.putExtras(bundle);
2158
Joe Onoratodeb98af2010-02-19 14:59:39 -08002159 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002160 }
2161
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 private class RenameFolder {
2163 private EditText mInput;
2164
2165 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2167 mInput = (EditText) layout.findViewById(R.id.folder_name);
2168
2169 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2170 builder.setIcon(0);
2171 builder.setTitle(getString(R.string.rename_folder_title));
2172 builder.setCancelable(true);
2173 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2174 public void onCancel(DialogInterface dialog) {
2175 cleanup();
2176 }
2177 });
2178 builder.setNegativeButton(getString(R.string.cancel_action),
2179 new Dialog.OnClickListener() {
2180 public void onClick(DialogInterface dialog, int which) {
2181 cleanup();
2182 }
2183 }
2184 );
2185 builder.setPositiveButton(getString(R.string.rename_action),
2186 new Dialog.OnClickListener() {
2187 public void onClick(DialogInterface dialog, int which) {
2188 changeFolderName();
2189 }
2190 }
2191 );
2192 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002193
2194 final AlertDialog dialog = builder.create();
2195 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2196 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002197 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002198 mInput.requestFocus();
2199 InputMethodManager inputManager = (InputMethodManager)
2200 getSystemService(Context.INPUT_METHOD_SERVICE);
2201 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002202 }
2203 });
2204
2205 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 }
2207
2208 private void changeFolderName() {
2209 final String name = mInput.getText().toString();
2210 if (!TextUtils.isEmpty(name)) {
2211 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002212 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 mFolderInfo.title = name;
2214 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2215
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002217 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002218 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 } else {
2220 final FolderIcon folderIcon = (FolderIcon)
2221 mWorkspace.getViewForTag(mFolderInfo);
2222 if (folderIcon != null) {
2223 folderIcon.setText(name);
2224 getWorkspace().requestLayout();
2225 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002226 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002227 mWorkspaceLoading = true;
2228 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 }
2230 }
2231 }
2232 cleanup();
2233 }
2234
2235 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236 dismissDialog(DIALOG_RENAME_FOLDER);
2237 mWaitingForResult = false;
2238 mFolderInfo = null;
2239 }
2240 }
2241
Daniel Sandler843e8602010-06-07 14:59:01 -04002242 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2243 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002244 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002245 }
2246
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002247 // AllAppsView.Watcher
2248 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002249 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2250 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002251 mWorkspace.setVisibility(View.GONE);
2252 }
2253 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002255 private void showToolbarButton(View button) {
2256 button.setAlpha(1.0f);
2257 button.setVisibility(View.VISIBLE);
2258 button.setFocusable(true);
2259 button.setClickable(true);
2260 }
2261
2262 private void hideToolbarButton(View button) {
2263 button.setAlpha(0.0f);
2264 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2265 button.setVisibility(View.INVISIBLE);
2266 button.setFocusable(false);
2267 button.setClickable(false);
2268 }
2269
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002270 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002271 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002272 *
2273 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2274 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002275 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002276 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002277 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002278 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002279 final boolean showing = show;
2280 final boolean hiding = !show;
2281
2282 final int duration = show ?
2283 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2284 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2285
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002286 if (seq != null) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002287 Animator anim = new ObjectAnimator<Float>(duration, view, "alpha", show ? 1.0f : 0.0f);
2288 anim.addListener(new AnimatorListenerAdapter() {
2289 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002290 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002292 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002293 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002294 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002295 });
2296 seq.play(anim);
2297 } else {
2298 if (showing) {
2299 showToolbarButton(view);
2300 } else {
2301 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002302 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002303 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 }
2305
2306 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 * Show/hide the appropriate toolbar buttons for newState.
2308 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309 *
2310 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002311 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2312 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002314 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002315 final View searchButton = findViewById(R.id.search_button);
2316 final View allAppsButton = findViewById(R.id.all_apps_button);
2317 final View marketButton = findViewById(R.id.market_button);
2318 final View configureButton = findViewById(R.id.configure_button);
2319
2320 switch (newState) {
2321 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002322 hideOrShowToolbarButton(true, searchButton, showSeq);
2323 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2324 hideOrShowToolbarButton(true, configureButton, showSeq);
2325 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002326 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002327 break;
2328 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002329 hideOrShowToolbarButton(true, configureButton, showSeq);
2330 hideOrShowToolbarButton(true, marketButton, showSeq);
2331 hideOrShowToolbarButton(false, searchButton, hideSeq);
2332 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002333 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002334 break;
2335 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002336 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2337 hideOrShowToolbarButton(false, searchButton, hideSeq);
2338 hideOrShowToolbarButton(false, marketButton, hideSeq);
2339 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002340 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002341 break;
2342 }
2343 }
2344
2345 /**
2346 * Helper method for the cameraZoomIn/cameraZoomOut animations
2347 * @param view The view being animated
2348 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2349 * @param scaleFactor The scale factor used for the zoom
2350 */
2351 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2352 final int height = view.getHeight();
2353 view.setPivotX(view.getWidth() / 2.0f);
2354 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2355 // Assumes that the view is normally anchored to either the top or bottom of the screen
2356 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2357 if (state == State.ALL_APPS) {
2358 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2359 } else {
2360 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2361 }
2362 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002363
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002364 /**
2365 * Zoom the camera out from the workspace to reveal 'toView'.
2366 * Assumes that the view to show is anchored at either the very top or very bottom
2367 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002368 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002369 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002370 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002371 final Resources res = getResources();
2372 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2373 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002374 final boolean toAllApps = (toState == State.ALL_APPS);
2375 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002376
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002377 setPivotsForZoom(toView, toState, scale);
2378
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002379 if (toState == State.ALL_APPS) {
2380 mWorkspace.shrinkToBottom(animated);
2381 } else {
2382 mWorkspace.shrinkToTop(animated);
2383 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002384
2385 if (animated) {
Chet Haaseb1254a62010-09-07 13:35:00 -07002386 ValueAnimator scaleAnim = new ObjectAnimator(duration, toView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002387 new PropertyValuesHolder<Float>("scaleX", scale, 1.0f),
2388 new PropertyValuesHolder<Float>("scaleY", scale, 1.0f));
2389 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002390 scaleAnim.addListener(new AnimatorListenerAdapter() {
2391 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002392 // Prepare the position
2393 toView.setTranslationX(0.0f);
2394 toView.setTranslationY(0.0f);
2395 toView.setVisibility(View.VISIBLE);
2396 }
2397 });
2398
Chet Haaseb1254a62010-09-07 13:35:00 -07002399 AnimatorSet toolbarHideAnim = new AnimatorSet();
2400 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002401 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2402
2403 // toView should appear right at the end of the workspace shrink animation
2404 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2405
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002406 if (mStateAnimation != null) mStateAnimation.cancel();
2407 mStateAnimation = new AnimatorSet();
2408 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2409 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410
2411 // Show the new toolbar buttons just as the main animation is ending
2412 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002413 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2414 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 } else {
2416 toView.setTranslationX(0.0f);
2417 toView.setTranslationY(0.0f);
2418 toView.setScaleX(1.0f);
2419 toView.setScaleY(1.0f);
2420 toView.setVisibility(View.VISIBLE);
2421 hideAndShowToolbarButtons(toState, null, null);
2422 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002423 }
2424
2425 /**
2426 * Zoom the camera back into the workspace, hiding 'fromView'.
2427 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002428 * @param fromState The current state (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 cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002432 Resources res = getResources();
2433 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2434 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002435 final View fromView =
2436 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2437
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002438 mCustomizePagedView.endChoiceMode();
2439 mAllAppsPagedView.endChoiceMode();
2440
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002441 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002443 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002444
2445 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002446 if (mStateAnimation != null) mStateAnimation.cancel();
2447 mStateAnimation = new AnimatorSet();
Chet Haaseb1254a62010-09-07 13:35:00 -07002448 ValueAnimator scaleAnim = new ObjectAnimator(duration, fromView,
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002449 new PropertyValuesHolder<Float>("scaleX", scaleFactor),
2450 new PropertyValuesHolder<Float>("scaleY", scaleFactor));
2451 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002452 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002453 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002454 fromView.setVisibility(View.GONE);
2455 fromView.setScaleX(1.0f);
2456 fromView.setScaleY(1.0f);
2457 }
2458 });
2459
Chet Haaseb1254a62010-09-07 13:35:00 -07002460 AnimatorSet toolbarHideAnim = new AnimatorSet();
2461 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002462 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2463
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002464 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002465
2466 // Show the new toolbar buttons at the very end of the whole animation
2467 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2468 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002469 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2470 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002471 } else {
2472 fromView.setVisibility(View.GONE);
2473 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2474 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002475 }
2476
2477 /**
2478 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2479 * This is the transition used on xlarge screens to go between all apps and
2480 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002481 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2482 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2483 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002484 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002485 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002486 final Resources res = getResources();
2487 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002488 final int workspaceHeight = mWorkspace.getHeight();
2489
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 final boolean fromAllApps = (fromState == State.ALL_APPS);
2491 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2492 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002493
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002494 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2495 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2496 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2497 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002498
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002499 mCustomizePagedView.endChoiceMode();
2500 mAllAppsPagedView.endChoiceMode();
2501
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002502 if (toState == State.ALL_APPS) {
2503 mWorkspace.shrinkToBottom(animated);
2504 } else {
2505 mWorkspace.shrinkToTop(animated);
2506 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002507
2508 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002509 if (mStateAnimation != null) mStateAnimation.cancel();
2510 mStateAnimation = new AnimatorSet();
2511 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002512 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002513 toView.setVisibility(View.VISIBLE);
2514 toView.setY(toViewStartY);
2515 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002516 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 fromView.setVisibility(View.GONE);
2518 }
2519 });
2520
Chet Haaseb1254a62010-09-07 13:35:00 -07002521 AnimatorSet toolbarHideAnim = new AnimatorSet();
2522 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2524
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002525 mStateAnimation.playTogether(
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002526 toolbarHideAnim,
Chet Haaseb1254a62010-09-07 13:35:00 -07002527 new ObjectAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2528 new ObjectAnimator(duration, toView, "y", toViewStartY, toViewEndY));
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529
2530 // Show the new toolbar buttons just as the main animation is ending
2531 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2533 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 } else {
2535 fromView.setY(fromViewEndY);
2536 fromView.setVisibility(View.GONE);
2537 toView.setY(toViewEndY);
2538 toView.setVisibility(View.VISIBLE);
2539 hideAndShowToolbarButtons(toState, null, null);
2540 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002541 }
2542
2543 void showAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002544 if (mState == State.ALL_APPS)
Winson Chung80baf5a2010-08-09 16:03:15 -07002545 return;
2546
Michael Jurka79212d82010-07-30 16:36:20 -07002547 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002548 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002549 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002550 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002552 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002553 } else {
2554 mAllAppsGrid.zoom(1.0f, animated);
2555 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002556
Romain Guyc16fea72010-03-12 17:17:56 -08002557 ((View) mAllAppsGrid).setFocusable(true);
2558 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002559
Joe Onorato7c312c12009-08-13 21:36:53 -07002560 // TODO: fade these two too
2561 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002562 // Change the state *after* we've called all the transition code
2563 mState = State.ALL_APPS;
2564 }
2565
2566
2567 void showWorkspace(boolean animated) {
2568 showWorkspace(animated, null);
2569 }
2570
2571 void showWorkspace(boolean animated, CellLayout layout) {
2572 if (layout != null && animated) {
2573 mWorkspace.unshrink(layout);
2574 } else {
2575 mWorkspace.unshrink(animated);
2576 }
2577 if (mState == State.ALL_APPS) {
2578 closeAllApps(animated);
2579 } else if (mState == State.CUSTOMIZE) {
2580 hideCustomizationDrawer(animated);
2581 }
2582 // Change the state *after* we've called all the transition code
2583 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002584 }
2585
Joe Onoratob2061212009-11-24 16:13:54 -05002586 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002587 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002588 * - Home from workspace
2589 * - from center screen
2590 * - from other screens
2591 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002592 * - from center screen
2593 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002594 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002595 * - from center screen
2596 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002597 * - Launch app from workspace and quit
2598 * - with back
2599 * - with home
2600 * - Launch app from all apps and quit
2601 * - with back
2602 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002603 * - Go to a screen that's not the default, then all
2604 * apps, and launch and app, and go back
2605 * - with back
2606 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002607 * - On workspace, long press power and go back
2608 * - with back
2609 * - with home
2610 * - On all apps, long press power and go back
2611 * - with back
2612 * - with home
2613 * - On workspace, power off
2614 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002615 * - Launch an app and turn off the screen while in that app
2616 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002617 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002618 * - From all apps
2619 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002620 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2621 * - From all apps
2622 * - From the center workspace
2623 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002624 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002625 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002626 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002627 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002628 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002630 } else {
2631 mAllAppsGrid.zoom(0.0f, animated);
2632 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002633 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002634 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002635 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002636 }
2637
Joe Onorato7c312c12009-08-13 21:36:53 -07002638 void lockAllApps() {
2639 // TODO
2640 }
2641
2642 void unlockAllApps() {
2643 // TODO
2644 }
2645
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002646 // Show the customization drawer (only exists in x-large configuration)
2647 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002648 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002649 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002650 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002652 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002653 // Change the state *after* we've called all the transition code
2654 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002655 }
2656
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002657 // Hide the customization drawer (only exists in x-large configuration)
2658 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002659 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002661 }
2662 }
2663
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002664 void onWorkspaceClick(CellLayout layout) {
2665 Object itemInfo = mAllAppsPagedView.getChosenItem();
2666 if (itemInfo == null) {
2667 itemInfo = mCustomizePagedView.getChosenItem();
Michael Jurka213d9632010-07-28 11:29:25 -07002668 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002669
2670 if (itemInfo == null) {
2671 // No items are chosen in All Apps or Customize, so just zoom into the workspace
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002672 showWorkspace(true, layout);
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002673 } else {
2674 // Act as if the chosen item was dropped on the given CellLayout
2675 if (mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2676 mAllAppsPagedView.endChoiceMode();
2677 mCustomizePagedView.endChoiceMode();
2678 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002679 showOutOfSpaceMessage();
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002680 }
Michael Jurka213d9632010-07-28 11:29:25 -07002681 }
2682 }
2683
Michael Jurka0423dcf2010-10-05 14:56:18 -07002684 private void updateButtonWithIconFromExternalActivity(
2685 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2686 ImageView button = (ImageView) findViewById(buttonId);
2687 Drawable toolbarIcon = null;
2688 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002689 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002690 // Look for the toolbar icon specified in the activity meta-data
2691 Bundle metaData = packageManager.getActivityInfo(
2692 activityName, PackageManager.GET_META_DATA).metaData;
2693 if (metaData != null) {
2694 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2695 if (iconResId != 0) {
2696 Resources res = packageManager.getResourcesForActivity(activityName);
2697 toolbarIcon = res.getDrawable(iconResId);
2698 }
2699 }
2700 } catch (NameNotFoundException e) {
2701 // Do nothing
2702 }
2703 // If we were unable to find the icon via the meta-data, use a generic one
2704 if (toolbarIcon == null) {
2705 button.setImageResource(fallbackDrawableId);
2706 } else {
2707 button.setImageDrawable(toolbarIcon);
2708 }
2709 }
2710
2711 private void updateGlobalSearchIcon() {
2712 if (LauncherApplication.isScreenXLarge()) {
2713 final SearchManager searchManager =
2714 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2715 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002716 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 updateButtonWithIconFromExternalActivity(
2718 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002719 }
2720 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002721 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002722
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002723 /**
2724 * Sets the app market icon (shown when all apps is visible on x-large screens)
2725 */
2726 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002727 if (LauncherApplication.isScreenXLarge()) {
2728 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2729 // Find the app market activity by resolving an intent.
2730 // (If multiple app markets are installed, it will return the ResolverActivity.)
2731 ComponentName activityName = intent.resolveActivity(getPackageManager());
2732 if (activityName != null) {
2733 mAppMarketIntent = intent;
2734 updateButtonWithIconFromExternalActivity(
2735 R.id.market_button, activityName, R.drawable.app_market_generic);
2736 }
2737 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002738 }
2739
2740 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 * Displays the shortcut creation dialog and launches, if necessary, the
2742 * appropriate activity.
2743 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002744 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002745 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2746 DialogInterface.OnShowListener {
2747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002748 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002751 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2754 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002755 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 builder.setInverseBackgroundForced(true);
2758
2759 AlertDialog dialog = builder.create();
2760 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002761 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002762 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 return dialog;
2765 }
2766
2767 public void onCancel(DialogInterface dialog) {
2768 mWaitingForResult = false;
2769 cleanup();
2770 }
2771
Romain Guycbb89e42009-06-08 15:52:54 -07002772 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002773 }
2774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002776 try {
2777 dismissDialog(DIALOG_CREATE_SHORTCUT);
2778 } catch (Exception e) {
2779 // An exception is thrown if the dialog is not visible, which is fine
2780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 }
2782
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002783 /**
2784 * Handle the action clicked in the "Add to home" dialog.
2785 */
2786 public void onClick(DialogInterface dialog, int which) {
2787 Resources res = getResources();
2788 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002789
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002790 switch (which) {
2791 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002792 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002793 break;
2794 }
Romain Guycbb89e42009-06-08 15:52:54 -07002795
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002796 case AddAdapter.ITEM_APPWIDGET: {
2797 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002798
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002799 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2800 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002801 // start the pick activity
2802 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2803 break;
2804 }
Romain Guycbb89e42009-06-08 15:52:54 -07002805
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002806 case AddAdapter.ITEM_LIVE_FOLDER: {
2807 // Insert extra item to handle inserting folder
2808 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002809
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002810 ArrayList<String> shortcutNames = new ArrayList<String>();
2811 shortcutNames.add(res.getString(R.string.group_folder));
2812 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002813
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002814 ArrayList<ShortcutIconResource> shortcutIcons =
2815 new ArrayList<ShortcutIconResource>();
2816 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2817 R.drawable.ic_launcher_folder));
2818 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2819
2820 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2821 pickIntent.putExtra(Intent.EXTRA_INTENT,
2822 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2823 pickIntent.putExtra(Intent.EXTRA_TITLE,
2824 getText(R.string.title_select_live_folder));
2825 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002826
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002827 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2828 break;
2829 }
2830
2831 case AddAdapter.ITEM_WALLPAPER: {
2832 startWallpaper();
2833 break;
2834 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002835 }
2836 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002837
2838 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002839 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002841 }
2842
2843 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002844 * Receives notifications when applications are added/removed.
2845 */
2846 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2847 @Override
2848 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002849 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002850 String reason = intent.getStringExtra("reason");
2851 if (!"homekey".equals(reason)) {
2852 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002853 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002854 animate = false;
2855 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002856 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002857 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002858 }
2859 }
2860
2861 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002862 * Receives notifications whenever the appwidgets are reset.
2863 */
2864 private class AppWidgetResetObserver extends ContentObserver {
2865 public AppWidgetResetObserver() {
2866 super(new Handler());
2867 }
2868
2869 @Override
2870 public void onChange(boolean selfChange) {
2871 onAppWidgetReset();
2872 }
2873 }
2874
2875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002876 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002879 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002880 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002881 } else {
2882 return SCREEN_COUNT / 2;
2883 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002885
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002886 void setAllAppsPagedView(PagedView view) {
2887 mAllAppsPagedView = view;
2888 }
2889
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 /**
2891 * Refreshes the shortcuts shown on the workspace.
2892 *
2893 * Implementation of the method from LauncherModel.Callbacks.
2894 */
2895 public void startBinding() {
2896 final Workspace workspace = mWorkspace;
2897 int count = workspace.getChildCount();
2898 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002899 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2901 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002902
Joe Onorato9c1289c2009-08-17 11:03:03 -04002903 if (DEBUG_USER_INTERFACE) {
2904 android.widget.Button finishButton = new android.widget.Button(this);
2905 finishButton.setText("Finish");
2906 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2907
2908 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2909 public void onClick(View v) {
2910 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
2914 }
2915
2916 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 * Bind the items start-end from the list.
2918 *
2919 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2922
2923 final Workspace workspace = mWorkspace;
2924
2925 for (int i=start; i<end; i++) {
2926 final ItemInfo item = shortcuts.get(i);
2927 mDesktopItems.add(item);
2928 switch (item.itemType) {
2929 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2930 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002931 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2933 false);
2934 break;
2935 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2936 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002937 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002938 (UserFolderInfo) item);
2939 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2940 false);
2941 break;
2942 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2943 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2944 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002945 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 (LiveFolderInfo) item);
2947 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2948 false);
2949 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002951 }
2952
Joe Onorato9c1289c2009-08-17 11:03:03 -04002953 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002954 }
2955
Joe Onorato9c1289c2009-08-17 11:03:03 -04002956 /**
2957 * Implementation of the method from LauncherModel.Callbacks.
2958 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002959 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002960 sFolders.clear();
2961 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963
2964 /**
2965 * Add the views for a widget to the workspace.
2966 *
2967 * Implementation of the method from LauncherModel.Callbacks.
2968 */
2969 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002970 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2971 if (DEBUG_WIDGETS) {
2972 Log.d(TAG, "bindAppWidget: " + item);
2973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 final Workspace workspace = mWorkspace;
2975
2976 final int appWidgetId = item.appWidgetId;
2977 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002978 if (DEBUG_WIDGETS) {
2979 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2980 }
2981
Joe Onorato9c1289c2009-08-17 11:03:03 -04002982 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2983
Joe Onorato9c1289c2009-08-17 11:03:03 -04002984 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2985 item.hostView.setTag(item);
2986
2987 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2988 item.cellY, item.spanX, item.spanY, false);
2989
2990 workspace.requestLayout();
2991
2992 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002993
2994 if (DEBUG_WIDGETS) {
2995 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2996 + (SystemClock.uptimeMillis()-start) + "ms");
2997 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 }
2999
3000 /**
3001 * Callback saying that there aren't any more items to bind.
3002 *
3003 * Implementation of the method from LauncherModel.Callbacks.
3004 */
3005 public void finishBindingItems() {
3006 if (mSavedState != null) {
3007 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003008 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 }
3010
3011 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3012 if (userFolders != null) {
3013 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003014 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 if (info != null) {
3016 openFolder(info);
3017 }
3018 }
3019 final Folder openFolder = mWorkspace.getOpenFolder();
3020 if (openFolder != null) {
3021 openFolder.requestFocus();
3022 }
3023 }
3024
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 mSavedState = null;
3026 }
3027
3028 if (mSavedInstanceState != null) {
3029 super.onRestoreInstanceState(mSavedInstanceState);
3030 mSavedInstanceState = null;
3031 }
3032
Joe Onorato9c1289c2009-08-17 11:03:03 -04003033 mWorkspaceLoading = false;
3034 }
3035
3036 /**
3037 * Add the icons for all apps.
3038 *
3039 * Implementation of the method from LauncherModel.Callbacks.
3040 */
3041 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003042 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003043 if (mCustomizePagedView != null) {
3044 mCustomizePagedView.setApps(apps);
3045 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003046 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003047 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 }
3049
3050 /**
3051 * A package was installed.
3052 *
3053 * Implementation of the method from LauncherModel.Callbacks.
3054 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003055 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003057 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003058 if (mCustomizePagedView != null) {
3059 mCustomizePagedView.addApps(apps);
3060 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003061 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003062 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 }
3064
3065 /**
3066 * A package was updated.
3067 *
3068 * Implementation of the method from LauncherModel.Callbacks.
3069 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003070 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003071 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003072 mWorkspace.updateShortcuts(apps);
3073 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003074 if (mCustomizePagedView != null) {
3075 mCustomizePagedView.updateApps(apps);
3076 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003077 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003078 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 }
3080
3081 /**
3082 * A package was uninstalled.
3083 *
3084 * Implementation of the method from LauncherModel.Callbacks.
3085 */
Joe Onorato36115782010-06-17 13:28:48 -04003086 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003088 if (permanent) {
3089 mWorkspace.removeItems(apps);
3090 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003091 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003092 if (mCustomizePagedView != null) {
3093 mCustomizePagedView.removeApps(apps);
3094 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003095 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003096 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003097 }
Joe Onoratobe386092009-11-17 17:32:16 -08003098
3099 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003100 * A number of packages were updated.
3101 */
3102 public void bindPackagesUpdated() {
3103 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003104 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003105 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003106 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003107 }
3108
3109 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003110 * Prints out out state for debugging.
3111 */
3112 public void dumpState() {
3113 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003114 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003115 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3116 Log.d(TAG, "mRestoring=" + mRestoring);
3117 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3118 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3119 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003120 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003121 mModel.dumpState();
3122 mAllAppsGrid.dumpState();
3123 Log.d(TAG, "END launcher2 dump state");
3124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003125}