blob: 232fef4ca7affe9f4509ccfc35bad87dfd193260 [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;
1104 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1105 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 {
1117 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1118 // if we long pressed on an empty cell to bring up a menu,
1119 // make sure we intersect the empty cell
1120 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1121 mAddIntersectCellX, mAddIntersectCellY);
1122 } else {
1123 // if we went through the menu -> add, just find any spot
1124 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1125 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001126 }
1127
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001129 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001130 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001131 return;
1132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001133
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001134 // Build Launcher-specific widget info and save to database
1135 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 launcherInfo.spanX = spanXY[0];
1137 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001138
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 LauncherModel.addItemToDatabase(this, launcherInfo,
1140 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001142
1143 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001144 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001145
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001147 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001148
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001149 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001153 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001154
1155 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157 }
Romain Guycbb89e42009-06-08 15:52:54 -07001158
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1160 @Override
1161 public void onReceive(Context context, Intent intent) {
1162 final String action = intent.getAction();
1163 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1164 mUserPresent = false;
1165 updateRunning();
1166 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1167 mUserPresent = true;
1168 updateRunning();
1169 }
1170 }
1171 };
1172
1173 @Override
1174 public void onAttachedToWindow() {
1175 super.onAttachedToWindow();
1176
1177 // Listen for broadcasts related to user-presence
1178 final IntentFilter filter = new IntentFilter();
1179 filter.addAction(Intent.ACTION_SCREEN_OFF);
1180 filter.addAction(Intent.ACTION_USER_PRESENT);
1181 registerReceiver(mReceiver, filter);
1182
Adam Cohend113e0c2010-11-11 10:48:05 -08001183 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 mVisible = true;
1185 }
1186
1187 @Override
1188 public void onDetachedFromWindow() {
1189 super.onDetachedFromWindow();
1190 mVisible = false;
1191
Adam Cohend113e0c2010-11-11 10:48:05 -08001192 if (mAttached) {
1193 unregisterReceiver(mReceiver);
1194 mAttached = false;
1195 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001196 updateRunning();
1197 }
1198
1199 public void onWindowVisibilityChanged(int visibility) {
1200 mVisible = visibility == View.VISIBLE;
1201 updateRunning();
1202 }
1203
1204 private void sendAdvanceMessage(long delay) {
1205 mHandler.removeMessages(ADVANCE_MSG);
1206 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1207 mHandler.sendMessageDelayed(msg, delay);
1208 mAutoAdvanceSentTime = System.currentTimeMillis();
1209 }
1210
1211 private void updateRunning() {
1212 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1213 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1214 mAutoAdvanceRunning = autoAdvanceRunning;
1215 if (autoAdvanceRunning) {
1216 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1217 sendAdvanceMessage(delay);
1218 } else {
1219 if (!mWidgetsToAdvance.isEmpty()) {
1220 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1221 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1222 }
1223 mHandler.removeMessages(ADVANCE_MSG);
1224 }
1225 }
1226 }
1227
1228 private final Handler mHandler = new Handler() {
1229 @Override
1230 public void handleMessage(Message msg) {
1231 if (msg.what == ADVANCE_MSG) {
1232 int i = 0;
1233 for (View key: mWidgetsToAdvance.keySet()) {
1234 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1235 final int delay = mAdvanceStagger * i;
1236 if (v instanceof Advanceable) {
1237 postDelayed(new Runnable() {
1238 public void run() {
1239 ((Advanceable) v).advance();
1240 }
1241 }, delay);
1242 }
1243 i++;
1244 }
1245 sendAdvanceMessage(mAdvanceInterval);
1246 }
1247 }
1248 };
1249
1250 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1251 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1252 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1253 if (v instanceof Advanceable) {
1254 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1255 ((Advanceable) v).willBeAdvancedByHost();
1256 updateRunning();
1257 }
1258 }
1259
1260 void removeWidgetToAutoAdvance(View hostView) {
1261 if (mWidgetsToAdvance.containsKey(hostView)) {
1262 mWidgetsToAdvance.remove(hostView);
1263 updateRunning();
1264 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001265 }
1266
Joe Onorato9c1289c2009-08-17 11:03:03 -04001267 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1268 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001269 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001270 launcherInfo.hostView = null;
1271 }
1272
Adam Cohended9f8d2010-11-03 13:25:16 -07001273 void showOutOfSpaceMessage() {
1274 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1275 }
1276
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001277 public LauncherAppWidgetHost getAppWidgetHost() {
1278 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Winson Chunga9abd0e2010-10-27 17:18:37 -07001281 public LauncherModel getModel() {
1282 return mModel;
1283 }
1284
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001285 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001286 getWindow().closeAllPanels();
1287
1288 try {
1289 dismissDialog(DIALOG_CREATE_SHORTCUT);
1290 // Unlock the workspace if the dialog was showing
1291 } catch (Exception e) {
1292 // An exception is thrown if the dialog is not visible, which is fine
1293 }
1294
1295 try {
1296 dismissDialog(DIALOG_RENAME_FOLDER);
1297 // Unlock the workspace if the dialog was showing
1298 } catch (Exception e) {
1299 // An exception is thrown if the dialog is not visible, which is fine
1300 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001301
1302 // Whatever we were doing is hereby canceled.
1303 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001304 }
1305
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 @Override
1307 protected void onNewIntent(Intent intent) {
1308 super.onNewIntent(intent);
1309
1310 // Close the menu
1311 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001312 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001313 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001314
Joe Onorato14f122b2009-11-19 14:06:36 -08001315 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1316 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001317
Michael Jurka4cb37242010-08-09 21:05:32 -07001318 // in all these cases, only animate if we're already on home
1319 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001320 mWorkspace.unshrink(alreadyOnHome);
1321 }
1322 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001323 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001324 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1325 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001326 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001327 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001328
Joe Onorato3a8820b2009-11-10 15:06:42 -08001329 final View v = getWindow().peekDecorView();
1330 if (v != null && v.getWindowToken() != null) {
1331 InputMethodManager imm = (InputMethodManager)getSystemService(
1332 INPUT_METHOD_SERVICE);
1333 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
1335 }
1336 }
1337
1338 @Override
1339 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1340 // Do not call super here
1341 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001342
1343 if (mHomeCustomizationDrawer != null) {
1344 String cur = savedInstanceState.getString("currentTab");
1345 if (cur != null) {
1346 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1347 }
1348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
1351 @Override
1352 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001353 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354
1355 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1356 if (folders.size() > 0) {
1357 final int count = folders.size();
1358 long[] ids = new long[count];
1359 for (int i = 0; i < count; i++) {
1360 final FolderInfo info = folders.get(i).getInfo();
1361 ids[i] = info.id;
1362 }
1363 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1364 } else {
1365 super.onSaveInstanceState(outState);
1366 }
1367
Michael Jurka883f55b2010-10-21 15:47:14 -07001368 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 if (mAddScreen > -1 && mWaitingForResult) {
1371 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1372 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1373 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 }
1375
1376 if (mFolderInfo != null && mWaitingForResult) {
1377 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1378 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1379 }
Michael Jurka946ad472010-07-09 18:05:18 -07001380
1381 if (mHomeCustomizationDrawer != null) {
1382 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1383 if (currentTabTag != null) {
1384 outState.putString("currentTab", currentTabTag);
1385 }
1386 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
1389 @Override
1390 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001392
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001394 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001396 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
1399 TextKeyListener.getInstance().release();
1400
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001404
1405 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001406
Patrick Dubroyab962b72010-07-26 12:10:10 -07001407 // Some launcher layouts don't have a previous and next view
1408 if (mPreviousView != null) {
1409 dismissPreview(mPreviousView);
1410 }
1411 if (mNextView != null) {
1412 dismissPreview(mNextView);
1413 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001414
1415 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
1417
1418 @Override
1419 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001420 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 super.startActivityForResult(intent, requestCode);
1422 }
1423
1424 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001425 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001426 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001427
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001428 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001429
Karl Rosaen138a0412009-04-23 19:00:21 -07001430 if (initialQuery == null) {
1431 // Use any text typed in the launcher as the initial query
1432 initialQuery = getTypedText();
1433 clearTypedText();
1434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 if (appSearchData == null) {
1436 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001437 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
Romain Guycbb89e42009-06-08 15:52:54 -07001439
Karl Rosaen138a0412009-04-23 19:00:21 -07001440 final SearchManager searchManager =
1441 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001442 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001443 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
1446 @Override
1447 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001448 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001449 return false;
1450 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451
1452 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1455 .setIcon(android.R.drawable.ic_menu_add)
1456 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001457 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1458 .setIcon(android.R.drawable.ic_menu_manage)
1459 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001460 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 .setIcon(android.R.drawable.ic_menu_gallery)
1462 .setAlphabeticShortcut('W');
1463 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1464 .setIcon(android.R.drawable.ic_search_category_default)
1465 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1466 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1467 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1468 .setAlphabeticShortcut('N');
1469
1470 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001471 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1472 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473
1474 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1475 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1476 .setIntent(settings);
1477
1478 return true;
1479 }
1480
1481 @Override
1482 public boolean onPrepareOptionsMenu(Menu menu) {
1483 super.onPrepareOptionsMenu(menu);
1484
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001485 // If all apps is animating, don't show the menu, because we don't know
1486 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001487 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001488 return false;
1489 }
1490
1491 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001492 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001493 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1494 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1495
1496 // Disable add if the workspace is full.
1497 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001498 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1499 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501
1502 return true;
1503 }
1504
1505 @Override
1506 public boolean onOptionsItemSelected(MenuItem item) {
1507 switch (item.getItemId()) {
1508 case MENU_ADD:
1509 addItems();
1510 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001511 case MENU_MANAGE_APPS:
1512 manageApps();
1513 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 case MENU_WALLPAPER_SETTINGS:
1515 startWallpaper();
1516 return true;
1517 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001518 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 return true;
1520 case MENU_NOTIFICATIONS:
1521 showNotifications();
1522 return true;
1523 }
1524
1525 return super.onOptionsItemSelected(item);
1526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Karl Rosaen138a0412009-04-23 19:00:21 -07001528 /**
1529 * Indicates that we want global search for this activity by setting the globalSearch
1530 * argument for {@link #startSearch} to true.
1531 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001533 @Override
1534 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001535 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001537 }
1538
Joe Onorato9c1289c2009-08-17 11:03:03 -04001539 public boolean isWorkspaceLocked() {
1540 return mWorkspaceLoading || mWaitingForResult;
1541 }
1542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001544 if (LauncherApplication.isScreenXLarge()) {
1545 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001546 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001547 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001548 }
1549 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001550 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001551 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554
Michael Jurka0280c3b2010-09-17 15:00:07 -07001555 private void resetAddInfo() {
1556 mAddScreen = -1;
1557 mAddIntersectCellX = -1;
1558 mAddIntersectCellY = -1;
1559 mAddDropPosition = null;
1560 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001561
Winson Chung55cef262010-10-28 14:14:18 -07001562 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 resetAddInfo();
1564 mAddScreen = screen;
1565
1566 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1567 mAddDropPosition = position;
1568
Michael Jurkaaf442092010-06-10 17:01:57 -07001569 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001570 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1571 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001572 }
1573
Winson Chung7ad01412010-09-27 11:33:03 -07001574 private void manageApps() {
1575 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1576 }
1577
Michael Jurkaaf442092010-06-10 17:01:57 -07001578 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001579 // TODO: catch bad widget exception when sent
1580 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001581 // TODO: Is this log message meaningful?
1582 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001583 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001584 }
1585
Winson Chung55cef262010-10-28 14:14:18 -07001586 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001587 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588
Bjorn Bringert7984c942009-12-09 15:38:25 +00001589 if (appWidget.configure != null) {
1590 // Launch over to configure widget, if needed
1591 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1592 intent.setComponent(appWidget.configure);
1593 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001594 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001595 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1596 intent.putExtra(
1597 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1598 info.mimeType);
1599
1600 final String mimeType = info.mimeType;
1601 final ClipData clipData = (ClipData) info.configurationData;
1602 final ClipDescription clipDesc = clipData.getDescription();
1603 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1604 if (clipDesc.getMimeType(i).equals(mimeType)) {
1605 final ClipData.Item item = clipData.getItem(i);
1606 final CharSequence stringData = item.getText();
1607 final Uri uriData = item.getUri();
1608 final Intent intentData = item.getIntent();
1609 final String key =
1610 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1611 if (uriData != null) {
1612 intent.putExtra(key, uriData);
1613 } else if (intentData != null) {
1614 intent.putExtra(key, intentData);
1615 } else if (stringData != null) {
1616 intent.putExtra(key, stringData);
1617 }
1618 break;
1619 }
1620 }
1621 }
Winson Chung55cef262010-10-28 14:14:18 -07001622 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001623
Romain Guy8e633c52010-03-04 12:51:36 -08001624 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001626 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001627 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629 }
Romain Guycbb89e42009-06-08 15:52:54 -07001630
Michael Jurka0280c3b2010-09-17 15:00:07 -07001631 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1632 resetAddInfo();
1633 mAddScreen = screen;
1634 mAddDropPosition = position;
1635
1636 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1637 createShortcutIntent.setComponent(componentName);
1638 processShortcut(createShortcutIntent);
1639 }
1640
Joe Onoratodeb98af2010-02-19 14:59:39 -08001641 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001642 // Handle case where user selected "Applications"
1643 String applicationName = getResources().getString(R.string.group_applications);
1644 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001645
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001646 if (applicationName != null && applicationName.equals(shortcutName)) {
1647 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1648 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1651 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001652 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001653 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001655 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
1658
Winson Chung24ab2f12010-09-16 14:10:47 -07001659 void processWallpaper(Intent intent) {
1660 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1661 }
1662
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1664 resetAddInfo();
1665 mAddScreen = screen;
1666 mAddDropPosition = position;
1667
1668 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1669 createFolderIntent.setComponent(componentName);
1670
1671 addLiveFolder(createFolderIntent);
1672 }
1673
1674 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001675 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001676 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001677 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001678
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001679 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001680 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001681 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001682 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685
Michael Jurka0280c3b2010-09-17 15:00:07 -07001686 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 UserFolderInfo folderInfo = new UserFolderInfo();
1688 folderInfo.title = getText(R.string.folder_name);
1689
Michael Jurka0280c3b2010-09-17 15:00:07 -07001690 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1691 final int[] cellXY = mTmpAddItemCellCoordinates;
1692 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1693 showOutOfSpaceMessage();
1694 return;
1695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696
1697 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001698 LauncherModel.addItemToDatabase(this, folderInfo,
1699 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001700 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001701 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702
1703 // Create the view
1704 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001705 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1706 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001707 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001708 }
Romain Guycbb89e42009-06-08 15:52:54 -07001709
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001711 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001712 }
1713
Michael Jurka28750fb2010-09-24 17:43:49 -07001714 private void completeAddLiveFolder(
1715 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001716 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1717 final int[] cellXY = mTmpAddItemCellCoordinates;
1718 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1719 showOutOfSpaceMessage();
1720 return;
1721 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722
Michael Jurka0280c3b2010-09-17 15:00:07 -07001723 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724
1725 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001727 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001728 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730 }
1731
1732 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001733 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734
1735 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1736 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1737
1738 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 Intent.ShortcutIconResource iconResource = null;
1740
1741 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1742 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1743 try {
1744 iconResource = (Intent.ShortcutIconResource) extra;
1745 final PackageManager packageManager = context.getPackageManager();
1746 Resources resources = packageManager.getResourcesForApplication(
1747 iconResource.packageName);
1748 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1749 icon = resources.getDrawable(id);
1750 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001751 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 }
1753 }
1754
1755 if (icon == null) {
1756 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1757 }
1758
1759 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001760 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 info.title = name;
1762 info.iconResource = iconResource;
1763 info.uri = data.getData();
1764 info.baseIntent = baseIntent;
1765 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1766 LiveFolders.DISPLAY_MODE_GRID);
1767
1768 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001769 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001770 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771
1772 return info;
1773 }
1774
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 private void showNotifications() {
1776 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1777 if (statusBar != null) {
1778 statusBar.expand();
1779 }
1780 }
1781
1782 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001783 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001785 Intent chooser = Intent.createChooser(pickWallpaper,
1786 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001787 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1788 // Removed in Eclair MR1
1789// WallpaperManager wm = (WallpaperManager)
1790// getSystemService(Context.WALLPAPER_SERVICE);
1791// WallpaperInfo wi = wm.getWallpaperInfo();
1792// if (wi != null && wi.getSettingsActivity() != null) {
1793// LabeledIntent li = new LabeledIntent(getPackageName(),
1794// R.string.configure_wallpaper, 0);
1795// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1796// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1797// }
Mike Clerona0618e42009-10-22 13:55:21 -07001798 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 }
1800
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001801 /**
1802 * Registers various content observers. The current implementation registers
1803 * only a favorites observer to keep track of the favorites applications.
1804 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001805 private void registerContentObservers() {
1806 ContentResolver resolver = getContentResolver();
1807 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1808 true, mWidgetObserver);
1809 }
1810
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 @Override
1812 public boolean dispatchKeyEvent(KeyEvent event) {
1813 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1814 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001816 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001817 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001818 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001819 dumpState();
1820 return true;
1821 }
1822 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001823 }
1824 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1825 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001826 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 return true;
1828 }
1829 }
1830
1831 return super.dispatchKeyEvent(event);
1832 }
1833
Joe Onorato88ec0992009-11-19 13:16:06 -08001834 @Override
1835 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001836 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1837 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001838 } else {
1839 closeFolder();
1840 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001841 // Some launcher layouts don't have a previous and next view
1842 if (mPreviousView != null) {
1843 dismissPreview(mPreviousView);
1844 dismissPreview(mNextView);
1845 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 private void closeFolder() {
1849 Folder folder = mWorkspace.getOpenFolder();
1850 if (folder != null) {
1851 closeFolder(folder);
1852 }
1853 }
1854
1855 void closeFolder(Folder folder) {
1856 folder.getInfo().opened = false;
1857 ViewGroup parent = (ViewGroup) folder.getParent();
1858 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001859 CellLayout cl = (CellLayout) parent;
1860 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001861 if (folder instanceof DropTarget) {
1862 // Live folders aren't DropTargets.
1863 mDragController.removeDropTarget((DropTarget)folder);
1864 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 }
1866 folder.onClose();
1867 }
1868
1869 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001870 * Re-listen when widgets are reset.
1871 */
1872 private void onAppWidgetReset() {
1873 mAppWidgetHost.startListening();
1874 }
1875
1876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001877 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1878 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001880 private void unbindDesktopItems() {
1881 for (ItemInfo item: mDesktopItems) {
1882 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
1884 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001885
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 /**
1887 * Launches the intent referred by the clicked shortcut.
1888 *
1889 * @param v The view representing the clicked shortcut.
1890 */
1891 public void onClick(View v) {
1892 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001893 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001895 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001896 int[] pos = new int[2];
1897 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001898 intent.setSourceBounds(new Rect(pos[0], pos[1],
1899 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001900 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 } else if (tag instanceof FolderInfo) {
1902 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001903 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001904 if (mState == State.ALL_APPS) {
1905 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001906 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001907 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
1910 }
1911
Michael Jurka0e260592010-06-30 17:07:39 -07001912 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001913 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001914 // clicking anywhere on the workspace causes the customization drawer to slide down
1915 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001916 return false;
1917 }
1918
Michael Jurkaaf442092010-06-10 17:01:57 -07001919 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001920 * Event handler for the search button
1921 *
1922 * @param v The view that was clicked.
1923 */
1924 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001925 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001926 // Use a custom animation for launching search
1927 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001928 }
1929
1930 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001931 * Event handler for the voice button
1932 *
1933 * @param v The view that was clicked.
1934 */
1935 public void onClickVoiceButton(View v) {
1936 startVoiceSearch();
1937 }
1938
1939 private void startVoiceSearch() {
1940 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1941 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1942 startActivity(intent);
1943 }
1944
1945 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001946 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001947 * enters home screen customization mode.
1948 *
1949 * @param v The view that was clicked.
1950 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001951 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001952 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001953 }
1954
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001955 /**
1956 * Event handler for the "grid" button that appears on the home screen, which
1957 * enters all apps mode.
1958 *
1959 * @param v The view that was clicked.
1960 */
1961 public void onClickAllAppsButton(View v) {
1962 showAllApps(true);
1963 }
1964
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001965 public void onClickAppMarketButton(View v) {
1966 if (mAppMarketIntent != null) {
1967 startActivitySafely(mAppMarketIntent, "app market");
1968 }
1969 }
1970
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001971 void startApplicationDetailsActivity(ComponentName componentName) {
1972 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001973 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1974 Uri.fromParts("package", packageName, null));
1975 startActivity(intent);
1976 }
1977
Patrick Dubroy5539af72010-09-07 15:22:01 -07001978 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1979 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1980 // System applications cannot be installed. For now, show a toast explaining that.
1981 // We may give them the option of disabling apps this way.
1982 int messageId = R.string.uninstall_system_app_text;
1983 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1984 } else {
1985 String packageName = appInfo.componentName.getPackageName();
1986 String className = appInfo.componentName.getClassName();
1987 Intent intent = new Intent(
1988 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1989 startActivity(intent);
1990 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001991 }
1992
Joe Onoratof984e852010-03-25 09:47:45 -07001993 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001994 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1995 try {
1996 startActivity(intent);
1997 } catch (ActivityNotFoundException e) {
1998 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001999 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 } catch (SecurityException e) {
2001 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002002 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002004 "or use the exported attribute for this activity. "
2005 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 }
2007 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002008
Romain Guy8e633c52010-03-04 12:51:36 -08002009 void startActivityForResultSafely(Intent intent, int requestCode) {
2010 try {
2011 startActivityForResult(intent, requestCode);
2012 } catch (ActivityNotFoundException e) {
2013 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2014 } catch (SecurityException e) {
2015 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2016 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2017 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2018 "or use the exported attribute for this activity.", e);
2019 }
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021
2022 private void handleFolderClick(FolderInfo folderInfo) {
2023 if (!folderInfo.opened) {
2024 // Close any open folder
2025 closeFolder();
2026 // Open the requested folder
2027 openFolder(folderInfo);
2028 } else {
2029 // Find the open folder...
2030 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2031 int folderScreen;
2032 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002033 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // .. and close it
2035 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002036 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 // Close any folder open on the current screen
2038 closeFolder();
2039 // Pull the folder onto this screen
2040 openFolder(folderInfo);
2041 }
2042 }
2043 }
2044 }
2045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002047 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 * is animated relative to the specified View. If the View is null, no animation
2049 * is played.
2050 *
2051 * @param folderInfo The FolderInfo describing the folder to open.
2052 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002053 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 Folder openFolder;
2055
2056 if (folderInfo instanceof UserFolderInfo) {
2057 openFolder = UserFolder.fromXml(this);
2058 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002059 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } else {
2061 return;
2062 }
2063
Joe Onorato00acb122009-08-04 16:04:30 -04002064 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 openFolder.setLauncher(this);
2066
2067 openFolder.bind(folderInfo);
2068 folderInfo.opened = true;
2069
Winson Chungaafa03c2010-06-11 17:34:16 -07002070 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 openFolder.onOpen();
2073 }
2074
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002076 switch (v.getId()) {
2077 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002078 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002079 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2080 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002081 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002082 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002083 return true;
2084 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002086 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2087 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002088 showPreviews(v);
2089 }
2090 return true;
2091 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002092 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002093 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2094 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2095 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002096 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002097 return true;
2098 }
2099
Joe Onorato9c1289c2009-08-17 11:03:03 -04002100 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 return false;
2102 }
2103
2104 if (!(v instanceof CellLayout)) {
2105 v = (View) v.getParent();
2106 }
2107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
Michael Jurka0280c3b2010-09-17 15:00:07 -07002109 resetAddInfo();
2110 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002112 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 return true;
2114 }
2115
Michael Jurka0280c3b2010-09-17 15:00:07 -07002116 final View itemUnderLongClick = longClickCellInfo.cell;
2117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002119 if (itemUnderLongClick == null) {
2120 // User long pressed on empty space
2121 mWorkspace.setAllowLongPress(false);
2122 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2123 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002124 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002125 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002128 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002129 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002130 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2131 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002132 mAddIntersectCellX = longClickCellInfo.cellX;
2133 mAddIntersectCellY = longClickCellInfo.cellY;
2134 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002135 }
2136 }
2137 }
2138 return true;
2139 }
2140
Romain Guye6b8e2f2009-11-10 11:56:55 -08002141 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002142 private void dismissPreview(final View v) {
2143 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002144 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002145 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2146 public void onDismiss() {
2147 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2148 int count = group.getChildCount();
2149 for (int i = 0; i < count; i++) {
2150 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2151 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002152 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2153 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2154
2155 v.setTag(R.id.workspace, null);
2156 v.setTag(R.id.icon, null);
2157 window.setOnDismissListener(null);
2158 }
2159 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002160 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002161 }
2162 v.setTag(null);
2163 }
2164
Romain Guyf8e6a802009-12-07 17:48:02 -08002165 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002166 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002167 }
2168
Romain Guya6abce82009-11-10 02:54:41 -08002169 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002170 final Resources resources = getResources();
2171 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002172
Romain Guya6abce82009-11-10 02:54:41 -08002173 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002174
Romain Guy9d31e9f2009-11-11 18:54:28 -08002175 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002176
Romain Guyf8e6a802009-12-07 17:48:02 -08002177 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002178 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002179 int extraW = (int) ((r.left + r.right) * max);
2180 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002181
2182 int aW = cell.getWidth() - extraW;
2183 float w = aW / max;
2184
2185 int width = cell.getWidth();
2186 int height = cell.getHeight();
2187 int x = cell.getLeftPadding();
2188 int y = cell.getTopPadding();
2189 width -= (x + cell.getRightPadding());
2190 height -= (y + cell.getBottomPadding());
2191
2192 float scale = w / width;
2193
2194 int count = end - start;
2195
2196 final float sWidth = width * scale;
2197 float sHeight = height * scale;
2198
Romain Guye6b8e2f2009-11-10 11:56:55 -08002199 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002200
Romain Guye6b8e2f2009-11-10 11:56:55 -08002201 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2202 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002203
2204 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002205 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002206 cell = (CellLayout) workspace.getChildAt(i);
2207
Romain Guyf8e6a802009-12-07 17:48:02 -08002208 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002209 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002210
2211 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002212 c.scale(scale, scale);
2213 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002214 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002215
Romain Guy9d31e9f2009-11-11 18:54:28 -08002216 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002217 image.setImageBitmap(bitmap);
2218 image.setTag(i);
2219 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002220 image.setOnFocusChangeListener(handler);
2221 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002222 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002223
2224 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002225 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2226
Winson Chungaafa03c2010-06-11 17:34:16 -07002227 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002228 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002229
2230 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002231 p.setContentView(preview);
2232 p.setWidth((int) (sWidth * count + extraW));
2233 p.setHeight((int) (sHeight + extraH));
2234 p.setAnimationStyle(R.style.AnimationPreview);
2235 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002236 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002237 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002238 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002239
Romain Guye6b8e2f2009-11-10 11:56:55 -08002240 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2241 public void onDismiss() {
2242 dismissPreview(anchor);
2243 }
2244 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002245
2246 anchor.setTag(p);
2247 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002248 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002249 }
2250
Romain Guy9d31e9f2009-11-11 18:54:28 -08002251 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002252 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002253
Romain Guye6b8e2f2009-11-10 11:56:55 -08002254 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002255 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002256 }
2257
2258 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002259 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002260 v.post(this);
2261 }
2262
2263 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002264 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002265 }
2266
2267 public void onFocusChange(View v, boolean hasFocus) {
2268 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002269 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002270 }
Romain Guya6abce82009-11-10 02:54:41 -08002271 }
2272 }
2273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 Workspace getWorkspace() {
2275 return mWorkspace;
2276 }
2277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 @Override
2279 protected Dialog onCreateDialog(int id) {
2280 switch (id) {
2281 case DIALOG_CREATE_SHORTCUT:
2282 return new CreateShortcut().createDialog();
2283 case DIALOG_RENAME_FOLDER:
2284 return new RenameFolder().createDialog();
2285 }
2286
2287 return super.onCreateDialog(id);
2288 }
2289
2290 @Override
2291 protected void onPrepareDialog(int id, Dialog dialog) {
2292 switch (id) {
2293 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 break;
2295 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002296 if (mFolderInfo != null) {
2297 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2298 final CharSequence text = mFolderInfo.title;
2299 input.setText(text);
2300 input.setSelection(0, text.length());
2301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 break;
2303 }
2304 }
2305
2306 void showRenameDialog(FolderInfo info) {
2307 mFolderInfo = info;
2308 mWaitingForResult = true;
2309 showDialog(DIALOG_RENAME_FOLDER);
2310 }
2311
Michael Jurka0280c3b2010-09-17 15:00:07 -07002312 private void showAddDialog(int intersectX, int intersectY) {
2313 resetAddInfo();
2314 mAddIntersectCellX = intersectX;
2315 mAddIntersectCellY = intersectY;
2316 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 mWaitingForResult = true;
2318 showDialog(DIALOG_CREATE_SHORTCUT);
2319 }
2320
Joe Onoratodeb98af2010-02-19 14:59:39 -08002321 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002322 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002323 Bundle bundle = new Bundle();
2324
2325 ArrayList<String> shortcutNames = new ArrayList<String>();
2326 shortcutNames.add(getString(R.string.group_applications));
2327 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2328
2329 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2330 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2331 R.drawable.ic_launcher_application));
2332 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2333
2334 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2335 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002336 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002337 pickIntent.putExtras(bundle);
2338
Joe Onoratodeb98af2010-02-19 14:59:39 -08002339 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002340 }
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 private class RenameFolder {
2343 private EditText mInput;
2344
2345 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2347 mInput = (EditText) layout.findViewById(R.id.folder_name);
2348
2349 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2350 builder.setIcon(0);
2351 builder.setTitle(getString(R.string.rename_folder_title));
2352 builder.setCancelable(true);
2353 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2354 public void onCancel(DialogInterface dialog) {
2355 cleanup();
2356 }
2357 });
2358 builder.setNegativeButton(getString(R.string.cancel_action),
2359 new Dialog.OnClickListener() {
2360 public void onClick(DialogInterface dialog, int which) {
2361 cleanup();
2362 }
2363 }
2364 );
2365 builder.setPositiveButton(getString(R.string.rename_action),
2366 new Dialog.OnClickListener() {
2367 public void onClick(DialogInterface dialog, int which) {
2368 changeFolderName();
2369 }
2370 }
2371 );
2372 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002373
2374 final AlertDialog dialog = builder.create();
2375 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2376 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002377 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002378 mInput.requestFocus();
2379 InputMethodManager inputManager = (InputMethodManager)
2380 getSystemService(Context.INPUT_METHOD_SERVICE);
2381 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002382 }
2383 });
2384
2385 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387
2388 private void changeFolderName() {
2389 final String name = mInput.getText().toString();
2390 if (!TextUtils.isEmpty(name)) {
2391 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002392 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 mFolderInfo.title = name;
2394 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2395
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002397 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002399 } else {
2400 final FolderIcon folderIcon = (FolderIcon)
2401 mWorkspace.getViewForTag(mFolderInfo);
2402 if (folderIcon != null) {
2403 folderIcon.setText(name);
2404 getWorkspace().requestLayout();
2405 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002406 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002407 mWorkspaceLoading = true;
2408 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410 }
2411 }
2412 cleanup();
2413 }
2414
2415 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 dismissDialog(DIALOG_RENAME_FOLDER);
2417 mWaitingForResult = false;
2418 mFolderInfo = null;
2419 }
2420 }
2421
Daniel Sandler843e8602010-06-07 14:59:01 -04002422 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2423 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002424 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002425 }
2426
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002427 // AllAppsView.Watcher
2428 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002429 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2430 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002431 mWorkspace.setVisibility(View.GONE);
2432 }
2433 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002434
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435 private void showToolbarButton(View button) {
2436 button.setAlpha(1.0f);
2437 button.setVisibility(View.VISIBLE);
2438 button.setFocusable(true);
2439 button.setClickable(true);
2440 }
2441
2442 private void hideToolbarButton(View button) {
2443 button.setAlpha(0.0f);
2444 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2445 button.setVisibility(View.INVISIBLE);
2446 button.setFocusable(false);
2447 button.setClickable(false);
2448 }
2449
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002450 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002451 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002452 *
2453 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2454 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002455 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002456 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002457 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002458 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459 final boolean showing = show;
2460 final boolean hiding = !show;
2461
2462 final int duration = show ?
2463 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2464 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2465
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002466 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002467 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2468 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002469 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002470 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002471 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002473 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002474 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002475 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002476 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002477 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 });
2479 seq.play(anim);
2480 } else {
2481 if (showing) {
2482 showToolbarButton(view);
2483 } else {
2484 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002485 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 }
2488
2489 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490 * Show/hide the appropriate toolbar buttons for newState.
2491 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 *
2493 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002494 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2495 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002496 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002497 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002498 final View searchButton = findViewById(R.id.search_button_cluster);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002499 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002500 final View configureButton = findViewById(R.id.configure_button);
2501
2502 switch (newState) {
2503 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504 hideOrShowToolbarButton(true, searchButton, showSeq);
2505 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2506 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002507 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002508 break;
2509 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002510 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002511 hideOrShowToolbarButton(false, searchButton, hideSeq);
2512 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002513 break;
2514 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002515 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002516 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002518 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002519 break;
2520 }
2521 }
2522
2523 /**
2524 * Helper method for the cameraZoomIn/cameraZoomOut animations
2525 * @param view The view being animated
2526 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2527 * @param scaleFactor The scale factor used for the zoom
2528 */
2529 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2530 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002531
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002532 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002533 // Set pivotY so that at the starting zoom factor, the view is partially
2534 // visible. Modifying initialHeightFactor changes how much of the view is
2535 // initially showing, and hence the perceived angle from which the view enters.
2536 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002538 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002539 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002540 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 }
2542 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002543
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002544 /**
2545 * Zoom the camera out from the workspace to reveal 'toView'.
2546 * Assumes that the view to show is anchored at either the very top or very bottom
2547 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002548 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002549 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002551 final Resources res = getResources();
2552 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2553 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002554 final boolean toAllApps = (toState == State.ALL_APPS);
2555 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002556
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002557 setPivotsForZoom(toView, toState, scale);
2558
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002559 if (toAllApps) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002560 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002561 } else {
2562 mWorkspace.shrinkToTop(animated);
2563 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002564
2565 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002566 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2567 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2568 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2569 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002570
2571 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002572 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002573 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002574 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 // Prepare the position
2576 toView.setTranslationX(0.0f);
2577 toView.setTranslationY(0.0f);
2578 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002579 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002581 @Override
2582 public void onAnimationEndOrCancel(Animator animation) {
2583 // If we don't set the final scale values here, if this animation is cancelled
2584 // it will have the wrong scale value and subsequent cameraPan animations will
2585 // not fix that
2586 toView.setScaleX(1.0f);
2587 toView.setScaleY(1.0f);
2588 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 });
2590
Chet Haaseb1254a62010-09-07 13:35:00 -07002591 AnimatorSet toolbarHideAnim = new AnimatorSet();
2592 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002593 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2594
2595 // toView should appear right at the end of the workspace shrink animation
2596 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2597
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002598 if (mStateAnimation != null) mStateAnimation.cancel();
2599 mStateAnimation = new AnimatorSet();
2600 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2601 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002602
2603 // Show the new toolbar buttons just as the main animation is ending
2604 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002605 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2606 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002607 } else {
2608 toView.setTranslationX(0.0f);
2609 toView.setTranslationY(0.0f);
2610 toView.setScaleX(1.0f);
2611 toView.setScaleY(1.0f);
2612 toView.setVisibility(View.VISIBLE);
2613 hideAndShowToolbarButtons(toState, null, null);
2614 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002615 }
2616
2617 /**
2618 * Zoom the camera back into the workspace, hiding 'fromView'.
2619 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2621 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002622 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002624 Resources res = getResources();
2625 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2626 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 final View fromView =
2628 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2629
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002630 mCustomizePagedView.endChoiceMode();
2631 mAllAppsPagedView.endChoiceMode();
2632
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002633 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002634
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002635 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636
2637 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002638 if (mStateAnimation != null) mStateAnimation.cancel();
2639 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002640 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2641 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2642 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2643 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002644 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2645
2646 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2647 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2648 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2649 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002650 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002651 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002652 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 }
2654 });
2655
Chet Haaseb1254a62010-09-07 13:35:00 -07002656 AnimatorSet toolbarHideAnim = new AnimatorSet();
2657 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002658 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2659
Adam Cohen61033d32010-11-15 18:29:44 -08002660 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661
2662 // Show the new toolbar buttons at the very end of the whole animation
2663 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2664 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002665 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2666 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002667 } else {
2668 fromView.setVisibility(View.GONE);
2669 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2670 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 }
2672
2673 /**
2674 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2675 * This is the transition used on xlarge screens to go between all apps and
2676 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2678 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2679 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002680 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 final Resources res = getResources();
2683 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002684 final int workspaceHeight = mWorkspace.getHeight();
2685
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002686 final boolean fromAllApps = (fromState == State.ALL_APPS);
2687 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2688 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002689
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002690 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2691 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2692 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2693 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002694
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002695 mCustomizePagedView.endChoiceMode();
2696 mAllAppsPagedView.endChoiceMode();
2697
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002698 if (toState == State.ALL_APPS) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002699 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002700 } else {
2701 mWorkspace.shrinkToTop(animated);
2702 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703
2704 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002705 if (mStateAnimation != null) mStateAnimation.cancel();
2706 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002707 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002708 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002709 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002710 toView.setVisibility(View.VISIBLE);
2711 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002712 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002713 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002714 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002715 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 fromView.setVisibility(View.GONE);
2717 }
2718 });
2719
Chet Haaseb1254a62010-09-07 13:35:00 -07002720 AnimatorSet toolbarHideAnim = new AnimatorSet();
2721 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2723
Chet Haase472b2812010-10-14 07:02:04 -07002724 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2725 fromViewStartY, fromViewEndY);
2726 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002727 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2728 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2729 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2730 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2731 );
Chet Haase472b2812010-10-14 07:02:04 -07002732 fromAnim.setDuration(duration);
2733 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002734
2735 // Show the new toolbar buttons just as the main animation is ending
2736 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002737 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2738 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002739 } else {
2740 fromView.setY(fromViewEndY);
2741 fromView.setVisibility(View.GONE);
2742 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002743 toView.setScaleX(1.0f);
2744 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002745 toView.setVisibility(View.VISIBLE);
2746 hideAndShowToolbarButtons(toState, null, null);
2747 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002748 }
2749
2750 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002751 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002752 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002753 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002754
Michael Jurka79212d82010-07-30 16:36:20 -07002755 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002756 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002757 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002758 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002759 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002760 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002761 } else {
2762 mAllAppsGrid.zoom(1.0f, animated);
2763 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002764
Romain Guyc16fea72010-03-12 17:17:56 -08002765 ((View) mAllAppsGrid).setFocusable(true);
2766 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002767
Joe Onorato7c312c12009-08-13 21:36:53 -07002768 // TODO: fade these two too
2769 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002770
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002771 // Change the state *after* we've called all the transition code
2772 mState = State.ALL_APPS;
2773 }
2774
2775
2776 void showWorkspace(boolean animated) {
2777 showWorkspace(animated, null);
2778 }
2779
2780 void showWorkspace(boolean animated, CellLayout layout) {
2781 if (layout != null && animated) {
2782 mWorkspace.unshrink(layout);
2783 } else {
2784 mWorkspace.unshrink(animated);
2785 }
2786 if (mState == State.ALL_APPS) {
2787 closeAllApps(animated);
2788 } else if (mState == State.CUSTOMIZE) {
2789 hideCustomizationDrawer(animated);
2790 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002791
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002792 // Change the state *after* we've called all the transition code
2793 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002794 }
2795
Joe Onoratob2061212009-11-24 16:13:54 -05002796 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002797 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002798 * - Home from workspace
2799 * - from center screen
2800 * - from other screens
2801 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002802 * - from center screen
2803 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002804 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002805 * - from center screen
2806 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002807 * - Launch app from workspace and quit
2808 * - with back
2809 * - with home
2810 * - Launch app from all apps and quit
2811 * - with back
2812 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002813 * - Go to a screen that's not the default, then all
2814 * apps, and launch and app, and go back
2815 * - with back
2816 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002817 * - On workspace, long press power and go back
2818 * - with back
2819 * - with home
2820 * - On all apps, long press power and go back
2821 * - with back
2822 * - with home
2823 * - On workspace, power off
2824 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002825 * - Launch an app and turn off the screen while in that app
2826 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002827 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002828 * - From all apps
2829 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002830 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2831 * - From all apps
2832 * - From the center workspace
2833 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002834 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002835 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002836 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002837 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002838 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002839 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002840 } else {
2841 mAllAppsGrid.zoom(0.0f, animated);
2842 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002843 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002844 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002845 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002846 }
2847
Joe Onorato7c312c12009-08-13 21:36:53 -07002848 void lockAllApps() {
2849 // TODO
2850 }
2851
2852 void unlockAllApps() {
2853 // TODO
2854 }
2855
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002856 // Show the customization drawer (only exists in x-large configuration)
2857 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002858 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002859 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002860 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002861 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002862 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002863 // Change the state *after* we've called all the transition code
2864 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002865 }
2866
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002867 // Hide the customization drawer (only exists in x-large configuration)
2868 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002869 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002870 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002871 }
2872 }
2873
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002874 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2875 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2876 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002877 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002878 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002879
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002880 void onWorkspaceClick(CellLayout layout) {
2881 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002882 }
2883
Michael Jurka0423dcf2010-10-05 14:56:18 -07002884 private void updateButtonWithIconFromExternalActivity(
2885 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2886 ImageView button = (ImageView) findViewById(buttonId);
2887 Drawable toolbarIcon = null;
2888 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002889 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002890 // Look for the toolbar icon specified in the activity meta-data
2891 Bundle metaData = packageManager.getActivityInfo(
2892 activityName, PackageManager.GET_META_DATA).metaData;
2893 if (metaData != null) {
2894 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2895 if (iconResId != 0) {
2896 Resources res = packageManager.getResourcesForActivity(activityName);
2897 toolbarIcon = res.getDrawable(iconResId);
2898 }
2899 }
2900 } catch (NameNotFoundException e) {
2901 // Do nothing
2902 }
2903 // If we were unable to find the icon via the meta-data, use a generic one
2904 if (toolbarIcon == null) {
2905 button.setImageResource(fallbackDrawableId);
2906 } else {
2907 button.setImageDrawable(toolbarIcon);
2908 }
2909 }
2910
2911 private void updateGlobalSearchIcon() {
2912 if (LauncherApplication.isScreenXLarge()) {
2913 final SearchManager searchManager =
2914 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2915 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002916 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002917 updateButtonWithIconFromExternalActivity(
2918 R.id.search_button, activityName, R.drawable.search_button_generic);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002919 } else {
2920 findViewById(R.id.search_button).setVisibility(View.GONE);
2921 }
2922 }
2923 }
2924
2925 private void updateVoiceSearchIcon() {
2926 if (LauncherApplication.isScreenXLarge()) {
2927 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2928 ComponentName activityName = intent.resolveActivity(getPackageManager());
2929 if (activityName != null) {
2930 updateButtonWithIconFromExternalActivity(
2931 R.id.voice_button, activityName, R.drawable.ic_voice_search);
2932 } else {
2933 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002934 }
2935 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002936 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002937
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002938 /**
2939 * Sets the app market icon (shown when all apps is visible on x-large screens)
2940 */
2941 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002942 if (LauncherApplication.isScreenXLarge()) {
2943 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2944 // Find the app market activity by resolving an intent.
2945 // (If multiple app markets are installed, it will return the ResolverActivity.)
2946 ComponentName activityName = intent.resolveActivity(getPackageManager());
2947 if (activityName != null) {
2948 mAppMarketIntent = intent;
2949 updateButtonWithIconFromExternalActivity(
2950 R.id.market_button, activityName, R.drawable.app_market_generic);
2951 }
2952 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002953 }
2954
2955 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 * Displays the shortcut creation dialog and launches, if necessary, the
2957 * appropriate activity.
2958 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002959 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002960 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2961 DialogInterface.OnShowListener {
2962
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002967
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2969 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002970 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002972 builder.setInverseBackgroundForced(true);
2973
2974 AlertDialog dialog = builder.create();
2975 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002976 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002977 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 return dialog;
2980 }
2981
2982 public void onCancel(DialogInterface dialog) {
2983 mWaitingForResult = false;
2984 cleanup();
2985 }
2986
Romain Guycbb89e42009-06-08 15:52:54 -07002987 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002988 }
2989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002991 try {
2992 dismissDialog(DIALOG_CREATE_SHORTCUT);
2993 } catch (Exception e) {
2994 // An exception is thrown if the dialog is not visible, which is fine
2995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002996 }
2997
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002998 /**
2999 * Handle the action clicked in the "Add to home" dialog.
3000 */
3001 public void onClick(DialogInterface dialog, int which) {
3002 Resources res = getResources();
3003 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003004
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003005 switch (which) {
3006 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003007 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003008 break;
3009 }
Romain Guycbb89e42009-06-08 15:52:54 -07003010
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003011 case AddAdapter.ITEM_APPWIDGET: {
3012 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003013
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003014 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3015 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003016 // start the pick activity
3017 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3018 break;
3019 }
Romain Guycbb89e42009-06-08 15:52:54 -07003020
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003021 case AddAdapter.ITEM_LIVE_FOLDER: {
3022 // Insert extra item to handle inserting folder
3023 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003024
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003025 ArrayList<String> shortcutNames = new ArrayList<String>();
3026 shortcutNames.add(res.getString(R.string.group_folder));
3027 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003028
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003029 ArrayList<ShortcutIconResource> shortcutIcons =
3030 new ArrayList<ShortcutIconResource>();
3031 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3032 R.drawable.ic_launcher_folder));
3033 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3034
3035 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3036 pickIntent.putExtra(Intent.EXTRA_INTENT,
3037 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3038 pickIntent.putExtra(Intent.EXTRA_TITLE,
3039 getText(R.string.title_select_live_folder));
3040 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003041
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003042 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3043 break;
3044 }
3045
3046 case AddAdapter.ITEM_WALLPAPER: {
3047 startWallpaper();
3048 break;
3049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050 }
3051 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003052
3053 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003054 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003055 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 }
3057
3058 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003059 * Receives notifications when applications are added/removed.
3060 */
3061 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3062 @Override
3063 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003064 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003065 String reason = intent.getStringExtra("reason");
3066 if (!"homekey".equals(reason)) {
3067 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003068 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003069 animate = false;
3070 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003071 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003072 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003073 }
3074 }
3075
3076 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003077 * Receives notifications whenever the appwidgets are reset.
3078 */
3079 private class AppWidgetResetObserver extends ContentObserver {
3080 public AppWidgetResetObserver() {
3081 super(new Handler());
3082 }
3083
3084 @Override
3085 public void onChange(boolean selfChange) {
3086 onAppWidgetReset();
3087 }
3088 }
3089
3090 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003091 * If the activity is currently paused, signal that we need to re-run the loader
3092 * in onResume.
3093 *
3094 * This needs to be called from incoming places where resources might have been loaded
3095 * while we are paused. That is becaues the Configuration might be wrong
3096 * when we're not running, and if it comes back to what it was when we
3097 * were paused, we are not restarted.
3098 *
3099 * Implementation of the method from LauncherModel.Callbacks.
3100 *
3101 * @return true if we are currently paused. The caller might be able to
3102 * skip some work in that case since we will come back again.
3103 */
3104 public boolean setLoadOnResume() {
3105 if (mPaused) {
3106 Log.i(TAG, "setLoadOnResume");
3107 mOnResumeNeedsLoad = true;
3108 return true;
3109 } else {
3110 return false;
3111 }
3112 }
3113
3114 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003116 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003118 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003119 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003120 } else {
3121 return SCREEN_COUNT / 2;
3122 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003124
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003125 void setAllAppsPagedView(PagedView view) {
3126 mAllAppsPagedView = view;
3127 }
3128
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 /**
3130 * Refreshes the shortcuts shown on the workspace.
3131 *
3132 * Implementation of the method from LauncherModel.Callbacks.
3133 */
3134 public void startBinding() {
3135 final Workspace workspace = mWorkspace;
3136 int count = workspace.getChildCount();
3137 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003138 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3140 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003141
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 if (DEBUG_USER_INTERFACE) {
3143 android.widget.Button finishButton = new android.widget.Button(this);
3144 finishButton.setText("Finish");
3145 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3146
3147 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3148 public void onClick(View v) {
3149 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003150 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 }
3153 }
3154
3155 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 * Bind the items start-end from the list.
3157 *
3158 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003160 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3161
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003162 setLoadOnResume();
3163
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 final Workspace workspace = mWorkspace;
3165
3166 for (int i=start; i<end; i++) {
3167 final ItemInfo item = shortcuts.get(i);
3168 mDesktopItems.add(item);
3169 switch (item.itemType) {
3170 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3171 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003172 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003173 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3174 false);
3175 break;
3176 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3177 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003178 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003179 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3181 false);
3182 break;
3183 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3184 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3185 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003186 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003187 (LiveFolderInfo) item);
3188 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3189 false);
3190 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192 }
3193
Joe Onorato9c1289c2009-08-17 11:03:03 -04003194 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 }
3196
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 /**
3198 * Implementation of the method from LauncherModel.Callbacks.
3199 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003200 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003201 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003202 sFolders.clear();
3203 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003204 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003205
3206 /**
3207 * Add the views for a widget to the workspace.
3208 *
3209 * Implementation of the method from LauncherModel.Callbacks.
3210 */
3211 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003212 setLoadOnResume();
3213
Daniel Sandler843e8602010-06-07 14:59:01 -04003214 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3215 if (DEBUG_WIDGETS) {
3216 Log.d(TAG, "bindAppWidget: " + item);
3217 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003218 final Workspace workspace = mWorkspace;
3219
3220 final int appWidgetId = item.appWidgetId;
3221 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003222 if (DEBUG_WIDGETS) {
3223 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3224 }
3225
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3227
Joe Onorato9c1289c2009-08-17 11:03:03 -04003228 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3229 item.hostView.setTag(item);
3230
3231 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3232 item.cellY, item.spanX, item.spanY, false);
3233
Adam Cohended9f8d2010-11-03 13:25:16 -07003234 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3235
Joe Onorato9c1289c2009-08-17 11:03:03 -04003236 workspace.requestLayout();
3237
3238 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003239
3240 if (DEBUG_WIDGETS) {
3241 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3242 + (SystemClock.uptimeMillis()-start) + "ms");
3243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003244 }
3245
3246 /**
3247 * Callback saying that there aren't any more items to bind.
3248 *
3249 * Implementation of the method from LauncherModel.Callbacks.
3250 */
3251 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003252 setLoadOnResume();
3253
Joe Onorato9c1289c2009-08-17 11:03:03 -04003254 if (mSavedState != null) {
3255 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003256 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003257 }
3258
3259 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3260 if (userFolders != null) {
3261 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003262 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 if (info != null) {
3264 openFolder(info);
3265 }
3266 }
3267 final Folder openFolder = mWorkspace.getOpenFolder();
3268 if (openFolder != null) {
3269 openFolder.requestFocus();
3270 }
3271 }
3272
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 mSavedState = null;
3274 }
3275
3276 if (mSavedInstanceState != null) {
3277 super.onRestoreInstanceState(mSavedInstanceState);
3278 mSavedInstanceState = null;
3279 }
3280
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 mWorkspaceLoading = false;
3282 }
3283
3284 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003285 * Updates the icons on the launcher that are affected by changes to the package list
3286 * on the device.
3287 */
3288 private void updateIconsAffectedByPackageManagerChanges() {
3289 updateAppMarketIcon();
3290 updateGlobalSearchIcon();
3291 updateVoiceSearchIcon();
3292 }
3293
3294 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003295 * Add the icons for all apps.
3296 *
3297 * Implementation of the method from LauncherModel.Callbacks.
3298 */
3299 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003300 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003301 if (mCustomizePagedView != null) {
3302 mCustomizePagedView.setApps(apps);
3303 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003304 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003305 }
3306
3307 /**
3308 * A package was installed.
3309 *
3310 * Implementation of the method from LauncherModel.Callbacks.
3311 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003312 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003313 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003315 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003316 if (mCustomizePagedView != null) {
3317 mCustomizePagedView.addApps(apps);
3318 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003319 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003320 }
3321
3322 /**
3323 * A package was updated.
3324 *
3325 * Implementation of the method from LauncherModel.Callbacks.
3326 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003327 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003328 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003329 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003330 mWorkspace.updateShortcuts(apps);
3331 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003332 if (mCustomizePagedView != null) {
3333 mCustomizePagedView.updateApps(apps);
3334 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003335 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
3337
3338 /**
3339 * A package was uninstalled.
3340 *
3341 * Implementation of the method from LauncherModel.Callbacks.
3342 */
Joe Onorato36115782010-06-17 13:28:48 -04003343 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003344 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003345 if (permanent) {
3346 mWorkspace.removeItems(apps);
3347 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003348 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003349 if (mCustomizePagedView != null) {
3350 mCustomizePagedView.removeApps(apps);
3351 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003352 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353 }
Joe Onoratobe386092009-11-17 17:32:16 -08003354
3355 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003356 * A number of packages were updated.
3357 */
3358 public void bindPackagesUpdated() {
3359 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003360 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003361 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003362 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003363 }
3364
3365 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003366 * Prints out out state for debugging.
3367 */
3368 public void dumpState() {
3369 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003370 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003371 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3372 Log.d(TAG, "mRestoring=" + mRestoring);
3373 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3374 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3375 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003376 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003377 mModel.dumpState();
3378 mAllAppsGrid.dumpState();
3379 Log.d(TAG, "END launcher2 dump state");
3380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003381}