blob: 2a2d3643db81512daadddb50764a4f8fc0bbe5dc [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
Winson Chung68846fd2010-10-29 11:00:27 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070030import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070031import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070033import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.Dialog;
36import android.app.SearchManager;
37import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070038import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070050import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070051import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070054import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070055import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080063import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040065import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020068import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080069import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070070import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070074import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080076import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.text.Selection;
78import android.text.SpannableStringBuilder;
79import android.text.TextUtils;
80import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070081import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080083import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.KeyEvent;
85import android.view.LayoutInflater;
86import android.view.Menu;
87import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070088import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.View;
90import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070091import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070092import android.view.View.OnLongClickListener;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070093import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070096import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080097import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070098import android.widget.ImageView;
99import android.widget.LinearLayout;
100import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700101import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700104import android.widget.TabHost.OnTabChangeListener;
105import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700106
Winson Chung68846fd2010-10-29 11:00:27 -0700107import com.android.common.Search;
108import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800109
Adam Cohended9f8d2010-11-03 13:25:16 -0700110
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111/**
112 * Default launcher application.
113 */
Michael Jurka946ad472010-07-09 18:05:18 -0700114public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700115 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
116 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800117 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700118 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
Joe Onorato9c1289c2009-08-17 11:03:03 -0400120 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400121 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400122 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700123
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800125 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
126
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700128 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
129 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
131 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700132 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
135 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
139 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700140 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700141 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Joe Onorato7c312c12009-08-13 21:36:53 -0700148 static final int DIALOG_CREATE_SHORTCUT = 1;
149 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Romain Guy98d01652009-06-30 16:21:04 -0700151 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
153 // Type: int
154 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700155 // Type: int
156 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: long
158 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
163 // Type: int
164 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Winson Chung80baf5a2010-08-09 16:03:15 -0700170 // tags for the customization tabs
171 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700172 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700173 private static final String SHORTCUTS_TAG = "shortcuts";
174 private static final String WALLPAPERS_TAG = "wallpapers";
175
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700176 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
177
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700178 /** The different states that Launcher can be in. */
179 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700180 private State mState = State.WORKSPACE;
181 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700182
Joe Onorato9c1289c2009-08-17 11:03:03 -0400183 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800186 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800188 private final BroadcastReceiver mCloseSystemDialogsReceiver
189 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800190 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
191
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private LayoutInflater mInflater;
193
Joe Onorato00acb122009-08-04 16:04:30 -0400194 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700196
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700197 private AppWidgetManager mAppWidgetManager;
198 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700199
Michael Jurka0280c3b2010-09-17 15:00:07 -0700200 private int mAddScreen = -1;
201 private int mAddIntersectCellX = -1;
202 private int mAddIntersectCellY = -1;
203 private int[] mAddDropPosition;
204 private int[] mTmpAddItemCellCoordinates = new int[2];
205
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private FolderInfo mFolderInfo;
207
Joe Onorato7c312c12009-08-13 21:36:53 -0700208 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700209 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700210 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700211 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700212 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700213
214 private PagedView mAllAppsPagedView = null;
215 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217 private Bundle mSavedState;
218
219 private SpannableStringBuilder mDefaultKeySsb = null;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private boolean mWorkspaceLoading = true;
222
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800223 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private boolean mRestoring;
225 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700226 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800227
228 private Bundle mSavedInstanceState;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800231 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800232 private boolean mUserPresent = true;
233 private boolean mVisible = false;
234 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static LocaleConfiguration sLocaleConfiguration = null;
237
Romain Guy84f296c2009-11-04 15:00:44 -0800238 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700239
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700240 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700241
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 @Override
263 protected void onCreate(Bundle savedInstanceState) {
264 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700265
Winson Chungaafa03c2010-06-11 17:34:16 -0700266 if (LauncherApplication.isInPlaceRotationEnabled()) {
267 // hide the status bar (temporary until we get the status bar design figured out)
268 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
269 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
270 }
271
Joe Onorato0589f0f2010-02-08 13:44:00 -0800272 LauncherApplication app = ((LauncherApplication)getApplication());
273 mModel = app.setLauncher(this);
274 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400275 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700277
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700279 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
280 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200283 android.os.Debug.startMethodTracing(
284 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
286
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400287 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 checkForLocaleChange();
289 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700291 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
292 if (mHomeCustomizationDrawer != null) {
293 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700294
Winson Chung80baf5a2010-08-09 16:03:15 -0700295 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700296 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
297 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700298 TabContentFactory contentFactory = new TabContentFactory() {
299 public View createTabContent(String tag) {
300 return mCustomizePagedView;
301 }
302 };
303
Michael Jurka946ad472010-07-09 18:05:18 -0700304 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700305 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
306 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700307 String applicationsLabel = getString(R.string.applications_tab_label);
308 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
309 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700310 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700311 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
312 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700313 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
314 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
315 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
317 public void onTabChanged(String tabId) {
318 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800319 final Resources res = getResources();
320 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700322 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700324 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700325 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700326 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700327 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
329 if (tag == WIDGETS_TAG) {
330 mCustomizePagedView.setCustomizationFilter(
331 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700332 } else if (tag == APPLICATIONS_TAG) {
333 mCustomizePagedView.setCustomizationFilter(
334 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 } else if (tag == WALLPAPERS_TAG) {
336 mCustomizePagedView.setCustomizationFilter(
337 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700338 } else if (tag == SHORTCUTS_TAG) {
339 mCustomizePagedView.setCustomizationFilter(
340 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 }
342
343 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700344 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700345 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700346 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 alphaAnim.start();
348 }
349 });
350 alphaAnim.start();
351 }
352 });
Michael Jurka946ad472010-07-09 18:05:18 -0700353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 setupViews();
355
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800356 registerContentObservers();
357
Joe Onorato7c312c12009-08-13 21:36:53 -0700358 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mSavedState = savedInstanceState;
361 restoreState(mSavedState);
362
363 if (PROFILE_STARTUP) {
364 android.os.Debug.stopMethodTracing();
365 }
366
367 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
371 // For handling default keys
372 mDefaultKeySsb = new SpannableStringBuilder();
373 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800374
375 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
376 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
Romain Guycbb89e42009-06-08 15:52:54 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400417 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418
419 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
420 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700421 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 public void run() {
423 writeConfiguration(Launcher.this, localeConfiguration);
424 }
425 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700426 }
427 }
428
429 private static class LocaleConfiguration {
430 public String locale;
431 public int mcc = -1;
432 public int mnc = -1;
433 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700434
Romain Guy98d01652009-06-30 16:21:04 -0700435 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
436 DataInputStream in = null;
437 try {
438 in = new DataInputStream(context.openFileInput(PREFERENCES));
439 configuration.locale = in.readUTF();
440 configuration.mcc = in.readInt();
441 configuration.mnc = in.readInt();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 // Ignore
446 } finally {
447 if (in != null) {
448 try {
449 in.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
454 }
455 }
456
457 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
458 DataOutputStream out = null;
459 try {
460 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
461 out.writeUTF(configuration.locale);
462 out.writeInt(configuration.mcc);
463 out.writeInt(configuration.mnc);
464 out.flush();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 //noinspection ResultOfMethodCallIgnored
469 context.getFileStreamPath(PREFERENCES).delete();
470 } finally {
471 if (out != null) {
472 try {
473 out.close();
474 } catch (IOException e) {
475 // Ignore
476 }
477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479 }
480
481 static int getScreen() {
482 synchronized (sLock) {
483 return sScreen;
484 }
485 }
486
487 static void setScreen(int screen) {
488 synchronized (sLock) {
489 sScreen = screen;
490 }
491 }
492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700494 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495
496 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700497 // TODO: Put back when we decide about scrolling the wallpaper
498 // boolean isPortrait = display.getWidth() < display.getHeight();
499 // final int width = isPortrait ? display.getWidth() : display.getHeight();
500 // final int height = isPortrait ? display.getHeight() : display.getWidth();
501 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
502 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 }
504
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 // Note: This doesn't do all the client-id magic that BrowserProvider does
506 // in Browser. (http://b/2425179)
507 private Uri getDefaultBrowserUri() {
508 String url = getString(R.string.default_browser_url);
509 if (url.indexOf("{CID}") != -1) {
510 url = url.replace("{CID}", "android-google");
511 }
512 return Uri.parse(url);
513 }
514
515 // Load the Intent templates from arrays.xml to populate the hotseats. For
516 // each Intent, if it resolves to a single app, use that as the launch
517 // intent & use that app's label as the contentDescription. Otherwise,
518 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400519 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 if (mHotseatConfig == null) {
521 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
522 if (mHotseatConfig.length > 0) {
523 mHotseats = new Intent[mHotseatConfig.length];
524 mHotseatLabels = new CharSequence[mHotseatConfig.length];
525 mHotseatIcons = new Drawable[mHotseatConfig.length];
526 } else {
527 mHotseats = null;
528 mHotseatIcons = null;
529 mHotseatLabels = null;
530 }
531
532 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
533 for (int i=0; i<mHotseatConfig.length; i++) {
534 // load icon for this slot; currently unrelated to the actual activity
535 try {
536 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
537 } catch (ArrayIndexOutOfBoundsException ex) {
538 Log.w(TAG, "Missing hotseat_icons array item #" + i);
539 mHotseatIcons[i] = null;
540 }
541 }
542 hotseatIconDrawables.recycle();
543 }
544
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 for (int i=0; i<mHotseatConfig.length; i++) {
547 Intent intent = null;
548 if (mHotseatConfig[i].equals("*BROWSER*")) {
549 // magic value meaning "launch user's default web browser"
550 // replace it with a generic web request so we can see if there is indeed a default
551 String defaultUri = getString(R.string.default_browser_url);
552 intent = new Intent(
553 Intent.ACTION_VIEW,
554 ((defaultUri != null)
555 ? Uri.parse(defaultUri)
556 : getDefaultBrowserUri())
557 ).addCategory(Intent.CATEGORY_BROWSABLE);
558 // note: if the user launches this without a default set, she
559 // will always be taken to the default URL above; this is
560 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 // URL is unavoidably sent to the chosen app.
563 } else {
564 try {
565 intent = Intent.parseUri(mHotseatConfig[i], 0);
566 } catch (java.net.URISyntaxException ex) {
567 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
568 // bogus; leave intent=null
569 }
570 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700571
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 if (intent == null) {
573 mHotseats[i] = null;
574 mHotseatLabels[i] = getText(R.string.activity_not_found);
575 continue;
576 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400577
578 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700579 Log.d(TAG, "loadHotseats: hotseat " + i
580 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582 + "]");
583 }
584
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
586 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700587 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 Log.d(TAG, "Best match for intent: " + bestMatch);
589 Log.d(TAG, "All matches: ");
590 for (ResolveInfo ri : allMatches) {
591 Log.d(TAG, " --> " + ri);
592 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400593 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 // did this resolve to a single app, or the resolver?
595 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 // can't find any activity to handle this. let's leave the
597 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400598 // to launch.
599 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400600
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 // set accessibility text to "Not installed"
602 mHotseatLabels[i] = getText(R.string.activity_not_found);
603 } else {
604 boolean found = false;
605 for (ResolveInfo ri : allMatches) {
606 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
607 && bestMatch.activityInfo.applicationInfo.packageName
608 .equals(ri.activityInfo.applicationInfo.packageName)) {
609 found = true;
610 break;
611 }
612 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700613
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400614 if (!found) {
615 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
616 // the bestMatch is probably the ResolveActivity, meaning the
617 // user has not yet selected a default
618 // so: we'll keep the original intent for now
619 mHotseats[i] = intent;
620
621 // set the accessibility text to "Select shortcut"
622 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
623 } else {
624 // we have an app!
625 // now reconstruct the intent to launch it through the front
626 // door
627 ComponentName com = new ComponentName(
628 bestMatch.activityInfo.applicationInfo.packageName,
629 bestMatch.activityInfo.name);
630 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
631
632 // load the app label for accessibility
633 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
634 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400635 }
636
637 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 Log.d(TAG, "loadHotseats: hotseat " + i
639 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400640 + ((mHotseats[i] == null)
641 ? "null"
642 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400643 + "] label=[" + mHotseatLabels[i]
644 + "]"
645 );
646 }
647 }
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
651 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700652 mWaitingForResult = false;
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // The pattern used here is that a user PICKs a specific application,
655 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
658 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 switch (requestCode) {
662 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700663 completeAddApplication(
664 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
666 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800667 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
672 case REQUEST_PICK_LIVE_FOLDER:
673 addLiveFolder(data);
674 break;
675 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700685 case REQUEST_PICK_WALLPAPER:
686 // We just wanted the activity result here so we can clear mWaitingForResult
687 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
Romain Guy18042c82009-11-06 11:44:55 -0800689 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
690 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
691 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 // Clean up the appWidgetId if we canceled
693 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
694 if (appWidgetId != -1) {
695 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699
700 @Override
701 protected void onResume() {
702 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800703 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700704 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 mWorkspaceLoading = true;
706 mModel.startLoader(this, true);
707 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700708 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700710 // When we resume Launcher, a different Activity might be responsible for the app
711 // market intent, so refresh the icon
712 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714
715 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 protected void onPause() {
717 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700718 // Some launcher layouts don't have a previous and next view
719 if (mPreviousView != null) {
720 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700721 }
722 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700723 dismissPreview(mNextView);
724 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700725 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800726 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700729 @Override
730 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 // Flag the loader to stop early before switching
732 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800734 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700736
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800737 // We can't hide the IME if it was forced open. So don't bother
738 /*
739 @Override
740 public void onWindowFocusChanged(boolean hasFocus) {
741 super.onWindowFocusChanged(hasFocus);
742
743 if (hasFocus) {
744 final InputMethodManager inputManager = (InputMethodManager)
745 getSystemService(Context.INPUT_METHOD_SERVICE);
746 WindowManager.LayoutParams lp = getWindow().getAttributes();
747 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
748 android.os.Handler()) {
749 protected void onReceiveResult(int resultCode, Bundle resultData) {
750 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
751 }
752 });
753 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
754 }
755 }
756 */
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 private boolean acceptFilter() {
759 final InputMethodManager inputManager = (InputMethodManager)
760 getSystemService(Context.INPUT_METHOD_SERVICE);
761 return !inputManager.isFullscreenMode();
762 }
763
764 @Override
765 public boolean onKeyDown(int keyCode, KeyEvent event) {
766 boolean handled = super.onKeyDown(keyCode, event);
767 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
768 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
769 keyCode, event);
770 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700772 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700773 // showSearchDialog()
774 // If there are multiple keystrokes before the search dialog takes focus,
775 // onSearchRequested() will be called for every keystroke,
776 // but it is idempotent, so it's fine.
777 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779 }
780
Joe Onorato8a9625e2010-01-28 15:55:35 -0800781 // Eat the long press event so the keyboard doesn't come up.
782 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
783 return true;
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return handled;
787 }
788
Karl Rosaen138a0412009-04-23 19:00:21 -0700789 private String getTypedText() {
790 return mDefaultKeySsb.toString();
791 }
792
793 private void clearTypedText() {
794 mDefaultKeySsb.clear();
795 mDefaultKeySsb.clearSpans();
796 Selection.setSelection(mDefaultKeySsb, 0);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700800 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
801 * State
802 */
803 private static State intToState(int stateOrdinal) {
804 State state = State.WORKSPACE;
805 final State[] stateValues = State.values();
806 for (int i = 0; i < stateValues.length; i++) {
807 if (stateValues[i].ordinal() == stateOrdinal) {
808 state = stateValues[i];
809 break;
810 }
811 }
812 return state;
813 }
814
815 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 * Restores the previous state, if it exists.
817 *
818 * @param savedState The previous state.
819 */
820 private void restoreState(Bundle savedState) {
821 if (savedState == null) {
822 return;
823 }
824
Michael Jurka883f55b2010-10-21 15:47:14 -0700825 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
826
827 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800828 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700829 } else if (state == State.CUSTOMIZE) {
830 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
834 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700835 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837
838 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700841 mAddScreen = addScreen;
842 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
843 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 mRestoring = true;
845 }
846
847 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
848 if (renameFolder) {
849 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700850 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 mRestoring = true;
852 }
853 }
854
855 /**
856 * Finds all the views we need and configure them properly.
857 */
858 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700859 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400860
Romain Guyb1b69f52009-08-10 15:10:15 -0700861 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400862 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863
Joe Onorato7c312c12009-08-13 21:36:53 -0700864 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700865 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700866 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800867 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700868 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700869 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700870
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700871 if (LauncherApplication.isScreenXLarge()) {
872 // They need to be INVISIBLE initially so that they will be measured in the layout.
873 // Otherwise the animations are messed up when we show them for the first time.
874 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
875 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700879
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500881 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700882
Joe Onorato7c312c12009-08-13 21:36:53 -0700883 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
884 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700886 View handleView = findViewById(R.id.all_apps_button);
887 if (handleView != null && handleView instanceof HandleView) {
888 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700889 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700890 mHandleView.setLauncher(this);
891 mHandleView.setOnClickListener(this);
892 mHandleView.setOnLongClickListener(this);
893 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800894
Winson Chung80baf5a2010-08-09 16:03:15 -0700895 if (mCustomizePagedView != null) {
896 mCustomizePagedView.setLauncher(this);
897 mCustomizePagedView.setDragController(dragController);
898 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700899 } else {
900 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
901 hotseatLeft.setContentDescription(mHotseatLabels[0]);
902 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
903 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
904 hotseatRight.setContentDescription(mHotseatLabels[1]);
905 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400906
Michael Jurkaaf442092010-06-10 17:01:57 -0700907 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
908 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800909
Michael Jurkaaf442092010-06-10 17:01:57 -0700910 Drawable previous = mPreviousView.getDrawable();
911 Drawable next = mNextView.getDrawable();
912 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913
Michael Jurkaaf442092010-06-10 17:01:57 -0700914 mPreviousView.setHapticFeedbackEnabled(false);
915 mPreviousView.setOnLongClickListener(this);
916 mNextView.setHapticFeedbackEnabled(false);
917 mNextView.setOnLongClickListener(this);
918 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800919
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400921 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923
924 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400925 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700926 int deleteZoneHandleId;
927 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700928 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700929 } else {
930 deleteZoneHandleId = R.id.all_apps_button_cluster;
931 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700932 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700933 dragController.addDragListener(deleteZone);
934
935 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
936 if (infoButton != null) {
937 infoButton.setLauncher(this);
938 infoButton.setHandle(findViewById(R.id.configure_button));
939 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
940 dragController.addDragListener(infoButton);
941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
Joe Onorato00acb122009-08-04 16:04:30 -0400943 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400944 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800945 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700946
Joe Onorato00acb122009-08-04 16:04:30 -0400947 // The order here is bottom to top.
948 dragController.addDropTarget(workspace);
949 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700950 if (infoButton != null) {
951 dragController.addDropTarget(infoButton);
952 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 }
954
Romain Guy8a73c512009-11-09 19:19:59 -0800955 @SuppressWarnings({"UnusedDeclaration"})
956 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700957 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800958 mWorkspace.scrollLeft();
959 }
Romain Guy8a73c512009-11-09 19:19:59 -0800960 }
961
962 @SuppressWarnings({"UnusedDeclaration"})
963 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700964 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800965 mWorkspace.scrollRight();
966 }
Romain Guy8a73c512009-11-09 19:19:59 -0800967 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400968
969 @SuppressWarnings({"UnusedDeclaration"})
970 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700971 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400972
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400973 int index = -1;
974 if (v.getId() == R.id.hotseat_left) {
975 index = 0;
976 } else if (v.getId() == R.id.hotseat_right) {
977 index = 1;
978 }
979
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400980 // reload these every tap; you never know when they might change
981 loadHotseats();
982 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
983 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400984 startActivitySafely(
985 mHotseats[index],
986 "hotseat"
987 );
988 }
989 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 /**
992 * Creates a view representing a shortcut.
993 *
994 * @param info The data structure describing the shortcut.
995 *
996 * @return A View inflated from R.layout.application.
997 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001000 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002
1003 /**
1004 * Creates a view representing a shortcut inflated from the specified resource.
1005 *
1006 * @param layoutResId The id of the XML layout used to create the shortcut.
1007 * @param parent The group the shortcut belongs to.
1008 * @param info The data structure describing the shortcut.
1009 *
1010 * @return A View inflated from layoutResId.
1011 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001012 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001013 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1014
Patrick Dubroy9e482382010-11-01 15:16:13 -07001015 Bitmap b = info.getIcon(mIconCache);
1016
Joe Onorato0589f0f2010-02-08 13:44:00 -08001017 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001018 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 favorite.setText(info.title);
1021 favorite.setTag(info);
1022 favorite.setOnClickListener(this);
1023
1024 return favorite;
1025 }
1026
1027 /**
1028 * Add an application shortcut to the workspace.
1029 *
1030 * @param data The intent describing the application.
1031 * @param cellInfo The position on screen where to create the shortcut.
1032 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001033 void completeAddApplication(Context context, Intent data, int screen,
1034 int intersectCellX, int intersectCellY) {
1035 final int[] cellXY = mTmpAddItemCellCoordinates;
1036 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1037
1038 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1039 showOutOfSpaceMessage();
1040 return;
1041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042
Joe Onorato0589f0f2010-02-08 13:44:00 -08001043 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1044 data, context);
1045
Romain Guy73b979d2009-06-09 12:57:21 -07001046 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001049 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001050 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1051 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001052 } else {
1053 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001054 }
1055 }
Romain Guycbb89e42009-06-08 15:52:54 -07001056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 /**
1058 * Add a shortcut to the workspace.
1059 *
1060 * @param data The intent describing the shortcut.
1061 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001062 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 private void completeAddShortcut(Intent data, int screen,
1064 int intersectCellX, int intersectCellY) {
1065 final int[] cellXY = mTmpAddItemCellCoordinates;
1066 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001067
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1069 showOutOfSpaceMessage();
1070 return;
1071 }
1072
1073 final ShortcutInfo info = mModel.addShortcut(
1074 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075
1076 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001078 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080 }
1081
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001083 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001085 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001088 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001092 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1093 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001096 // We have saved the position to which the widget was dragged-- this really only matters
1097 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001099
1100 // For now, we don't save the coordinate where we dropped the icon because we're not
1101 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1102 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001103 int[] touchXY = null;
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001104 if (mAddDropPosition != null && mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
Winson Chung55cef262010-10-28 14:14:18 -07001105 touchXY = mAddDropPosition;
1106 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001107 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001108 boolean foundCellSpan = false;
1109 if (touchXY != null) {
1110 // when dragging and dropping, just find the closest free spot
1111 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1112 int[] result = screenLayout.findNearestVacantArea(
1113 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001114 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001115 foundCellSpan = !findNearestVacantAreaFailed;
1116 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001117 // if we long pressed on an empty cell to bring up a menu,
1118 // make sure we intersect the empty cell
1119 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1120 // findCellForSpanThatIntersects will just ignore them
1121 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1122 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001123 }
1124
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001126 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001128 return;
1129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001131 // Build Launcher-specific widget info and save to database
1132 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 launcherInfo.spanX = spanXY[0];
1134 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001135
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001136 LauncherModel.addItemToDatabase(this, launcherInfo,
1137 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139
1140 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001142
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001143 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001144 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001145
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001150 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001151
1152 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154 }
Romain Guycbb89e42009-06-08 15:52:54 -07001155
Adam Cohended9f8d2010-11-03 13:25:16 -07001156 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1157 @Override
1158 public void onReceive(Context context, Intent intent) {
1159 final String action = intent.getAction();
1160 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1161 mUserPresent = false;
1162 updateRunning();
1163 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1164 mUserPresent = true;
1165 updateRunning();
1166 }
1167 }
1168 };
1169
1170 @Override
1171 public void onAttachedToWindow() {
1172 super.onAttachedToWindow();
1173
1174 // Listen for broadcasts related to user-presence
1175 final IntentFilter filter = new IntentFilter();
1176 filter.addAction(Intent.ACTION_SCREEN_OFF);
1177 filter.addAction(Intent.ACTION_USER_PRESENT);
1178 registerReceiver(mReceiver, filter);
1179
Adam Cohend113e0c2010-11-11 10:48:05 -08001180 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001181 mVisible = true;
1182 }
1183
1184 @Override
1185 public void onDetachedFromWindow() {
1186 super.onDetachedFromWindow();
1187 mVisible = false;
1188
Adam Cohend113e0c2010-11-11 10:48:05 -08001189 if (mAttached) {
1190 unregisterReceiver(mReceiver);
1191 mAttached = false;
1192 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 updateRunning();
1194 }
1195
1196 public void onWindowVisibilityChanged(int visibility) {
1197 mVisible = visibility == View.VISIBLE;
1198 updateRunning();
1199 }
1200
1201 private void sendAdvanceMessage(long delay) {
1202 mHandler.removeMessages(ADVANCE_MSG);
1203 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1204 mHandler.sendMessageDelayed(msg, delay);
1205 mAutoAdvanceSentTime = System.currentTimeMillis();
1206 }
1207
1208 private void updateRunning() {
1209 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1210 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1211 mAutoAdvanceRunning = autoAdvanceRunning;
1212 if (autoAdvanceRunning) {
1213 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1214 sendAdvanceMessage(delay);
1215 } else {
1216 if (!mWidgetsToAdvance.isEmpty()) {
1217 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1218 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1219 }
1220 mHandler.removeMessages(ADVANCE_MSG);
1221 }
1222 }
1223 }
1224
1225 private final Handler mHandler = new Handler() {
1226 @Override
1227 public void handleMessage(Message msg) {
1228 if (msg.what == ADVANCE_MSG) {
1229 int i = 0;
1230 for (View key: mWidgetsToAdvance.keySet()) {
1231 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1232 final int delay = mAdvanceStagger * i;
1233 if (v instanceof Advanceable) {
1234 postDelayed(new Runnable() {
1235 public void run() {
1236 ((Advanceable) v).advance();
1237 }
1238 }, delay);
1239 }
1240 i++;
1241 }
1242 sendAdvanceMessage(mAdvanceInterval);
1243 }
1244 }
1245 };
1246
1247 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1248 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1249 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1250 if (v instanceof Advanceable) {
1251 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1252 ((Advanceable) v).willBeAdvancedByHost();
1253 updateRunning();
1254 }
1255 }
1256
1257 void removeWidgetToAutoAdvance(View hostView) {
1258 if (mWidgetsToAdvance.containsKey(hostView)) {
1259 mWidgetsToAdvance.remove(hostView);
1260 updateRunning();
1261 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001262 }
1263
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1265 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001266 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 launcherInfo.hostView = null;
1268 }
1269
Adam Cohended9f8d2010-11-03 13:25:16 -07001270 void showOutOfSpaceMessage() {
1271 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1272 }
1273
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001274 public LauncherAppWidgetHost getAppWidgetHost() {
1275 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
Romain Guycbb89e42009-06-08 15:52:54 -07001277
Winson Chunga9abd0e2010-10-27 17:18:37 -07001278 public LauncherModel getModel() {
1279 return mModel;
1280 }
1281
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001282 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001283 getWindow().closeAllPanels();
1284
1285 try {
1286 dismissDialog(DIALOG_CREATE_SHORTCUT);
1287 // Unlock the workspace if the dialog was showing
1288 } catch (Exception e) {
1289 // An exception is thrown if the dialog is not visible, which is fine
1290 }
1291
1292 try {
1293 dismissDialog(DIALOG_RENAME_FOLDER);
1294 // Unlock the workspace if the dialog was showing
1295 } catch (Exception e) {
1296 // An exception is thrown if the dialog is not visible, which is fine
1297 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001298
1299 // Whatever we were doing is hereby canceled.
1300 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 @Override
1304 protected void onNewIntent(Intent intent) {
1305 super.onNewIntent(intent);
1306
1307 // Close the menu
1308 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001309 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001310 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001311
Joe Onorato14f122b2009-11-19 14:06:36 -08001312 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1313 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001314
Michael Jurka4cb37242010-08-09 21:05:32 -07001315 // in all these cases, only animate if we're already on home
1316 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001317 mWorkspace.unshrink(alreadyOnHome);
1318 }
1319 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001320 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001321 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1322 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001323 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001324 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001325
Joe Onorato3a8820b2009-11-10 15:06:42 -08001326 final View v = getWindow().peekDecorView();
1327 if (v != null && v.getWindowToken() != null) {
1328 InputMethodManager imm = (InputMethodManager)getSystemService(
1329 INPUT_METHOD_SERVICE);
1330 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332 }
1333 }
1334
1335 @Override
1336 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1337 // Do not call super here
1338 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001339
1340 if (mHomeCustomizationDrawer != null) {
1341 String cur = savedInstanceState.getString("currentTab");
1342 if (cur != null) {
1343 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1344 }
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
1347
1348 @Override
1349 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001350 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351
1352 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1353 if (folders.size() > 0) {
1354 final int count = folders.size();
1355 long[] ids = new long[count];
1356 for (int i = 0; i < count; i++) {
1357 final FolderInfo info = folders.get(i).getInfo();
1358 ids[i] = info.id;
1359 }
1360 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1361 } else {
1362 super.onSaveInstanceState(outState);
1363 }
1364
Michael Jurka883f55b2010-10-21 15:47:14 -07001365 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366
Michael Jurka0280c3b2010-09-17 15:00:07 -07001367 if (mAddScreen > -1 && mWaitingForResult) {
1368 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1369 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1370 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 }
1372
1373 if (mFolderInfo != null && mWaitingForResult) {
1374 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1375 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1376 }
Michael Jurka946ad472010-07-09 18:05:18 -07001377
1378 if (mHomeCustomizationDrawer != null) {
1379 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1380 if (currentTabTag != null) {
1381 outState.putString("currentTab", currentTabTag);
1382 }
1383 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 @Override
1387 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001388 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001389
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001391 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001393 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 }
1395
1396 TextKeyListener.getInstance().release();
1397
Joe Onorato9c1289c2009-08-17 11:03:03 -04001398 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001401
1402 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001403
Patrick Dubroyab962b72010-07-26 12:10:10 -07001404 // Some launcher layouts don't have a previous and next view
1405 if (mPreviousView != null) {
1406 dismissPreview(mPreviousView);
1407 }
1408 if (mNextView != null) {
1409 dismissPreview(mNextView);
1410 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001411
1412 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414
1415 @Override
1416 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001417 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 super.startActivityForResult(intent, requestCode);
1419 }
1420
1421 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001422 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001424
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001425 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001426
Karl Rosaen138a0412009-04-23 19:00:21 -07001427 if (initialQuery == null) {
1428 // Use any text typed in the launcher as the initial query
1429 initialQuery = getTypedText();
1430 clearTypedText();
1431 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 if (appSearchData == null) {
1433 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001434 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
Romain Guycbb89e42009-06-08 15:52:54 -07001436
Karl Rosaen138a0412009-04-23 19:00:21 -07001437 final SearchManager searchManager =
1438 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001439 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001440 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 @Override
1444 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001445 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001446 return false;
1447 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448
1449 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1452 .setIcon(android.R.drawable.ic_menu_add)
1453 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001454 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1455 .setIcon(android.R.drawable.ic_menu_manage)
1456 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001457 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 .setIcon(android.R.drawable.ic_menu_gallery)
1459 .setAlphabeticShortcut('W');
1460 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1461 .setIcon(android.R.drawable.ic_search_category_default)
1462 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1463 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1464 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1465 .setAlphabeticShortcut('N');
1466
1467 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001468 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1469 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470
1471 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1472 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1473 .setIntent(settings);
1474
1475 return true;
1476 }
1477
1478 @Override
1479 public boolean onPrepareOptionsMenu(Menu menu) {
1480 super.onPrepareOptionsMenu(menu);
1481
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001482 // If all apps is animating, don't show the menu, because we don't know
1483 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001484 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001485 return false;
1486 }
1487
1488 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001489 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001490 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1491 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1492
1493 // Disable add if the workspace is full.
1494 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001495 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1496 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498
1499 return true;
1500 }
1501
1502 @Override
1503 public boolean onOptionsItemSelected(MenuItem item) {
1504 switch (item.getItemId()) {
1505 case MENU_ADD:
1506 addItems();
1507 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001508 case MENU_MANAGE_APPS:
1509 manageApps();
1510 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 case MENU_WALLPAPER_SETTINGS:
1512 startWallpaper();
1513 return true;
1514 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001515 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 return true;
1517 case MENU_NOTIFICATIONS:
1518 showNotifications();
1519 return true;
1520 }
1521
1522 return super.onOptionsItemSelected(item);
1523 }
Romain Guycbb89e42009-06-08 15:52:54 -07001524
Karl Rosaen138a0412009-04-23 19:00:21 -07001525 /**
1526 * Indicates that we want global search for this activity by setting the globalSearch
1527 * argument for {@link #startSearch} to true.
1528 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001530 @Override
1531 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001532 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001534 }
1535
Joe Onorato9c1289c2009-08-17 11:03:03 -04001536 public boolean isWorkspaceLocked() {
1537 return mWorkspaceLoading || mWaitingForResult;
1538 }
1539
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001541 if (LauncherApplication.isScreenXLarge()) {
1542 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001543 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001544 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001545 }
1546 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001547 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001548 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551
Michael Jurka0280c3b2010-09-17 15:00:07 -07001552 private void resetAddInfo() {
1553 mAddScreen = -1;
1554 mAddIntersectCellX = -1;
1555 mAddIntersectCellY = -1;
1556 mAddDropPosition = null;
1557 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001558
Winson Chung55cef262010-10-28 14:14:18 -07001559 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001560 resetAddInfo();
1561 mAddScreen = screen;
1562
1563 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1564 mAddDropPosition = position;
1565
Michael Jurkaaf442092010-06-10 17:01:57 -07001566 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001567 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1568 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001569 }
1570
Winson Chung7ad01412010-09-27 11:33:03 -07001571 private void manageApps() {
1572 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1573 }
1574
Michael Jurkaaf442092010-06-10 17:01:57 -07001575 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001576 // TODO: catch bad widget exception when sent
1577 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001578 // TODO: Is this log message meaningful?
1579 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001580 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001581 }
1582
Winson Chung55cef262010-10-28 14:14:18 -07001583 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001584 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
Bjorn Bringert7984c942009-12-09 15:38:25 +00001586 if (appWidget.configure != null) {
1587 // Launch over to configure widget, if needed
1588 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1589 intent.setComponent(appWidget.configure);
1590 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001591 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001592 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1593 intent.putExtra(
1594 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1595 info.mimeType);
1596
1597 final String mimeType = info.mimeType;
1598 final ClipData clipData = (ClipData) info.configurationData;
1599 final ClipDescription clipDesc = clipData.getDescription();
1600 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1601 if (clipDesc.getMimeType(i).equals(mimeType)) {
1602 final ClipData.Item item = clipData.getItem(i);
1603 final CharSequence stringData = item.getText();
1604 final Uri uriData = item.getUri();
1605 final Intent intentData = item.getIntent();
1606 final String key =
1607 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1608 if (uriData != null) {
1609 intent.putExtra(key, uriData);
1610 } else if (intentData != null) {
1611 intent.putExtra(key, intentData);
1612 } else if (stringData != null) {
1613 intent.putExtra(key, stringData);
1614 }
1615 break;
1616 }
1617 }
1618 }
Winson Chung55cef262010-10-28 14:14:18 -07001619 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001620
Romain Guy8e633c52010-03-04 12:51:36 -08001621 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001623 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001624 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 }
1626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Michael Jurka0280c3b2010-09-17 15:00:07 -07001628 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1629 resetAddInfo();
1630 mAddScreen = screen;
1631 mAddDropPosition = position;
1632
1633 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1634 createShortcutIntent.setComponent(componentName);
1635 processShortcut(createShortcutIntent);
1636 }
1637
Joe Onoratodeb98af2010-02-19 14:59:39 -08001638 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001639 // Handle case where user selected "Applications"
1640 String applicationName = getResources().getString(R.string.group_applications);
1641 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001642
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001643 if (applicationName != null && applicationName.equals(shortcutName)) {
1644 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1645 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001646
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001647 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1648 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001649 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001650 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001651 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001652 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
1655
Winson Chung24ab2f12010-09-16 14:10:47 -07001656 void processWallpaper(Intent intent) {
1657 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1658 }
1659
Michael Jurka0280c3b2010-09-17 15:00:07 -07001660 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1661 resetAddInfo();
1662 mAddScreen = screen;
1663 mAddDropPosition = position;
1664
1665 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1666 createFolderIntent.setComponent(componentName);
1667
1668 addLiveFolder(createFolderIntent);
1669 }
1670
1671 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001672 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001673 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001674 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001675
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001676 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001677 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001678 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001679 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001680 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682
Michael Jurka0280c3b2010-09-17 15:00:07 -07001683 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 UserFolderInfo folderInfo = new UserFolderInfo();
1685 folderInfo.title = getText(R.string.folder_name);
1686
Michael Jurka0280c3b2010-09-17 15:00:07 -07001687 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1688 final int[] cellXY = mTmpAddItemCellCoordinates;
1689 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1690 showOutOfSpaceMessage();
1691 return;
1692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693
1694 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001695 LauncherModel.addItemToDatabase(this, folderInfo,
1696 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001697 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001698 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699
1700 // Create the view
1701 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001702 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1703 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001704 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 }
Romain Guycbb89e42009-06-08 15:52:54 -07001706
Joe Onorato9c1289c2009-08-17 11:03:03 -04001707 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001708 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709 }
1710
Michael Jurka28750fb2010-09-24 17:43:49 -07001711 private void completeAddLiveFolder(
1712 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001713 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1714 final int[] cellXY = mTmpAddItemCellCoordinates;
1715 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1716 showOutOfSpaceMessage();
1717 return;
1718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719
Michael Jurka0280c3b2010-09-17 15:00:07 -07001720 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721
1722 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001724 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001725 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
1727 }
1728
1729 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001730 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001731
1732 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1733 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1734
1735 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 Intent.ShortcutIconResource iconResource = null;
1737
1738 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1739 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1740 try {
1741 iconResource = (Intent.ShortcutIconResource) extra;
1742 final PackageManager packageManager = context.getPackageManager();
1743 Resources resources = packageManager.getResourcesForApplication(
1744 iconResource.packageName);
1745 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1746 icon = resources.getDrawable(id);
1747 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001748 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
1750 }
1751
1752 if (icon == null) {
1753 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1754 }
1755
1756 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001757 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 info.title = name;
1759 info.iconResource = iconResource;
1760 info.uri = data.getData();
1761 info.baseIntent = baseIntent;
1762 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1763 LiveFolders.DISPLAY_MODE_GRID);
1764
1765 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001766 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001767 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768
1769 return info;
1770 }
1771
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 private void showNotifications() {
1773 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1774 if (statusBar != null) {
1775 statusBar.expand();
1776 }
1777 }
1778
1779 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001780 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001782 Intent chooser = Intent.createChooser(pickWallpaper,
1783 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001784 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1785 // Removed in Eclair MR1
1786// WallpaperManager wm = (WallpaperManager)
1787// getSystemService(Context.WALLPAPER_SERVICE);
1788// WallpaperInfo wi = wm.getWallpaperInfo();
1789// if (wi != null && wi.getSettingsActivity() != null) {
1790// LabeledIntent li = new LabeledIntent(getPackageName(),
1791// R.string.configure_wallpaper, 0);
1792// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1793// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1794// }
Mike Clerona0618e42009-10-22 13:55:21 -07001795 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001796 }
1797
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001798 /**
1799 * Registers various content observers. The current implementation registers
1800 * only a favorites observer to keep track of the favorites applications.
1801 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001802 private void registerContentObservers() {
1803 ContentResolver resolver = getContentResolver();
1804 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1805 true, mWidgetObserver);
1806 }
1807
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 @Override
1809 public boolean dispatchKeyEvent(KeyEvent event) {
1810 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1811 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001813 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001814 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001815 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001816 dumpState();
1817 return true;
1818 }
1819 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001820 }
1821 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1822 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001823 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 return true;
1825 }
1826 }
1827
1828 return super.dispatchKeyEvent(event);
1829 }
1830
Joe Onorato88ec0992009-11-19 13:16:06 -08001831 @Override
1832 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001833 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1834 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001835 } else {
1836 closeFolder();
1837 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001838 // Some launcher layouts don't have a previous and next view
1839 if (mPreviousView != null) {
1840 dismissPreview(mPreviousView);
1841 dismissPreview(mNextView);
1842 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001843 }
1844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 private void closeFolder() {
1846 Folder folder = mWorkspace.getOpenFolder();
1847 if (folder != null) {
1848 closeFolder(folder);
1849 }
1850 }
1851
1852 void closeFolder(Folder folder) {
1853 folder.getInfo().opened = false;
1854 ViewGroup parent = (ViewGroup) folder.getParent();
1855 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001856 CellLayout cl = (CellLayout) parent;
1857 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001858 if (folder instanceof DropTarget) {
1859 // Live folders aren't DropTargets.
1860 mDragController.removeDropTarget((DropTarget)folder);
1861 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 }
1863 folder.onClose();
1864 }
1865
1866 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001867 * Re-listen when widgets are reset.
1868 */
1869 private void onAppWidgetReset() {
1870 mAppWidgetHost.startListening();
1871 }
1872
1873 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1875 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 private void unbindDesktopItems() {
1878 for (ItemInfo item: mDesktopItems) {
1879 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001882
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 /**
1884 * Launches the intent referred by the clicked shortcut.
1885 *
1886 * @param v The view representing the clicked shortcut.
1887 */
1888 public void onClick(View v) {
1889 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001890 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001892 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001893 int[] pos = new int[2];
1894 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001895 intent.setSourceBounds(new Rect(pos[0], pos[1],
1896 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001897 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 } else if (tag instanceof FolderInfo) {
1899 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001900 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001901 if (mState == State.ALL_APPS) {
1902 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001903 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001904 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907 }
1908
Michael Jurka0e260592010-06-30 17:07:39 -07001909 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001910 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001911 // clicking anywhere on the workspace causes the customization drawer to slide down
1912 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001913 return false;
1914 }
1915
Michael Jurkaaf442092010-06-10 17:01:57 -07001916 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001917 * Event handler for the search button
1918 *
1919 * @param v The view that was clicked.
1920 */
1921 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001922 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001923 // Use a custom animation for launching search
1924 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001925 }
1926
1927 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001928 * Event handler for the voice button
1929 *
1930 * @param v The view that was clicked.
1931 */
1932 public void onClickVoiceButton(View v) {
1933 startVoiceSearch();
1934 }
1935
1936 private void startVoiceSearch() {
1937 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1938 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1939 startActivity(intent);
1940 }
1941
1942 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001943 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001944 * enters home screen customization mode.
1945 *
1946 * @param v The view that was clicked.
1947 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001948 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001949 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001950 }
1951
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001952 /**
1953 * Event handler for the "grid" button that appears on the home screen, which
1954 * enters all apps mode.
1955 *
1956 * @param v The view that was clicked.
1957 */
1958 public void onClickAllAppsButton(View v) {
1959 showAllApps(true);
1960 }
1961
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001962 public void onClickAppMarketButton(View v) {
1963 if (mAppMarketIntent != null) {
1964 startActivitySafely(mAppMarketIntent, "app market");
1965 }
1966 }
1967
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001968 void startApplicationDetailsActivity(ComponentName componentName) {
1969 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001970 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1971 Uri.fromParts("package", packageName, null));
1972 startActivity(intent);
1973 }
1974
Patrick Dubroy5539af72010-09-07 15:22:01 -07001975 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1976 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1977 // System applications cannot be installed. For now, show a toast explaining that.
1978 // We may give them the option of disabling apps this way.
1979 int messageId = R.string.uninstall_system_app_text;
1980 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1981 } else {
1982 String packageName = appInfo.componentName.getPackageName();
1983 String className = appInfo.componentName.getClassName();
1984 Intent intent = new Intent(
1985 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1986 startActivity(intent);
1987 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001988 }
1989
Joe Onoratof984e852010-03-25 09:47:45 -07001990 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1992 try {
1993 startActivity(intent);
1994 } catch (ActivityNotFoundException e) {
1995 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001996 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 } catch (SecurityException e) {
1998 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001999 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002001 "or use the exported attribute for this activity. "
2002 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 }
2004 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002005
Romain Guy8e633c52010-03-04 12:51:36 -08002006 void startActivityForResultSafely(Intent intent, int requestCode) {
2007 try {
2008 startActivityForResult(intent, requestCode);
2009 } catch (ActivityNotFoundException e) {
2010 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2011 } catch (SecurityException e) {
2012 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2013 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2014 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2015 "or use the exported attribute for this activity.", e);
2016 }
2017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018
2019 private void handleFolderClick(FolderInfo folderInfo) {
2020 if (!folderInfo.opened) {
2021 // Close any open folder
2022 closeFolder();
2023 // Open the requested folder
2024 openFolder(folderInfo);
2025 } else {
2026 // Find the open folder...
2027 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2028 int folderScreen;
2029 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002030 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 // .. and close it
2032 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002033 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // Close any folder open on the current screen
2035 closeFolder();
2036 // Pull the folder onto this screen
2037 openFolder(folderInfo);
2038 }
2039 }
2040 }
2041 }
2042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002044 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 * is animated relative to the specified View. If the View is null, no animation
2046 * is played.
2047 *
2048 * @param folderInfo The FolderInfo describing the folder to open.
2049 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002050 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 Folder openFolder;
2052
2053 if (folderInfo instanceof UserFolderInfo) {
2054 openFolder = UserFolder.fromXml(this);
2055 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002056 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 } else {
2058 return;
2059 }
2060
Joe Onorato00acb122009-08-04 16:04:30 -04002061 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 openFolder.setLauncher(this);
2063
2064 openFolder.bind(folderInfo);
2065 folderInfo.opened = true;
2066
Winson Chungaafa03c2010-06-11 17:34:16 -07002067 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 openFolder.onOpen();
2070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002073 switch (v.getId()) {
2074 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002075 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002076 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2077 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002078 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002079 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002080 return true;
2081 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002082 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002083 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2084 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002085 showPreviews(v);
2086 }
2087 return true;
2088 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002089 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002090 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2091 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2092 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002093 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002094 return true;
2095 }
2096
Joe Onorato9c1289c2009-08-17 11:03:03 -04002097 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 return false;
2099 }
2100
2101 if (!(v instanceof CellLayout)) {
2102 v = (View) v.getParent();
2103 }
2104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105
Michael Jurka0280c3b2010-09-17 15:00:07 -07002106 resetAddInfo();
2107 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002109 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 return true;
2111 }
2112
Michael Jurka0280c3b2010-09-17 15:00:07 -07002113 final View itemUnderLongClick = longClickCellInfo.cell;
2114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002116 if (itemUnderLongClick == null) {
2117 // User long pressed on empty space
2118 mWorkspace.setAllowLongPress(false);
2119 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2120 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002121 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002122 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 }
2124 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002125 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002127 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2128 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002129 mAddIntersectCellX = longClickCellInfo.cellX;
2130 mAddIntersectCellY = longClickCellInfo.cellY;
2131 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 }
2133 }
2134 }
2135 return true;
2136 }
2137
Romain Guye6b8e2f2009-11-10 11:56:55 -08002138 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002139 private void dismissPreview(final View v) {
2140 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002141 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002142 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2143 public void onDismiss() {
2144 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2145 int count = group.getChildCount();
2146 for (int i = 0; i < count; i++) {
2147 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2148 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002149 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2150 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2151
2152 v.setTag(R.id.workspace, null);
2153 v.setTag(R.id.icon, null);
2154 window.setOnDismissListener(null);
2155 }
2156 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002157 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002158 }
2159 v.setTag(null);
2160 }
2161
Romain Guyf8e6a802009-12-07 17:48:02 -08002162 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002163 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002164 }
2165
Romain Guya6abce82009-11-10 02:54:41 -08002166 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002167 final Resources resources = getResources();
2168 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002169
Romain Guya6abce82009-11-10 02:54:41 -08002170 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002171
Romain Guy9d31e9f2009-11-11 18:54:28 -08002172 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002173
Romain Guyf8e6a802009-12-07 17:48:02 -08002174 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002175 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002176 int extraW = (int) ((r.left + r.right) * max);
2177 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002178
2179 int aW = cell.getWidth() - extraW;
2180 float w = aW / max;
2181
2182 int width = cell.getWidth();
2183 int height = cell.getHeight();
2184 int x = cell.getLeftPadding();
2185 int y = cell.getTopPadding();
2186 width -= (x + cell.getRightPadding());
2187 height -= (y + cell.getBottomPadding());
2188
2189 float scale = w / width;
2190
2191 int count = end - start;
2192
2193 final float sWidth = width * scale;
2194 float sHeight = height * scale;
2195
Romain Guye6b8e2f2009-11-10 11:56:55 -08002196 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002197
Romain Guye6b8e2f2009-11-10 11:56:55 -08002198 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2199 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002200
2201 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002202 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002203 cell = (CellLayout) workspace.getChildAt(i);
2204
Romain Guyf8e6a802009-12-07 17:48:02 -08002205 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002206 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002207
2208 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002209 c.scale(scale, scale);
2210 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002211 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002212
Romain Guy9d31e9f2009-11-11 18:54:28 -08002213 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002214 image.setImageBitmap(bitmap);
2215 image.setTag(i);
2216 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002217 image.setOnFocusChangeListener(handler);
2218 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002219 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002220
2221 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002222 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2223
Winson Chungaafa03c2010-06-11 17:34:16 -07002224 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002225 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002226
2227 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002228 p.setContentView(preview);
2229 p.setWidth((int) (sWidth * count + extraW));
2230 p.setHeight((int) (sHeight + extraH));
2231 p.setAnimationStyle(R.style.AnimationPreview);
2232 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002233 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002234 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002235 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002236
Romain Guye6b8e2f2009-11-10 11:56:55 -08002237 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2238 public void onDismiss() {
2239 dismissPreview(anchor);
2240 }
2241 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002242
2243 anchor.setTag(p);
2244 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002245 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002246 }
2247
Romain Guy9d31e9f2009-11-11 18:54:28 -08002248 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002249 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002250
Romain Guye6b8e2f2009-11-10 11:56:55 -08002251 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002252 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002253 }
2254
2255 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002256 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002257 v.post(this);
2258 }
2259
2260 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002261 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002262 }
2263
2264 public void onFocusChange(View v, boolean hasFocus) {
2265 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002266 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002267 }
Romain Guya6abce82009-11-10 02:54:41 -08002268 }
2269 }
2270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 Workspace getWorkspace() {
2272 return mWorkspace;
2273 }
2274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 @Override
2276 protected Dialog onCreateDialog(int id) {
2277 switch (id) {
2278 case DIALOG_CREATE_SHORTCUT:
2279 return new CreateShortcut().createDialog();
2280 case DIALOG_RENAME_FOLDER:
2281 return new RenameFolder().createDialog();
2282 }
2283
2284 return super.onCreateDialog(id);
2285 }
2286
2287 @Override
2288 protected void onPrepareDialog(int id, Dialog dialog) {
2289 switch (id) {
2290 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 break;
2292 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002293 if (mFolderInfo != null) {
2294 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2295 final CharSequence text = mFolderInfo.title;
2296 input.setText(text);
2297 input.setSelection(0, text.length());
2298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 break;
2300 }
2301 }
2302
2303 void showRenameDialog(FolderInfo info) {
2304 mFolderInfo = info;
2305 mWaitingForResult = true;
2306 showDialog(DIALOG_RENAME_FOLDER);
2307 }
2308
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 private void showAddDialog(int intersectX, int intersectY) {
2310 resetAddInfo();
2311 mAddIntersectCellX = intersectX;
2312 mAddIntersectCellY = intersectY;
2313 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 mWaitingForResult = true;
2315 showDialog(DIALOG_CREATE_SHORTCUT);
2316 }
2317
Joe Onoratodeb98af2010-02-19 14:59:39 -08002318 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002319 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002320 Bundle bundle = new Bundle();
2321
2322 ArrayList<String> shortcutNames = new ArrayList<String>();
2323 shortcutNames.add(getString(R.string.group_applications));
2324 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2325
2326 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2327 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2328 R.drawable.ic_launcher_application));
2329 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2330
2331 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2332 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002333 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002334 pickIntent.putExtras(bundle);
2335
Joe Onoratodeb98af2010-02-19 14:59:39 -08002336 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002337 }
2338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 private class RenameFolder {
2340 private EditText mInput;
2341
2342 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002343 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2344 mInput = (EditText) layout.findViewById(R.id.folder_name);
2345
2346 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2347 builder.setIcon(0);
2348 builder.setTitle(getString(R.string.rename_folder_title));
2349 builder.setCancelable(true);
2350 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2351 public void onCancel(DialogInterface dialog) {
2352 cleanup();
2353 }
2354 });
2355 builder.setNegativeButton(getString(R.string.cancel_action),
2356 new Dialog.OnClickListener() {
2357 public void onClick(DialogInterface dialog, int which) {
2358 cleanup();
2359 }
2360 }
2361 );
2362 builder.setPositiveButton(getString(R.string.rename_action),
2363 new Dialog.OnClickListener() {
2364 public void onClick(DialogInterface dialog, int which) {
2365 changeFolderName();
2366 }
2367 }
2368 );
2369 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002370
2371 final AlertDialog dialog = builder.create();
2372 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2373 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002374 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002375 mInput.requestFocus();
2376 InputMethodManager inputManager = (InputMethodManager)
2377 getSystemService(Context.INPUT_METHOD_SERVICE);
2378 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002379 }
2380 });
2381
2382 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002383 }
2384
2385 private void changeFolderName() {
2386 final String name = mInput.getText().toString();
2387 if (!TextUtils.isEmpty(name)) {
2388 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002389 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 mFolderInfo.title = name;
2391 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2392
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002394 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 } else {
2397 final FolderIcon folderIcon = (FolderIcon)
2398 mWorkspace.getViewForTag(mFolderInfo);
2399 if (folderIcon != null) {
2400 folderIcon.setText(name);
2401 getWorkspace().requestLayout();
2402 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002403 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002404 mWorkspaceLoading = true;
2405 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
2407 }
2408 }
2409 cleanup();
2410 }
2411
2412 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002413 dismissDialog(DIALOG_RENAME_FOLDER);
2414 mWaitingForResult = false;
2415 mFolderInfo = null;
2416 }
2417 }
2418
Daniel Sandler843e8602010-06-07 14:59:01 -04002419 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2420 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002421 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002422 }
2423
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002424 // AllAppsView.Watcher
2425 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002426 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2427 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002428 mWorkspace.setVisibility(View.GONE);
2429 }
2430 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002431
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002432 private void showToolbarButton(View button) {
2433 button.setAlpha(1.0f);
2434 button.setVisibility(View.VISIBLE);
2435 button.setFocusable(true);
2436 button.setClickable(true);
2437 }
2438
2439 private void hideToolbarButton(View button) {
2440 button.setAlpha(0.0f);
2441 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2442 button.setVisibility(View.INVISIBLE);
2443 button.setFocusable(false);
2444 button.setClickable(false);
2445 }
2446
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002447 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002448 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002449 *
2450 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2451 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002452 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002453 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002454 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002455 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002456 final boolean showing = show;
2457 final boolean hiding = !show;
2458
2459 final int duration = show ?
2460 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2461 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2462
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002464 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2465 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002466 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002467 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002468 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002470 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002471 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002472 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002474 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 });
2476 seq.play(anim);
2477 } else {
2478 if (showing) {
2479 showToolbarButton(view);
2480 } else {
2481 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002482 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002483 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002484 }
2485
2486 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002487 * Show/hide the appropriate toolbar buttons for newState.
2488 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002489 *
2490 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002491 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2492 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002493 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002494 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002495 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002497 final View configureButton = findViewById(R.id.configure_button);
2498
2499 switch (newState) {
2500 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002501 hideOrShowToolbarButton(true, searchButton, showSeq);
2502 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2503 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002504 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 break;
2506 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002507 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002508 hideOrShowToolbarButton(false, searchButton, hideSeq);
2509 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002510 break;
2511 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002512 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002513 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002514 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002515 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002516 break;
2517 }
2518 }
2519
2520 /**
2521 * Helper method for the cameraZoomIn/cameraZoomOut animations
2522 * @param view The view being animated
2523 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2524 * @param scaleFactor The scale factor used for the zoom
2525 */
2526 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2527 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002528
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002529 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002530 // Set pivotY so that at the starting zoom factor, the view is partially
2531 // visible. Modifying initialHeightFactor changes how much of the view is
2532 // initially showing, and hence the perceived angle from which the view enters.
2533 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002535 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002537 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 }
2539 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002540
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002541 /**
2542 * Zoom the camera out from the workspace to reveal 'toView'.
2543 * Assumes that the view to show is anchored at either the very top or very bottom
2544 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002545 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002546 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002548 final Resources res = getResources();
2549 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2550 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002551 final boolean toAllApps = (toState == State.ALL_APPS);
2552 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002553
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 setPivotsForZoom(toView, toState, scale);
2555
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002556 if (toAllApps) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002557 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002558 } else {
2559 mWorkspace.shrinkToTop(animated);
2560 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002561
2562 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002563 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2564 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2565 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2566 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002567
2568 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002569 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002570 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002571 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002572 // Prepare the position
2573 toView.setTranslationX(0.0f);
2574 toView.setTranslationY(0.0f);
2575 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002576 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002578 @Override
2579 public void onAnimationEndOrCancel(Animator animation) {
2580 // If we don't set the final scale values here, if this animation is cancelled
2581 // it will have the wrong scale value and subsequent cameraPan animations will
2582 // not fix that
2583 toView.setScaleX(1.0f);
2584 toView.setScaleY(1.0f);
2585 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002586 });
2587
Chet Haaseb1254a62010-09-07 13:35:00 -07002588 AnimatorSet toolbarHideAnim = new AnimatorSet();
2589 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002590 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2591
2592 // toView should appear right at the end of the workspace shrink animation
2593 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2594
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002595 if (mStateAnimation != null) mStateAnimation.cancel();
2596 mStateAnimation = new AnimatorSet();
2597 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2598 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599
2600 // Show the new toolbar buttons just as the main animation is ending
2601 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002602 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2603 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002604 } else {
2605 toView.setTranslationX(0.0f);
2606 toView.setTranslationY(0.0f);
2607 toView.setScaleX(1.0f);
2608 toView.setScaleY(1.0f);
2609 toView.setVisibility(View.VISIBLE);
2610 hideAndShowToolbarButtons(toState, null, null);
2611 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002612 }
2613
2614 /**
2615 * Zoom the camera back into the workspace, hiding 'fromView'.
2616 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2618 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002619 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002620 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002621 Resources res = getResources();
2622 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2623 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002624 final View fromView =
2625 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2626
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002627 mCustomizePagedView.endChoiceMode();
2628 mAllAppsPagedView.endChoiceMode();
2629
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002631
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002632 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633
2634 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002635 if (mStateAnimation != null) mStateAnimation.cancel();
2636 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002637 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2638 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2639 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2640 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002641 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2642
2643 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2644 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2645 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2646 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002647 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002648 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002649 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002650 }
2651 });
2652
Chet Haaseb1254a62010-09-07 13:35:00 -07002653 AnimatorSet toolbarHideAnim = new AnimatorSet();
2654 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2656
Adam Cohen61033d32010-11-15 18:29:44 -08002657 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658
2659 // Show the new toolbar buttons at the very end of the whole animation
2660 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2661 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002662 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2663 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 } else {
2665 fromView.setVisibility(View.GONE);
2666 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2667 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002668 }
2669
2670 /**
2671 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2672 * This is the transition used on xlarge screens to go between all apps and
2673 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002674 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2675 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2676 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002677 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002678 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002679 final Resources res = getResources();
2680 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002681 final int workspaceHeight = mWorkspace.getHeight();
2682
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002683 final boolean fromAllApps = (fromState == State.ALL_APPS);
2684 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2685 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002687 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2688 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2689 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2690 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002691
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002692 mCustomizePagedView.endChoiceMode();
2693 mAllAppsPagedView.endChoiceMode();
2694
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002695 if (toState == State.ALL_APPS) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002696 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697 } else {
2698 mWorkspace.shrinkToTop(animated);
2699 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700
2701 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002702 if (mStateAnimation != null) mStateAnimation.cancel();
2703 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002704 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002705 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002706 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002707 toView.setVisibility(View.VISIBLE);
2708 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002709 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002710 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002711 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002712 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002713 fromView.setVisibility(View.GONE);
2714 }
2715 });
2716
Chet Haaseb1254a62010-09-07 13:35:00 -07002717 AnimatorSet toolbarHideAnim = new AnimatorSet();
2718 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002719 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2720
Chet Haase472b2812010-10-14 07:02:04 -07002721 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2722 fromViewStartY, fromViewEndY);
2723 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002724 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2725 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2726 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2727 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2728 );
Chet Haase472b2812010-10-14 07:02:04 -07002729 fromAnim.setDuration(duration);
2730 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002731
2732 // Show the new toolbar buttons just as the main animation is ending
2733 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002734 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2735 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002736 } else {
2737 fromView.setY(fromViewEndY);
2738 fromView.setVisibility(View.GONE);
2739 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002740 toView.setScaleX(1.0f);
2741 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742 toView.setVisibility(View.VISIBLE);
2743 hideAndShowToolbarButtons(toState, null, null);
2744 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002745 }
2746
2747 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002748 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002749 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002750 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002751
Michael Jurka79212d82010-07-30 16:36:20 -07002752 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002753 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002754 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002755 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002756 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002757 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002758 } else {
2759 mAllAppsGrid.zoom(1.0f, animated);
2760 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002761
Romain Guyc16fea72010-03-12 17:17:56 -08002762 ((View) mAllAppsGrid).setFocusable(true);
2763 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002764
Joe Onorato7c312c12009-08-13 21:36:53 -07002765 // TODO: fade these two too
2766 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002767
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002768 // Change the state *after* we've called all the transition code
2769 mState = State.ALL_APPS;
2770 }
2771
2772
2773 void showWorkspace(boolean animated) {
2774 showWorkspace(animated, null);
2775 }
2776
2777 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002778 if (layout != null) {
2779 // always animated, but that's ok since we never specify a layout and
2780 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002781 mWorkspace.unshrink(layout);
2782 } else {
2783 mWorkspace.unshrink(animated);
2784 }
2785 if (mState == State.ALL_APPS) {
2786 closeAllApps(animated);
2787 } else if (mState == State.CUSTOMIZE) {
2788 hideCustomizationDrawer(animated);
2789 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002790
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002791 // Change the state *after* we've called all the transition code
2792 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002793 }
2794
Joe Onoratob2061212009-11-24 16:13:54 -05002795 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002796 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002797 * - Home from workspace
2798 * - from center screen
2799 * - from other screens
2800 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002801 * - from center screen
2802 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002803 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002804 * - from center screen
2805 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002806 * - Launch app from workspace and quit
2807 * - with back
2808 * - with home
2809 * - Launch app from all apps and quit
2810 * - with back
2811 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002812 * - Go to a screen that's not the default, then all
2813 * apps, and launch and app, and go back
2814 * - with back
2815 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002816 * - On workspace, long press power and go back
2817 * - with back
2818 * - with home
2819 * - On all apps, long press power and go back
2820 * - with back
2821 * - with home
2822 * - On workspace, power off
2823 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002824 * - Launch an app and turn off the screen while in that app
2825 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002826 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002827 * - From all apps
2828 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002829 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2830 * - From all apps
2831 * - From the center workspace
2832 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002833 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002834 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002835 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002836 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002838 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002839 } else {
2840 mAllAppsGrid.zoom(0.0f, animated);
2841 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002842 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002843 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002844 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002845 }
2846
Joe Onorato7c312c12009-08-13 21:36:53 -07002847 void lockAllApps() {
2848 // TODO
2849 }
2850
2851 void unlockAllApps() {
2852 // TODO
2853 }
2854
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002855 // Show the customization drawer (only exists in x-large configuration)
2856 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002857 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002858 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002859 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002860 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002861 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002862 // Change the state *after* we've called all the transition code
2863 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002864 }
2865
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002866 // Hide the customization drawer (only exists in x-large configuration)
2867 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002868 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002869 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002870 }
2871 }
2872
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002873 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2874 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2875 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002876 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002877 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002878
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002879 void onWorkspaceClick(CellLayout layout) {
2880 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002881 }
2882
Michael Jurka0423dcf2010-10-05 14:56:18 -07002883 private void updateButtonWithIconFromExternalActivity(
2884 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2885 ImageView button = (ImageView) findViewById(buttonId);
2886 Drawable toolbarIcon = null;
2887 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002888 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002889 // Look for the toolbar icon specified in the activity meta-data
2890 Bundle metaData = packageManager.getActivityInfo(
2891 activityName, PackageManager.GET_META_DATA).metaData;
2892 if (metaData != null) {
2893 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2894 if (iconResId != 0) {
2895 Resources res = packageManager.getResourcesForActivity(activityName);
2896 toolbarIcon = res.getDrawable(iconResId);
2897 }
2898 }
2899 } catch (NameNotFoundException e) {
2900 // Do nothing
2901 }
2902 // If we were unable to find the icon via the meta-data, use a generic one
2903 if (toolbarIcon == null) {
2904 button.setImageResource(fallbackDrawableId);
2905 } else {
2906 button.setImageDrawable(toolbarIcon);
2907 }
2908 }
2909
2910 private void updateGlobalSearchIcon() {
2911 if (LauncherApplication.isScreenXLarge()) {
2912 final SearchManager searchManager =
2913 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2914 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002915 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002916 updateButtonWithIconFromExternalActivity(
2917 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002918 } else {
2919 findViewById(R.id.search_button).setVisibility(View.GONE);
2920 }
2921 }
2922 }
2923
2924 private void updateVoiceSearchIcon() {
2925 if (LauncherApplication.isScreenXLarge()) {
2926 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2927 ComponentName activityName = intent.resolveActivity(getPackageManager());
2928 if (activityName != null) {
2929 updateButtonWithIconFromExternalActivity(
2930 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2931 } else {
2932 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002933 }
2934 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002935 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002936
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002937 /**
2938 * Sets the app market icon (shown when all apps is visible on x-large screens)
2939 */
2940 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002941 if (LauncherApplication.isScreenXLarge()) {
2942 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2943 // Find the app market activity by resolving an intent.
2944 // (If multiple app markets are installed, it will return the ResolverActivity.)
2945 ComponentName activityName = intent.resolveActivity(getPackageManager());
2946 if (activityName != null) {
2947 mAppMarketIntent = intent;
2948 updateButtonWithIconFromExternalActivity(
2949 R.id.market_button, activityName, R.drawable.app_market_generic);
2950 }
2951 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002952 }
2953
2954 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002955 * Displays the shortcut creation dialog and launches, if necessary, the
2956 * appropriate activity.
2957 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002958 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002959 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2960 DialogInterface.OnShowListener {
2961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002963
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2968 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002969 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 builder.setInverseBackgroundForced(true);
2972
2973 AlertDialog dialog = builder.create();
2974 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002975 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002976 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 return dialog;
2979 }
2980
2981 public void onCancel(DialogInterface dialog) {
2982 mWaitingForResult = false;
2983 cleanup();
2984 }
2985
Romain Guycbb89e42009-06-08 15:52:54 -07002986 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002987 }
2988
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002990 try {
2991 dismissDialog(DIALOG_CREATE_SHORTCUT);
2992 } catch (Exception e) {
2993 // An exception is thrown if the dialog is not visible, which is fine
2994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 }
2996
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002997 /**
2998 * Handle the action clicked in the "Add to home" dialog.
2999 */
3000 public void onClick(DialogInterface dialog, int which) {
3001 Resources res = getResources();
3002 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003003
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003004 switch (which) {
3005 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003006 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003007 break;
3008 }
Romain Guycbb89e42009-06-08 15:52:54 -07003009
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003010 case AddAdapter.ITEM_APPWIDGET: {
3011 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003012
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003013 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3014 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003015 // start the pick activity
3016 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3017 break;
3018 }
Romain Guycbb89e42009-06-08 15:52:54 -07003019
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003020 case AddAdapter.ITEM_LIVE_FOLDER: {
3021 // Insert extra item to handle inserting folder
3022 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003023
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003024 ArrayList<String> shortcutNames = new ArrayList<String>();
3025 shortcutNames.add(res.getString(R.string.group_folder));
3026 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003027
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003028 ArrayList<ShortcutIconResource> shortcutIcons =
3029 new ArrayList<ShortcutIconResource>();
3030 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3031 R.drawable.ic_launcher_folder));
3032 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3033
3034 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3035 pickIntent.putExtra(Intent.EXTRA_INTENT,
3036 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3037 pickIntent.putExtra(Intent.EXTRA_TITLE,
3038 getText(R.string.title_select_live_folder));
3039 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003040
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003041 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3042 break;
3043 }
3044
3045 case AddAdapter.ITEM_WALLPAPER: {
3046 startWallpaper();
3047 break;
3048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
3050 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003051
3052 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003053 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003054 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003055 }
3056
3057 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003058 * Receives notifications when applications are added/removed.
3059 */
3060 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3061 @Override
3062 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003063 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003064 String reason = intent.getStringExtra("reason");
3065 if (!"homekey".equals(reason)) {
3066 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003067 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003068 animate = false;
3069 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003070 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003071 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003072 }
3073 }
3074
3075 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003076 * Receives notifications whenever the appwidgets are reset.
3077 */
3078 private class AppWidgetResetObserver extends ContentObserver {
3079 public AppWidgetResetObserver() {
3080 super(new Handler());
3081 }
3082
3083 @Override
3084 public void onChange(boolean selfChange) {
3085 onAppWidgetReset();
3086 }
3087 }
3088
3089 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003090 * If the activity is currently paused, signal that we need to re-run the loader
3091 * in onResume.
3092 *
3093 * This needs to be called from incoming places where resources might have been loaded
3094 * while we are paused. That is becaues the Configuration might be wrong
3095 * when we're not running, and if it comes back to what it was when we
3096 * were paused, we are not restarted.
3097 *
3098 * Implementation of the method from LauncherModel.Callbacks.
3099 *
3100 * @return true if we are currently paused. The caller might be able to
3101 * skip some work in that case since we will come back again.
3102 */
3103 public boolean setLoadOnResume() {
3104 if (mPaused) {
3105 Log.i(TAG, "setLoadOnResume");
3106 mOnResumeNeedsLoad = true;
3107 return true;
3108 } else {
3109 return false;
3110 }
3111 }
3112
3113 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003117 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003118 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003119 } else {
3120 return SCREEN_COUNT / 2;
3121 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003122 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003123
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003124 void setAllAppsPagedView(PagedView view) {
3125 mAllAppsPagedView = view;
3126 }
3127
Joe Onorato9c1289c2009-08-17 11:03:03 -04003128 /**
3129 * Refreshes the shortcuts shown on the workspace.
3130 *
3131 * Implementation of the method from LauncherModel.Callbacks.
3132 */
3133 public void startBinding() {
3134 final Workspace workspace = mWorkspace;
3135 int count = workspace.getChildCount();
3136 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003137 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003138 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3139 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003140
Joe Onorato9c1289c2009-08-17 11:03:03 -04003141 if (DEBUG_USER_INTERFACE) {
3142 android.widget.Button finishButton = new android.widget.Button(this);
3143 finishButton.setText("Finish");
3144 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3145
3146 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3147 public void onClick(View v) {
3148 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003149 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
3152 }
3153
3154 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003155 * Bind the items start-end from the list.
3156 *
3157 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003158 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003159 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3160
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003161 setLoadOnResume();
3162
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 final Workspace workspace = mWorkspace;
3164
3165 for (int i=start; i<end; i++) {
3166 final ItemInfo item = shortcuts.get(i);
3167 mDesktopItems.add(item);
3168 switch (item.itemType) {
3169 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3170 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003171 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3173 false);
3174 break;
3175 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3176 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003177 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003178 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003179 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3180 false);
3181 break;
3182 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3183 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3184 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003185 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 (LiveFolderInfo) item);
3187 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3188 false);
3189 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 }
3192
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 }
3195
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 /**
3197 * Implementation of the method from LauncherModel.Callbacks.
3198 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003199 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003200 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003201 sFolders.clear();
3202 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003204
3205 /**
3206 * Add the views for a widget to the workspace.
3207 *
3208 * Implementation of the method from LauncherModel.Callbacks.
3209 */
3210 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003211 setLoadOnResume();
3212
Daniel Sandler843e8602010-06-07 14:59:01 -04003213 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3214 if (DEBUG_WIDGETS) {
3215 Log.d(TAG, "bindAppWidget: " + item);
3216 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217 final Workspace workspace = mWorkspace;
3218
3219 final int appWidgetId = item.appWidgetId;
3220 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003221 if (DEBUG_WIDGETS) {
3222 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3223 }
3224
Joe Onorato9c1289c2009-08-17 11:03:03 -04003225 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3226
Joe Onorato9c1289c2009-08-17 11:03:03 -04003227 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3228 item.hostView.setTag(item);
3229
3230 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3231 item.cellY, item.spanX, item.spanY, false);
3232
Adam Cohended9f8d2010-11-03 13:25:16 -07003233 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3234
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 workspace.requestLayout();
3236
3237 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003238
3239 if (DEBUG_WIDGETS) {
3240 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3241 + (SystemClock.uptimeMillis()-start) + "ms");
3242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 }
3244
3245 /**
3246 * Callback saying that there aren't any more items to bind.
3247 *
3248 * Implementation of the method from LauncherModel.Callbacks.
3249 */
3250 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003251 setLoadOnResume();
3252
Joe Onorato9c1289c2009-08-17 11:03:03 -04003253 if (mSavedState != null) {
3254 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003255 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 }
3257
3258 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3259 if (userFolders != null) {
3260 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003261 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 if (info != null) {
3263 openFolder(info);
3264 }
3265 }
3266 final Folder openFolder = mWorkspace.getOpenFolder();
3267 if (openFolder != null) {
3268 openFolder.requestFocus();
3269 }
3270 }
3271
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 mSavedState = null;
3273 }
3274
3275 if (mSavedInstanceState != null) {
3276 super.onRestoreInstanceState(mSavedInstanceState);
3277 mSavedInstanceState = null;
3278 }
3279
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 mWorkspaceLoading = false;
3281 }
3282
3283 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003284 * Updates the icons on the launcher that are affected by changes to the package list
3285 * on the device.
3286 */
3287 private void updateIconsAffectedByPackageManagerChanges() {
3288 updateAppMarketIcon();
3289 updateGlobalSearchIcon();
3290 updateVoiceSearchIcon();
3291 }
3292
3293 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003294 * Add the icons for all apps.
3295 *
3296 * Implementation of the method from LauncherModel.Callbacks.
3297 */
3298 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003299 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003300 if (mCustomizePagedView != null) {
3301 mCustomizePagedView.setApps(apps);
3302 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003303 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 }
3305
3306 /**
3307 * A package was installed.
3308 *
3309 * Implementation of the method from LauncherModel.Callbacks.
3310 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003311 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003312 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003314 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003315 if (mCustomizePagedView != null) {
3316 mCustomizePagedView.addApps(apps);
3317 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003318 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 }
3320
3321 /**
3322 * A package was updated.
3323 *
3324 * Implementation of the method from LauncherModel.Callbacks.
3325 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003326 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003327 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003329 mWorkspace.updateShortcuts(apps);
3330 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003331 if (mCustomizePagedView != null) {
3332 mCustomizePagedView.updateApps(apps);
3333 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003334 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 }
3336
3337 /**
3338 * A package was uninstalled.
3339 *
3340 * Implementation of the method from LauncherModel.Callbacks.
3341 */
Joe Onorato36115782010-06-17 13:28:48 -04003342 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003343 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003344 if (permanent) {
3345 mWorkspace.removeItems(apps);
3346 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003347 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003348 if (mCustomizePagedView != null) {
3349 mCustomizePagedView.removeApps(apps);
3350 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003351 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 }
Joe Onoratobe386092009-11-17 17:32:16 -08003353
3354 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003355 * A number of packages were updated.
3356 */
3357 public void bindPackagesUpdated() {
3358 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003359 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003360 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003361 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003362 }
3363
3364 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003365 * Prints out out state for debugging.
3366 */
3367 public void dumpState() {
3368 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003369 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003370 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3371 Log.d(TAG, "mRestoring=" + mRestoring);
3372 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3373 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3374 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003375 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003376 mModel.dumpState();
3377 mAllAppsGrid.dumpState();
3378 Log.d(TAG, "END launcher2 dump state");
3379 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003380}