blob: 471cb3ee8368b3b6adcfaf29aa86ea8a33a5c931 [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 Haase472b2812010-10-14 07:02:04 -0700321 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700323 alphaAnim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -0700324 alphaAnim.addListener(new AnimatorListenerAdapter() {
325 public void onAnimationEnd(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700326 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
327 if (tag == WIDGETS_TAG) {
328 mCustomizePagedView.setCustomizationFilter(
329 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700330 } else if (tag == APPLICATIONS_TAG) {
331 mCustomizePagedView.setCustomizationFilter(
332 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700333 } else if (tag == FOLDERS_TAG) {
334 mCustomizePagedView.setCustomizationFilter(
335 CustomizePagedView.CustomizationType.FolderCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700336 } else if (tag == WALLPAPERS_TAG) {
337 mCustomizePagedView.setCustomizationFilter(
338 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700339 } else if (tag == SHORTCUTS_TAG) {
340 mCustomizePagedView.setCustomizationFilter(
341 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700342 }
343
344 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700345 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700346 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700347 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700348 alphaAnim.start();
349 }
350 });
351 alphaAnim.start();
352 }
353 });
Michael Jurka946ad472010-07-09 18:05:18 -0700354
Winson Chungb0b2e6f2010-10-12 17:49:56 -0700355 // TEMP: Working around a bug in tab host where the current tab does not initially have
356 // a highlight on it by selecting something else, then selecting the actual tab we want..
357 mHomeCustomizationDrawer.setCurrentTab(1);
Michael Jurka946ad472010-07-09 18:05:18 -0700358 mHomeCustomizationDrawer.setCurrentTab(0);
359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 setupViews();
361
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800362 registerContentObservers();
363
Joe Onorato7c312c12009-08-13 21:36:53 -0700364 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700365
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366 mSavedState = savedInstanceState;
367 restoreState(mSavedState);
368
369 if (PROFILE_STARTUP) {
370 android.os.Debug.stopMethodTracing();
371 }
372
373 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400374 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 }
376
377 // For handling default keys
378 mDefaultKeySsb = new SpannableStringBuilder();
379 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800380
381 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
382 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383 }
Romain Guycbb89e42009-06-08 15:52:54 -0700384
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700386 if (sLocaleConfiguration == null) {
387 new AsyncTask<Void, Void, LocaleConfiguration>() {
388 @Override
389 protected LocaleConfiguration doInBackground(Void... unused) {
390 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
391 readConfiguration(Launcher.this, localeConfiguration);
392 return localeConfiguration;
393 }
394
395 @Override
396 protected void onPostExecute(LocaleConfiguration result) {
397 sLocaleConfiguration = result;
398 checkForLocaleChange(); // recursive, but now with a locale configuration
399 }
400 }.execute();
401 return;
402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final Configuration configuration = getResources().getConfiguration();
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final String locale = configuration.locale.toString();
408
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700409 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 final int mcc = configuration.mcc;
411
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final int mnc = configuration.mnc;
414
Romain Guy84f296c2009-11-04 15:00:44 -0800415 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416
Romain Guy84f296c2009-11-04 15:00:44 -0800417 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 sLocaleConfiguration.locale = locale;
419 sLocaleConfiguration.mcc = mcc;
420 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700421
Joe Onorato0589f0f2010-02-08 13:44:00 -0800422 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400423 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424
425 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
426 new Thread("WriteLocaleConfiguration") {
427 public void run() {
428 writeConfiguration(Launcher.this, localeConfiguration);
429 }
430 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700431 }
432 }
433
434 private static class LocaleConfiguration {
435 public String locale;
436 public int mcc = -1;
437 public int mnc = -1;
438 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700439
Romain Guy98d01652009-06-30 16:21:04 -0700440 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
441 DataInputStream in = null;
442 try {
443 in = new DataInputStream(context.openFileInput(PREFERENCES));
444 configuration.locale = in.readUTF();
445 configuration.mcc = in.readInt();
446 configuration.mnc = in.readInt();
447 } catch (FileNotFoundException e) {
448 // Ignore
449 } catch (IOException e) {
450 // Ignore
451 } finally {
452 if (in != null) {
453 try {
454 in.close();
455 } catch (IOException e) {
456 // Ignore
457 }
458 }
459 }
460 }
461
462 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
463 DataOutputStream out = null;
464 try {
465 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
466 out.writeUTF(configuration.locale);
467 out.writeInt(configuration.mcc);
468 out.writeInt(configuration.mnc);
469 out.flush();
470 } catch (FileNotFoundException e) {
471 // Ignore
472 } catch (IOException e) {
473 //noinspection ResultOfMethodCallIgnored
474 context.getFileStreamPath(PREFERENCES).delete();
475 } finally {
476 if (out != null) {
477 try {
478 out.close();
479 } catch (IOException e) {
480 // Ignore
481 }
482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484 }
485
486 static int getScreen() {
487 synchronized (sLock) {
488 return sScreen;
489 }
490 }
491
492 static void setScreen(int screen) {
493 synchronized (sLock) {
494 sScreen = screen;
495 }
496 }
497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700499 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500
501 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700502 // TODO: Put back when we decide about scrolling the wallpaper
503 // boolean isPortrait = display.getWidth() < display.getHeight();
504 // final int width = isPortrait ? display.getWidth() : display.getHeight();
505 // final int height = isPortrait ? display.getHeight() : display.getWidth();
506 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
507 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 }
509
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400510 // Note: This doesn't do all the client-id magic that BrowserProvider does
511 // in Browser. (http://b/2425179)
512 private Uri getDefaultBrowserUri() {
513 String url = getString(R.string.default_browser_url);
514 if (url.indexOf("{CID}") != -1) {
515 url = url.replace("{CID}", "android-google");
516 }
517 return Uri.parse(url);
518 }
519
520 // Load the Intent templates from arrays.xml to populate the hotseats. For
521 // each Intent, if it resolves to a single app, use that as the launch
522 // intent & use that app's label as the contentDescription. Otherwise,
523 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400524 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400525 if (mHotseatConfig == null) {
526 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
527 if (mHotseatConfig.length > 0) {
528 mHotseats = new Intent[mHotseatConfig.length];
529 mHotseatLabels = new CharSequence[mHotseatConfig.length];
530 mHotseatIcons = new Drawable[mHotseatConfig.length];
531 } else {
532 mHotseats = null;
533 mHotseatIcons = null;
534 mHotseatLabels = null;
535 }
536
537 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
538 for (int i=0; i<mHotseatConfig.length; i++) {
539 // load icon for this slot; currently unrelated to the actual activity
540 try {
541 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
542 } catch (ArrayIndexOutOfBoundsException ex) {
543 Log.w(TAG, "Missing hotseat_icons array item #" + i);
544 mHotseatIcons[i] = null;
545 }
546 }
547 hotseatIconDrawables.recycle();
548 }
549
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400550 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400551 for (int i=0; i<mHotseatConfig.length; i++) {
552 Intent intent = null;
553 if (mHotseatConfig[i].equals("*BROWSER*")) {
554 // magic value meaning "launch user's default web browser"
555 // replace it with a generic web request so we can see if there is indeed a default
556 String defaultUri = getString(R.string.default_browser_url);
557 intent = new Intent(
558 Intent.ACTION_VIEW,
559 ((defaultUri != null)
560 ? Uri.parse(defaultUri)
561 : getDefaultBrowserUri())
562 ).addCategory(Intent.CATEGORY_BROWSABLE);
563 // note: if the user launches this without a default set, she
564 // will always be taken to the default URL above; this is
565 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700566 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400567 // URL is unavoidably sent to the chosen app.
568 } else {
569 try {
570 intent = Intent.parseUri(mHotseatConfig[i], 0);
571 } catch (java.net.URISyntaxException ex) {
572 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
573 // bogus; leave intent=null
574 }
575 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700576
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 if (intent == null) {
578 mHotseats[i] = null;
579 mHotseatLabels[i] = getText(R.string.activity_not_found);
580 continue;
581 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582
583 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 Log.d(TAG, "loadHotseats: hotseat " + i
585 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400587 + "]");
588 }
589
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400590 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
591 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700592 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 Log.d(TAG, "Best match for intent: " + bestMatch);
594 Log.d(TAG, "All matches: ");
595 for (ResolveInfo ri : allMatches) {
596 Log.d(TAG, " --> " + ri);
597 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400598 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400599 // did this resolve to a single app, or the resolver?
600 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700601 // can't find any activity to handle this. let's leave the
602 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400603 // to launch.
604 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400605
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400606 // set accessibility text to "Not installed"
607 mHotseatLabels[i] = getText(R.string.activity_not_found);
608 } else {
609 boolean found = false;
610 for (ResolveInfo ri : allMatches) {
611 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
612 && bestMatch.activityInfo.applicationInfo.packageName
613 .equals(ri.activityInfo.applicationInfo.packageName)) {
614 found = true;
615 break;
616 }
617 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700618
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400619 if (!found) {
620 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
621 // the bestMatch is probably the ResolveActivity, meaning the
622 // user has not yet selected a default
623 // so: we'll keep the original intent for now
624 mHotseats[i] = intent;
625
626 // set the accessibility text to "Select shortcut"
627 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
628 } else {
629 // we have an app!
630 // now reconstruct the intent to launch it through the front
631 // door
632 ComponentName com = new ComponentName(
633 bestMatch.activityInfo.applicationInfo.packageName,
634 bestMatch.activityInfo.name);
635 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
636
637 // load the app label for accessibility
638 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
639 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400640 }
641
642 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700643 Log.d(TAG, "loadHotseats: hotseat " + i
644 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400645 + ((mHotseats[i] == null)
646 ? "null"
647 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400648 + "] label=[" + mHotseatLabels[i]
649 + "]"
650 );
651 }
652 }
653 }
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 @Override
656 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700657 mWaitingForResult = false;
658
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 // The pattern used here is that a user PICKs a specific application,
660 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
663 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700664
Michael Jurka0280c3b2010-09-17 15:00:07 -0700665 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 switch (requestCode) {
667 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700668 completeAddApplication(
669 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 break;
671 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800672 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 break;
674 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 break;
677 case REQUEST_PICK_LIVE_FOLDER:
678 addLiveFolder(data);
679 break;
680 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700681 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700684 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700687 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700688 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700690 case REQUEST_PICK_WALLPAPER:
691 // We just wanted the activity result here so we can clear mWaitingForResult
692 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
Romain Guy18042c82009-11-06 11:44:55 -0800694 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
695 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
696 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697 // Clean up the appWidgetId if we canceled
698 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
699 if (appWidgetId != -1) {
700 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 }
704
705 @Override
706 protected void onResume() {
707 super.onResume();
708
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800709 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800710
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800711 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400712 mWorkspaceLoading = true;
713 mModel.startLoader(this, true);
714 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700716 // When we resume Launcher, a different Activity might be responsible for the app
717 // market intent, so refresh the icon
718 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720
721 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 protected void onPause() {
723 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 // Some launcher layouts don't have a previous and next view
725 if (mPreviousView != null) {
726 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700727 }
728 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 dismissPreview(mNextView);
730 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800731 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732 }
Romain Guycbb89e42009-06-08 15:52:54 -0700733
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700734 @Override
735 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 // Flag the loader to stop early before switching
737 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800738 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800739 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700740 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700741
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800742 // We can't hide the IME if it was forced open. So don't bother
743 /*
744 @Override
745 public void onWindowFocusChanged(boolean hasFocus) {
746 super.onWindowFocusChanged(hasFocus);
747
748 if (hasFocus) {
749 final InputMethodManager inputManager = (InputMethodManager)
750 getSystemService(Context.INPUT_METHOD_SERVICE);
751 WindowManager.LayoutParams lp = getWindow().getAttributes();
752 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
753 android.os.Handler()) {
754 protected void onReceiveResult(int resultCode, Bundle resultData) {
755 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
756 }
757 });
758 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
759 }
760 }
761 */
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 private boolean acceptFilter() {
764 final InputMethodManager inputManager = (InputMethodManager)
765 getSystemService(Context.INPUT_METHOD_SERVICE);
766 return !inputManager.isFullscreenMode();
767 }
768
769 @Override
770 public boolean onKeyDown(int keyCode, KeyEvent event) {
771 boolean handled = super.onKeyDown(keyCode, event);
772 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
773 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
774 keyCode, event);
775 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700776 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700777 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700778 // showSearchDialog()
779 // If there are multiple keystrokes before the search dialog takes focus,
780 // onSearchRequested() will be called for every keystroke,
781 // but it is idempotent, so it's fine.
782 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 }
784 }
785
Joe Onorato8a9625e2010-01-28 15:55:35 -0800786 // Eat the long press event so the keyboard doesn't come up.
787 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
788 return true;
789 }
790
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800791 return handled;
792 }
793
Karl Rosaen138a0412009-04-23 19:00:21 -0700794 private String getTypedText() {
795 return mDefaultKeySsb.toString();
796 }
797
798 private void clearTypedText() {
799 mDefaultKeySsb.clear();
800 mDefaultKeySsb.clearSpans();
801 Selection.setSelection(mDefaultKeySsb, 0);
802 }
803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 /**
805 * Restores the previous state, if it exists.
806 *
807 * @param savedState The previous state.
808 */
809 private void restoreState(Bundle savedState) {
810 if (savedState == null) {
811 return;
812 }
813
Joe Onorato3a8820b2009-11-10 15:06:42 -0800814 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
815 if (allApps) {
816 showAllApps(false);
817 }
818
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
820 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700821 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 }
823
824 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700825
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700827 mAddScreen = addScreen;
828 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
829 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 mRestoring = true;
831 }
832
833 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
834 if (renameFolder) {
835 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700836 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 mRestoring = true;
838 }
839 }
840
841 /**
842 * Finds all the views we need and configure them properly.
843 */
844 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700845 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400846
Romain Guyb1b69f52009-08-10 15:10:15 -0700847 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400848 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
Joe Onorato7c312c12009-08-13 21:36:53 -0700850 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700851 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700852 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800853 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700854 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700855 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700856
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700857 if (LauncherApplication.isScreenXLarge()) {
858 // They need to be INVISIBLE initially so that they will be measured in the layout.
859 // Otherwise the animations are messed up when we show them for the first time.
860 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
861 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
862 }
863
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
865 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500866 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700867
Joe Onorato7c312c12009-08-13 21:36:53 -0700868 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
869 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700871 View handleView = findViewById(R.id.all_apps_button);
872 if (handleView != null && handleView instanceof HandleView) {
873 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700874 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700875 mHandleView.setLauncher(this);
876 mHandleView.setOnClickListener(this);
877 mHandleView.setOnLongClickListener(this);
878 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800879
Winson Chung80baf5a2010-08-09 16:03:15 -0700880 if (mCustomizePagedView != null) {
881 mCustomizePagedView.setLauncher(this);
882 mCustomizePagedView.setDragController(dragController);
883 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700884 } else {
885 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
886 hotseatLeft.setContentDescription(mHotseatLabels[0]);
887 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
888 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
889 hotseatRight.setContentDescription(mHotseatLabels[1]);
890 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400891
Michael Jurkaaf442092010-06-10 17:01:57 -0700892 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
893 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800894
Michael Jurkaaf442092010-06-10 17:01:57 -0700895 Drawable previous = mPreviousView.getDrawable();
896 Drawable next = mNextView.getDrawable();
897 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898
Michael Jurkaaf442092010-06-10 17:01:57 -0700899 mPreviousView.setHapticFeedbackEnabled(false);
900 mPreviousView.setOnLongClickListener(this);
901 mNextView.setHapticFeedbackEnabled(false);
902 mNextView.setOnLongClickListener(this);
903 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800904
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400906 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
909 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400910 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700911 int deleteZoneHandleId;
912 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700913 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700914 } else {
915 deleteZoneHandleId = R.id.all_apps_button_cluster;
916 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700917 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700918 dragController.addDragListener(deleteZone);
919
920 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
921 if (infoButton != null) {
922 infoButton.setLauncher(this);
923 infoButton.setHandle(findViewById(R.id.configure_button));
924 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
925 dragController.addDragListener(infoButton);
926 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
Joe Onorato00acb122009-08-04 16:04:30 -0400928 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400929 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800930 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700931
Joe Onorato00acb122009-08-04 16:04:30 -0400932 // The order here is bottom to top.
933 dragController.addDropTarget(workspace);
934 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700935 if (infoButton != null) {
936 dragController.addDropTarget(infoButton);
937 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 }
939
Romain Guy8a73c512009-11-09 19:19:59 -0800940 @SuppressWarnings({"UnusedDeclaration"})
941 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700942 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800943 mWorkspace.scrollLeft();
944 }
Romain Guy8a73c512009-11-09 19:19:59 -0800945 }
946
947 @SuppressWarnings({"UnusedDeclaration"})
948 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700949 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800950 mWorkspace.scrollRight();
951 }
Romain Guy8a73c512009-11-09 19:19:59 -0800952 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400953
954 @SuppressWarnings({"UnusedDeclaration"})
955 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700956 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400957
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400958 int index = -1;
959 if (v.getId() == R.id.hotseat_left) {
960 index = 0;
961 } else if (v.getId() == R.id.hotseat_right) {
962 index = 1;
963 }
964
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400965 // reload these every tap; you never know when they might change
966 loadHotseats();
967 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
968 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400969 startActivitySafely(
970 mHotseats[index],
971 "hotseat"
972 );
973 }
974 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 /**
977 * Creates a view representing a shortcut.
978 *
979 * @param info The data structure describing the shortcut.
980 *
981 * @return A View inflated from R.layout.application.
982 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800983 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700985 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 }
987
988 /**
989 * Creates a view representing a shortcut inflated from the specified resource.
990 *
991 * @param layoutResId The id of the XML layout used to create the shortcut.
992 * @param parent The group the shortcut belongs to.
993 * @param info The data structure describing the shortcut.
994 *
995 * @return A View inflated from layoutResId.
996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
999
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1001 new FastBitmapDrawable(info.getIcon(mIconCache)),
1002 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 favorite.setText(info.title);
1004 favorite.setTag(info);
1005 favorite.setOnClickListener(this);
1006
1007 return favorite;
1008 }
1009
1010 /**
1011 * Add an application shortcut to the workspace.
1012 *
1013 * @param data The intent describing the application.
1014 * @param cellInfo The position on screen where to create the shortcut.
1015 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 void completeAddApplication(Context context, Intent data, int screen,
1017 int intersectCellX, int intersectCellY) {
1018 final int[] cellXY = mTmpAddItemCellCoordinates;
1019 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1020
1021 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1022 showOutOfSpaceMessage();
1023 return;
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
Joe Onorato0589f0f2010-02-08 13:44:00 -08001026 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1027 data, context);
1028
Romain Guy73b979d2009-06-09 12:57:21 -07001029 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001030 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001032 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001033 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1034 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001035 } else {
1036 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 }
1038 }
Romain Guycbb89e42009-06-08 15:52:54 -07001039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 /**
1041 * Add a shortcut to the workspace.
1042 *
1043 * @param data The intent describing the shortcut.
1044 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001046 private void completeAddShortcut(Intent data, int screen,
1047 int intersectCellX, int intersectCellY) {
1048 final int[] cellXY = mTmpAddItemCellCoordinates;
1049 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001050
Michael Jurka0280c3b2010-09-17 15:00:07 -07001051 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1052 showOutOfSpaceMessage();
1053 return;
1054 }
1055
1056 final ShortcutInfo info = mModel.addShortcut(
1057 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058
1059 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 }
1063 }
1064
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001067 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001069 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001070 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001071 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001072 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001074
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001075 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1077 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001080 // We have saved the position to which the widget was dragged-- this really only matters
1081 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001083
1084 // For now, we don't save the coordinate where we dropped the icon because we're not
1085 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1086 // a widget on the home screen in case we want to add it in the future
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 final int[] touchXY = null;
1088 //final int[] touchXY = mAddDropPosition;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001089 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 boolean foundCellSpan = false;
1091 if (touchXY != null) {
1092 // when dragging and dropping, just find the closest free spot
1093 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1094 int[] result = screenLayout.findNearestVacantArea(
1095 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001096 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 foundCellSpan = !findNearestVacantAreaFailed;
1098 } else {
1099 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1100 // if we long pressed on an empty cell to bring up a menu,
1101 // make sure we intersect the empty cell
1102 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1103 mAddIntersectCellX, mAddIntersectCellY);
1104 } else {
1105 // if we went through the menu -> add, just find any spot
1106 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1107 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001108 }
1109
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001111 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001113 return;
1114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 // Build Launcher-specific widget info and save to database
1117 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001118 launcherInfo.spanX = spanXY[0];
1119 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 LauncherModel.addItemToDatabase(this, launcherInfo,
1122 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001123 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124
1125 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001127
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001130
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001131 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001133
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001135 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 }
1137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 void showOutOfSpaceMessage() {
1140 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1141 }
1142
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1144 mDesktopItems.remove(launcherInfo);
1145 launcherInfo.hostView = null;
1146 }
1147
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 public LauncherAppWidgetHost getAppWidgetHost() {
1149 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001152 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001153 getWindow().closeAllPanels();
1154
1155 try {
1156 dismissDialog(DIALOG_CREATE_SHORTCUT);
1157 // Unlock the workspace if the dialog was showing
1158 } catch (Exception e) {
1159 // An exception is thrown if the dialog is not visible, which is fine
1160 }
1161
1162 try {
1163 dismissDialog(DIALOG_RENAME_FOLDER);
1164 // Unlock the workspace if the dialog was showing
1165 } catch (Exception e) {
1166 // An exception is thrown if the dialog is not visible, which is fine
1167 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001168
1169 // Whatever we were doing is hereby canceled.
1170 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001171 }
1172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 @Override
1174 protected void onNewIntent(Intent intent) {
1175 super.onNewIntent(intent);
1176
1177 // Close the menu
1178 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001179 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001180 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001181
Joe Onorato14f122b2009-11-19 14:06:36 -08001182 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1183 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001184
Michael Jurka4cb37242010-08-09 21:05:32 -07001185 // in all these cases, only animate if we're already on home
1186 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001187 mWorkspace.unshrink(alreadyOnHome);
1188 }
1189 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001190 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001191 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1192 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001193 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001194 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001195
Joe Onorato3a8820b2009-11-10 15:06:42 -08001196 final View v = getWindow().peekDecorView();
1197 if (v != null && v.getWindowToken() != null) {
1198 InputMethodManager imm = (InputMethodManager)getSystemService(
1199 INPUT_METHOD_SERVICE);
1200 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
1202 }
1203 }
1204
1205 @Override
1206 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1207 // Do not call super here
1208 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001209
1210 if (mHomeCustomizationDrawer != null) {
1211 String cur = savedInstanceState.getString("currentTab");
1212 if (cur != null) {
1213 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1214 }
1215 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
1218 @Override
1219 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001220 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
1222 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1223 if (folders.size() > 0) {
1224 final int count = folders.size();
1225 long[] ids = new long[count];
1226 for (int i = 0; i < count; i++) {
1227 final FolderInfo info = folders.get(i).getInfo();
1228 ids[i] = info.id;
1229 }
1230 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1231 } else {
1232 super.onSaveInstanceState(outState);
1233 }
1234
Joe Onoratofb0ca672009-09-14 17:55:46 -04001235 // TODO should not do this if the drawer is currently closing.
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001236 if (mState == State.ALL_APPS) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 if (mAddScreen > -1 && mWaitingForResult) {
1241 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1242 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1243 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245
1246 if (mFolderInfo != null && mWaitingForResult) {
1247 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1248 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1249 }
Michael Jurka946ad472010-07-09 18:05:18 -07001250
1251 if (mHomeCustomizationDrawer != null) {
1252 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1253 if (currentTabTag != null) {
1254 outState.putString("currentTab", currentTabTag);
1255 }
1256 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 }
1258
1259 @Override
1260 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001264 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001266 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268
1269 TextKeyListener.getInstance().release();
1270
Joe Onorato9c1289c2009-08-17 11:03:03 -04001271 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272
Joe Onorato9c1289c2009-08-17 11:03:03 -04001273 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001274
1275 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001276
Patrick Dubroyab962b72010-07-26 12:10:10 -07001277 // Some launcher layouts don't have a previous and next view
1278 if (mPreviousView != null) {
1279 dismissPreview(mPreviousView);
1280 }
1281 if (mNextView != null) {
1282 dismissPreview(mNextView);
1283 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001284
1285 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 }
1287
1288 @Override
1289 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001290 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 super.startActivityForResult(intent, requestCode);
1292 }
1293
1294 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001295 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001297
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001298 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001299
Karl Rosaen138a0412009-04-23 19:00:21 -07001300 if (initialQuery == null) {
1301 // Use any text typed in the launcher as the initial query
1302 initialQuery = getTypedText();
1303 clearTypedText();
1304 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 if (appSearchData == null) {
1306 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001307 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 }
Romain Guycbb89e42009-06-08 15:52:54 -07001309
Karl Rosaen138a0412009-04-23 19:00:21 -07001310 final SearchManager searchManager =
1311 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001312 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001313 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 @Override
1317 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001318 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 return false;
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321
1322 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1325 .setIcon(android.R.drawable.ic_menu_add)
1326 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001327 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1328 .setIcon(android.R.drawable.ic_menu_manage)
1329 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001330 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 .setIcon(android.R.drawable.ic_menu_gallery)
1332 .setAlphabeticShortcut('W');
1333 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1334 .setIcon(android.R.drawable.ic_search_category_default)
1335 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1336 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1337 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1338 .setAlphabeticShortcut('N');
1339
1340 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001341 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1342 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343
1344 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1345 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1346 .setIntent(settings);
1347
1348 return true;
1349 }
1350
1351 @Override
1352 public boolean onPrepareOptionsMenu(Menu menu) {
1353 super.onPrepareOptionsMenu(menu);
1354
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001355 // If all apps is animating, don't show the menu, because we don't know
1356 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001357 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001358 return false;
1359 }
1360
1361 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001362 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001363 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1364 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1365
1366 // Disable add if the workspace is full.
1367 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1369 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
1372 return true;
1373 }
1374
1375 @Override
1376 public boolean onOptionsItemSelected(MenuItem item) {
1377 switch (item.getItemId()) {
1378 case MENU_ADD:
1379 addItems();
1380 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001381 case MENU_MANAGE_APPS:
1382 manageApps();
1383 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 case MENU_WALLPAPER_SETTINGS:
1385 startWallpaper();
1386 return true;
1387 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001388 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 return true;
1390 case MENU_NOTIFICATIONS:
1391 showNotifications();
1392 return true;
1393 }
1394
1395 return super.onOptionsItemSelected(item);
1396 }
Romain Guycbb89e42009-06-08 15:52:54 -07001397
Karl Rosaen138a0412009-04-23 19:00:21 -07001398 /**
1399 * Indicates that we want global search for this activity by setting the globalSearch
1400 * argument for {@link #startSearch} to true.
1401 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001403 @Override
1404 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001405 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001406 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001407 }
1408
Joe Onorato9c1289c2009-08-17 11:03:03 -04001409 public boolean isWorkspaceLocked() {
1410 return mWorkspaceLoading || mWaitingForResult;
1411 }
1412
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001414 if (LauncherApplication.isScreenXLarge()) {
1415 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001416 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001417 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001418 }
1419 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001420 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001421 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424
Michael Jurka0280c3b2010-09-17 15:00:07 -07001425 private void resetAddInfo() {
1426 mAddScreen = -1;
1427 mAddIntersectCellX = -1;
1428 mAddIntersectCellY = -1;
1429 mAddDropPosition = null;
1430 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001431
Michael Jurka0280c3b2010-09-17 15:00:07 -07001432 void addAppWidgetFromDrop(ComponentName appWidgetProvider, int screen, int[] position) {
1433 resetAddInfo();
1434 mAddScreen = screen;
1435
1436 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1437 mAddDropPosition = position;
1438
Michael Jurkaaf442092010-06-10 17:01:57 -07001439 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1440 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1441 addAppWidgetImpl(appWidgetId);
1442 }
1443
Winson Chung7ad01412010-09-27 11:33:03 -07001444 private void manageApps() {
1445 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1446 }
1447
Michael Jurkaaf442092010-06-10 17:01:57 -07001448 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001449 // TODO: catch bad widget exception when sent
1450 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001451 // TODO: Is this log message meaningful?
1452 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1453 addAppWidgetImpl(appWidgetId);
1454 }
1455
1456 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001457 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458
Bjorn Bringert7984c942009-12-09 15:38:25 +00001459 if (appWidget.configure != null) {
1460 // Launch over to configure widget, if needed
1461 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1462 intent.setComponent(appWidget.configure);
1463 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1464
Romain Guy8e633c52010-03-04 12:51:36 -08001465 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001466 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001467 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 }
1470 }
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1473 resetAddInfo();
1474 mAddScreen = screen;
1475 mAddDropPosition = position;
1476
1477 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1478 createShortcutIntent.setComponent(componentName);
1479 processShortcut(createShortcutIntent);
1480 }
1481
Joe Onoratodeb98af2010-02-19 14:59:39 -08001482 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001483 // Handle case where user selected "Applications"
1484 String applicationName = getResources().getString(R.string.group_applications);
1485 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001486
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001487 if (applicationName != null && applicationName.equals(shortcutName)) {
1488 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1489 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001490
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001491 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1492 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001493 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001494 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001495 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001496 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
Winson Chung24ab2f12010-09-16 14:10:47 -07001500 void processWallpaper(Intent intent) {
1501 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1502 }
1503
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1505 resetAddInfo();
1506 mAddScreen = screen;
1507 mAddDropPosition = position;
1508
1509 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1510 createFolderIntent.setComponent(componentName);
1511
1512 addLiveFolder(createFolderIntent);
1513 }
1514
1515 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001516 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001517 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001518 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001520 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001521 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001522 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001523 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001524 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
1526
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 UserFolderInfo folderInfo = new UserFolderInfo();
1529 folderInfo.title = getText(R.string.folder_name);
1530
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1532 final int[] cellXY = mTmpAddItemCellCoordinates;
1533 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1534 showOutOfSpaceMessage();
1535 return;
1536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537
1538 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 LauncherModel.addItemToDatabase(this, folderInfo,
1540 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001541 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001542 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 // Create the view
1545 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001546 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
Romain Guycbb89e42009-06-08 15:52:54 -07001549
Joe Onorato9c1289c2009-08-17 11:03:03 -04001550 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001551 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 }
1553
Michael Jurka28750fb2010-09-24 17:43:49 -07001554 private void completeAddLiveFolder(
1555 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1557 final int[] cellXY = mTmpAddItemCellCoordinates;
1558 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1559 showOutOfSpaceMessage();
1560 return;
1561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001562
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564
1565 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001567 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001568 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 }
1570 }
1571
1572 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001573 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574
1575 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1576 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1577
1578 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 Intent.ShortcutIconResource iconResource = null;
1580
1581 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1582 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1583 try {
1584 iconResource = (Intent.ShortcutIconResource) extra;
1585 final PackageManager packageManager = context.getPackageManager();
1586 Resources resources = packageManager.getResourcesForApplication(
1587 iconResource.packageName);
1588 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1589 icon = resources.getDrawable(id);
1590 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001591 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 }
1593 }
1594
1595 if (icon == null) {
1596 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1597 }
1598
1599 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001600 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 info.title = name;
1602 info.iconResource = iconResource;
1603 info.uri = data.getData();
1604 info.baseIntent = baseIntent;
1605 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1606 LiveFolders.DISPLAY_MODE_GRID);
1607
1608 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001609 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001610 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
1612 return info;
1613 }
1614
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 private void showNotifications() {
1616 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1617 if (statusBar != null) {
1618 statusBar.expand();
1619 }
1620 }
1621
1622 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001623 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001625 Intent chooser = Intent.createChooser(pickWallpaper,
1626 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001627 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1628 // Removed in Eclair MR1
1629// WallpaperManager wm = (WallpaperManager)
1630// getSystemService(Context.WALLPAPER_SERVICE);
1631// WallpaperInfo wi = wm.getWallpaperInfo();
1632// if (wi != null && wi.getSettingsActivity() != null) {
1633// LabeledIntent li = new LabeledIntent(getPackageName(),
1634// R.string.configure_wallpaper, 0);
1635// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1636// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1637// }
Mike Clerona0618e42009-10-22 13:55:21 -07001638 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 }
1640
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001641 /**
1642 * Registers various content observers. The current implementation registers
1643 * only a favorites observer to keep track of the favorites applications.
1644 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001645 private void registerContentObservers() {
1646 ContentResolver resolver = getContentResolver();
1647 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1648 true, mWidgetObserver);
1649 }
1650
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 @Override
1652 public boolean dispatchKeyEvent(KeyEvent event) {
1653 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1654 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001656 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001657 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001658 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001659 dumpState();
1660 return true;
1661 }
1662 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001663 }
1664 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1665 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001666 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 return true;
1668 }
1669 }
1670
1671 return super.dispatchKeyEvent(event);
1672 }
1673
Joe Onorato88ec0992009-11-19 13:16:06 -08001674 @Override
1675 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001676 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1677 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001678 } else {
1679 closeFolder();
1680 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001681 // Some launcher layouts don't have a previous and next view
1682 if (mPreviousView != null) {
1683 dismissPreview(mPreviousView);
1684 dismissPreview(mNextView);
1685 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001686 }
1687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 private void closeFolder() {
1689 Folder folder = mWorkspace.getOpenFolder();
1690 if (folder != null) {
1691 closeFolder(folder);
1692 }
1693 }
1694
1695 void closeFolder(Folder folder) {
1696 folder.getInfo().opened = false;
1697 ViewGroup parent = (ViewGroup) folder.getParent();
1698 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001699 CellLayout cl = (CellLayout) parent;
1700 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001701 if (folder instanceof DropTarget) {
1702 // Live folders aren't DropTargets.
1703 mDragController.removeDropTarget((DropTarget)folder);
1704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
1706 folder.onClose();
1707 }
1708
1709 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001710 * Re-listen when widgets are reset.
1711 */
1712 private void onAppWidgetReset() {
1713 mAppWidgetHost.startListening();
1714 }
1715
1716 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001717 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1718 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001720 private void unbindDesktopItems() {
1721 for (ItemInfo item: mDesktopItems) {
1722 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 /**
1727 * Launches the intent referred by the clicked shortcut.
1728 *
1729 * @param v The view representing the clicked shortcut.
1730 */
1731 public void onClick(View v) {
1732 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001733 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001735 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001736 int[] pos = new int[2];
1737 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001738 intent.setSourceBounds(new Rect(pos[0], pos[1],
1739 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001740 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 } else if (tag instanceof FolderInfo) {
1742 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001743 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001744 if (mState == State.ALL_APPS) {
1745 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001746 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001747 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750 }
1751
Michael Jurka0e260592010-06-30 17:07:39 -07001752 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001753 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001754 // clicking anywhere on the workspace causes the customization drawer to slide down
1755 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001756 return false;
1757 }
1758
Michael Jurkaaf442092010-06-10 17:01:57 -07001759 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001760 * Event handler for the search button
1761 *
1762 * @param v The view that was clicked.
1763 */
1764 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001765 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001766 }
1767
1768 /**
1769 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001770 * enters home screen customization mode.
1771 *
1772 * @param v The view that was clicked.
1773 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001774 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001775 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001776 }
1777
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001778 /**
1779 * Event handler for the "grid" button that appears on the home screen, which
1780 * enters all apps mode.
1781 *
1782 * @param v The view that was clicked.
1783 */
1784 public void onClickAllAppsButton(View v) {
1785 showAllApps(true);
1786 }
1787
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001788 public void onClickAppMarketButton(View v) {
1789 if (mAppMarketIntent != null) {
1790 startActivitySafely(mAppMarketIntent, "app market");
1791 }
1792 }
1793
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001794 void startApplicationDetailsActivity(ComponentName componentName) {
1795 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001796 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1797 Uri.fromParts("package", packageName, null));
1798 startActivity(intent);
1799 }
1800
Patrick Dubroy5539af72010-09-07 15:22:01 -07001801 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1802 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1803 // System applications cannot be installed. For now, show a toast explaining that.
1804 // We may give them the option of disabling apps this way.
1805 int messageId = R.string.uninstall_system_app_text;
1806 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1807 } else {
1808 String packageName = appInfo.componentName.getPackageName();
1809 String className = appInfo.componentName.getClassName();
1810 Intent intent = new Intent(
1811 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1812 startActivity(intent);
1813 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001814 }
1815
Joe Onoratof984e852010-03-25 09:47:45 -07001816 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1818 try {
1819 startActivity(intent);
1820 } catch (ActivityNotFoundException e) {
1821 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001822 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001823 } catch (SecurityException e) {
1824 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001825 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001827 "or use the exported attribute for this activity. "
1828 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 }
1830 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001831
Romain Guy8e633c52010-03-04 12:51:36 -08001832 void startActivityForResultSafely(Intent intent, int requestCode) {
1833 try {
1834 startActivityForResult(intent, requestCode);
1835 } catch (ActivityNotFoundException e) {
1836 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1837 } catch (SecurityException e) {
1838 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1839 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1840 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1841 "or use the exported attribute for this activity.", e);
1842 }
1843 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844
1845 private void handleFolderClick(FolderInfo folderInfo) {
1846 if (!folderInfo.opened) {
1847 // Close any open folder
1848 closeFolder();
1849 // Open the requested folder
1850 openFolder(folderInfo);
1851 } else {
1852 // Find the open folder...
1853 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1854 int folderScreen;
1855 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001856 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857 // .. and close it
1858 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001859 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 // Close any folder open on the current screen
1861 closeFolder();
1862 // Pull the folder onto this screen
1863 openFolder(folderInfo);
1864 }
1865 }
1866 }
1867 }
1868
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869 /**
1870 * Opens the user fodler described by the specified tag. The opening of the folder
1871 * is animated relative to the specified View. If the View is null, no animation
1872 * is played.
1873 *
1874 * @param folderInfo The FolderInfo describing the folder to open.
1875 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001876 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 Folder openFolder;
1878
1879 if (folderInfo instanceof UserFolderInfo) {
1880 openFolder = UserFolder.fromXml(this);
1881 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001882 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 } else {
1884 return;
1885 }
1886
Joe Onorato00acb122009-08-04 16:04:30 -04001887 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 openFolder.setLauncher(this);
1889
1890 openFolder.bind(folderInfo);
1891 folderInfo.opened = true;
1892
Winson Chungaafa03c2010-06-11 17:34:16 -07001893 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1894
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 openFolder.onOpen();
1896 }
1897
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001899 switch (v.getId()) {
1900 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001901 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001902 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1903 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001904 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001905 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001906 return true;
1907 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001908 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001909 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1910 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001911 showPreviews(v);
1912 }
1913 return true;
1914 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001915 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001916 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1917 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1918 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001919 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001920 return true;
1921 }
1922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 return false;
1925 }
1926
1927 if (!(v instanceof CellLayout)) {
1928 v = (View) v.getParent();
1929 }
1930
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931
Michael Jurka0280c3b2010-09-17 15:00:07 -07001932 resetAddInfo();
1933 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001935 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 return true;
1937 }
1938
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939 final View itemUnderLongClick = longClickCellInfo.cell;
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001942 if (itemUnderLongClick == null) {
1943 // User long pressed on empty space
1944 mWorkspace.setAllowLongPress(false);
1945 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1946 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001947 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 }
1950 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001953 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1954 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001955 mAddIntersectCellX = longClickCellInfo.cellX;
1956 mAddIntersectCellY = longClickCellInfo.cellY;
1957 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
1959 }
1960 }
1961 return true;
1962 }
1963
Romain Guye6b8e2f2009-11-10 11:56:55 -08001964 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001965 private void dismissPreview(final View v) {
1966 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001967 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001968 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1969 public void onDismiss() {
1970 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1971 int count = group.getChildCount();
1972 for (int i = 0; i < count; i++) {
1973 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1974 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001975 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1976 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1977
1978 v.setTag(R.id.workspace, null);
1979 v.setTag(R.id.icon, null);
1980 window.setOnDismissListener(null);
1981 }
1982 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001983 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001984 }
1985 v.setTag(null);
1986 }
1987
Romain Guyf8e6a802009-12-07 17:48:02 -08001988 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001989 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001990 }
1991
Romain Guya6abce82009-11-10 02:54:41 -08001992 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001993 final Resources resources = getResources();
1994 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001995
Romain Guya6abce82009-11-10 02:54:41 -08001996 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001997
Romain Guy9d31e9f2009-11-11 18:54:28 -08001998 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001999
Romain Guyf8e6a802009-12-07 17:48:02 -08002000 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002001 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002002 int extraW = (int) ((r.left + r.right) * max);
2003 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002004
2005 int aW = cell.getWidth() - extraW;
2006 float w = aW / max;
2007
2008 int width = cell.getWidth();
2009 int height = cell.getHeight();
2010 int x = cell.getLeftPadding();
2011 int y = cell.getTopPadding();
2012 width -= (x + cell.getRightPadding());
2013 height -= (y + cell.getBottomPadding());
2014
2015 float scale = w / width;
2016
2017 int count = end - start;
2018
2019 final float sWidth = width * scale;
2020 float sHeight = height * scale;
2021
Romain Guye6b8e2f2009-11-10 11:56:55 -08002022 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002023
Romain Guye6b8e2f2009-11-10 11:56:55 -08002024 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2025 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002026
2027 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002028 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002029 cell = (CellLayout) workspace.getChildAt(i);
2030
Romain Guyf8e6a802009-12-07 17:48:02 -08002031 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002032 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002033
2034 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002035 c.scale(scale, scale);
2036 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002037 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002038
Romain Guy9d31e9f2009-11-11 18:54:28 -08002039 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002040 image.setImageBitmap(bitmap);
2041 image.setTag(i);
2042 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002043 image.setOnFocusChangeListener(handler);
2044 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002045 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002046
2047 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002048 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2049
Winson Chungaafa03c2010-06-11 17:34:16 -07002050 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002051 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002052
2053 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002054 p.setContentView(preview);
2055 p.setWidth((int) (sWidth * count + extraW));
2056 p.setHeight((int) (sHeight + extraH));
2057 p.setAnimationStyle(R.style.AnimationPreview);
2058 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002060 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002061 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002062
Romain Guye6b8e2f2009-11-10 11:56:55 -08002063 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2064 public void onDismiss() {
2065 dismissPreview(anchor);
2066 }
2067 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002068
2069 anchor.setTag(p);
2070 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002071 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002072 }
2073
Romain Guy9d31e9f2009-11-11 18:54:28 -08002074 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002075 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002076
Romain Guye6b8e2f2009-11-10 11:56:55 -08002077 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002078 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002079 }
2080
2081 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002082 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002083 v.post(this);
2084 }
2085
2086 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002087 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002088 }
2089
2090 public void onFocusChange(View v, boolean hasFocus) {
2091 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002092 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002093 }
Romain Guya6abce82009-11-10 02:54:41 -08002094 }
2095 }
2096
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 Workspace getWorkspace() {
2098 return mWorkspace;
2099 }
2100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 @Override
2102 protected Dialog onCreateDialog(int id) {
2103 switch (id) {
2104 case DIALOG_CREATE_SHORTCUT:
2105 return new CreateShortcut().createDialog();
2106 case DIALOG_RENAME_FOLDER:
2107 return new RenameFolder().createDialog();
2108 }
2109
2110 return super.onCreateDialog(id);
2111 }
2112
2113 @Override
2114 protected void onPrepareDialog(int id, Dialog dialog) {
2115 switch (id) {
2116 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 break;
2118 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002119 if (mFolderInfo != null) {
2120 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2121 final CharSequence text = mFolderInfo.title;
2122 input.setText(text);
2123 input.setSelection(0, text.length());
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 break;
2126 }
2127 }
2128
2129 void showRenameDialog(FolderInfo info) {
2130 mFolderInfo = info;
2131 mWaitingForResult = true;
2132 showDialog(DIALOG_RENAME_FOLDER);
2133 }
2134
Michael Jurka0280c3b2010-09-17 15:00:07 -07002135 private void showAddDialog(int intersectX, int intersectY) {
2136 resetAddInfo();
2137 mAddIntersectCellX = intersectX;
2138 mAddIntersectCellY = intersectY;
2139 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 mWaitingForResult = true;
2141 showDialog(DIALOG_CREATE_SHORTCUT);
2142 }
2143
Joe Onoratodeb98af2010-02-19 14:59:39 -08002144 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002145 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002146 Bundle bundle = new Bundle();
2147
2148 ArrayList<String> shortcutNames = new ArrayList<String>();
2149 shortcutNames.add(getString(R.string.group_applications));
2150 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2151
2152 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2153 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2154 R.drawable.ic_launcher_application));
2155 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2156
2157 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2158 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002159 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002160 pickIntent.putExtras(bundle);
2161
Joe Onoratodeb98af2010-02-19 14:59:39 -08002162 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002163 }
2164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002165 private class RenameFolder {
2166 private EditText mInput;
2167
2168 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2170 mInput = (EditText) layout.findViewById(R.id.folder_name);
2171
2172 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2173 builder.setIcon(0);
2174 builder.setTitle(getString(R.string.rename_folder_title));
2175 builder.setCancelable(true);
2176 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2177 public void onCancel(DialogInterface dialog) {
2178 cleanup();
2179 }
2180 });
2181 builder.setNegativeButton(getString(R.string.cancel_action),
2182 new Dialog.OnClickListener() {
2183 public void onClick(DialogInterface dialog, int which) {
2184 cleanup();
2185 }
2186 }
2187 );
2188 builder.setPositiveButton(getString(R.string.rename_action),
2189 new Dialog.OnClickListener() {
2190 public void onClick(DialogInterface dialog, int which) {
2191 changeFolderName();
2192 }
2193 }
2194 );
2195 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002196
2197 final AlertDialog dialog = builder.create();
2198 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2199 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002200 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002201 mInput.requestFocus();
2202 InputMethodManager inputManager = (InputMethodManager)
2203 getSystemService(Context.INPUT_METHOD_SERVICE);
2204 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002205 }
2206 });
2207
2208 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 }
2210
2211 private void changeFolderName() {
2212 final String name = mInput.getText().toString();
2213 if (!TextUtils.isEmpty(name)) {
2214 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002215 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 mFolderInfo.title = name;
2217 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2218
Joe Onorato9c1289c2009-08-17 11:03:03 -04002219 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002220 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002221 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222 } else {
2223 final FolderIcon folderIcon = (FolderIcon)
2224 mWorkspace.getViewForTag(mFolderInfo);
2225 if (folderIcon != null) {
2226 folderIcon.setText(name);
2227 getWorkspace().requestLayout();
2228 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002229 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002230 mWorkspaceLoading = true;
2231 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 }
2233 }
2234 }
2235 cleanup();
2236 }
2237
2238 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 dismissDialog(DIALOG_RENAME_FOLDER);
2240 mWaitingForResult = false;
2241 mFolderInfo = null;
2242 }
2243 }
2244
Daniel Sandler843e8602010-06-07 14:59:01 -04002245 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2246 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002247 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002248 }
2249
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002250 // AllAppsView.Watcher
2251 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002252 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2253 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002254 mWorkspace.setVisibility(View.GONE);
2255 }
2256 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002257
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 private void showToolbarButton(View button) {
2259 button.setAlpha(1.0f);
2260 button.setVisibility(View.VISIBLE);
2261 button.setFocusable(true);
2262 button.setClickable(true);
2263 }
2264
2265 private void hideToolbarButton(View button) {
2266 button.setAlpha(0.0f);
2267 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2268 button.setVisibility(View.INVISIBLE);
2269 button.setFocusable(false);
2270 button.setClickable(false);
2271 }
2272
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002274 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002275 *
2276 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2277 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002278 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002279 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002280 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002281 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002282 final boolean showing = show;
2283 final boolean hiding = !show;
2284
2285 final int duration = show ?
2286 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2287 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2288
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002289 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002290 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2291 anim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -07002292 anim.addListener(new AnimatorListenerAdapter() {
2293 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002294 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002295 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002296 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002297 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002298 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002299 });
2300 seq.play(anim);
2301 } else {
2302 if (showing) {
2303 showToolbarButton(view);
2304 } else {
2305 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002308 }
2309
2310 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002311 * Show/hide the appropriate toolbar buttons for newState.
2312 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002313 *
2314 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002315 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2316 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002317 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002318 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002319 final View searchButton = findViewById(R.id.search_button);
2320 final View allAppsButton = findViewById(R.id.all_apps_button);
2321 final View marketButton = findViewById(R.id.market_button);
2322 final View configureButton = findViewById(R.id.configure_button);
2323
2324 switch (newState) {
2325 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002326 hideOrShowToolbarButton(true, searchButton, showSeq);
2327 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2328 hideOrShowToolbarButton(true, configureButton, showSeq);
2329 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002330 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002331 break;
2332 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002333 hideOrShowToolbarButton(true, configureButton, showSeq);
2334 hideOrShowToolbarButton(true, marketButton, showSeq);
2335 hideOrShowToolbarButton(false, searchButton, hideSeq);
2336 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002337 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002338 break;
2339 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2341 hideOrShowToolbarButton(false, searchButton, hideSeq);
2342 hideOrShowToolbarButton(false, marketButton, hideSeq);
2343 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002344 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002345 break;
2346 }
2347 }
2348
2349 /**
2350 * Helper method for the cameraZoomIn/cameraZoomOut animations
2351 * @param view The view being animated
2352 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2353 * @param scaleFactor The scale factor used for the zoom
2354 */
2355 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2356 final int height = view.getHeight();
2357 view.setPivotX(view.getWidth() / 2.0f);
2358 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2359 // Assumes that the view is normally anchored to either the top or bottom of the screen
2360 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2361 if (state == State.ALL_APPS) {
2362 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2363 } else {
2364 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2365 }
2366 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002367
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002368 /**
2369 * Zoom the camera out from the workspace to reveal 'toView'.
2370 * Assumes that the view to show is anchored at either the very top or very bottom
2371 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002372 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002373 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002374 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002375 final Resources res = getResources();
2376 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2377 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002378 final boolean toAllApps = (toState == State.ALL_APPS);
2379 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002380
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002381 setPivotsForZoom(toView, toState, scale);
2382
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002383 if (toState == State.ALL_APPS) {
2384 mWorkspace.shrinkToBottom(animated);
2385 } else {
2386 mWorkspace.shrinkToTop(animated);
2387 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002388
2389 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002390 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2391 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2392 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2393 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002394 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002395 scaleAnim.addListener(new AnimatorListenerAdapter() {
2396 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002397 // Prepare the position
2398 toView.setTranslationX(0.0f);
2399 toView.setTranslationY(0.0f);
2400 toView.setVisibility(View.VISIBLE);
2401 }
2402 });
2403
Chet Haaseb1254a62010-09-07 13:35:00 -07002404 AnimatorSet toolbarHideAnim = new AnimatorSet();
2405 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002406 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2407
2408 // toView should appear right at the end of the workspace shrink animation
2409 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2410
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002411 if (mStateAnimation != null) mStateAnimation.cancel();
2412 mStateAnimation = new AnimatorSet();
2413 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2414 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415
2416 // Show the new toolbar buttons just as the main animation is ending
2417 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002418 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2419 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002420 } else {
2421 toView.setTranslationX(0.0f);
2422 toView.setTranslationY(0.0f);
2423 toView.setScaleX(1.0f);
2424 toView.setScaleY(1.0f);
2425 toView.setVisibility(View.VISIBLE);
2426 hideAndShowToolbarButtons(toState, null, null);
2427 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002428 }
2429
2430 /**
2431 * Zoom the camera back into the workspace, hiding 'fromView'.
2432 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002433 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2434 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002435 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002436 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002437 Resources res = getResources();
2438 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2439 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002440 final View fromView =
2441 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2442
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002443 mCustomizePagedView.endChoiceMode();
2444 mAllAppsPagedView.endChoiceMode();
2445
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002446 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002447
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002448 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002449
2450 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002451 if (mStateAnimation != null) mStateAnimation.cancel();
2452 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002453 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2454 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2455 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2456 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002457 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002458 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002459 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002460 fromView.setVisibility(View.GONE);
2461 fromView.setScaleX(1.0f);
2462 fromView.setScaleY(1.0f);
2463 }
2464 });
2465
Chet Haaseb1254a62010-09-07 13:35:00 -07002466 AnimatorSet toolbarHideAnim = new AnimatorSet();
2467 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002468 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2469
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002470 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002471
2472 // Show the new toolbar buttons at the very end of the whole animation
2473 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2474 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002475 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2476 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 } else {
2478 fromView.setVisibility(View.GONE);
2479 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2480 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002481 }
2482
2483 /**
2484 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2485 * This is the transition used on xlarge screens to go between all apps and
2486 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2488 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2489 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002490 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002491 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 final Resources res = getResources();
2493 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002494 final int workspaceHeight = mWorkspace.getHeight();
2495
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 final boolean fromAllApps = (fromState == State.ALL_APPS);
2497 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2498 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002499
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002500 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2501 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2502 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2503 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002504
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002505 mCustomizePagedView.endChoiceMode();
2506 mAllAppsPagedView.endChoiceMode();
2507
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002508 if (toState == State.ALL_APPS) {
2509 mWorkspace.shrinkToBottom(animated);
2510 } else {
2511 mWorkspace.shrinkToTop(animated);
2512 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002513
2514 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002515 if (mStateAnimation != null) mStateAnimation.cancel();
2516 mStateAnimation = new AnimatorSet();
2517 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002518 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002519 toView.setVisibility(View.VISIBLE);
2520 toView.setY(toViewStartY);
2521 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002522 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002523 fromView.setVisibility(View.GONE);
2524 }
2525 });
2526
Chet Haaseb1254a62010-09-07 13:35:00 -07002527 AnimatorSet toolbarHideAnim = new AnimatorSet();
2528 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2530
Chet Haase472b2812010-10-14 07:02:04 -07002531 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2532 fromViewStartY, fromViewEndY);
2533 fromAnim.setDuration(duration);
2534 ObjectAnimator toAnim = ObjectAnimator.ofFloat(toView, "y",
2535 toViewStartY, toViewEndY);
2536 fromAnim.setDuration(duration);
2537 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002538
2539 // Show the new toolbar buttons just as the main animation is ending
2540 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002541 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2542 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543 } else {
2544 fromView.setY(fromViewEndY);
2545 fromView.setVisibility(View.GONE);
2546 toView.setY(toViewEndY);
2547 toView.setVisibility(View.VISIBLE);
2548 hideAndShowToolbarButtons(toState, null, null);
2549 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002550 }
2551
2552 void showAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002553 if (mState == State.ALL_APPS)
Winson Chung80baf5a2010-08-09 16:03:15 -07002554 return;
2555
Michael Jurka79212d82010-07-30 16:36:20 -07002556 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002557 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002559 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002560 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002561 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002562 } else {
2563 mAllAppsGrid.zoom(1.0f, animated);
2564 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002565
Romain Guyc16fea72010-03-12 17:17:56 -08002566 ((View) mAllAppsGrid).setFocusable(true);
2567 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002568
Joe Onorato7c312c12009-08-13 21:36:53 -07002569 // TODO: fade these two too
2570 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002571 // Change the state *after* we've called all the transition code
2572 mState = State.ALL_APPS;
2573 }
2574
2575
2576 void showWorkspace(boolean animated) {
2577 showWorkspace(animated, null);
2578 }
2579
2580 void showWorkspace(boolean animated, CellLayout layout) {
2581 if (layout != null && animated) {
2582 mWorkspace.unshrink(layout);
2583 } else {
2584 mWorkspace.unshrink(animated);
2585 }
2586 if (mState == State.ALL_APPS) {
2587 closeAllApps(animated);
2588 } else if (mState == State.CUSTOMIZE) {
2589 hideCustomizationDrawer(animated);
2590 }
2591 // Change the state *after* we've called all the transition code
2592 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002593 }
2594
Joe Onoratob2061212009-11-24 16:13:54 -05002595 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002596 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002597 * - Home from workspace
2598 * - from center screen
2599 * - from other screens
2600 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002601 * - from center screen
2602 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002603 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002604 * - from center screen
2605 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002606 * - Launch app from workspace and quit
2607 * - with back
2608 * - with home
2609 * - Launch app from all apps and quit
2610 * - with back
2611 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002612 * - Go to a screen that's not the default, then all
2613 * apps, and launch and app, and go back
2614 * - with back
2615 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002616 * - On workspace, long press power and go back
2617 * - with back
2618 * - with home
2619 * - On all apps, long press power and go back
2620 * - with back
2621 * - with home
2622 * - On workspace, power off
2623 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002624 * - Launch an app and turn off the screen while in that app
2625 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002626 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002627 * - From all apps
2628 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002629 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2630 * - From all apps
2631 * - From the center workspace
2632 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002633 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002634 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002635 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002636 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002637 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002639 } else {
2640 mAllAppsGrid.zoom(0.0f, animated);
2641 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002642 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002643 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002644 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002645 }
2646
Joe Onorato7c312c12009-08-13 21:36:53 -07002647 void lockAllApps() {
2648 // TODO
2649 }
2650
2651 void unlockAllApps() {
2652 // TODO
2653 }
2654
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 // Show the customization drawer (only exists in x-large configuration)
2656 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002657 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002658 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002659 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002661 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002662 // Change the state *after* we've called all the transition code
2663 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002664 }
2665
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002666 // Hide the customization drawer (only exists in x-large configuration)
2667 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002668 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002670 }
2671 }
2672
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002673 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2674 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2675 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002676 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002677 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002678
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002679 void onWorkspaceClick(CellLayout layout) {
2680 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002681 }
2682
Michael Jurka0423dcf2010-10-05 14:56:18 -07002683 private void updateButtonWithIconFromExternalActivity(
2684 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2685 ImageView button = (ImageView) findViewById(buttonId);
2686 Drawable toolbarIcon = null;
2687 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002688 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002689 // Look for the toolbar icon specified in the activity meta-data
2690 Bundle metaData = packageManager.getActivityInfo(
2691 activityName, PackageManager.GET_META_DATA).metaData;
2692 if (metaData != null) {
2693 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2694 if (iconResId != 0) {
2695 Resources res = packageManager.getResourcesForActivity(activityName);
2696 toolbarIcon = res.getDrawable(iconResId);
2697 }
2698 }
2699 } catch (NameNotFoundException e) {
2700 // Do nothing
2701 }
2702 // If we were unable to find the icon via the meta-data, use a generic one
2703 if (toolbarIcon == null) {
2704 button.setImageResource(fallbackDrawableId);
2705 } else {
2706 button.setImageDrawable(toolbarIcon);
2707 }
2708 }
2709
2710 private void updateGlobalSearchIcon() {
2711 if (LauncherApplication.isScreenXLarge()) {
2712 final SearchManager searchManager =
2713 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2714 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002715 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002716 updateButtonWithIconFromExternalActivity(
2717 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002718 }
2719 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002720 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002721
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002722 /**
2723 * Sets the app market icon (shown when all apps is visible on x-large screens)
2724 */
2725 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002726 if (LauncherApplication.isScreenXLarge()) {
2727 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2728 // Find the app market activity by resolving an intent.
2729 // (If multiple app markets are installed, it will return the ResolverActivity.)
2730 ComponentName activityName = intent.resolveActivity(getPackageManager());
2731 if (activityName != null) {
2732 mAppMarketIntent = intent;
2733 updateButtonWithIconFromExternalActivity(
2734 R.id.market_button, activityName, R.drawable.app_market_generic);
2735 }
2736 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002737 }
2738
2739 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 * Displays the shortcut creation dialog and launches, if necessary, the
2741 * appropriate activity.
2742 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002743 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002744 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2745 DialogInterface.OnShowListener {
2746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002748
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002749 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002750 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002751
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002752 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2753 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002754 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 builder.setInverseBackgroundForced(true);
2757
2758 AlertDialog dialog = builder.create();
2759 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002760 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002761 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 return dialog;
2764 }
2765
2766 public void onCancel(DialogInterface dialog) {
2767 mWaitingForResult = false;
2768 cleanup();
2769 }
2770
Romain Guycbb89e42009-06-08 15:52:54 -07002771 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002772 }
2773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002774 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002775 try {
2776 dismissDialog(DIALOG_CREATE_SHORTCUT);
2777 } catch (Exception e) {
2778 // An exception is thrown if the dialog is not visible, which is fine
2779 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 }
2781
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002782 /**
2783 * Handle the action clicked in the "Add to home" dialog.
2784 */
2785 public void onClick(DialogInterface dialog, int which) {
2786 Resources res = getResources();
2787 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002788
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002789 switch (which) {
2790 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002791 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002792 break;
2793 }
Romain Guycbb89e42009-06-08 15:52:54 -07002794
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002795 case AddAdapter.ITEM_APPWIDGET: {
2796 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002797
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002798 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2799 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002800 // start the pick activity
2801 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2802 break;
2803 }
Romain Guycbb89e42009-06-08 15:52:54 -07002804
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002805 case AddAdapter.ITEM_LIVE_FOLDER: {
2806 // Insert extra item to handle inserting folder
2807 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002808
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002809 ArrayList<String> shortcutNames = new ArrayList<String>();
2810 shortcutNames.add(res.getString(R.string.group_folder));
2811 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002812
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002813 ArrayList<ShortcutIconResource> shortcutIcons =
2814 new ArrayList<ShortcutIconResource>();
2815 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2816 R.drawable.ic_launcher_folder));
2817 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2818
2819 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2820 pickIntent.putExtra(Intent.EXTRA_INTENT,
2821 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2822 pickIntent.putExtra(Intent.EXTRA_TITLE,
2823 getText(R.string.title_select_live_folder));
2824 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002825
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002826 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2827 break;
2828 }
2829
2830 case AddAdapter.ITEM_WALLPAPER: {
2831 startWallpaper();
2832 break;
2833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 }
2835 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002836
2837 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002838 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002839 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 }
2841
2842 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002843 * Receives notifications when applications are added/removed.
2844 */
2845 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2846 @Override
2847 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002848 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002849 String reason = intent.getStringExtra("reason");
2850 if (!"homekey".equals(reason)) {
2851 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002852 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002853 animate = false;
2854 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002855 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002856 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002857 }
2858 }
2859
2860 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002861 * Receives notifications whenever the appwidgets are reset.
2862 */
2863 private class AppWidgetResetObserver extends ContentObserver {
2864 public AppWidgetResetObserver() {
2865 super(new Handler());
2866 }
2867
2868 @Override
2869 public void onChange(boolean selfChange) {
2870 onAppWidgetReset();
2871 }
2872 }
2873
2874 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002878 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002879 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002880 } else {
2881 return SCREEN_COUNT / 2;
2882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002884
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002885 void setAllAppsPagedView(PagedView view) {
2886 mAllAppsPagedView = view;
2887 }
2888
Joe Onorato9c1289c2009-08-17 11:03:03 -04002889 /**
2890 * Refreshes the shortcuts shown on the workspace.
2891 *
2892 * Implementation of the method from LauncherModel.Callbacks.
2893 */
2894 public void startBinding() {
2895 final Workspace workspace = mWorkspace;
2896 int count = workspace.getChildCount();
2897 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002898 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002899 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2900 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002901
Joe Onorato9c1289c2009-08-17 11:03:03 -04002902 if (DEBUG_USER_INTERFACE) {
2903 android.widget.Button finishButton = new android.widget.Button(this);
2904 finishButton.setText("Finish");
2905 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2906
2907 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2908 public void onClick(View v) {
2909 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 }
2913 }
2914
2915 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 * Bind the items start-end from the list.
2917 *
2918 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2921
2922 final Workspace workspace = mWorkspace;
2923
2924 for (int i=start; i<end; i++) {
2925 final ItemInfo item = shortcuts.get(i);
2926 mDesktopItems.add(item);
2927 switch (item.itemType) {
2928 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2929 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002930 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2932 false);
2933 break;
2934 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2935 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002936 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002937 (UserFolderInfo) item);
2938 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2939 false);
2940 break;
2941 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2942 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2943 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002944 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 (LiveFolderInfo) item);
2946 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2947 false);
2948 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 }
2951
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 }
2954
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 /**
2956 * Implementation of the method from LauncherModel.Callbacks.
2957 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002958 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002959 sFolders.clear();
2960 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962
2963 /**
2964 * Add the views for a widget to the workspace.
2965 *
2966 * Implementation of the method from LauncherModel.Callbacks.
2967 */
2968 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002969 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2970 if (DEBUG_WIDGETS) {
2971 Log.d(TAG, "bindAppWidget: " + item);
2972 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 final Workspace workspace = mWorkspace;
2974
2975 final int appWidgetId = item.appWidgetId;
2976 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002977 if (DEBUG_WIDGETS) {
2978 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2979 }
2980
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2982
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2984 item.hostView.setTag(item);
2985
2986 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2987 item.cellY, item.spanX, item.spanY, false);
2988
2989 workspace.requestLayout();
2990
2991 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002992
2993 if (DEBUG_WIDGETS) {
2994 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2995 + (SystemClock.uptimeMillis()-start) + "ms");
2996 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 }
2998
2999 /**
3000 * Callback saying that there aren't any more items to bind.
3001 *
3002 * Implementation of the method from LauncherModel.Callbacks.
3003 */
3004 public void finishBindingItems() {
3005 if (mSavedState != null) {
3006 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003007 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 }
3009
3010 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3011 if (userFolders != null) {
3012 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003013 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 if (info != null) {
3015 openFolder(info);
3016 }
3017 }
3018 final Folder openFolder = mWorkspace.getOpenFolder();
3019 if (openFolder != null) {
3020 openFolder.requestFocus();
3021 }
3022 }
3023
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 mSavedState = null;
3025 }
3026
3027 if (mSavedInstanceState != null) {
3028 super.onRestoreInstanceState(mSavedInstanceState);
3029 mSavedInstanceState = null;
3030 }
3031
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 mWorkspaceLoading = false;
3033 }
3034
3035 /**
3036 * Add the icons for all apps.
3037 *
3038 * Implementation of the method from LauncherModel.Callbacks.
3039 */
3040 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003041 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003042 if (mCustomizePagedView != null) {
3043 mCustomizePagedView.setApps(apps);
3044 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003045 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003046 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 }
3048
3049 /**
3050 * A package was installed.
3051 *
3052 * Implementation of the method from LauncherModel.Callbacks.
3053 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003054 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003056 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003057 if (mCustomizePagedView != null) {
3058 mCustomizePagedView.addApps(apps);
3059 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003060 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003061 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003062 }
3063
3064 /**
3065 * A package was updated.
3066 *
3067 * Implementation of the method from LauncherModel.Callbacks.
3068 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003069 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003070 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003071 mWorkspace.updateShortcuts(apps);
3072 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003073 if (mCustomizePagedView != null) {
3074 mCustomizePagedView.updateApps(apps);
3075 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003076 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003077 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 }
3079
3080 /**
3081 * A package was uninstalled.
3082 *
3083 * Implementation of the method from LauncherModel.Callbacks.
3084 */
Joe Onorato36115782010-06-17 13:28:48 -04003085 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003087 if (permanent) {
3088 mWorkspace.removeItems(apps);
3089 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003090 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003091 if (mCustomizePagedView != null) {
3092 mCustomizePagedView.removeApps(apps);
3093 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003094 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003095 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 }
Joe Onoratobe386092009-11-17 17:32:16 -08003097
3098 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003099 * A number of packages were updated.
3100 */
3101 public void bindPackagesUpdated() {
3102 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003103 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003104 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003105 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003106 }
3107
3108 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003109 * Prints out out state for debugging.
3110 */
3111 public void dumpState() {
3112 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003113 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003114 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3115 Log.d(TAG, "mRestoring=" + mRestoring);
3116 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3117 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3118 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003119 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003120 mModel.dumpState();
3121 mAllAppsGrid.dumpState();
3122 Log.d(TAG, "END launcher2 dump state");
3123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124}