blob: 9954f39d52bfd17d17bc6862f3679dd57df12edc [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.view.View.OnLongClickListener;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070092import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700101import android.widget.TextView;
102import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700103import android.widget.TabHost.OnTabChangeListener;
104import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Winson Chung68846fd2010-10-29 11:00:27 -0700106import com.android.common.Search;
107import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800108
Adam Cohended9f8d2010-11-03 13:25:16 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
134 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
138 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato7c312c12009-08-13 21:36:53 -0700147 static final int DIALOG_CREATE_SHORTCUT = 1;
148 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: long
157 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 // tags for the customization tabs
170 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700171 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700172 private static final String SHORTCUTS_TAG = "shortcuts";
173 private static final String WALLPAPERS_TAG = "wallpapers";
174
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700175 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
176
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177 /** The different states that Launcher can be in. */
178 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700179 private State mState = State.WORKSPACE;
180 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800187 private final BroadcastReceiver mCloseSystemDialogsReceiver
188 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800189 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private LayoutInflater mInflater;
192
Joe Onorato00acb122009-08-04 16:04:30 -0400193 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 private int mAddScreen = -1;
200 private int mAddIntersectCellX = -1;
201 private int mAddIntersectCellY = -1;
202 private int[] mAddDropPosition;
203 private int[] mTmpAddItemCellCoordinates = new int[2];
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private FolderInfo mFolderInfo;
206
Joe Onorato7c312c12009-08-13 21:36:53 -0700207 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700209 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700210 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700211 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700212
213 private PagedView mAllAppsPagedView = null;
214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Romain Guy1fbc1c82009-11-09 20:43:08 -0800241 private ImageView mPreviousView;
242 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500243
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400244 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400245 private String[] mHotseatConfig = null;
246 private Intent[] mHotseats = null;
247 private Drawable[] mHotseatIcons = null;
248 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400249
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700250 private Intent mAppMarketIntent = null;
251
Adam Cohended9f8d2010-11-03 13:25:16 -0700252 // Related to the auto-advancing of widgets
253 private final int ADVANCE_MSG = 1;
254 private final int mAdvanceInterval = 20000;
255 private final int mAdvanceStagger = 250;
256 private long mAutoAdvanceSentTime;
257 private long mAutoAdvanceTimeLeft = -1;
258 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
259 new HashMap<View, AppWidgetProviderInfo>();
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 @Override
262 protected void onCreate(Bundle savedInstanceState) {
263 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700264
Winson Chungaafa03c2010-06-11 17:34:16 -0700265 if (LauncherApplication.isInPlaceRotationEnabled()) {
266 // hide the status bar (temporary until we get the status bar design figured out)
267 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
268 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
269 }
270
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 LauncherApplication app = ((LauncherApplication)getApplication());
272 mModel = app.setLauncher(this);
273 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400274 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700276
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
279 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200282 android.os.Debug.startMethodTracing(
283 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 }
285
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400286 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 checkForLocaleChange();
288 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700290 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
291 if (mHomeCustomizationDrawer != null) {
292 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700293
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700295 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
296 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700297 TabContentFactory contentFactory = new TabContentFactory() {
298 public View createTabContent(String tag) {
299 return mCustomizePagedView;
300 }
301 };
302
Michael Jurka946ad472010-07-09 18:05:18 -0700303 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
305 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700306 String applicationsLabel = getString(R.string.applications_tab_label);
307 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
308 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700309 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
311 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700312 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
313 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
314 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700315 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
316 public void onTabChanged(String tabId) {
317 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800318 final Resources res = getResources();
319 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700320 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700321 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700323 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700324 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700325 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700326 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
328 if (tag == WIDGETS_TAG) {
329 mCustomizePagedView.setCustomizationFilter(
330 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700331 } else if (tag == APPLICATIONS_TAG) {
332 mCustomizePagedView.setCustomizationFilter(
333 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 } else if (tag == WALLPAPERS_TAG) {
335 mCustomizePagedView.setCustomizationFilter(
336 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700337 } else if (tag == SHORTCUTS_TAG) {
338 mCustomizePagedView.setCustomizationFilter(
339 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 }
341
342 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700343 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700344 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700345 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 alphaAnim.start();
347 }
348 });
349 alphaAnim.start();
350 }
351 });
Michael Jurka946ad472010-07-09 18:05:18 -0700352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 setupViews();
354
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800355 registerContentObservers();
356
Joe Onorato7c312c12009-08-13 21:36:53 -0700357 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 mSavedState = savedInstanceState;
360 restoreState(mSavedState);
361
362 if (PROFILE_STARTUP) {
363 android.os.Debug.stopMethodTracing();
364 }
365
366 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
369
370 // For handling default keys
371 mDefaultKeySsb = new SpannableStringBuilder();
372 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800373
374 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
375 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
Romain Guycbb89e42009-06-08 15:52:54 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700379 if (sLocaleConfiguration == null) {
380 new AsyncTask<Void, Void, LocaleConfiguration>() {
381 @Override
382 protected LocaleConfiguration doInBackground(Void... unused) {
383 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
384 readConfiguration(Launcher.this, localeConfiguration);
385 return localeConfiguration;
386 }
387
388 @Override
389 protected void onPostExecute(LocaleConfiguration result) {
390 sLocaleConfiguration = result;
391 checkForLocaleChange(); // recursive, but now with a locale configuration
392 }
393 }.execute();
394 return;
395 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final Configuration configuration = getResources().getConfiguration();
398
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final String locale = configuration.locale.toString();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final int mcc = configuration.mcc;
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mnc = configuration.mnc;
407
Romain Guy84f296c2009-11-04 15:00:44 -0800408 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409
Romain Guy84f296c2009-11-04 15:00:44 -0800410 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700411 sLocaleConfiguration.locale = locale;
412 sLocaleConfiguration.mcc = mcc;
413 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700414
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400416 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417
418 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
419 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700420 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 public void run() {
422 writeConfiguration(Launcher.this, localeConfiguration);
423 }
424 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700425 }
426 }
427
428 private static class LocaleConfiguration {
429 public String locale;
430 public int mcc = -1;
431 public int mnc = -1;
432 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700433
Romain Guy98d01652009-06-30 16:21:04 -0700434 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
435 DataInputStream in = null;
436 try {
437 in = new DataInputStream(context.openFileInput(PREFERENCES));
438 configuration.locale = in.readUTF();
439 configuration.mcc = in.readInt();
440 configuration.mnc = in.readInt();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 // Ignore
445 } finally {
446 if (in != null) {
447 try {
448 in.close();
449 } catch (IOException e) {
450 // Ignore
451 }
452 }
453 }
454 }
455
456 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
457 DataOutputStream out = null;
458 try {
459 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
460 out.writeUTF(configuration.locale);
461 out.writeInt(configuration.mcc);
462 out.writeInt(configuration.mnc);
463 out.flush();
464 } catch (FileNotFoundException e) {
465 // Ignore
466 } catch (IOException e) {
467 //noinspection ResultOfMethodCallIgnored
468 context.getFileStreamPath(PREFERENCES).delete();
469 } finally {
470 if (out != null) {
471 try {
472 out.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478 }
479
480 static int getScreen() {
481 synchronized (sLock) {
482 return sScreen;
483 }
484 }
485
486 static void setScreen(int screen) {
487 synchronized (sLock) {
488 sScreen = screen;
489 }
490 }
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700493 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
495 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700496 // TODO: Put back when we decide about scrolling the wallpaper
497 // boolean isPortrait = display.getWidth() < display.getHeight();
498 // final int width = isPortrait ? display.getWidth() : display.getHeight();
499 // final int height = isPortrait ? display.getHeight() : display.getWidth();
500 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
501 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 }
503
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 // Note: This doesn't do all the client-id magic that BrowserProvider does
505 // in Browser. (http://b/2425179)
506 private Uri getDefaultBrowserUri() {
507 String url = getString(R.string.default_browser_url);
508 if (url.indexOf("{CID}") != -1) {
509 url = url.replace("{CID}", "android-google");
510 }
511 return Uri.parse(url);
512 }
513
514 // Load the Intent templates from arrays.xml to populate the hotseats. For
515 // each Intent, if it resolves to a single app, use that as the launch
516 // intent & use that app's label as the contentDescription. Otherwise,
517 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400518 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 if (mHotseatConfig == null) {
520 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
521 if (mHotseatConfig.length > 0) {
522 mHotseats = new Intent[mHotseatConfig.length];
523 mHotseatLabels = new CharSequence[mHotseatConfig.length];
524 mHotseatIcons = new Drawable[mHotseatConfig.length];
525 } else {
526 mHotseats = null;
527 mHotseatIcons = null;
528 mHotseatLabels = null;
529 }
530
531 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
532 for (int i=0; i<mHotseatConfig.length; i++) {
533 // load icon for this slot; currently unrelated to the actual activity
534 try {
535 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
536 } catch (ArrayIndexOutOfBoundsException ex) {
537 Log.w(TAG, "Missing hotseat_icons array item #" + i);
538 mHotseatIcons[i] = null;
539 }
540 }
541 hotseatIconDrawables.recycle();
542 }
543
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400544 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 for (int i=0; i<mHotseatConfig.length; i++) {
546 Intent intent = null;
547 if (mHotseatConfig[i].equals("*BROWSER*")) {
548 // magic value meaning "launch user's default web browser"
549 // replace it with a generic web request so we can see if there is indeed a default
550 String defaultUri = getString(R.string.default_browser_url);
551 intent = new Intent(
552 Intent.ACTION_VIEW,
553 ((defaultUri != null)
554 ? Uri.parse(defaultUri)
555 : getDefaultBrowserUri())
556 ).addCategory(Intent.CATEGORY_BROWSABLE);
557 // note: if the user launches this without a default set, she
558 // will always be taken to the default URL above; this is
559 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 // URL is unavoidably sent to the chosen app.
562 } else {
563 try {
564 intent = Intent.parseUri(mHotseatConfig[i], 0);
565 } catch (java.net.URISyntaxException ex) {
566 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
567 // bogus; leave intent=null
568 }
569 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700570
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 if (intent == null) {
572 mHotseats[i] = null;
573 mHotseatLabels[i] = getText(R.string.activity_not_found);
574 continue;
575 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400576
577 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700578 Log.d(TAG, "loadHotseats: hotseat " + i
579 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 + "]");
582 }
583
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
585 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400587 Log.d(TAG, "Best match for intent: " + bestMatch);
588 Log.d(TAG, "All matches: ");
589 for (ResolveInfo ri : allMatches) {
590 Log.d(TAG, " --> " + ri);
591 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400592 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 // did this resolve to a single app, or the resolver?
594 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 // can't find any activity to handle this. let's leave the
596 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400597 // to launch.
598 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400599
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 // set accessibility text to "Not installed"
601 mHotseatLabels[i] = getText(R.string.activity_not_found);
602 } else {
603 boolean found = false;
604 for (ResolveInfo ri : allMatches) {
605 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
606 && bestMatch.activityInfo.applicationInfo.packageName
607 .equals(ri.activityInfo.applicationInfo.packageName)) {
608 found = true;
609 break;
610 }
611 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700612
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400613 if (!found) {
614 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
615 // the bestMatch is probably the ResolveActivity, meaning the
616 // user has not yet selected a default
617 // so: we'll keep the original intent for now
618 mHotseats[i] = intent;
619
620 // set the accessibility text to "Select shortcut"
621 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
622 } else {
623 // we have an app!
624 // now reconstruct the intent to launch it through the front
625 // door
626 ComponentName com = new ComponentName(
627 bestMatch.activityInfo.applicationInfo.packageName,
628 bestMatch.activityInfo.name);
629 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
630
631 // load the app label for accessibility
632 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
633 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400634 }
635
636 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 Log.d(TAG, "loadHotseats: hotseat " + i
638 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 + ((mHotseats[i] == null)
640 ? "null"
641 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400642 + "] label=[" + mHotseatLabels[i]
643 + "]"
644 );
645 }
646 }
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 @Override
650 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700651 mWaitingForResult = false;
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700658
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 switch (requestCode) {
661 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700662 completeAddApplication(
663 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
665 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800666 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
668 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 break;
671 case REQUEST_PICK_LIVE_FOLDER:
672 addLiveFolder(data);
673 break;
674 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700684 case REQUEST_PICK_WALLPAPER:
685 // We just wanted the activity result here so we can clear mWaitingForResult
686 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Romain Guy18042c82009-11-06 11:44:55 -0800688 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
689 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
690 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 // Clean up the appWidgetId if we canceled
692 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
693 if (appWidgetId != -1) {
694 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
698
699 @Override
700 protected void onResume() {
701 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800702 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700703 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 mWorkspaceLoading = true;
705 mModel.startLoader(this, true);
706 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700707 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700709 // When we resume Launcher, a different Activity might be responsible for the app
710 // market intent, so refresh the icon
711 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713
714 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 protected void onPause() {
716 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717 // Some launcher layouts don't have a previous and next view
718 if (mPreviousView != null) {
719 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700720 }
721 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 dismissPreview(mNextView);
723 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700724 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800725 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 }
Romain Guycbb89e42009-06-08 15:52:54 -0700727
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700728 @Override
729 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730 // Flag the loader to stop early before switching
731 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800732 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700734 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800736 // We can't hide the IME if it was forced open. So don't bother
737 /*
738 @Override
739 public void onWindowFocusChanged(boolean hasFocus) {
740 super.onWindowFocusChanged(hasFocus);
741
742 if (hasFocus) {
743 final InputMethodManager inputManager = (InputMethodManager)
744 getSystemService(Context.INPUT_METHOD_SERVICE);
745 WindowManager.LayoutParams lp = getWindow().getAttributes();
746 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
747 android.os.Handler()) {
748 protected void onReceiveResult(int resultCode, Bundle resultData) {
749 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
750 }
751 });
752 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
753 }
754 }
755 */
756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 private boolean acceptFilter() {
758 final InputMethodManager inputManager = (InputMethodManager)
759 getSystemService(Context.INPUT_METHOD_SERVICE);
760 return !inputManager.isFullscreenMode();
761 }
762
763 @Override
764 public boolean onKeyDown(int keyCode, KeyEvent event) {
765 boolean handled = super.onKeyDown(keyCode, event);
766 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
767 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
768 keyCode, event);
769 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700771 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700772 // showSearchDialog()
773 // If there are multiple keystrokes before the search dialog takes focus,
774 // onSearchRequested() will be called for every keystroke,
775 // but it is idempotent, so it's fine.
776 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778 }
779
Joe Onorato8a9625e2010-01-28 15:55:35 -0800780 // Eat the long press event so the keyboard doesn't come up.
781 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
782 return true;
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 return handled;
786 }
787
Karl Rosaen138a0412009-04-23 19:00:21 -0700788 private String getTypedText() {
789 return mDefaultKeySsb.toString();
790 }
791
792 private void clearTypedText() {
793 mDefaultKeySsb.clear();
794 mDefaultKeySsb.clearSpans();
795 Selection.setSelection(mDefaultKeySsb, 0);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700799 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
800 * State
801 */
802 private static State intToState(int stateOrdinal) {
803 State state = State.WORKSPACE;
804 final State[] stateValues = State.values();
805 for (int i = 0; i < stateValues.length; i++) {
806 if (stateValues[i].ordinal() == stateOrdinal) {
807 state = stateValues[i];
808 break;
809 }
810 }
811 return state;
812 }
813
814 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 * Restores the previous state, if it exists.
816 *
817 * @param savedState The previous state.
818 */
819 private void restoreState(Bundle savedState) {
820 if (savedState == null) {
821 return;
822 }
823
Michael Jurka883f55b2010-10-21 15:47:14 -0700824 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
825
826 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800827 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700828 } else if (state == State.CUSTOMIZE) {
829 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
833 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700834 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836
837 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700840 mAddScreen = addScreen;
841 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
842 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 mRestoring = true;
844 }
845
846 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
847 if (renameFolder) {
848 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700849 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 mRestoring = true;
851 }
852 }
853
854 /**
855 * Finds all the views we need and configure them properly.
856 */
857 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700858 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400859
Romain Guyb1b69f52009-08-10 15:10:15 -0700860 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400861 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
Joe Onorato7c312c12009-08-13 21:36:53 -0700863 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700864 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700865 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800866 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700867 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700869
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700870 if (LauncherApplication.isScreenXLarge()) {
871 // They need to be INVISIBLE initially so that they will be measured in the layout.
872 // Otherwise the animations are messed up when we show them for the first time.
873 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
874 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
875 }
876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700878
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800879 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500880 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700881
Joe Onorato7c312c12009-08-13 21:36:53 -0700882 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
883 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700885 View handleView = findViewById(R.id.all_apps_button);
886 if (handleView != null && handleView instanceof HandleView) {
887 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700888 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700889 mHandleView.setLauncher(this);
890 mHandleView.setOnClickListener(this);
891 mHandleView.setOnLongClickListener(this);
892 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800893
Winson Chung80baf5a2010-08-09 16:03:15 -0700894 if (mCustomizePagedView != null) {
895 mCustomizePagedView.setLauncher(this);
896 mCustomizePagedView.setDragController(dragController);
897 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700898 } else {
899 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
900 hotseatLeft.setContentDescription(mHotseatLabels[0]);
901 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
902 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
903 hotseatRight.setContentDescription(mHotseatLabels[1]);
904 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400905
Michael Jurkaaf442092010-06-10 17:01:57 -0700906 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
907 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800908
Michael Jurkaaf442092010-06-10 17:01:57 -0700909 Drawable previous = mPreviousView.getDrawable();
910 Drawable next = mNextView.getDrawable();
911 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912
Michael Jurkaaf442092010-06-10 17:01:57 -0700913 mPreviousView.setHapticFeedbackEnabled(false);
914 mPreviousView.setOnLongClickListener(this);
915 mNextView.setHapticFeedbackEnabled(false);
916 mNextView.setOnLongClickListener(this);
917 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400920 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922
923 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400924 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700925 int deleteZoneHandleId;
926 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700927 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700928 } else {
929 deleteZoneHandleId = R.id.all_apps_button_cluster;
930 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700931 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700932 dragController.addDragListener(deleteZone);
933
934 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
935 if (infoButton != null) {
936 infoButton.setLauncher(this);
937 infoButton.setHandle(findViewById(R.id.configure_button));
938 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
939 dragController.addDragListener(infoButton);
940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941
Joe Onorato00acb122009-08-04 16:04:30 -0400942 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400943 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800944 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700945
Joe Onorato00acb122009-08-04 16:04:30 -0400946 // The order here is bottom to top.
947 dragController.addDropTarget(workspace);
948 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700949 if (infoButton != null) {
950 dragController.addDropTarget(infoButton);
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953
Romain Guy8a73c512009-11-09 19:19:59 -0800954 @SuppressWarnings({"UnusedDeclaration"})
955 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700956 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800957 mWorkspace.scrollLeft();
958 }
Romain Guy8a73c512009-11-09 19:19:59 -0800959 }
960
961 @SuppressWarnings({"UnusedDeclaration"})
962 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700963 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800964 mWorkspace.scrollRight();
965 }
Romain Guy8a73c512009-11-09 19:19:59 -0800966 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400967
968 @SuppressWarnings({"UnusedDeclaration"})
969 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700970 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400971
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400972 int index = -1;
973 if (v.getId() == R.id.hotseat_left) {
974 index = 0;
975 } else if (v.getId() == R.id.hotseat_right) {
976 index = 1;
977 }
978
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400979 // reload these every tap; you never know when they might change
980 loadHotseats();
981 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
982 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400983 startActivitySafely(
984 mHotseats[index],
985 "hotseat"
986 );
987 }
988 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700989
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 /**
991 * Creates a view representing a shortcut.
992 *
993 * @param info The data structure describing the shortcut.
994 *
995 * @return A View inflated from R.layout.application.
996 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700999 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 }
1001
1002 /**
1003 * Creates a view representing a shortcut inflated from the specified resource.
1004 *
1005 * @param layoutResId The id of the XML layout used to create the shortcut.
1006 * @param parent The group the shortcut belongs to.
1007 * @param info The data structure describing the shortcut.
1008 *
1009 * @return A View inflated from layoutResId.
1010 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001011 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1013
Patrick Dubroy9e482382010-11-01 15:16:13 -07001014 Bitmap b = info.getIcon(mIconCache);
1015
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001017 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001018 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001019 favorite.setText(info.title);
1020 favorite.setTag(info);
1021 favorite.setOnClickListener(this);
1022
1023 return favorite;
1024 }
1025
1026 /**
1027 * Add an application shortcut to the workspace.
1028 *
1029 * @param data The intent describing the application.
1030 * @param cellInfo The position on screen where to create the shortcut.
1031 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001032 void completeAddApplication(Context context, Intent data, int screen,
1033 int intersectCellX, int intersectCellY) {
1034 final int[] cellXY = mTmpAddItemCellCoordinates;
1035 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1036
1037 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1038 showOutOfSpaceMessage();
1039 return;
1040 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041
Joe Onorato0589f0f2010-02-08 13:44:00 -08001042 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1043 data, context);
1044
Romain Guy73b979d2009-06-09 12:57:21 -07001045 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001046 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001048 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001049 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1050 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001051 } else {
1052 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 }
1054 }
Romain Guycbb89e42009-06-08 15:52:54 -07001055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056 /**
1057 * Add a shortcut to the workspace.
1058 *
1059 * @param data The intent describing the shortcut.
1060 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001062 private void completeAddShortcut(Intent data, int screen,
1063 int intersectCellX, int intersectCellY) {
1064 final int[] cellXY = mTmpAddItemCellCoordinates;
1065 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Michael Jurka0280c3b2010-09-17 15:00:07 -07001067 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1068 showOutOfSpaceMessage();
1069 return;
1070 }
1071
1072 final ShortcutInfo info = mModel.addShortcut(
1073 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074
1075 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001076 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001078 }
1079 }
1080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001082 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001084 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001085 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1092 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001093
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001094 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001095 // We have saved the position to which the widget was dragged-- this really only matters
1096 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001097 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001098
1099 // For now, we don't save the coordinate where we dropped the icon because we're not
1100 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1101 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001102 int[] touchXY = null;
1103 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1104 touchXY = mAddDropPosition;
1105 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001106 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 boolean foundCellSpan = false;
1108 if (touchXY != null) {
1109 // when dragging and dropping, just find the closest free spot
1110 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1111 int[] result = screenLayout.findNearestVacantArea(
1112 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001113 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 foundCellSpan = !findNearestVacantAreaFailed;
1115 } else {
1116 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1117 // if we long pressed on an empty cell to bring up a menu,
1118 // make sure we intersect the empty cell
1119 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1120 mAddIntersectCellX, mAddIntersectCellY);
1121 } else {
1122 // if we went through the menu -> add, just find any spot
1123 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1124 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001125 }
1126
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001128 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001130 return;
1131 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001133 // Build Launcher-specific widget info and save to database
1134 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 launcherInfo.spanX = spanXY[0];
1136 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001137
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 LauncherModel.addItemToDatabase(this, launcherInfo,
1139 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141
1142 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001143 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001146 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001147
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001150
Michael Jurka0280c3b2010-09-17 15:00:07 -07001151 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001152 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001153
1154 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 }
1156 }
Romain Guycbb89e42009-06-08 15:52:54 -07001157
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1159 @Override
1160 public void onReceive(Context context, Intent intent) {
1161 final String action = intent.getAction();
1162 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1163 mUserPresent = false;
1164 updateRunning();
1165 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1166 mUserPresent = true;
1167 updateRunning();
1168 }
1169 }
1170 };
1171
1172 @Override
1173 public void onAttachedToWindow() {
1174 super.onAttachedToWindow();
1175
1176 // Listen for broadcasts related to user-presence
1177 final IntentFilter filter = new IntentFilter();
1178 filter.addAction(Intent.ACTION_SCREEN_OFF);
1179 filter.addAction(Intent.ACTION_USER_PRESENT);
1180 registerReceiver(mReceiver, filter);
1181
Adam Cohend113e0c2010-11-11 10:48:05 -08001182 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001183 mVisible = true;
1184 }
1185
1186 @Override
1187 public void onDetachedFromWindow() {
1188 super.onDetachedFromWindow();
1189 mVisible = false;
1190
Adam Cohend113e0c2010-11-11 10:48:05 -08001191 if (mAttached) {
1192 unregisterReceiver(mReceiver);
1193 mAttached = false;
1194 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001195 updateRunning();
1196 }
1197
1198 public void onWindowVisibilityChanged(int visibility) {
1199 mVisible = visibility == View.VISIBLE;
1200 updateRunning();
1201 }
1202
1203 private void sendAdvanceMessage(long delay) {
1204 mHandler.removeMessages(ADVANCE_MSG);
1205 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1206 mHandler.sendMessageDelayed(msg, delay);
1207 mAutoAdvanceSentTime = System.currentTimeMillis();
1208 }
1209
1210 private void updateRunning() {
1211 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1212 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1213 mAutoAdvanceRunning = autoAdvanceRunning;
1214 if (autoAdvanceRunning) {
1215 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1216 sendAdvanceMessage(delay);
1217 } else {
1218 if (!mWidgetsToAdvance.isEmpty()) {
1219 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1220 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1221 }
1222 mHandler.removeMessages(ADVANCE_MSG);
1223 }
1224 }
1225 }
1226
1227 private final Handler mHandler = new Handler() {
1228 @Override
1229 public void handleMessage(Message msg) {
1230 if (msg.what == ADVANCE_MSG) {
1231 int i = 0;
1232 for (View key: mWidgetsToAdvance.keySet()) {
1233 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1234 final int delay = mAdvanceStagger * i;
1235 if (v instanceof Advanceable) {
1236 postDelayed(new Runnable() {
1237 public void run() {
1238 ((Advanceable) v).advance();
1239 }
1240 }, delay);
1241 }
1242 i++;
1243 }
1244 sendAdvanceMessage(mAdvanceInterval);
1245 }
1246 }
1247 };
1248
1249 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1250 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1251 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1252 if (v instanceof Advanceable) {
1253 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1254 ((Advanceable) v).willBeAdvancedByHost();
1255 updateRunning();
1256 }
1257 }
1258
1259 void removeWidgetToAutoAdvance(View hostView) {
1260 if (mWidgetsToAdvance.containsKey(hostView)) {
1261 mWidgetsToAdvance.remove(hostView);
1262 updateRunning();
1263 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001264 }
1265
Joe Onorato9c1289c2009-08-17 11:03:03 -04001266 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1267 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001268 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001269 launcherInfo.hostView = null;
1270 }
1271
Adam Cohended9f8d2010-11-03 13:25:16 -07001272 void showOutOfSpaceMessage() {
1273 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1274 }
1275
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001276 public LauncherAppWidgetHost getAppWidgetHost() {
1277 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 }
Romain Guycbb89e42009-06-08 15:52:54 -07001279
Winson Chunga9abd0e2010-10-27 17:18:37 -07001280 public LauncherModel getModel() {
1281 return mModel;
1282 }
1283
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001284 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001285 getWindow().closeAllPanels();
1286
1287 try {
1288 dismissDialog(DIALOG_CREATE_SHORTCUT);
1289 // Unlock the workspace if the dialog was showing
1290 } catch (Exception e) {
1291 // An exception is thrown if the dialog is not visible, which is fine
1292 }
1293
1294 try {
1295 dismissDialog(DIALOG_RENAME_FOLDER);
1296 // Unlock the workspace if the dialog was showing
1297 } catch (Exception e) {
1298 // An exception is thrown if the dialog is not visible, which is fine
1299 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001300
1301 // Whatever we were doing is hereby canceled.
1302 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001303 }
1304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 @Override
1306 protected void onNewIntent(Intent intent) {
1307 super.onNewIntent(intent);
1308
1309 // Close the menu
1310 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001311 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001312 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001313
Joe Onorato14f122b2009-11-19 14:06:36 -08001314 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1315 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001316
Michael Jurka4cb37242010-08-09 21:05:32 -07001317 // in all these cases, only animate if we're already on home
1318 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001319 mWorkspace.unshrink(alreadyOnHome);
1320 }
1321 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001322 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001323 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1324 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001325 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001326 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001327
Joe Onorato3a8820b2009-11-10 15:06:42 -08001328 final View v = getWindow().peekDecorView();
1329 if (v != null && v.getWindowToken() != null) {
1330 InputMethodManager imm = (InputMethodManager)getSystemService(
1331 INPUT_METHOD_SERVICE);
1332 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 }
1334 }
1335 }
1336
1337 @Override
1338 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1339 // Do not call super here
1340 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001341
1342 if (mHomeCustomizationDrawer != null) {
1343 String cur = savedInstanceState.getString("currentTab");
1344 if (cur != null) {
1345 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1346 }
1347 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348 }
1349
1350 @Override
1351 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001352 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353
1354 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1355 if (folders.size() > 0) {
1356 final int count = folders.size();
1357 long[] ids = new long[count];
1358 for (int i = 0; i < count; i++) {
1359 final FolderInfo info = folders.get(i).getInfo();
1360 ids[i] = info.id;
1361 }
1362 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1363 } else {
1364 super.onSaveInstanceState(outState);
1365 }
1366
Michael Jurka883f55b2010-10-21 15:47:14 -07001367 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
Michael Jurka0280c3b2010-09-17 15:00:07 -07001369 if (mAddScreen > -1 && mWaitingForResult) {
1370 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1371 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1372 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
1374
1375 if (mFolderInfo != null && mWaitingForResult) {
1376 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1377 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1378 }
Michael Jurka946ad472010-07-09 18:05:18 -07001379
1380 if (mHomeCustomizationDrawer != null) {
1381 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1382 if (currentTabTag != null) {
1383 outState.putString("currentTab", currentTabTag);
1384 }
1385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
1388 @Override
1389 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001393 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001395 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 }
1397
1398 TextKeyListener.getInstance().release();
1399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001401
Joe Onorato9c1289c2009-08-17 11:03:03 -04001402 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001403
1404 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001405
Patrick Dubroyab962b72010-07-26 12:10:10 -07001406 // Some launcher layouts don't have a previous and next view
1407 if (mPreviousView != null) {
1408 dismissPreview(mPreviousView);
1409 }
1410 if (mNextView != null) {
1411 dismissPreview(mNextView);
1412 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001413
1414 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416
1417 @Override
1418 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001419 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 super.startActivityForResult(intent, requestCode);
1421 }
1422
1423 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001424 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001426
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001427 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001428
Karl Rosaen138a0412009-04-23 19:00:21 -07001429 if (initialQuery == null) {
1430 // Use any text typed in the launcher as the initial query
1431 initialQuery = getTypedText();
1432 clearTypedText();
1433 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 if (appSearchData == null) {
1435 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001436 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 }
Romain Guycbb89e42009-06-08 15:52:54 -07001438
Karl Rosaen138a0412009-04-23 19:00:21 -07001439 final SearchManager searchManager =
1440 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001441 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001442 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001443 }
1444
1445 @Override
1446 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001447 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001448 return false;
1449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450
1451 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1454 .setIcon(android.R.drawable.ic_menu_add)
1455 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001456 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1457 .setIcon(android.R.drawable.ic_menu_manage)
1458 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001459 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 .setIcon(android.R.drawable.ic_menu_gallery)
1461 .setAlphabeticShortcut('W');
1462 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1463 .setIcon(android.R.drawable.ic_search_category_default)
1464 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1465 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1466 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1467 .setAlphabeticShortcut('N');
1468
1469 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001470 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1471 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472
1473 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1474 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1475 .setIntent(settings);
1476
1477 return true;
1478 }
1479
1480 @Override
1481 public boolean onPrepareOptionsMenu(Menu menu) {
1482 super.onPrepareOptionsMenu(menu);
1483
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001484 // If all apps is animating, don't show the menu, because we don't know
1485 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001486 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001487 return false;
1488 }
1489
1490 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001491 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001492 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1493 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1494
1495 // Disable add if the workspace is full.
1496 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001497 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1498 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001499 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500
1501 return true;
1502 }
1503
1504 @Override
1505 public boolean onOptionsItemSelected(MenuItem item) {
1506 switch (item.getItemId()) {
1507 case MENU_ADD:
1508 addItems();
1509 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001510 case MENU_MANAGE_APPS:
1511 manageApps();
1512 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 case MENU_WALLPAPER_SETTINGS:
1514 startWallpaper();
1515 return true;
1516 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001517 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 return true;
1519 case MENU_NOTIFICATIONS:
1520 showNotifications();
1521 return true;
1522 }
1523
1524 return super.onOptionsItemSelected(item);
1525 }
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Karl Rosaen138a0412009-04-23 19:00:21 -07001527 /**
1528 * Indicates that we want global search for this activity by setting the globalSearch
1529 * argument for {@link #startSearch} to true.
1530 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001532 @Override
1533 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001534 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001535 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001536 }
1537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 public boolean isWorkspaceLocked() {
1539 return mWorkspaceLoading || mWaitingForResult;
1540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001543 if (LauncherApplication.isScreenXLarge()) {
1544 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001545 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001546 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001547 }
1548 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001549 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001550 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
Michael Jurka0280c3b2010-09-17 15:00:07 -07001554 private void resetAddInfo() {
1555 mAddScreen = -1;
1556 mAddIntersectCellX = -1;
1557 mAddIntersectCellY = -1;
1558 mAddDropPosition = null;
1559 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001560
Winson Chung55cef262010-10-28 14:14:18 -07001561 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001562 resetAddInfo();
1563 mAddScreen = screen;
1564
1565 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1566 mAddDropPosition = position;
1567
Michael Jurkaaf442092010-06-10 17:01:57 -07001568 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001569 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1570 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001571 }
1572
Winson Chung7ad01412010-09-27 11:33:03 -07001573 private void manageApps() {
1574 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1575 }
1576
Michael Jurkaaf442092010-06-10 17:01:57 -07001577 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001578 // TODO: catch bad widget exception when sent
1579 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001580 // TODO: Is this log message meaningful?
1581 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001582 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001583 }
1584
Winson Chung55cef262010-10-28 14:14:18 -07001585 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001586 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
Bjorn Bringert7984c942009-12-09 15:38:25 +00001588 if (appWidget.configure != null) {
1589 // Launch over to configure widget, if needed
1590 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1591 intent.setComponent(appWidget.configure);
1592 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001593 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001594 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1595 intent.putExtra(
1596 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1597 info.mimeType);
1598
1599 final String mimeType = info.mimeType;
1600 final ClipData clipData = (ClipData) info.configurationData;
1601 final ClipDescription clipDesc = clipData.getDescription();
1602 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1603 if (clipDesc.getMimeType(i).equals(mimeType)) {
1604 final ClipData.Item item = clipData.getItem(i);
1605 final CharSequence stringData = item.getText();
1606 final Uri uriData = item.getUri();
1607 final Intent intentData = item.getIntent();
1608 final String key =
1609 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1610 if (uriData != null) {
1611 intent.putExtra(key, uriData);
1612 } else if (intentData != null) {
1613 intent.putExtra(key, intentData);
1614 } else if (stringData != null) {
1615 intent.putExtra(key, stringData);
1616 }
1617 break;
1618 }
1619 }
1620 }
Winson Chung55cef262010-10-28 14:14:18 -07001621 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001622
Romain Guy8e633c52010-03-04 12:51:36 -08001623 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001625 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001626 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001627 }
1628 }
Romain Guycbb89e42009-06-08 15:52:54 -07001629
Michael Jurka0280c3b2010-09-17 15:00:07 -07001630 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1631 resetAddInfo();
1632 mAddScreen = screen;
1633 mAddDropPosition = position;
1634
1635 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1636 createShortcutIntent.setComponent(componentName);
1637 processShortcut(createShortcutIntent);
1638 }
1639
Joe Onoratodeb98af2010-02-19 14:59:39 -08001640 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001641 // Handle case where user selected "Applications"
1642 String applicationName = getResources().getString(R.string.group_applications);
1643 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001644
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001645 if (applicationName != null && applicationName.equals(shortcutName)) {
1646 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1647 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001649 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1650 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001651 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001652 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001653 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001654 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
Winson Chung24ab2f12010-09-16 14:10:47 -07001658 void processWallpaper(Intent intent) {
1659 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1660 }
1661
Michael Jurka0280c3b2010-09-17 15:00:07 -07001662 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1663 resetAddInfo();
1664 mAddScreen = screen;
1665 mAddDropPosition = position;
1666
1667 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1668 createFolderIntent.setComponent(componentName);
1669
1670 addLiveFolder(createFolderIntent);
1671 }
1672
1673 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001674 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001675 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001676 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001677
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001678 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001679 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001680 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001681 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 }
1684
Michael Jurka0280c3b2010-09-17 15:00:07 -07001685 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 UserFolderInfo folderInfo = new UserFolderInfo();
1687 folderInfo.title = getText(R.string.folder_name);
1688
Michael Jurka0280c3b2010-09-17 15:00:07 -07001689 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1690 final int[] cellXY = mTmpAddItemCellCoordinates;
1691 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1692 showOutOfSpaceMessage();
1693 return;
1694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695
1696 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001697 LauncherModel.addItemToDatabase(this, folderInfo,
1698 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001699 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001700 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701
1702 // Create the view
1703 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001704 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1705 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001706 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
Romain Guycbb89e42009-06-08 15:52:54 -07001708
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001710 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001711 }
1712
Michael Jurka28750fb2010-09-24 17:43:49 -07001713 private void completeAddLiveFolder(
1714 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001715 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1716 final int[] cellXY = mTmpAddItemCellCoordinates;
1717 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1718 showOutOfSpaceMessage();
1719 return;
1720 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721
Michael Jurka0280c3b2010-09-17 15:00:07 -07001722 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723
1724 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001725 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001726 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001727 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 }
1729 }
1730
1731 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001732 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733
1734 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1735 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1736
1737 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 Intent.ShortcutIconResource iconResource = null;
1739
1740 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1741 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1742 try {
1743 iconResource = (Intent.ShortcutIconResource) extra;
1744 final PackageManager packageManager = context.getPackageManager();
1745 Resources resources = packageManager.getResourcesForApplication(
1746 iconResource.packageName);
1747 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1748 icon = resources.getDrawable(id);
1749 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001750 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
1752 }
1753
1754 if (icon == null) {
1755 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1756 }
1757
1758 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001759 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 info.title = name;
1761 info.iconResource = iconResource;
1762 info.uri = data.getData();
1763 info.baseIntent = baseIntent;
1764 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1765 LiveFolders.DISPLAY_MODE_GRID);
1766
1767 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001768 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001769 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770
1771 return info;
1772 }
1773
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 private void showNotifications() {
1775 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1776 if (statusBar != null) {
1777 statusBar.expand();
1778 }
1779 }
1780
1781 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001782 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001784 Intent chooser = Intent.createChooser(pickWallpaper,
1785 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001786 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1787 // Removed in Eclair MR1
1788// WallpaperManager wm = (WallpaperManager)
1789// getSystemService(Context.WALLPAPER_SERVICE);
1790// WallpaperInfo wi = wm.getWallpaperInfo();
1791// if (wi != null && wi.getSettingsActivity() != null) {
1792// LabeledIntent li = new LabeledIntent(getPackageName(),
1793// R.string.configure_wallpaper, 0);
1794// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1795// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1796// }
Mike Clerona0618e42009-10-22 13:55:21 -07001797 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001800 /**
1801 * Registers various content observers. The current implementation registers
1802 * only a favorites observer to keep track of the favorites applications.
1803 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001804 private void registerContentObservers() {
1805 ContentResolver resolver = getContentResolver();
1806 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1807 true, mWidgetObserver);
1808 }
1809
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 @Override
1811 public boolean dispatchKeyEvent(KeyEvent event) {
1812 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1813 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001815 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001816 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001817 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001818 dumpState();
1819 return true;
1820 }
1821 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001822 }
1823 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1824 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001825 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 return true;
1827 }
1828 }
1829
1830 return super.dispatchKeyEvent(event);
1831 }
1832
Joe Onorato88ec0992009-11-19 13:16:06 -08001833 @Override
1834 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001835 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1836 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001837 } else {
1838 closeFolder();
1839 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001840 // Some launcher layouts don't have a previous and next view
1841 if (mPreviousView != null) {
1842 dismissPreview(mPreviousView);
1843 dismissPreview(mNextView);
1844 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 private void closeFolder() {
1848 Folder folder = mWorkspace.getOpenFolder();
1849 if (folder != null) {
1850 closeFolder(folder);
1851 }
1852 }
1853
1854 void closeFolder(Folder folder) {
1855 folder.getInfo().opened = false;
1856 ViewGroup parent = (ViewGroup) folder.getParent();
1857 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001858 CellLayout cl = (CellLayout) parent;
1859 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001860 if (folder instanceof DropTarget) {
1861 // Live folders aren't DropTargets.
1862 mDragController.removeDropTarget((DropTarget)folder);
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 }
1865 folder.onClose();
1866 }
1867
1868 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001869 * Re-listen when widgets are reset.
1870 */
1871 private void onAppWidgetReset() {
1872 mAppWidgetHost.startListening();
1873 }
1874
1875 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001876 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1877 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001879 private void unbindDesktopItems() {
1880 for (ItemInfo item: mDesktopItems) {
1881 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
1883 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 /**
1886 * Launches the intent referred by the clicked shortcut.
1887 *
1888 * @param v The view representing the clicked shortcut.
1889 */
1890 public void onClick(View v) {
1891 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001892 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001894 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001895 int[] pos = new int[2];
1896 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001897 intent.setSourceBounds(new Rect(pos[0], pos[1],
1898 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001899 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 } else if (tag instanceof FolderInfo) {
1901 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001902 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001903 if (mState == State.ALL_APPS) {
1904 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001905 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001906 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
1909 }
1910
Michael Jurka0e260592010-06-30 17:07:39 -07001911 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001912 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001913 // clicking anywhere on the workspace causes the customization drawer to slide down
1914 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001915 return false;
1916 }
1917
Michael Jurkaaf442092010-06-10 17:01:57 -07001918 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001919 * Event handler for the search button
1920 *
1921 * @param v The view that was clicked.
1922 */
1923 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001924 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001925 }
1926
1927 /**
1928 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001929 * enters home screen customization mode.
1930 *
1931 * @param v The view that was clicked.
1932 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001933 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001934 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001935 }
1936
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001937 /**
1938 * Event handler for the "grid" button that appears on the home screen, which
1939 * enters all apps mode.
1940 *
1941 * @param v The view that was clicked.
1942 */
1943 public void onClickAllAppsButton(View v) {
1944 showAllApps(true);
1945 }
1946
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001947 public void onClickAppMarketButton(View v) {
1948 if (mAppMarketIntent != null) {
1949 startActivitySafely(mAppMarketIntent, "app market");
1950 }
1951 }
1952
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001953 void startApplicationDetailsActivity(ComponentName componentName) {
1954 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001955 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1956 Uri.fromParts("package", packageName, null));
1957 startActivity(intent);
1958 }
1959
Patrick Dubroy5539af72010-09-07 15:22:01 -07001960 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1961 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1962 // System applications cannot be installed. For now, show a toast explaining that.
1963 // We may give them the option of disabling apps this way.
1964 int messageId = R.string.uninstall_system_app_text;
1965 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1966 } else {
1967 String packageName = appInfo.componentName.getPackageName();
1968 String className = appInfo.componentName.getClassName();
1969 Intent intent = new Intent(
1970 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1971 startActivity(intent);
1972 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001973 }
1974
Joe Onoratof984e852010-03-25 09:47:45 -07001975 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1977 try {
1978 startActivity(intent);
1979 } catch (ActivityNotFoundException e) {
1980 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001981 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 } catch (SecurityException e) {
1983 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001984 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001986 "or use the exported attribute for this activity. "
1987 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001990
Romain Guy8e633c52010-03-04 12:51:36 -08001991 void startActivityForResultSafely(Intent intent, int requestCode) {
1992 try {
1993 startActivityForResult(intent, requestCode);
1994 } catch (ActivityNotFoundException e) {
1995 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1996 } catch (SecurityException e) {
1997 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1998 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1999 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2000 "or use the exported attribute for this activity.", e);
2001 }
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
2004 private void handleFolderClick(FolderInfo folderInfo) {
2005 if (!folderInfo.opened) {
2006 // Close any open folder
2007 closeFolder();
2008 // Open the requested folder
2009 openFolder(folderInfo);
2010 } else {
2011 // Find the open folder...
2012 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2013 int folderScreen;
2014 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002015 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 // .. and close it
2017 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002018 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 // Close any folder open on the current screen
2020 closeFolder();
2021 // Pull the folder onto this screen
2022 openFolder(folderInfo);
2023 }
2024 }
2025 }
2026 }
2027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002029 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 * is animated relative to the specified View. If the View is null, no animation
2031 * is played.
2032 *
2033 * @param folderInfo The FolderInfo describing the folder to open.
2034 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002035 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 Folder openFolder;
2037
2038 if (folderInfo instanceof UserFolderInfo) {
2039 openFolder = UserFolder.fromXml(this);
2040 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002041 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } else {
2043 return;
2044 }
2045
Joe Onorato00acb122009-08-04 16:04:30 -04002046 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 openFolder.setLauncher(this);
2048
2049 openFolder.bind(folderInfo);
2050 folderInfo.opened = true;
2051
Winson Chungaafa03c2010-06-11 17:34:16 -07002052 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2053
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 openFolder.onOpen();
2055 }
2056
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002058 switch (v.getId()) {
2059 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002060 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002061 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2062 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002063 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002064 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002065 return true;
2066 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002067 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002068 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2069 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002070 showPreviews(v);
2071 }
2072 return true;
2073 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002074 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002075 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2076 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2077 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002078 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002079 return true;
2080 }
2081
Joe Onorato9c1289c2009-08-17 11:03:03 -04002082 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 return false;
2084 }
2085
2086 if (!(v instanceof CellLayout)) {
2087 v = (View) v.getParent();
2088 }
2089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090
Michael Jurka0280c3b2010-09-17 15:00:07 -07002091 resetAddInfo();
2092 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002094 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 return true;
2096 }
2097
Michael Jurka0280c3b2010-09-17 15:00:07 -07002098 final View itemUnderLongClick = longClickCellInfo.cell;
2099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002101 if (itemUnderLongClick == null) {
2102 // User long pressed on empty space
2103 mWorkspace.setAllowLongPress(false);
2104 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2105 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002106 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002107 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002110 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002112 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2113 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002114 mAddIntersectCellX = longClickCellInfo.cellX;
2115 mAddIntersectCellY = longClickCellInfo.cellY;
2116 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 }
2118 }
2119 }
2120 return true;
2121 }
2122
Romain Guye6b8e2f2009-11-10 11:56:55 -08002123 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002124 private void dismissPreview(final View v) {
2125 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002126 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002127 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2128 public void onDismiss() {
2129 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2130 int count = group.getChildCount();
2131 for (int i = 0; i < count; i++) {
2132 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2133 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002134 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2135 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2136
2137 v.setTag(R.id.workspace, null);
2138 v.setTag(R.id.icon, null);
2139 window.setOnDismissListener(null);
2140 }
2141 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002142 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002143 }
2144 v.setTag(null);
2145 }
2146
Romain Guyf8e6a802009-12-07 17:48:02 -08002147 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002148 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002149 }
2150
Romain Guya6abce82009-11-10 02:54:41 -08002151 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002152 final Resources resources = getResources();
2153 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002154
Romain Guya6abce82009-11-10 02:54:41 -08002155 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002156
Romain Guy9d31e9f2009-11-11 18:54:28 -08002157 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002158
Romain Guyf8e6a802009-12-07 17:48:02 -08002159 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002160 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002161 int extraW = (int) ((r.left + r.right) * max);
2162 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002163
2164 int aW = cell.getWidth() - extraW;
2165 float w = aW / max;
2166
2167 int width = cell.getWidth();
2168 int height = cell.getHeight();
2169 int x = cell.getLeftPadding();
2170 int y = cell.getTopPadding();
2171 width -= (x + cell.getRightPadding());
2172 height -= (y + cell.getBottomPadding());
2173
2174 float scale = w / width;
2175
2176 int count = end - start;
2177
2178 final float sWidth = width * scale;
2179 float sHeight = height * scale;
2180
Romain Guye6b8e2f2009-11-10 11:56:55 -08002181 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002182
Romain Guye6b8e2f2009-11-10 11:56:55 -08002183 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2184 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002185
2186 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002187 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002188 cell = (CellLayout) workspace.getChildAt(i);
2189
Romain Guyf8e6a802009-12-07 17:48:02 -08002190 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002191 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002192
2193 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002194 c.scale(scale, scale);
2195 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002196 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002197
Romain Guy9d31e9f2009-11-11 18:54:28 -08002198 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002199 image.setImageBitmap(bitmap);
2200 image.setTag(i);
2201 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002202 image.setOnFocusChangeListener(handler);
2203 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002204 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002205
2206 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002207 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2208
Winson Chungaafa03c2010-06-11 17:34:16 -07002209 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002210 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002211
2212 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002213 p.setContentView(preview);
2214 p.setWidth((int) (sWidth * count + extraW));
2215 p.setHeight((int) (sHeight + extraH));
2216 p.setAnimationStyle(R.style.AnimationPreview);
2217 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002218 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002219 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002220 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002221
Romain Guye6b8e2f2009-11-10 11:56:55 -08002222 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2223 public void onDismiss() {
2224 dismissPreview(anchor);
2225 }
2226 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002227
2228 anchor.setTag(p);
2229 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002230 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002231 }
2232
Romain Guy9d31e9f2009-11-11 18:54:28 -08002233 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002234 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002235
Romain Guye6b8e2f2009-11-10 11:56:55 -08002236 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002237 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002238 }
2239
2240 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002241 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002242 v.post(this);
2243 }
2244
2245 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002246 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002247 }
2248
2249 public void onFocusChange(View v, boolean hasFocus) {
2250 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002251 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002252 }
Romain Guya6abce82009-11-10 02:54:41 -08002253 }
2254 }
2255
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 Workspace getWorkspace() {
2257 return mWorkspace;
2258 }
2259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002260 @Override
2261 protected Dialog onCreateDialog(int id) {
2262 switch (id) {
2263 case DIALOG_CREATE_SHORTCUT:
2264 return new CreateShortcut().createDialog();
2265 case DIALOG_RENAME_FOLDER:
2266 return new RenameFolder().createDialog();
2267 }
2268
2269 return super.onCreateDialog(id);
2270 }
2271
2272 @Override
2273 protected void onPrepareDialog(int id, Dialog dialog) {
2274 switch (id) {
2275 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 break;
2277 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002278 if (mFolderInfo != null) {
2279 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2280 final CharSequence text = mFolderInfo.title;
2281 input.setText(text);
2282 input.setSelection(0, text.length());
2283 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 break;
2285 }
2286 }
2287
2288 void showRenameDialog(FolderInfo info) {
2289 mFolderInfo = info;
2290 mWaitingForResult = true;
2291 showDialog(DIALOG_RENAME_FOLDER);
2292 }
2293
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 private void showAddDialog(int intersectX, int intersectY) {
2295 resetAddInfo();
2296 mAddIntersectCellX = intersectX;
2297 mAddIntersectCellY = intersectY;
2298 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 mWaitingForResult = true;
2300 showDialog(DIALOG_CREATE_SHORTCUT);
2301 }
2302
Joe Onoratodeb98af2010-02-19 14:59:39 -08002303 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002304 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002305 Bundle bundle = new Bundle();
2306
2307 ArrayList<String> shortcutNames = new ArrayList<String>();
2308 shortcutNames.add(getString(R.string.group_applications));
2309 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2310
2311 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2312 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2313 R.drawable.ic_launcher_application));
2314 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2315
2316 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2317 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002318 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002319 pickIntent.putExtras(bundle);
2320
Joe Onoratodeb98af2010-02-19 14:59:39 -08002321 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002322 }
2323
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 private class RenameFolder {
2325 private EditText mInput;
2326
2327 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2329 mInput = (EditText) layout.findViewById(R.id.folder_name);
2330
2331 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2332 builder.setIcon(0);
2333 builder.setTitle(getString(R.string.rename_folder_title));
2334 builder.setCancelable(true);
2335 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2336 public void onCancel(DialogInterface dialog) {
2337 cleanup();
2338 }
2339 });
2340 builder.setNegativeButton(getString(R.string.cancel_action),
2341 new Dialog.OnClickListener() {
2342 public void onClick(DialogInterface dialog, int which) {
2343 cleanup();
2344 }
2345 }
2346 );
2347 builder.setPositiveButton(getString(R.string.rename_action),
2348 new Dialog.OnClickListener() {
2349 public void onClick(DialogInterface dialog, int which) {
2350 changeFolderName();
2351 }
2352 }
2353 );
2354 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002355
2356 final AlertDialog dialog = builder.create();
2357 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2358 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002359 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002360 mInput.requestFocus();
2361 InputMethodManager inputManager = (InputMethodManager)
2362 getSystemService(Context.INPUT_METHOD_SERVICE);
2363 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002364 }
2365 });
2366
2367 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002368 }
2369
2370 private void changeFolderName() {
2371 final String name = mInput.getText().toString();
2372 if (!TextUtils.isEmpty(name)) {
2373 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002374 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 mFolderInfo.title = name;
2376 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2377
Joe Onorato9c1289c2009-08-17 11:03:03 -04002378 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002379 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002380 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381 } else {
2382 final FolderIcon folderIcon = (FolderIcon)
2383 mWorkspace.getViewForTag(mFolderInfo);
2384 if (folderIcon != null) {
2385 folderIcon.setText(name);
2386 getWorkspace().requestLayout();
2387 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002388 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002389 mWorkspaceLoading = true;
2390 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 }
2392 }
2393 }
2394 cleanup();
2395 }
2396
2397 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 dismissDialog(DIALOG_RENAME_FOLDER);
2399 mWaitingForResult = false;
2400 mFolderInfo = null;
2401 }
2402 }
2403
Daniel Sandler843e8602010-06-07 14:59:01 -04002404 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2405 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002406 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002407 }
2408
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002409 // AllAppsView.Watcher
2410 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002411 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2412 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002413 mWorkspace.setVisibility(View.GONE);
2414 }
2415 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002416
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002417 private void showToolbarButton(View button) {
2418 button.setAlpha(1.0f);
2419 button.setVisibility(View.VISIBLE);
2420 button.setFocusable(true);
2421 button.setClickable(true);
2422 }
2423
2424 private void hideToolbarButton(View button) {
2425 button.setAlpha(0.0f);
2426 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2427 button.setVisibility(View.INVISIBLE);
2428 button.setFocusable(false);
2429 button.setClickable(false);
2430 }
2431
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002432 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002434 *
2435 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2436 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002437 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002438 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002439 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002440 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002441 final boolean showing = show;
2442 final boolean hiding = !show;
2443
2444 final int duration = show ?
2445 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2446 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2447
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002448 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002449 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2450 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002451 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002452 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002453 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002454 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002455 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002456 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002457 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002458 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002459 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002460 });
2461 seq.play(anim);
2462 } else {
2463 if (showing) {
2464 showToolbarButton(view);
2465 } else {
2466 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002467 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002468 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002469 }
2470
2471 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 * Show/hide the appropriate toolbar buttons for newState.
2473 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002474 *
2475 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002476 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2477 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002478 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002479 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002480 final View searchButton = findViewById(R.id.search_button);
2481 final View allAppsButton = findViewById(R.id.all_apps_button);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002482 final View configureButton = findViewById(R.id.configure_button);
2483
2484 switch (newState) {
2485 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 hideOrShowToolbarButton(true, searchButton, showSeq);
2487 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2488 hideOrShowToolbarButton(true, configureButton, showSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002489 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002490 break;
2491 case ALL_APPS:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002492 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 hideOrShowToolbarButton(false, searchButton, hideSeq);
2494 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002495 break;
2496 case CUSTOMIZE:
Adam Lesinski6b879f02010-11-04 16:15:23 -07002497 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498 hideOrShowToolbarButton(false, searchButton, hideSeq);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002499 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002500 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002501 break;
2502 }
2503 }
2504
2505 /**
2506 * Helper method for the cameraZoomIn/cameraZoomOut animations
2507 * @param view The view being animated
2508 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2509 * @param scaleFactor The scale factor used for the zoom
2510 */
2511 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2512 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002513
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002514 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002515 // Set pivotY so that at the starting zoom factor, the view is partially
2516 // visible. Modifying initialHeightFactor changes how much of the view is
2517 // initially showing, and hence the perceived angle from which the view enters.
2518 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002519 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002520 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002521 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002522 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002523 }
2524 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002525
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002526 /**
2527 * Zoom the camera out from the workspace to reveal 'toView'.
2528 * Assumes that the view to show is anchored at either the very top or very bottom
2529 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002530 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002531 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002532 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002533 final Resources res = getResources();
2534 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2535 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002536 final boolean toAllApps = (toState == State.ALL_APPS);
2537 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002538
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002539 setPivotsForZoom(toView, toState, scale);
2540
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002541 if (toAllApps) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002542 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002543 } else {
2544 mWorkspace.shrinkToTop(animated);
2545 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002546
2547 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002548 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2549 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2550 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2551 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002552
2553 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002554 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002555 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002556 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002557 // Prepare the position
2558 toView.setTranslationX(0.0f);
2559 toView.setTranslationY(0.0f);
2560 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002561 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002563 @Override
2564 public void onAnimationEndOrCancel(Animator animation) {
2565 // If we don't set the final scale values here, if this animation is cancelled
2566 // it will have the wrong scale value and subsequent cameraPan animations will
2567 // not fix that
2568 toView.setScaleX(1.0f);
2569 toView.setScaleY(1.0f);
2570 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 });
2572
Chet Haaseb1254a62010-09-07 13:35:00 -07002573 AnimatorSet toolbarHideAnim = new AnimatorSet();
2574 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2576
2577 // toView should appear right at the end of the workspace shrink animation
2578 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2579
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002580 if (mStateAnimation != null) mStateAnimation.cancel();
2581 mStateAnimation = new AnimatorSet();
2582 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2583 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584
2585 // Show the new toolbar buttons just as the main animation is ending
2586 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002587 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2588 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 } else {
2590 toView.setTranslationX(0.0f);
2591 toView.setTranslationY(0.0f);
2592 toView.setScaleX(1.0f);
2593 toView.setScaleY(1.0f);
2594 toView.setVisibility(View.VISIBLE);
2595 hideAndShowToolbarButtons(toState, null, null);
2596 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002597 }
2598
2599 /**
2600 * Zoom the camera back into the workspace, hiding 'fromView'.
2601 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2603 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002604 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002606 Resources res = getResources();
2607 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2608 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 final View fromView =
2610 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2611
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002612 mCustomizePagedView.endChoiceMode();
2613 mAllAppsPagedView.endChoiceMode();
2614
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002616
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618
2619 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002620 if (mStateAnimation != null) mStateAnimation.cancel();
2621 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002622 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2623 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2624 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2625 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002626 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2627
2628 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2629 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2630 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2631 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002632 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002633 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002634 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 }
2636 });
2637
Chet Haaseb1254a62010-09-07 13:35:00 -07002638 AnimatorSet toolbarHideAnim = new AnimatorSet();
2639 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2641
Adam Cohen61033d32010-11-15 18:29:44 -08002642 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002643
2644 // Show the new toolbar buttons at the very end of the whole animation
2645 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2646 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002647 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2648 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002649 } else {
2650 fromView.setVisibility(View.GONE);
2651 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2652 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002653 }
2654
2655 /**
2656 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2657 * This is the transition used on xlarge screens to go between all apps and
2658 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002659 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2660 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2661 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002662 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002663 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 final Resources res = getResources();
2665 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002666 final int workspaceHeight = mWorkspace.getHeight();
2667
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 final boolean fromAllApps = (fromState == State.ALL_APPS);
2669 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2670 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2673 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2674 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2675 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002676
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002677 mCustomizePagedView.endChoiceMode();
2678 mAllAppsPagedView.endChoiceMode();
2679
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002680 if (toState == State.ALL_APPS) {
Adam Lesinski6b879f02010-11-04 16:15:23 -07002681 mWorkspace.shrinkToBottomHidden(animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 } else {
2683 mWorkspace.shrinkToTop(animated);
2684 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002685
2686 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002687 if (mStateAnimation != null) mStateAnimation.cancel();
2688 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002689 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002690 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002691 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692 toView.setVisibility(View.VISIBLE);
2693 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002694 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002695 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002696 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002697 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 fromView.setVisibility(View.GONE);
2699 }
2700 });
2701
Chet Haaseb1254a62010-09-07 13:35:00 -07002702 AnimatorSet toolbarHideAnim = new AnimatorSet();
2703 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002704 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2705
Chet Haase472b2812010-10-14 07:02:04 -07002706 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2707 fromViewStartY, fromViewEndY);
2708 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002709 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2710 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2711 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2712 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2713 );
Chet Haase472b2812010-10-14 07:02:04 -07002714 fromAnim.setDuration(duration);
2715 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716
2717 // Show the new toolbar buttons just as the main animation is ending
2718 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002719 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2720 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721 } else {
2722 fromView.setY(fromViewEndY);
2723 fromView.setVisibility(View.GONE);
2724 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002725 toView.setScaleX(1.0f);
2726 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002727 toView.setVisibility(View.VISIBLE);
2728 hideAndShowToolbarButtons(toState, null, null);
2729 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002730 }
2731
2732 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002733 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002734 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002735 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002736
Michael Jurka79212d82010-07-30 16:36:20 -07002737 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002738 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002739 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002740 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002741 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002742 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002743 } else {
2744 mAllAppsGrid.zoom(1.0f, animated);
2745 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002746
Romain Guyc16fea72010-03-12 17:17:56 -08002747 ((View) mAllAppsGrid).setFocusable(true);
2748 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002749
Joe Onorato7c312c12009-08-13 21:36:53 -07002750 // TODO: fade these two too
2751 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002752
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002753 // Change the state *after* we've called all the transition code
2754 mState = State.ALL_APPS;
2755 }
2756
2757
2758 void showWorkspace(boolean animated) {
2759 showWorkspace(animated, null);
2760 }
2761
2762 void showWorkspace(boolean animated, CellLayout layout) {
2763 if (layout != null && animated) {
2764 mWorkspace.unshrink(layout);
2765 } else {
2766 mWorkspace.unshrink(animated);
2767 }
2768 if (mState == State.ALL_APPS) {
2769 closeAllApps(animated);
2770 } else if (mState == State.CUSTOMIZE) {
2771 hideCustomizationDrawer(animated);
2772 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002773
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002774 // Change the state *after* we've called all the transition code
2775 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002776 }
2777
Joe Onoratob2061212009-11-24 16:13:54 -05002778 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002779 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002780 * - Home from workspace
2781 * - from center screen
2782 * - from other screens
2783 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002784 * - from center screen
2785 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002786 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002787 * - from center screen
2788 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002789 * - Launch app from workspace and quit
2790 * - with back
2791 * - with home
2792 * - Launch app from all apps and quit
2793 * - with back
2794 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002795 * - Go to a screen that's not the default, then all
2796 * apps, and launch and app, and go back
2797 * - with back
2798 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002799 * - On workspace, long press power and go back
2800 * - with back
2801 * - with home
2802 * - On all apps, long press power and go back
2803 * - with back
2804 * - with home
2805 * - On workspace, power off
2806 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002807 * - Launch an app and turn off the screen while in that app
2808 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002809 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002810 * - From all apps
2811 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002812 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2813 * - From all apps
2814 * - From the center workspace
2815 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002816 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002817 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002818 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002819 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002820 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002821 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002822 } else {
2823 mAllAppsGrid.zoom(0.0f, animated);
2824 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002825 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002826 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002827 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002828 }
2829
Joe Onorato7c312c12009-08-13 21:36:53 -07002830 void lockAllApps() {
2831 // TODO
2832 }
2833
2834 void unlockAllApps() {
2835 // TODO
2836 }
2837
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002838 // Show the customization drawer (only exists in x-large configuration)
2839 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002840 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002842 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002844 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002845 // Change the state *after* we've called all the transition code
2846 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002847 }
2848
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002849 // Hide the customization drawer (only exists in x-large configuration)
2850 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002851 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002852 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002853 }
2854 }
2855
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002856 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2857 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2858 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002859 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002860 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002861
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002862 void onWorkspaceClick(CellLayout layout) {
2863 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002864 }
2865
Michael Jurka0423dcf2010-10-05 14:56:18 -07002866 private void updateButtonWithIconFromExternalActivity(
2867 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2868 ImageView button = (ImageView) findViewById(buttonId);
2869 Drawable toolbarIcon = null;
2870 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002871 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002872 // Look for the toolbar icon specified in the activity meta-data
2873 Bundle metaData = packageManager.getActivityInfo(
2874 activityName, PackageManager.GET_META_DATA).metaData;
2875 if (metaData != null) {
2876 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2877 if (iconResId != 0) {
2878 Resources res = packageManager.getResourcesForActivity(activityName);
2879 toolbarIcon = res.getDrawable(iconResId);
2880 }
2881 }
2882 } catch (NameNotFoundException e) {
2883 // Do nothing
2884 }
2885 // If we were unable to find the icon via the meta-data, use a generic one
2886 if (toolbarIcon == null) {
2887 button.setImageResource(fallbackDrawableId);
2888 } else {
2889 button.setImageDrawable(toolbarIcon);
2890 }
2891 }
2892
2893 private void updateGlobalSearchIcon() {
2894 if (LauncherApplication.isScreenXLarge()) {
2895 final SearchManager searchManager =
2896 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2897 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002898 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002899 updateButtonWithIconFromExternalActivity(
2900 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002901 }
2902 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002903 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002904
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002905 /**
2906 * Sets the app market icon (shown when all apps is visible on x-large screens)
2907 */
2908 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002909 if (LauncherApplication.isScreenXLarge()) {
2910 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2911 // Find the app market activity by resolving an intent.
2912 // (If multiple app markets are installed, it will return the ResolverActivity.)
2913 ComponentName activityName = intent.resolveActivity(getPackageManager());
2914 if (activityName != null) {
2915 mAppMarketIntent = intent;
2916 updateButtonWithIconFromExternalActivity(
2917 R.id.market_button, activityName, R.drawable.app_market_generic);
2918 }
2919 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002920 }
2921
2922 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 * Displays the shortcut creation dialog and launches, if necessary, the
2924 * appropriate activity.
2925 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002926 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002927 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2928 DialogInterface.OnShowListener {
2929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2936 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002937 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 builder.setInverseBackgroundForced(true);
2940
2941 AlertDialog dialog = builder.create();
2942 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002943 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002944 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 return dialog;
2947 }
2948
2949 public void onCancel(DialogInterface dialog) {
2950 mWaitingForResult = false;
2951 cleanup();
2952 }
2953
Romain Guycbb89e42009-06-08 15:52:54 -07002954 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002955 }
2956
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002957 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002958 try {
2959 dismissDialog(DIALOG_CREATE_SHORTCUT);
2960 } catch (Exception e) {
2961 // An exception is thrown if the dialog is not visible, which is fine
2962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 }
2964
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002965 /**
2966 * Handle the action clicked in the "Add to home" dialog.
2967 */
2968 public void onClick(DialogInterface dialog, int which) {
2969 Resources res = getResources();
2970 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002971
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002972 switch (which) {
2973 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002974 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002975 break;
2976 }
Romain Guycbb89e42009-06-08 15:52:54 -07002977
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002978 case AddAdapter.ITEM_APPWIDGET: {
2979 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002980
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002981 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2982 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002983 // start the pick activity
2984 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2985 break;
2986 }
Romain Guycbb89e42009-06-08 15:52:54 -07002987
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002988 case AddAdapter.ITEM_LIVE_FOLDER: {
2989 // Insert extra item to handle inserting folder
2990 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002991
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002992 ArrayList<String> shortcutNames = new ArrayList<String>();
2993 shortcutNames.add(res.getString(R.string.group_folder));
2994 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002995
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002996 ArrayList<ShortcutIconResource> shortcutIcons =
2997 new ArrayList<ShortcutIconResource>();
2998 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2999 R.drawable.ic_launcher_folder));
3000 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3001
3002 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3003 pickIntent.putExtra(Intent.EXTRA_INTENT,
3004 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3005 pickIntent.putExtra(Intent.EXTRA_TITLE,
3006 getText(R.string.title_select_live_folder));
3007 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003008
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003009 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3010 break;
3011 }
3012
3013 case AddAdapter.ITEM_WALLPAPER: {
3014 startWallpaper();
3015 break;
3016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 }
3018 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003019
3020 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003021 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003022 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003023 }
3024
3025 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003026 * Receives notifications when applications are added/removed.
3027 */
3028 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3029 @Override
3030 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003031 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003032 String reason = intent.getStringExtra("reason");
3033 if (!"homekey".equals(reason)) {
3034 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003035 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003036 animate = false;
3037 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003038 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003039 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003040 }
3041 }
3042
3043 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003044 * Receives notifications whenever the appwidgets are reset.
3045 */
3046 private class AppWidgetResetObserver extends ContentObserver {
3047 public AppWidgetResetObserver() {
3048 super(new Handler());
3049 }
3050
3051 @Override
3052 public void onChange(boolean selfChange) {
3053 onAppWidgetReset();
3054 }
3055 }
3056
3057 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003058 * If the activity is currently paused, signal that we need to re-run the loader
3059 * in onResume.
3060 *
3061 * This needs to be called from incoming places where resources might have been loaded
3062 * while we are paused. That is becaues the Configuration might be wrong
3063 * when we're not running, and if it comes back to what it was when we
3064 * were paused, we are not restarted.
3065 *
3066 * Implementation of the method from LauncherModel.Callbacks.
3067 *
3068 * @return true if we are currently paused. The caller might be able to
3069 * skip some work in that case since we will come back again.
3070 */
3071 public boolean setLoadOnResume() {
3072 if (mPaused) {
3073 Log.i(TAG, "setLoadOnResume");
3074 mOnResumeNeedsLoad = true;
3075 return true;
3076 } else {
3077 return false;
3078 }
3079 }
3080
3081 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003082 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003083 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003085 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003086 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003087 } else {
3088 return SCREEN_COUNT / 2;
3089 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003090 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003091
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003092 void setAllAppsPagedView(PagedView view) {
3093 mAllAppsPagedView = view;
3094 }
3095
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 /**
3097 * Refreshes the shortcuts shown on the workspace.
3098 *
3099 * Implementation of the method from LauncherModel.Callbacks.
3100 */
3101 public void startBinding() {
3102 final Workspace workspace = mWorkspace;
3103 int count = workspace.getChildCount();
3104 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003105 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3107 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003108
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 if (DEBUG_USER_INTERFACE) {
3110 android.widget.Button finishButton = new android.widget.Button(this);
3111 finishButton.setText("Finish");
3112 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3113
3114 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3115 public void onClick(View v) {
3116 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003117 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003118 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 }
3120 }
3121
3122 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003123 * Bind the items start-end from the list.
3124 *
3125 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003126 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3128
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003129 setLoadOnResume();
3130
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 final Workspace workspace = mWorkspace;
3132
3133 for (int i=start; i<end; i++) {
3134 final ItemInfo item = shortcuts.get(i);
3135 mDesktopItems.add(item);
3136 switch (item.itemType) {
3137 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3138 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003139 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3141 false);
3142 break;
3143 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3144 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003145 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003146 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3148 false);
3149 break;
3150 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3151 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3152 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003153 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 (LiveFolderInfo) item);
3155 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3156 false);
3157 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159 }
3160
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 /**
3165 * Implementation of the method from LauncherModel.Callbacks.
3166 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003167 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003168 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003169 sFolders.clear();
3170 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172
3173 /**
3174 * Add the views for a widget to the workspace.
3175 *
3176 * Implementation of the method from LauncherModel.Callbacks.
3177 */
3178 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003179 setLoadOnResume();
3180
Daniel Sandler843e8602010-06-07 14:59:01 -04003181 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3182 if (DEBUG_WIDGETS) {
3183 Log.d(TAG, "bindAppWidget: " + item);
3184 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003185 final Workspace workspace = mWorkspace;
3186
3187 final int appWidgetId = item.appWidgetId;
3188 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003189 if (DEBUG_WIDGETS) {
3190 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3191 }
3192
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3194
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3196 item.hostView.setTag(item);
3197
3198 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3199 item.cellY, item.spanX, item.spanY, false);
3200
Adam Cohended9f8d2010-11-03 13:25:16 -07003201 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3202
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 workspace.requestLayout();
3204
3205 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003206
3207 if (DEBUG_WIDGETS) {
3208 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3209 + (SystemClock.uptimeMillis()-start) + "ms");
3210 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003211 }
3212
3213 /**
3214 * Callback saying that there aren't any more items to bind.
3215 *
3216 * Implementation of the method from LauncherModel.Callbacks.
3217 */
3218 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003219 setLoadOnResume();
3220
Joe Onorato9c1289c2009-08-17 11:03:03 -04003221 if (mSavedState != null) {
3222 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003223 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 }
3225
3226 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3227 if (userFolders != null) {
3228 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003229 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003230 if (info != null) {
3231 openFolder(info);
3232 }
3233 }
3234 final Folder openFolder = mWorkspace.getOpenFolder();
3235 if (openFolder != null) {
3236 openFolder.requestFocus();
3237 }
3238 }
3239
Joe Onorato9c1289c2009-08-17 11:03:03 -04003240 mSavedState = null;
3241 }
3242
3243 if (mSavedInstanceState != null) {
3244 super.onRestoreInstanceState(mSavedInstanceState);
3245 mSavedInstanceState = null;
3246 }
3247
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 mWorkspaceLoading = false;
3249 }
3250
3251 /**
3252 * Add the icons for all apps.
3253 *
3254 * Implementation of the method from LauncherModel.Callbacks.
3255 */
3256 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003257 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003258 if (mCustomizePagedView != null) {
3259 mCustomizePagedView.setApps(apps);
3260 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003261 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003262 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 }
3264
3265 /**
3266 * A package was installed.
3267 *
3268 * Implementation of the method from LauncherModel.Callbacks.
3269 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003270 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003271 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003273 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003274 if (mCustomizePagedView != null) {
3275 mCustomizePagedView.addApps(apps);
3276 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003277 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003278 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003279 }
3280
3281 /**
3282 * A package was updated.
3283 *
3284 * Implementation of the method from LauncherModel.Callbacks.
3285 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003286 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003287 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003288 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003289 mWorkspace.updateShortcuts(apps);
3290 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003291 if (mCustomizePagedView != null) {
3292 mCustomizePagedView.updateApps(apps);
3293 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003294 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003295 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 }
3297
3298 /**
3299 * A package was uninstalled.
3300 *
3301 * Implementation of the method from LauncherModel.Callbacks.
3302 */
Joe Onorato36115782010-06-17 13:28:48 -04003303 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003305 if (permanent) {
3306 mWorkspace.removeItems(apps);
3307 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003308 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003309 if (mCustomizePagedView != null) {
3310 mCustomizePagedView.removeApps(apps);
3311 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003312 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003313 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 }
Joe Onoratobe386092009-11-17 17:32:16 -08003315
3316 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003317 * A number of packages were updated.
3318 */
3319 public void bindPackagesUpdated() {
3320 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003321 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003322 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003323 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003324 }
3325
3326 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003327 * Prints out out state for debugging.
3328 */
3329 public void dumpState() {
3330 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003331 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003332 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3333 Log.d(TAG, "mRestoring=" + mRestoring);
3334 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3335 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3336 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003337 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003338 mModel.dumpState();
3339 mAllAppsGrid.dumpState();
3340 Log.d(TAG, "END launcher2 dump state");
3341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003342}