blob: 9e661917e65601f80d6e440921b103ce700b643a [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";
Michael Jurka883f55b2010-10-21 15:47:14 -0700153 // Type: int
154 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // 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 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700805 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
806 * State
807 */
808 private static State intToState(int stateOrdinal) {
809 State state = State.WORKSPACE;
810 final State[] stateValues = State.values();
811 for (int i = 0; i < stateValues.length; i++) {
812 if (stateValues[i].ordinal() == stateOrdinal) {
813 state = stateValues[i];
814 break;
815 }
816 }
817 return state;
818 }
819
820 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 * Restores the previous state, if it exists.
822 *
823 * @param savedState The previous state.
824 */
825 private void restoreState(Bundle savedState) {
826 if (savedState == null) {
827 return;
828 }
829
Michael Jurka883f55b2010-10-21 15:47:14 -0700830 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
831
832 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800833 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700834 } else if (state == State.CUSTOMIZE) {
835 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800836 }
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
839 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700840 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 }
842
843 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800845 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700846 mAddScreen = addScreen;
847 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
848 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849 mRestoring = true;
850 }
851
852 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
853 if (renameFolder) {
854 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700855 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 mRestoring = true;
857 }
858 }
859
860 /**
861 * Finds all the views we need and configure them properly.
862 */
863 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700864 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400865
Romain Guyb1b69f52009-08-10 15:10:15 -0700866 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400867 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868
Joe Onorato7c312c12009-08-13 21:36:53 -0700869 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700870 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700871 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800872 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700873 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700874 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700875
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700876 if (LauncherApplication.isScreenXLarge()) {
877 // They need to be INVISIBLE initially so that they will be measured in the layout.
878 // Otherwise the animations are messed up when we show them for the first time.
879 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
880 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
881 }
882
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
884 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500885 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700886
Joe Onorato7c312c12009-08-13 21:36:53 -0700887 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
888 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700890 View handleView = findViewById(R.id.all_apps_button);
891 if (handleView != null && handleView instanceof HandleView) {
892 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700893 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700894 mHandleView.setLauncher(this);
895 mHandleView.setOnClickListener(this);
896 mHandleView.setOnLongClickListener(this);
897 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800898
Winson Chung80baf5a2010-08-09 16:03:15 -0700899 if (mCustomizePagedView != null) {
900 mCustomizePagedView.setLauncher(this);
901 mCustomizePagedView.setDragController(dragController);
902 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700903 } else {
904 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
905 hotseatLeft.setContentDescription(mHotseatLabels[0]);
906 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
907 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
908 hotseatRight.setContentDescription(mHotseatLabels[1]);
909 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400910
Michael Jurkaaf442092010-06-10 17:01:57 -0700911 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
912 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800913
Michael Jurkaaf442092010-06-10 17:01:57 -0700914 Drawable previous = mPreviousView.getDrawable();
915 Drawable next = mNextView.getDrawable();
916 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917
Michael Jurkaaf442092010-06-10 17:01:57 -0700918 mPreviousView.setHapticFeedbackEnabled(false);
919 mPreviousView.setOnLongClickListener(this);
920 mNextView.setHapticFeedbackEnabled(false);
921 mNextView.setOnLongClickListener(this);
922 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800923
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400925 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800926 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927
928 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400929 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700930 int deleteZoneHandleId;
931 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700932 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700933 } else {
934 deleteZoneHandleId = R.id.all_apps_button_cluster;
935 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700936 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700937 dragController.addDragListener(deleteZone);
938
939 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
940 if (infoButton != null) {
941 infoButton.setLauncher(this);
942 infoButton.setHandle(findViewById(R.id.configure_button));
943 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
944 dragController.addDragListener(infoButton);
945 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
Joe Onorato00acb122009-08-04 16:04:30 -0400947 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400948 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800949 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700950
Joe Onorato00acb122009-08-04 16:04:30 -0400951 // The order here is bottom to top.
952 dragController.addDropTarget(workspace);
953 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700954 if (infoButton != null) {
955 dragController.addDropTarget(infoButton);
956 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
Romain Guy8a73c512009-11-09 19:19:59 -0800959 @SuppressWarnings({"UnusedDeclaration"})
960 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700961 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800962 mWorkspace.scrollLeft();
963 }
Romain Guy8a73c512009-11-09 19:19:59 -0800964 }
965
966 @SuppressWarnings({"UnusedDeclaration"})
967 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700968 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800969 mWorkspace.scrollRight();
970 }
Romain Guy8a73c512009-11-09 19:19:59 -0800971 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400972
973 @SuppressWarnings({"UnusedDeclaration"})
974 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700975 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400976
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400977 int index = -1;
978 if (v.getId() == R.id.hotseat_left) {
979 index = 0;
980 } else if (v.getId() == R.id.hotseat_right) {
981 index = 1;
982 }
983
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400984 // reload these every tap; you never know when they might change
985 loadHotseats();
986 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
987 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400988 startActivitySafely(
989 mHotseats[index],
990 "hotseat"
991 );
992 }
993 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700994
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 /**
996 * Creates a view representing a shortcut.
997 *
998 * @param info The data structure describing the shortcut.
999 *
1000 * @return A View inflated from R.layout.application.
1001 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001002 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001004 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006
1007 /**
1008 * Creates a view representing a shortcut inflated from the specified resource.
1009 *
1010 * @param layoutResId The id of the XML layout used to create the shortcut.
1011 * @param parent The group the shortcut belongs to.
1012 * @param info The data structure describing the shortcut.
1013 *
1014 * @return A View inflated from layoutResId.
1015 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1018
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
1020 new FastBitmapDrawable(info.getIcon(mIconCache)),
1021 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001022 favorite.setText(info.title);
1023 favorite.setTag(info);
1024 favorite.setOnClickListener(this);
1025
1026 return favorite;
1027 }
1028
1029 /**
1030 * Add an application shortcut to the workspace.
1031 *
1032 * @param data The intent describing the application.
1033 * @param cellInfo The position on screen where to create the shortcut.
1034 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001035 void completeAddApplication(Context context, Intent data, int screen,
1036 int intersectCellX, int intersectCellY) {
1037 final int[] cellXY = mTmpAddItemCellCoordinates;
1038 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1039
1040 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1041 showOutOfSpaceMessage();
1042 return;
1043 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044
Joe Onorato0589f0f2010-02-08 13:44:00 -08001045 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1046 data, context);
1047
Romain Guy73b979d2009-06-09 12:57:21 -07001048 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001049 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001051 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1053 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001054 } else {
1055 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 }
1057 }
Romain Guycbb89e42009-06-08 15:52:54 -07001058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 /**
1060 * Add a shortcut to the workspace.
1061 *
1062 * @param data The intent describing the shortcut.
1063 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001064 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001065 private void completeAddShortcut(Intent data, int screen,
1066 int intersectCellX, int intersectCellY) {
1067 final int[] cellXY = mTmpAddItemCellCoordinates;
1068 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Michael Jurka0280c3b2010-09-17 15:00:07 -07001070 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1071 showOutOfSpaceMessage();
1072 return;
1073 }
1074
1075 final ShortcutInfo info = mModel.addShortcut(
1076 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
1078 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001080 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082 }
1083
Romain Guycbb89e42009-06-08 15:52:54 -07001084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001088 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001095 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1096 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001098 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001099 // We have saved the position to which the widget was dragged-- this really only matters
1100 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001102
1103 // For now, we don't save the coordinate where we dropped the icon because we're not
1104 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1105 // a widget on the home screen in case we want to add it in the future
Michael Jurka0280c3b2010-09-17 15:00:07 -07001106 final int[] touchXY = null;
1107 //final int[] touchXY = mAddDropPosition;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001108 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001109 boolean foundCellSpan = false;
1110 if (touchXY != null) {
1111 // when dragging and dropping, just find the closest free spot
1112 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1113 int[] result = screenLayout.findNearestVacantArea(
1114 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001115 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001116 foundCellSpan = !findNearestVacantAreaFailed;
1117 } else {
1118 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1119 // if we long pressed on an empty cell to bring up a menu,
1120 // make sure we intersect the empty cell
1121 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1122 mAddIntersectCellX, mAddIntersectCellY);
1123 } else {
1124 // if we went through the menu -> add, just find any spot
1125 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1126 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001127 }
1128
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001130 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001132 return;
1133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001135 // Build Launcher-specific widget info and save to database
1136 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001137 launcherInfo.spanX = spanXY[0];
1138 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 LauncherModel.addItemToDatabase(this, launcherInfo,
1141 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001142 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143
1144 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001145 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001149
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001150 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001152
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001154 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156 }
Romain Guycbb89e42009-06-08 15:52:54 -07001157
Michael Jurka0280c3b2010-09-17 15:00:07 -07001158 void showOutOfSpaceMessage() {
1159 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1160 }
1161
Joe Onorato9c1289c2009-08-17 11:03:03 -04001162 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1163 mDesktopItems.remove(launcherInfo);
1164 launcherInfo.hostView = null;
1165 }
1166
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001167 public LauncherAppWidgetHost getAppWidgetHost() {
1168 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
Romain Guycbb89e42009-06-08 15:52:54 -07001170
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001171 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001172 getWindow().closeAllPanels();
1173
1174 try {
1175 dismissDialog(DIALOG_CREATE_SHORTCUT);
1176 // Unlock the workspace if the dialog was showing
1177 } catch (Exception e) {
1178 // An exception is thrown if the dialog is not visible, which is fine
1179 }
1180
1181 try {
1182 dismissDialog(DIALOG_RENAME_FOLDER);
1183 // Unlock the workspace if the dialog was showing
1184 } catch (Exception e) {
1185 // An exception is thrown if the dialog is not visible, which is fine
1186 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001187
1188 // Whatever we were doing is hereby canceled.
1189 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001190 }
1191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 @Override
1193 protected void onNewIntent(Intent intent) {
1194 super.onNewIntent(intent);
1195
1196 // Close the menu
1197 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001198 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001199 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001200
Joe Onorato14f122b2009-11-19 14:06:36 -08001201 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1202 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001203
Michael Jurka4cb37242010-08-09 21:05:32 -07001204 // in all these cases, only animate if we're already on home
1205 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001206 mWorkspace.unshrink(alreadyOnHome);
1207 }
1208 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001209 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001210 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1211 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001212 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001213 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001214
Joe Onorato3a8820b2009-11-10 15:06:42 -08001215 final View v = getWindow().peekDecorView();
1216 if (v != null && v.getWindowToken() != null) {
1217 InputMethodManager imm = (InputMethodManager)getSystemService(
1218 INPUT_METHOD_SERVICE);
1219 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220 }
1221 }
1222 }
1223
1224 @Override
1225 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1226 // Do not call super here
1227 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001228
1229 if (mHomeCustomizationDrawer != null) {
1230 String cur = savedInstanceState.getString("currentTab");
1231 if (cur != null) {
1232 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1233 }
1234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235 }
1236
1237 @Override
1238 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001239 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240
1241 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1242 if (folders.size() > 0) {
1243 final int count = folders.size();
1244 long[] ids = new long[count];
1245 for (int i = 0; i < count; i++) {
1246 final FolderInfo info = folders.get(i).getInfo();
1247 ids[i] = info.id;
1248 }
1249 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1250 } else {
1251 super.onSaveInstanceState(outState);
1252 }
1253
Michael Jurka883f55b2010-10-21 15:47:14 -07001254 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
Michael Jurka0280c3b2010-09-17 15:00:07 -07001256 if (mAddScreen > -1 && mWaitingForResult) {
1257 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1258 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1259 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
1262 if (mFolderInfo != null && mWaitingForResult) {
1263 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1264 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1265 }
Michael Jurka946ad472010-07-09 18:05:18 -07001266
1267 if (mHomeCustomizationDrawer != null) {
1268 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1269 if (currentTabTag != null) {
1270 outState.putString("currentTab", currentTabTag);
1271 }
1272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274
1275 @Override
1276 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001280 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001282 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
1285 TextKeyListener.getInstance().release();
1286
Joe Onorato9c1289c2009-08-17 11:03:03 -04001287 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288
Joe Onorato9c1289c2009-08-17 11:03:03 -04001289 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001290
1291 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001292
Patrick Dubroyab962b72010-07-26 12:10:10 -07001293 // Some launcher layouts don't have a previous and next view
1294 if (mPreviousView != null) {
1295 dismissPreview(mPreviousView);
1296 }
1297 if (mNextView != null) {
1298 dismissPreview(mNextView);
1299 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001300
1301 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303
1304 @Override
1305 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001306 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 super.startActivityForResult(intent, requestCode);
1308 }
1309
1310 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001311 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001313
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001314 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Karl Rosaen138a0412009-04-23 19:00:21 -07001316 if (initialQuery == null) {
1317 // Use any text typed in the launcher as the initial query
1318 initialQuery = getTypedText();
1319 clearTypedText();
1320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 if (appSearchData == null) {
1322 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001323 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Karl Rosaen138a0412009-04-23 19:00:21 -07001326 final SearchManager searchManager =
1327 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001328 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001329 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
1332 @Override
1333 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001334 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001335 return false;
1336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337
1338 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001339
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1341 .setIcon(android.R.drawable.ic_menu_add)
1342 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001343 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1344 .setIcon(android.R.drawable.ic_menu_manage)
1345 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001346 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 .setIcon(android.R.drawable.ic_menu_gallery)
1348 .setAlphabeticShortcut('W');
1349 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1350 .setIcon(android.R.drawable.ic_search_category_default)
1351 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1352 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1353 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1354 .setAlphabeticShortcut('N');
1355
1356 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001357 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1358 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359
1360 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1361 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1362 .setIntent(settings);
1363
1364 return true;
1365 }
1366
1367 @Override
1368 public boolean onPrepareOptionsMenu(Menu menu) {
1369 super.onPrepareOptionsMenu(menu);
1370
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001371 // If all apps is animating, don't show the menu, because we don't know
1372 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001373 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001374 return false;
1375 }
1376
1377 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001378 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001379 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1380 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1381
1382 // Disable add if the workspace is full.
1383 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1385 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387
1388 return true;
1389 }
1390
1391 @Override
1392 public boolean onOptionsItemSelected(MenuItem item) {
1393 switch (item.getItemId()) {
1394 case MENU_ADD:
1395 addItems();
1396 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001397 case MENU_MANAGE_APPS:
1398 manageApps();
1399 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 case MENU_WALLPAPER_SETTINGS:
1401 startWallpaper();
1402 return true;
1403 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001404 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 return true;
1406 case MENU_NOTIFICATIONS:
1407 showNotifications();
1408 return true;
1409 }
1410
1411 return super.onOptionsItemSelected(item);
1412 }
Romain Guycbb89e42009-06-08 15:52:54 -07001413
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 /**
1415 * Indicates that we want global search for this activity by setting the globalSearch
1416 * argument for {@link #startSearch} to true.
1417 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001419 @Override
1420 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001421 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001422 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001423 }
1424
Joe Onorato9c1289c2009-08-17 11:03:03 -04001425 public boolean isWorkspaceLocked() {
1426 return mWorkspaceLoading || mWaitingForResult;
1427 }
1428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001430 if (LauncherApplication.isScreenXLarge()) {
1431 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001432 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001433 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001434 }
1435 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001436 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001437 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001438 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440
Michael Jurka0280c3b2010-09-17 15:00:07 -07001441 private void resetAddInfo() {
1442 mAddScreen = -1;
1443 mAddIntersectCellX = -1;
1444 mAddIntersectCellY = -1;
1445 mAddDropPosition = null;
1446 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001447
Michael Jurka0280c3b2010-09-17 15:00:07 -07001448 void addAppWidgetFromDrop(ComponentName appWidgetProvider, int screen, int[] position) {
1449 resetAddInfo();
1450 mAddScreen = screen;
1451
1452 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1453 mAddDropPosition = position;
1454
Michael Jurkaaf442092010-06-10 17:01:57 -07001455 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1456 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1457 addAppWidgetImpl(appWidgetId);
1458 }
1459
Winson Chung7ad01412010-09-27 11:33:03 -07001460 private void manageApps() {
1461 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1462 }
1463
Michael Jurkaaf442092010-06-10 17:01:57 -07001464 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001465 // TODO: catch bad widget exception when sent
1466 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001467 // TODO: Is this log message meaningful?
1468 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1469 addAppWidgetImpl(appWidgetId);
1470 }
1471
1472 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001473 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474
Bjorn Bringert7984c942009-12-09 15:38:25 +00001475 if (appWidget.configure != null) {
1476 // Launch over to configure widget, if needed
1477 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1478 intent.setComponent(appWidget.configure);
1479 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1480
Romain Guy8e633c52010-03-04 12:51:36 -08001481 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001483 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001484 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486 }
Romain Guycbb89e42009-06-08 15:52:54 -07001487
Michael Jurka0280c3b2010-09-17 15:00:07 -07001488 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1489 resetAddInfo();
1490 mAddScreen = screen;
1491 mAddDropPosition = position;
1492
1493 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1494 createShortcutIntent.setComponent(componentName);
1495 processShortcut(createShortcutIntent);
1496 }
1497
Joe Onoratodeb98af2010-02-19 14:59:39 -08001498 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001499 // Handle case where user selected "Applications"
1500 String applicationName = getResources().getString(R.string.group_applications);
1501 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001503 if (applicationName != null && applicationName.equals(shortcutName)) {
1504 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1505 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001507 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1508 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001509 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001510 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001511 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001512 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
Winson Chung24ab2f12010-09-16 14:10:47 -07001516 void processWallpaper(Intent intent) {
1517 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1518 }
1519
Michael Jurka0280c3b2010-09-17 15:00:07 -07001520 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1521 resetAddInfo();
1522 mAddScreen = screen;
1523 mAddDropPosition = position;
1524
1525 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1526 createFolderIntent.setComponent(componentName);
1527
1528 addLiveFolder(createFolderIntent);
1529 }
1530
1531 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001532 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001533 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001534 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001535
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001536 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001537 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001538 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001539 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
Michael Jurka0280c3b2010-09-17 15:00:07 -07001543 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 UserFolderInfo folderInfo = new UserFolderInfo();
1545 folderInfo.title = getText(R.string.folder_name);
1546
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1548 final int[] cellXY = mTmpAddItemCellCoordinates;
1549 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1550 showOutOfSpaceMessage();
1551 return;
1552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001555 LauncherModel.addItemToDatabase(this, folderInfo,
1556 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001558 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559
1560 // Create the view
1561 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001562 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), folderInfo);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 }
Romain Guycbb89e42009-06-08 15:52:54 -07001565
Joe Onorato9c1289c2009-08-17 11:03:03 -04001566 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001567 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001568 }
1569
Michael Jurka28750fb2010-09-24 17:43:49 -07001570 private void completeAddLiveFolder(
1571 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1573 final int[] cellXY = mTmpAddItemCellCoordinates;
1574 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1575 showOutOfSpaceMessage();
1576 return;
1577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
Michael Jurka0280c3b2010-09-17 15:00:07 -07001579 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001583 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586 }
1587
1588 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001589 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590
1591 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1592 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1593
1594 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 Intent.ShortcutIconResource iconResource = null;
1596
1597 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1598 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1599 try {
1600 iconResource = (Intent.ShortcutIconResource) extra;
1601 final PackageManager packageManager = context.getPackageManager();
1602 Resources resources = packageManager.getResourcesForApplication(
1603 iconResource.packageName);
1604 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1605 icon = resources.getDrawable(id);
1606 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001607 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 }
1609 }
1610
1611 if (icon == null) {
1612 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1613 }
1614
1615 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001616 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 info.title = name;
1618 info.iconResource = iconResource;
1619 info.uri = data.getData();
1620 info.baseIntent = baseIntent;
1621 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1622 LiveFolders.DISPLAY_MODE_GRID);
1623
1624 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001626 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627
1628 return info;
1629 }
1630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 private void showNotifications() {
1632 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1633 if (statusBar != null) {
1634 statusBar.expand();
1635 }
1636 }
1637
1638 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001639 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001641 Intent chooser = Intent.createChooser(pickWallpaper,
1642 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001643 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1644 // Removed in Eclair MR1
1645// WallpaperManager wm = (WallpaperManager)
1646// getSystemService(Context.WALLPAPER_SERVICE);
1647// WallpaperInfo wi = wm.getWallpaperInfo();
1648// if (wi != null && wi.getSettingsActivity() != null) {
1649// LabeledIntent li = new LabeledIntent(getPackageName(),
1650// R.string.configure_wallpaper, 0);
1651// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1652// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1653// }
Mike Clerona0618e42009-10-22 13:55:21 -07001654 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
1656
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001657 /**
1658 * Registers various content observers. The current implementation registers
1659 * only a favorites observer to keep track of the favorites applications.
1660 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001661 private void registerContentObservers() {
1662 ContentResolver resolver = getContentResolver();
1663 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1664 true, mWidgetObserver);
1665 }
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 @Override
1668 public boolean dispatchKeyEvent(KeyEvent event) {
1669 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1670 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001672 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001673 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001674 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001675 dumpState();
1676 return true;
1677 }
1678 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001679 }
1680 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1681 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001682 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 return true;
1684 }
1685 }
1686
1687 return super.dispatchKeyEvent(event);
1688 }
1689
Joe Onorato88ec0992009-11-19 13:16:06 -08001690 @Override
1691 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001692 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1693 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001694 } else {
1695 closeFolder();
1696 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001697 // Some launcher layouts don't have a previous and next view
1698 if (mPreviousView != null) {
1699 dismissPreview(mPreviousView);
1700 dismissPreview(mNextView);
1701 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001702 }
1703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 private void closeFolder() {
1705 Folder folder = mWorkspace.getOpenFolder();
1706 if (folder != null) {
1707 closeFolder(folder);
1708 }
1709 }
1710
1711 void closeFolder(Folder folder) {
1712 folder.getInfo().opened = false;
1713 ViewGroup parent = (ViewGroup) folder.getParent();
1714 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001715 CellLayout cl = (CellLayout) parent;
1716 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001717 if (folder instanceof DropTarget) {
1718 // Live folders aren't DropTargets.
1719 mDragController.removeDropTarget((DropTarget)folder);
1720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 }
1722 folder.onClose();
1723 }
1724
1725 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001726 * Re-listen when widgets are reset.
1727 */
1728 private void onAppWidgetReset() {
1729 mAppWidgetHost.startListening();
1730 }
1731
1732 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001733 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1734 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001736 private void unbindDesktopItems() {
1737 for (ItemInfo item: mDesktopItems) {
1738 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 }
1740 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001741
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 /**
1743 * Launches the intent referred by the clicked shortcut.
1744 *
1745 * @param v The view representing the clicked shortcut.
1746 */
1747 public void onClick(View v) {
1748 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001749 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001751 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001752 int[] pos = new int[2];
1753 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001754 intent.setSourceBounds(new Rect(pos[0], pos[1],
1755 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001756 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 } else if (tag instanceof FolderInfo) {
1758 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001759 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001760 if (mState == State.ALL_APPS) {
1761 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001762 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001763 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
1766 }
1767
Michael Jurka0e260592010-06-30 17:07:39 -07001768 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001769 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001770 // clicking anywhere on the workspace causes the customization drawer to slide down
1771 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001772 return false;
1773 }
1774
Michael Jurkaaf442092010-06-10 17:01:57 -07001775 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001776 * Event handler for the search button
1777 *
1778 * @param v The view that was clicked.
1779 */
1780 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001781 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001782 }
1783
1784 /**
1785 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001786 * enters home screen customization mode.
1787 *
1788 * @param v The view that was clicked.
1789 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001790 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001791 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001792 }
1793
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001794 /**
1795 * Event handler for the "grid" button that appears on the home screen, which
1796 * enters all apps mode.
1797 *
1798 * @param v The view that was clicked.
1799 */
1800 public void onClickAllAppsButton(View v) {
1801 showAllApps(true);
1802 }
1803
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001804 public void onClickAppMarketButton(View v) {
1805 if (mAppMarketIntent != null) {
1806 startActivitySafely(mAppMarketIntent, "app market");
1807 }
1808 }
1809
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001810 void startApplicationDetailsActivity(ComponentName componentName) {
1811 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001812 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1813 Uri.fromParts("package", packageName, null));
1814 startActivity(intent);
1815 }
1816
Patrick Dubroy5539af72010-09-07 15:22:01 -07001817 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1818 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1819 // System applications cannot be installed. For now, show a toast explaining that.
1820 // We may give them the option of disabling apps this way.
1821 int messageId = R.string.uninstall_system_app_text;
1822 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1823 } else {
1824 String packageName = appInfo.componentName.getPackageName();
1825 String className = appInfo.componentName.getClassName();
1826 Intent intent = new Intent(
1827 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1828 startActivity(intent);
1829 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001830 }
1831
Joe Onoratof984e852010-03-25 09:47:45 -07001832 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001833 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1834 try {
1835 startActivity(intent);
1836 } catch (ActivityNotFoundException e) {
1837 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001838 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001839 } catch (SecurityException e) {
1840 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001841 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001843 "or use the exported attribute for this activity. "
1844 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
1846 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001847
Romain Guy8e633c52010-03-04 12:51:36 -08001848 void startActivityForResultSafely(Intent intent, int requestCode) {
1849 try {
1850 startActivityForResult(intent, requestCode);
1851 } catch (ActivityNotFoundException e) {
1852 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1853 } catch (SecurityException e) {
1854 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1855 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1856 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1857 "or use the exported attribute for this activity.", e);
1858 }
1859 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860
1861 private void handleFolderClick(FolderInfo folderInfo) {
1862 if (!folderInfo.opened) {
1863 // Close any open folder
1864 closeFolder();
1865 // Open the requested folder
1866 openFolder(folderInfo);
1867 } else {
1868 // Find the open folder...
1869 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1870 int folderScreen;
1871 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001872 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 // .. and close it
1874 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001875 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 // Close any folder open on the current screen
1877 closeFolder();
1878 // Pull the folder onto this screen
1879 openFolder(folderInfo);
1880 }
1881 }
1882 }
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 /**
1886 * Opens the user fodler described by the specified tag. The opening of the folder
1887 * is animated relative to the specified View. If the View is null, no animation
1888 * is played.
1889 *
1890 * @param folderInfo The FolderInfo describing the folder to open.
1891 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001892 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 Folder openFolder;
1894
1895 if (folderInfo instanceof UserFolderInfo) {
1896 openFolder = UserFolder.fromXml(this);
1897 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001898 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } else {
1900 return;
1901 }
1902
Joe Onorato00acb122009-08-04 16:04:30 -04001903 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 openFolder.setLauncher(this);
1905
1906 openFolder.bind(folderInfo);
1907 folderInfo.opened = true;
1908
Winson Chungaafa03c2010-06-11 17:34:16 -07001909 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1910
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 openFolder.onOpen();
1912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001915 switch (v.getId()) {
1916 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001917 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001918 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1919 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001920 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001921 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001922 return true;
1923 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001924 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08001925 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1926 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001927 showPreviews(v);
1928 }
1929 return true;
1930 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001931 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001932 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1933 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1934 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001935 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001936 return true;
1937 }
1938
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 return false;
1941 }
1942
1943 if (!(v instanceof CellLayout)) {
1944 v = (View) v.getParent();
1945 }
1946
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 resetAddInfo();
1949 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 return true;
1953 }
1954
Michael Jurka0280c3b2010-09-17 15:00:07 -07001955 final View itemUnderLongClick = longClickCellInfo.cell;
1956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001958 if (itemUnderLongClick == null) {
1959 // User long pressed on empty space
1960 mWorkspace.setAllowLongPress(false);
1961 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1962 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07001963 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001964 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 }
1966 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001967 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001968 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001969 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1970 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001971 mAddIntersectCellX = longClickCellInfo.cellX;
1972 mAddIntersectCellY = longClickCellInfo.cellY;
1973 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975 }
1976 }
1977 return true;
1978 }
1979
Romain Guye6b8e2f2009-11-10 11:56:55 -08001980 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001981 private void dismissPreview(final View v) {
1982 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001983 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001984 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1985 public void onDismiss() {
1986 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1987 int count = group.getChildCount();
1988 for (int i = 0; i < count; i++) {
1989 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1990 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001991 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1992 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1993
1994 v.setTag(R.id.workspace, null);
1995 v.setTag(R.id.icon, null);
1996 window.setOnDismissListener(null);
1997 }
1998 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001999 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002000 }
2001 v.setTag(null);
2002 }
2003
Romain Guyf8e6a802009-12-07 17:48:02 -08002004 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002005 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002006 }
2007
Romain Guya6abce82009-11-10 02:54:41 -08002008 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002009 final Resources resources = getResources();
2010 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002011
Romain Guya6abce82009-11-10 02:54:41 -08002012 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002013
Romain Guy9d31e9f2009-11-11 18:54:28 -08002014 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002015
Romain Guyf8e6a802009-12-07 17:48:02 -08002016 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002017 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002018 int extraW = (int) ((r.left + r.right) * max);
2019 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002020
2021 int aW = cell.getWidth() - extraW;
2022 float w = aW / max;
2023
2024 int width = cell.getWidth();
2025 int height = cell.getHeight();
2026 int x = cell.getLeftPadding();
2027 int y = cell.getTopPadding();
2028 width -= (x + cell.getRightPadding());
2029 height -= (y + cell.getBottomPadding());
2030
2031 float scale = w / width;
2032
2033 int count = end - start;
2034
2035 final float sWidth = width * scale;
2036 float sHeight = height * scale;
2037
Romain Guye6b8e2f2009-11-10 11:56:55 -08002038 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002039
Romain Guye6b8e2f2009-11-10 11:56:55 -08002040 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2041 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002042
2043 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002044 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002045 cell = (CellLayout) workspace.getChildAt(i);
2046
Romain Guyf8e6a802009-12-07 17:48:02 -08002047 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002048 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002049
2050 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002051 c.scale(scale, scale);
2052 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002053 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002054
Romain Guy9d31e9f2009-11-11 18:54:28 -08002055 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002056 image.setImageBitmap(bitmap);
2057 image.setTag(i);
2058 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002059 image.setOnFocusChangeListener(handler);
2060 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002061 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002062
2063 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002064 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2065
Winson Chungaafa03c2010-06-11 17:34:16 -07002066 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002067 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002068
2069 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002070 p.setContentView(preview);
2071 p.setWidth((int) (sWidth * count + extraW));
2072 p.setHeight((int) (sHeight + extraH));
2073 p.setAnimationStyle(R.style.AnimationPreview);
2074 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002075 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002076 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002077 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002078
Romain Guye6b8e2f2009-11-10 11:56:55 -08002079 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2080 public void onDismiss() {
2081 dismissPreview(anchor);
2082 }
2083 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002084
2085 anchor.setTag(p);
2086 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002087 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002088 }
2089
Romain Guy9d31e9f2009-11-11 18:54:28 -08002090 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002091 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002092
Romain Guye6b8e2f2009-11-10 11:56:55 -08002093 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002094 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002095 }
2096
2097 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002098 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002099 v.post(this);
2100 }
2101
2102 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002103 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002104 }
2105
2106 public void onFocusChange(View v, boolean hasFocus) {
2107 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002108 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002109 }
Romain Guya6abce82009-11-10 02:54:41 -08002110 }
2111 }
2112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 Workspace getWorkspace() {
2114 return mWorkspace;
2115 }
2116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 @Override
2118 protected Dialog onCreateDialog(int id) {
2119 switch (id) {
2120 case DIALOG_CREATE_SHORTCUT:
2121 return new CreateShortcut().createDialog();
2122 case DIALOG_RENAME_FOLDER:
2123 return new RenameFolder().createDialog();
2124 }
2125
2126 return super.onCreateDialog(id);
2127 }
2128
2129 @Override
2130 protected void onPrepareDialog(int id, Dialog dialog) {
2131 switch (id) {
2132 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133 break;
2134 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002135 if (mFolderInfo != null) {
2136 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2137 final CharSequence text = mFolderInfo.title;
2138 input.setText(text);
2139 input.setSelection(0, text.length());
2140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 break;
2142 }
2143 }
2144
2145 void showRenameDialog(FolderInfo info) {
2146 mFolderInfo = info;
2147 mWaitingForResult = true;
2148 showDialog(DIALOG_RENAME_FOLDER);
2149 }
2150
Michael Jurka0280c3b2010-09-17 15:00:07 -07002151 private void showAddDialog(int intersectX, int intersectY) {
2152 resetAddInfo();
2153 mAddIntersectCellX = intersectX;
2154 mAddIntersectCellY = intersectY;
2155 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002156 mWaitingForResult = true;
2157 showDialog(DIALOG_CREATE_SHORTCUT);
2158 }
2159
Joe Onoratodeb98af2010-02-19 14:59:39 -08002160 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002161 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002162 Bundle bundle = new Bundle();
2163
2164 ArrayList<String> shortcutNames = new ArrayList<String>();
2165 shortcutNames.add(getString(R.string.group_applications));
2166 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2167
2168 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2169 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2170 R.drawable.ic_launcher_application));
2171 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2172
2173 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2174 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002175 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002176 pickIntent.putExtras(bundle);
2177
Joe Onoratodeb98af2010-02-19 14:59:39 -08002178 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002179 }
2180
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 private class RenameFolder {
2182 private EditText mInput;
2183
2184 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2186 mInput = (EditText) layout.findViewById(R.id.folder_name);
2187
2188 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2189 builder.setIcon(0);
2190 builder.setTitle(getString(R.string.rename_folder_title));
2191 builder.setCancelable(true);
2192 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2193 public void onCancel(DialogInterface dialog) {
2194 cleanup();
2195 }
2196 });
2197 builder.setNegativeButton(getString(R.string.cancel_action),
2198 new Dialog.OnClickListener() {
2199 public void onClick(DialogInterface dialog, int which) {
2200 cleanup();
2201 }
2202 }
2203 );
2204 builder.setPositiveButton(getString(R.string.rename_action),
2205 new Dialog.OnClickListener() {
2206 public void onClick(DialogInterface dialog, int which) {
2207 changeFolderName();
2208 }
2209 }
2210 );
2211 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002212
2213 final AlertDialog dialog = builder.create();
2214 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2215 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002216 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002217 mInput.requestFocus();
2218 InputMethodManager inputManager = (InputMethodManager)
2219 getSystemService(Context.INPUT_METHOD_SERVICE);
2220 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002221 }
2222 });
2223
2224 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 }
2226
2227 private void changeFolderName() {
2228 final String name = mInput.getText().toString();
2229 if (!TextUtils.isEmpty(name)) {
2230 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002231 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232 mFolderInfo.title = name;
2233 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2234
Joe Onorato9c1289c2009-08-17 11:03:03 -04002235 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002236 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002237 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002238 } else {
2239 final FolderIcon folderIcon = (FolderIcon)
2240 mWorkspace.getViewForTag(mFolderInfo);
2241 if (folderIcon != null) {
2242 folderIcon.setText(name);
2243 getWorkspace().requestLayout();
2244 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002245 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002246 mWorkspaceLoading = true;
2247 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
2250 }
2251 cleanup();
2252 }
2253
2254 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 dismissDialog(DIALOG_RENAME_FOLDER);
2256 mWaitingForResult = false;
2257 mFolderInfo = null;
2258 }
2259 }
2260
Daniel Sandler843e8602010-06-07 14:59:01 -04002261 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2262 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002263 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002264 }
2265
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002266 // AllAppsView.Watcher
2267 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002268 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2269 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002270 mWorkspace.setVisibility(View.GONE);
2271 }
2272 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002273
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002274 private void showToolbarButton(View button) {
2275 button.setAlpha(1.0f);
2276 button.setVisibility(View.VISIBLE);
2277 button.setFocusable(true);
2278 button.setClickable(true);
2279 }
2280
2281 private void hideToolbarButton(View button) {
2282 button.setAlpha(0.0f);
2283 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2284 button.setVisibility(View.INVISIBLE);
2285 button.setFocusable(false);
2286 button.setClickable(false);
2287 }
2288
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002289 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002290 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 *
2292 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2293 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002294 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002295 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002296 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002297 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002298 final boolean showing = show;
2299 final boolean hiding = !show;
2300
2301 final int duration = show ?
2302 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2303 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2304
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002305 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002306 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2307 anim.setDuration(duration);
Chet Haaseb1254a62010-09-07 13:35:00 -07002308 anim.addListener(new AnimatorListenerAdapter() {
2309 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002310 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002312 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002313 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002314 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002315 });
2316 seq.play(anim);
2317 } else {
2318 if (showing) {
2319 showToolbarButton(view);
2320 } else {
2321 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002322 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002323 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002324 }
2325
2326 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002327 * Show/hide the appropriate toolbar buttons for newState.
2328 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002329 *
2330 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002331 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2332 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002333 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002334 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002335 final View searchButton = findViewById(R.id.search_button);
2336 final View allAppsButton = findViewById(R.id.all_apps_button);
2337 final View marketButton = findViewById(R.id.market_button);
2338 final View configureButton = findViewById(R.id.configure_button);
2339
2340 switch (newState) {
2341 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002342 hideOrShowToolbarButton(true, searchButton, showSeq);
2343 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2344 hideOrShowToolbarButton(true, configureButton, showSeq);
2345 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002346 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 break;
2348 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002349 hideOrShowToolbarButton(true, configureButton, showSeq);
2350 hideOrShowToolbarButton(true, marketButton, showSeq);
2351 hideOrShowToolbarButton(false, searchButton, hideSeq);
2352 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002353 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002354 break;
2355 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2357 hideOrShowToolbarButton(false, searchButton, hideSeq);
2358 hideOrShowToolbarButton(false, marketButton, hideSeq);
2359 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002360 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002361 break;
2362 }
2363 }
2364
2365 /**
2366 * Helper method for the cameraZoomIn/cameraZoomOut animations
2367 * @param view The view being animated
2368 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2369 * @param scaleFactor The scale factor used for the zoom
2370 */
2371 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2372 final int height = view.getHeight();
2373 view.setPivotX(view.getWidth() / 2.0f);
2374 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2375 // Assumes that the view is normally anchored to either the top or bottom of the screen
2376 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2377 if (state == State.ALL_APPS) {
2378 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2379 } else {
2380 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2381 }
2382 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002383
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002384 /**
2385 * Zoom the camera out from the workspace to reveal 'toView'.
2386 * Assumes that the view to show is anchored at either the very top or very bottom
2387 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002388 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002389 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002390 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002391 final Resources res = getResources();
2392 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2393 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002394 final boolean toAllApps = (toState == State.ALL_APPS);
2395 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002396
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002397 setPivotsForZoom(toView, toState, scale);
2398
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002399 if (toState == State.ALL_APPS) {
2400 mWorkspace.shrinkToBottom(animated);
2401 } else {
2402 mWorkspace.shrinkToTop(animated);
2403 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002404
2405 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002406 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2407 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2408 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2409 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002410 scaleAnim.setInterpolator(new DecelerateInterpolator());
Chet Haaseb1254a62010-09-07 13:35:00 -07002411 scaleAnim.addListener(new AnimatorListenerAdapter() {
2412 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002413 // Prepare the position
2414 toView.setTranslationX(0.0f);
2415 toView.setTranslationY(0.0f);
2416 toView.setVisibility(View.VISIBLE);
2417 }
2418 });
2419
Chet Haaseb1254a62010-09-07 13:35:00 -07002420 AnimatorSet toolbarHideAnim = new AnimatorSet();
2421 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2423
2424 // toView should appear right at the end of the workspace shrink animation
2425 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2426
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002427 if (mStateAnimation != null) mStateAnimation.cancel();
2428 mStateAnimation = new AnimatorSet();
2429 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2430 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431
2432 // Show the new toolbar buttons just as the main animation is ending
2433 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002434 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2435 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002436 } else {
2437 toView.setTranslationX(0.0f);
2438 toView.setTranslationY(0.0f);
2439 toView.setScaleX(1.0f);
2440 toView.setScaleY(1.0f);
2441 toView.setVisibility(View.VISIBLE);
2442 hideAndShowToolbarButtons(toState, null, null);
2443 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002444 }
2445
2446 /**
2447 * Zoom the camera back into the workspace, hiding 'fromView'.
2448 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002449 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2450 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002452 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453 Resources res = getResources();
2454 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2455 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456 final View fromView =
2457 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2458
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002459 mCustomizePagedView.endChoiceMode();
2460 mAllAppsPagedView.endChoiceMode();
2461
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002462 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002463
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002465
2466 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002467 if (mStateAnimation != null) mStateAnimation.cancel();
2468 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002469 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2470 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2471 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2472 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002474 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002475 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002476 fromView.setVisibility(View.GONE);
2477 fromView.setScaleX(1.0f);
2478 fromView.setScaleY(1.0f);
2479 }
2480 });
2481
Chet Haaseb1254a62010-09-07 13:35:00 -07002482 AnimatorSet toolbarHideAnim = new AnimatorSet();
2483 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002484 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2485
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002486 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002487
2488 // Show the new toolbar buttons at the very end of the whole animation
2489 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2490 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002491 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2492 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 } else {
2494 fromView.setVisibility(View.GONE);
2495 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2496 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002497 }
2498
2499 /**
2500 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2501 * This is the transition used on xlarge screens to go between all apps and
2502 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002503 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2504 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2505 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002506 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002507 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002508 final Resources res = getResources();
2509 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002510 final int workspaceHeight = mWorkspace.getHeight();
2511
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002512 final boolean fromAllApps = (fromState == State.ALL_APPS);
2513 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2514 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002515
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002516 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2517 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2518 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2519 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002520
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002521 mCustomizePagedView.endChoiceMode();
2522 mAllAppsPagedView.endChoiceMode();
2523
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002524 if (toState == State.ALL_APPS) {
2525 mWorkspace.shrinkToBottom(animated);
2526 } else {
2527 mWorkspace.shrinkToTop(animated);
2528 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002529
2530 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002531 if (mStateAnimation != null) mStateAnimation.cancel();
2532 mStateAnimation = new AnimatorSet();
2533 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Chet Haaseb1254a62010-09-07 13:35:00 -07002534 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002535 toView.setVisibility(View.VISIBLE);
2536 toView.setY(toViewStartY);
2537 }
Chet Haaseb1254a62010-09-07 13:35:00 -07002538 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002539 fromView.setVisibility(View.GONE);
2540 }
2541 });
2542
Chet Haaseb1254a62010-09-07 13:35:00 -07002543 AnimatorSet toolbarHideAnim = new AnimatorSet();
2544 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002545 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2546
Chet Haase472b2812010-10-14 07:02:04 -07002547 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2548 fromViewStartY, fromViewEndY);
2549 fromAnim.setDuration(duration);
2550 ObjectAnimator toAnim = ObjectAnimator.ofFloat(toView, "y",
2551 toViewStartY, toViewEndY);
2552 fromAnim.setDuration(duration);
2553 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554
2555 // Show the new toolbar buttons just as the main animation is ending
2556 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002557 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2558 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002559 } else {
2560 fromView.setY(fromViewEndY);
2561 fromView.setVisibility(View.GONE);
2562 toView.setY(toViewEndY);
2563 toView.setVisibility(View.VISIBLE);
2564 hideAndShowToolbarButtons(toState, null, null);
2565 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002566 }
2567
2568 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002569 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002570 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002571 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002572
Michael Jurka79212d82010-07-30 16:36:20 -07002573 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002574 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002576 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002577 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002578 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002579 } else {
2580 mAllAppsGrid.zoom(1.0f, animated);
2581 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002582
Romain Guyc16fea72010-03-12 17:17:56 -08002583 ((View) mAllAppsGrid).setFocusable(true);
2584 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002585
Joe Onorato7c312c12009-08-13 21:36:53 -07002586 // TODO: fade these two too
2587 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002588 // Change the state *after* we've called all the transition code
2589 mState = State.ALL_APPS;
2590 }
2591
2592
2593 void showWorkspace(boolean animated) {
2594 showWorkspace(animated, null);
2595 }
2596
2597 void showWorkspace(boolean animated, CellLayout layout) {
2598 if (layout != null && animated) {
2599 mWorkspace.unshrink(layout);
2600 } else {
2601 mWorkspace.unshrink(animated);
2602 }
2603 if (mState == State.ALL_APPS) {
2604 closeAllApps(animated);
2605 } else if (mState == State.CUSTOMIZE) {
2606 hideCustomizationDrawer(animated);
2607 }
2608 // Change the state *after* we've called all the transition code
2609 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002610 }
2611
Joe Onoratob2061212009-11-24 16:13:54 -05002612 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002613 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002614 * - Home from workspace
2615 * - from center screen
2616 * - from other screens
2617 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002618 * - from center screen
2619 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002620 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002621 * - from center screen
2622 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002623 * - Launch app from workspace and quit
2624 * - with back
2625 * - with home
2626 * - Launch app from all apps and quit
2627 * - with back
2628 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002629 * - Go to a screen that's not the default, then all
2630 * apps, and launch and app, and go back
2631 * - with back
2632 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002633 * - On workspace, long press power and go back
2634 * - with back
2635 * - with home
2636 * - On all apps, long press power and go back
2637 * - with back
2638 * - with home
2639 * - On workspace, power off
2640 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002641 * - Launch an app and turn off the screen while in that app
2642 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002643 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002644 * - From all apps
2645 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002646 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2647 * - From all apps
2648 * - From the center workspace
2649 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002650 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002651 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002652 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002653 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002656 } else {
2657 mAllAppsGrid.zoom(0.0f, animated);
2658 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002659 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002660 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002661 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002662 }
2663
Joe Onorato7c312c12009-08-13 21:36:53 -07002664 void lockAllApps() {
2665 // TODO
2666 }
2667
2668 void unlockAllApps() {
2669 // TODO
2670 }
2671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 // Show the customization drawer (only exists in x-large configuration)
2673 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002674 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002675 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002676 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002678 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002679 // Change the state *after* we've called all the transition code
2680 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002681 }
2682
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 // Hide the customization drawer (only exists in x-large configuration)
2684 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002685 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002687 }
2688 }
2689
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002690 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2691 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2692 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002693 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002694 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002695
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002696 void onWorkspaceClick(CellLayout layout) {
2697 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002698 }
2699
Michael Jurka0423dcf2010-10-05 14:56:18 -07002700 private void updateButtonWithIconFromExternalActivity(
2701 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2702 ImageView button = (ImageView) findViewById(buttonId);
2703 Drawable toolbarIcon = null;
2704 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002705 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002706 // Look for the toolbar icon specified in the activity meta-data
2707 Bundle metaData = packageManager.getActivityInfo(
2708 activityName, PackageManager.GET_META_DATA).metaData;
2709 if (metaData != null) {
2710 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2711 if (iconResId != 0) {
2712 Resources res = packageManager.getResourcesForActivity(activityName);
2713 toolbarIcon = res.getDrawable(iconResId);
2714 }
2715 }
2716 } catch (NameNotFoundException e) {
2717 // Do nothing
2718 }
2719 // If we were unable to find the icon via the meta-data, use a generic one
2720 if (toolbarIcon == null) {
2721 button.setImageResource(fallbackDrawableId);
2722 } else {
2723 button.setImageDrawable(toolbarIcon);
2724 }
2725 }
2726
2727 private void updateGlobalSearchIcon() {
2728 if (LauncherApplication.isScreenXLarge()) {
2729 final SearchManager searchManager =
2730 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2731 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002732 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002733 updateButtonWithIconFromExternalActivity(
2734 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002735 }
2736 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002737 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002738
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002739 /**
2740 * Sets the app market icon (shown when all apps is visible on x-large screens)
2741 */
2742 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002743 if (LauncherApplication.isScreenXLarge()) {
2744 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2745 // Find the app market activity by resolving an intent.
2746 // (If multiple app markets are installed, it will return the ResolverActivity.)
2747 ComponentName activityName = intent.resolveActivity(getPackageManager());
2748 if (activityName != null) {
2749 mAppMarketIntent = intent;
2750 updateButtonWithIconFromExternalActivity(
2751 R.id.market_button, activityName, R.drawable.app_market_generic);
2752 }
2753 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002754 }
2755
2756 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002757 * Displays the shortcut creation dialog and launches, if necessary, the
2758 * appropriate activity.
2759 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002760 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002761 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2762 DialogInterface.OnShowListener {
2763
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002764 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002765
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002767 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2770 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002771 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002773 builder.setInverseBackgroundForced(true);
2774
2775 AlertDialog dialog = builder.create();
2776 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002777 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002778 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 return dialog;
2781 }
2782
2783 public void onCancel(DialogInterface dialog) {
2784 mWaitingForResult = false;
2785 cleanup();
2786 }
2787
Romain Guycbb89e42009-06-08 15:52:54 -07002788 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002789 }
2790
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002792 try {
2793 dismissDialog(DIALOG_CREATE_SHORTCUT);
2794 } catch (Exception e) {
2795 // An exception is thrown if the dialog is not visible, which is fine
2796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002797 }
2798
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002799 /**
2800 * Handle the action clicked in the "Add to home" dialog.
2801 */
2802 public void onClick(DialogInterface dialog, int which) {
2803 Resources res = getResources();
2804 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002805
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002806 switch (which) {
2807 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002808 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002809 break;
2810 }
Romain Guycbb89e42009-06-08 15:52:54 -07002811
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002812 case AddAdapter.ITEM_APPWIDGET: {
2813 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002814
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002815 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2816 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002817 // start the pick activity
2818 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2819 break;
2820 }
Romain Guycbb89e42009-06-08 15:52:54 -07002821
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002822 case AddAdapter.ITEM_LIVE_FOLDER: {
2823 // Insert extra item to handle inserting folder
2824 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002825
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002826 ArrayList<String> shortcutNames = new ArrayList<String>();
2827 shortcutNames.add(res.getString(R.string.group_folder));
2828 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002829
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002830 ArrayList<ShortcutIconResource> shortcutIcons =
2831 new ArrayList<ShortcutIconResource>();
2832 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2833 R.drawable.ic_launcher_folder));
2834 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2835
2836 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2837 pickIntent.putExtra(Intent.EXTRA_INTENT,
2838 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2839 pickIntent.putExtra(Intent.EXTRA_TITLE,
2840 getText(R.string.title_select_live_folder));
2841 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002842
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002843 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2844 break;
2845 }
2846
2847 case AddAdapter.ITEM_WALLPAPER: {
2848 startWallpaper();
2849 break;
2850 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 }
2852 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002853
2854 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002855 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002856 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 }
2858
2859 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002860 * Receives notifications when applications are added/removed.
2861 */
2862 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2863 @Override
2864 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002865 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002866 String reason = intent.getStringExtra("reason");
2867 if (!"homekey".equals(reason)) {
2868 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002869 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002870 animate = false;
2871 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002872 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002873 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002874 }
2875 }
2876
2877 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002878 * Receives notifications whenever the appwidgets are reset.
2879 */
2880 private class AppWidgetResetObserver extends ContentObserver {
2881 public AppWidgetResetObserver() {
2882 super(new Handler());
2883 }
2884
2885 @Override
2886 public void onChange(boolean selfChange) {
2887 onAppWidgetReset();
2888 }
2889 }
2890
2891 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002895 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002896 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002897 } else {
2898 return SCREEN_COUNT / 2;
2899 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002901
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002902 void setAllAppsPagedView(PagedView view) {
2903 mAllAppsPagedView = view;
2904 }
2905
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 /**
2907 * Refreshes the shortcuts shown on the workspace.
2908 *
2909 * Implementation of the method from LauncherModel.Callbacks.
2910 */
2911 public void startBinding() {
2912 final Workspace workspace = mWorkspace;
2913 int count = workspace.getChildCount();
2914 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002915 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2917 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002918
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 if (DEBUG_USER_INTERFACE) {
2920 android.widget.Button finishButton = new android.widget.Button(this);
2921 finishButton.setText("Finish");
2922 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2923
2924 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2925 public void onClick(View v) {
2926 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002927 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 }
2930 }
2931
2932 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002933 * Bind the items start-end from the list.
2934 *
2935 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002937 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2938
2939 final Workspace workspace = mWorkspace;
2940
2941 for (int i=start; i<end; i++) {
2942 final ItemInfo item = shortcuts.get(i);
2943 mDesktopItems.add(item);
2944 switch (item.itemType) {
2945 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2946 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002947 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2949 false);
2950 break;
2951 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2952 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002953 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 (UserFolderInfo) item);
2955 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2956 false);
2957 break;
2958 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2959 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2960 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002961 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04002962 (LiveFolderInfo) item);
2963 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2964 false);
2965 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
2968
Joe Onorato9c1289c2009-08-17 11:03:03 -04002969 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002970 }
2971
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 /**
2973 * Implementation of the method from LauncherModel.Callbacks.
2974 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002975 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002976 sFolders.clear();
2977 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979
2980 /**
2981 * Add the views for a widget to the workspace.
2982 *
2983 * Implementation of the method from LauncherModel.Callbacks.
2984 */
2985 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002986 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2987 if (DEBUG_WIDGETS) {
2988 Log.d(TAG, "bindAppWidget: " + item);
2989 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002990 final Workspace workspace = mWorkspace;
2991
2992 final int appWidgetId = item.appWidgetId;
2993 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002994 if (DEBUG_WIDGETS) {
2995 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2996 }
2997
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2999
Joe Onorato9c1289c2009-08-17 11:03:03 -04003000 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3001 item.hostView.setTag(item);
3002
3003 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3004 item.cellY, item.spanX, item.spanY, false);
3005
3006 workspace.requestLayout();
3007
3008 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003009
3010 if (DEBUG_WIDGETS) {
3011 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3012 + (SystemClock.uptimeMillis()-start) + "ms");
3013 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003014 }
3015
3016 /**
3017 * Callback saying that there aren't any more items to bind.
3018 *
3019 * Implementation of the method from LauncherModel.Callbacks.
3020 */
3021 public void finishBindingItems() {
3022 if (mSavedState != null) {
3023 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003024 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003025 }
3026
3027 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3028 if (userFolders != null) {
3029 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003030 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 if (info != null) {
3032 openFolder(info);
3033 }
3034 }
3035 final Folder openFolder = mWorkspace.getOpenFolder();
3036 if (openFolder != null) {
3037 openFolder.requestFocus();
3038 }
3039 }
3040
Joe Onorato9c1289c2009-08-17 11:03:03 -04003041 mSavedState = null;
3042 }
3043
3044 if (mSavedInstanceState != null) {
3045 super.onRestoreInstanceState(mSavedInstanceState);
3046 mSavedInstanceState = null;
3047 }
3048
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 mWorkspaceLoading = false;
3050 }
3051
3052 /**
3053 * Add the icons for all apps.
3054 *
3055 * Implementation of the method from LauncherModel.Callbacks.
3056 */
3057 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003058 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003059 if (mCustomizePagedView != null) {
3060 mCustomizePagedView.setApps(apps);
3061 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003062 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003063 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 }
3065
3066 /**
3067 * A package was installed.
3068 *
3069 * Implementation of the method from LauncherModel.Callbacks.
3070 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003071 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003073 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003074 if (mCustomizePagedView != null) {
3075 mCustomizePagedView.addApps(apps);
3076 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003077 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003078 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 }
3080
3081 /**
3082 * A package was updated.
3083 *
3084 * Implementation of the method from LauncherModel.Callbacks.
3085 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003086 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003088 mWorkspace.updateShortcuts(apps);
3089 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003090 if (mCustomizePagedView != null) {
3091 mCustomizePagedView.updateApps(apps);
3092 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003093 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003094 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 }
3096
3097 /**
3098 * A package was uninstalled.
3099 *
3100 * Implementation of the method from LauncherModel.Callbacks.
3101 */
Joe Onorato36115782010-06-17 13:28:48 -04003102 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003104 if (permanent) {
3105 mWorkspace.removeItems(apps);
3106 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003107 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003108 if (mCustomizePagedView != null) {
3109 mCustomizePagedView.removeApps(apps);
3110 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003111 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003112 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 }
Joe Onoratobe386092009-11-17 17:32:16 -08003114
3115 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003116 * A number of packages were updated.
3117 */
3118 public void bindPackagesUpdated() {
3119 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003120 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003121 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003122 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003123 }
3124
3125 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003126 * Prints out out state for debugging.
3127 */
3128 public void dumpState() {
3129 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003130 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003131 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3132 Log.d(TAG, "mRestoring=" + mRestoring);
3133 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3134 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3135 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003136 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003137 mModel.dumpState();
3138 mAllAppsGrid.dumpState();
3139 Log.d(TAG, "END launcher2 dump state");
3140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003141}