blob: 464ea64dab2630ec91baa0f10d74f231838ad45c [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
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
317 public void onTabChanged(String tabId) {
318 // animate the changing of the tab content by fading pages in and out
Winson Chungbbc60d82010-11-11 16:34:41 -0800319 final Resources res = getResources();
320 final int duration = res.getInteger(R.integer.config_tabTransitionTime);
Winson Chung80baf5a2010-08-09 16:03:15 -0700321 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700322 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700323 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700324 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700325 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700326 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700327 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700328 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
329 if (tag == WIDGETS_TAG) {
330 mCustomizePagedView.setCustomizationFilter(
331 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700332 } else if (tag == APPLICATIONS_TAG) {
333 mCustomizePagedView.setCustomizationFilter(
334 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700335 } else if (tag == WALLPAPERS_TAG) {
336 mCustomizePagedView.setCustomizationFilter(
337 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700338 } else if (tag == SHORTCUTS_TAG) {
339 mCustomizePagedView.setCustomizationFilter(
340 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700341 }
342
343 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700344 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700345 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700346 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700347 alphaAnim.start();
348 }
349 });
350 alphaAnim.start();
351 }
352 });
Michael Jurka946ad472010-07-09 18:05:18 -0700353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 setupViews();
355
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800356 registerContentObservers();
357
Joe Onorato7c312c12009-08-13 21:36:53 -0700358 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700359
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 mSavedState = savedInstanceState;
361 restoreState(mSavedState);
362
363 if (PROFILE_STARTUP) {
364 android.os.Debug.stopMethodTracing();
365 }
366
367 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400368 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 }
370
371 // For handling default keys
372 mDefaultKeySsb = new SpannableStringBuilder();
373 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800374
375 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
376 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 }
Romain Guycbb89e42009-06-08 15:52:54 -0700378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400417 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418
419 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
420 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700421 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 public void run() {
423 writeConfiguration(Launcher.this, localeConfiguration);
424 }
425 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700426 }
427 }
428
429 private static class LocaleConfiguration {
430 public String locale;
431 public int mcc = -1;
432 public int mnc = -1;
433 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700434
Romain Guy98d01652009-06-30 16:21:04 -0700435 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
436 DataInputStream in = null;
437 try {
438 in = new DataInputStream(context.openFileInput(PREFERENCES));
439 configuration.locale = in.readUTF();
440 configuration.mcc = in.readInt();
441 configuration.mnc = in.readInt();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 // Ignore
446 } finally {
447 if (in != null) {
448 try {
449 in.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
454 }
455 }
456
457 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
458 DataOutputStream out = null;
459 try {
460 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
461 out.writeUTF(configuration.locale);
462 out.writeInt(configuration.mcc);
463 out.writeInt(configuration.mnc);
464 out.flush();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 //noinspection ResultOfMethodCallIgnored
469 context.getFileStreamPath(PREFERENCES).delete();
470 } finally {
471 if (out != null) {
472 try {
473 out.close();
474 } catch (IOException e) {
475 // Ignore
476 }
477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479 }
480
481 static int getScreen() {
482 synchronized (sLock) {
483 return sScreen;
484 }
485 }
486
487 static void setScreen(int screen) {
488 synchronized (sLock) {
489 sScreen = screen;
490 }
491 }
492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700494 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495
496 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700497 // TODO: Put back when we decide about scrolling the wallpaper
498 // boolean isPortrait = display.getWidth() < display.getHeight();
499 // final int width = isPortrait ? display.getWidth() : display.getHeight();
500 // final int height = isPortrait ? display.getHeight() : display.getWidth();
501 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
502 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 }
504
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400505 // Note: This doesn't do all the client-id magic that BrowserProvider does
506 // in Browser. (http://b/2425179)
507 private Uri getDefaultBrowserUri() {
508 String url = getString(R.string.default_browser_url);
509 if (url.indexOf("{CID}") != -1) {
510 url = url.replace("{CID}", "android-google");
511 }
512 return Uri.parse(url);
513 }
514
515 // Load the Intent templates from arrays.xml to populate the hotseats. For
516 // each Intent, if it resolves to a single app, use that as the launch
517 // intent & use that app's label as the contentDescription. Otherwise,
518 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400519 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400520 if (mHotseatConfig == null) {
521 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
522 if (mHotseatConfig.length > 0) {
523 mHotseats = new Intent[mHotseatConfig.length];
524 mHotseatLabels = new CharSequence[mHotseatConfig.length];
525 mHotseatIcons = new Drawable[mHotseatConfig.length];
526 } else {
527 mHotseats = null;
528 mHotseatIcons = null;
529 mHotseatLabels = null;
530 }
531
532 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
533 for (int i=0; i<mHotseatConfig.length; i++) {
534 // load icon for this slot; currently unrelated to the actual activity
535 try {
536 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
537 } catch (ArrayIndexOutOfBoundsException ex) {
538 Log.w(TAG, "Missing hotseat_icons array item #" + i);
539 mHotseatIcons[i] = null;
540 }
541 }
542 hotseatIconDrawables.recycle();
543 }
544
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 for (int i=0; i<mHotseatConfig.length; i++) {
547 Intent intent = null;
548 if (mHotseatConfig[i].equals("*BROWSER*")) {
549 // magic value meaning "launch user's default web browser"
550 // replace it with a generic web request so we can see if there is indeed a default
551 String defaultUri = getString(R.string.default_browser_url);
552 intent = new Intent(
553 Intent.ACTION_VIEW,
554 ((defaultUri != null)
555 ? Uri.parse(defaultUri)
556 : getDefaultBrowserUri())
557 ).addCategory(Intent.CATEGORY_BROWSABLE);
558 // note: if the user launches this without a default set, she
559 // will always be taken to the default URL above; this is
560 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700561 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400562 // URL is unavoidably sent to the chosen app.
563 } else {
564 try {
565 intent = Intent.parseUri(mHotseatConfig[i], 0);
566 } catch (java.net.URISyntaxException ex) {
567 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
568 // bogus; leave intent=null
569 }
570 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700571
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400572 if (intent == null) {
573 mHotseats[i] = null;
574 mHotseatLabels[i] = getText(R.string.activity_not_found);
575 continue;
576 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400577
578 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700579 Log.d(TAG, "loadHotseats: hotseat " + i
580 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582 + "]");
583 }
584
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
586 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700587 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400588 Log.d(TAG, "Best match for intent: " + bestMatch);
589 Log.d(TAG, "All matches: ");
590 for (ResolveInfo ri : allMatches) {
591 Log.d(TAG, " --> " + ri);
592 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400593 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 // did this resolve to a single app, or the resolver?
595 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700596 // can't find any activity to handle this. let's leave the
597 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400598 // to launch.
599 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400600
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400601 // set accessibility text to "Not installed"
602 mHotseatLabels[i] = getText(R.string.activity_not_found);
603 } else {
604 boolean found = false;
605 for (ResolveInfo ri : allMatches) {
606 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
607 && bestMatch.activityInfo.applicationInfo.packageName
608 .equals(ri.activityInfo.applicationInfo.packageName)) {
609 found = true;
610 break;
611 }
612 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700613
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400614 if (!found) {
615 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
616 // the bestMatch is probably the ResolveActivity, meaning the
617 // user has not yet selected a default
618 // so: we'll keep the original intent for now
619 mHotseats[i] = intent;
620
621 // set the accessibility text to "Select shortcut"
622 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
623 } else {
624 // we have an app!
625 // now reconstruct the intent to launch it through the front
626 // door
627 ComponentName com = new ComponentName(
628 bestMatch.activityInfo.applicationInfo.packageName,
629 bestMatch.activityInfo.name);
630 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
631
632 // load the app label for accessibility
633 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
634 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400635 }
636
637 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700638 Log.d(TAG, "loadHotseats: hotseat " + i
639 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400640 + ((mHotseats[i] == null)
641 ? "null"
642 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400643 + "] label=[" + mHotseatLabels[i]
644 + "]"
645 );
646 }
647 }
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 @Override
651 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700652 mWaitingForResult = false;
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 // The pattern used here is that a user PICKs a specific application,
655 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
658 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700659
Michael Jurka0280c3b2010-09-17 15:00:07 -0700660 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 switch (requestCode) {
662 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700663 completeAddApplication(
664 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 break;
666 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800667 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 break;
669 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 break;
672 case REQUEST_PICK_LIVE_FOLDER:
673 addLiveFolder(data);
674 break;
675 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700676 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700678 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700679 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700681 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700682 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700685 case REQUEST_PICK_WALLPAPER:
686 // We just wanted the activity result here so we can clear mWaitingForResult
687 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800688 }
Romain Guy18042c82009-11-06 11:44:55 -0800689 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
690 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
691 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700692 // Clean up the appWidgetId if we canceled
693 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
694 if (appWidgetId != -1) {
695 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 }
699
700 @Override
701 protected void onResume() {
702 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800703 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700704 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400705 mWorkspaceLoading = true;
706 mModel.startLoader(this, true);
707 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700708 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700710 // When we resume Launcher, a different Activity might be responsible for the app
711 // market intent, so refresh the icon
712 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
714
715 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700716 protected void onPause() {
717 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700718 // Some launcher layouts don't have a previous and next view
719 if (mPreviousView != null) {
720 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700721 }
722 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700723 dismissPreview(mNextView);
724 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700725 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800726 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700727 }
Romain Guycbb89e42009-06-08 15:52:54 -0700728
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700729 @Override
730 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400731 // Flag the loader to stop early before switching
732 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800734 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700736
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800737 // We can't hide the IME if it was forced open. So don't bother
738 /*
739 @Override
740 public void onWindowFocusChanged(boolean hasFocus) {
741 super.onWindowFocusChanged(hasFocus);
742
743 if (hasFocus) {
744 final InputMethodManager inputManager = (InputMethodManager)
745 getSystemService(Context.INPUT_METHOD_SERVICE);
746 WindowManager.LayoutParams lp = getWindow().getAttributes();
747 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
748 android.os.Handler()) {
749 protected void onReceiveResult(int resultCode, Bundle resultData) {
750 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
751 }
752 });
753 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
754 }
755 }
756 */
757
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 private boolean acceptFilter() {
759 final InputMethodManager inputManager = (InputMethodManager)
760 getSystemService(Context.INPUT_METHOD_SERVICE);
761 return !inputManager.isFullscreenMode();
762 }
763
764 @Override
765 public boolean onKeyDown(int keyCode, KeyEvent event) {
766 boolean handled = super.onKeyDown(keyCode, event);
767 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
768 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
769 keyCode, event);
770 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700771 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700772 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700773 // showSearchDialog()
774 // If there are multiple keystrokes before the search dialog takes focus,
775 // onSearchRequested() will be called for every keystroke,
776 // but it is idempotent, so it's fine.
777 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 }
779 }
780
Joe Onorato8a9625e2010-01-28 15:55:35 -0800781 // Eat the long press event so the keyboard doesn't come up.
782 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
783 return true;
784 }
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return handled;
787 }
788
Karl Rosaen138a0412009-04-23 19:00:21 -0700789 private String getTypedText() {
790 return mDefaultKeySsb.toString();
791 }
792
793 private void clearTypedText() {
794 mDefaultKeySsb.clear();
795 mDefaultKeySsb.clearSpans();
796 Selection.setSelection(mDefaultKeySsb, 0);
797 }
798
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700800 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
801 * State
802 */
803 private static State intToState(int stateOrdinal) {
804 State state = State.WORKSPACE;
805 final State[] stateValues = State.values();
806 for (int i = 0; i < stateValues.length; i++) {
807 if (stateValues[i].ordinal() == stateOrdinal) {
808 state = stateValues[i];
809 break;
810 }
811 }
812 return state;
813 }
814
815 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 * Restores the previous state, if it exists.
817 *
818 * @param savedState The previous state.
819 */
820 private void restoreState(Bundle savedState) {
821 if (savedState == null) {
822 return;
823 }
824
Michael Jurka883f55b2010-10-21 15:47:14 -0700825 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
826
827 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800828 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700829 } else if (state == State.CUSTOMIZE) {
830 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800831 }
832
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800833 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
834 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700835 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837
838 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700839
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700841 mAddScreen = addScreen;
842 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
843 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 mRestoring = true;
845 }
846
847 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
848 if (renameFolder) {
849 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700850 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 mRestoring = true;
852 }
853 }
854
855 /**
856 * Finds all the views we need and configure them properly.
857 */
858 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700859 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400860
Romain Guyb1b69f52009-08-10 15:10:15 -0700861 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400862 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863
Joe Onorato7c312c12009-08-13 21:36:53 -0700864 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700865 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700866 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800867 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700868 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700869 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700870
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700871 if (LauncherApplication.isScreenXLarge()) {
872 // They need to be INVISIBLE initially so that they will be measured in the layout.
873 // Otherwise the animations are messed up when we show them for the first time.
874 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
875 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
876 }
877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
879 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);
2482 final View marketButton = findViewById(R.id.market_button);
2483 final View configureButton = findViewById(R.id.configure_button);
2484
2485 switch (newState) {
2486 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002487 hideOrShowToolbarButton(true, searchButton, showSeq);
2488 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2489 hideOrShowToolbarButton(true, configureButton, showSeq);
2490 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002491 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002492 break;
2493 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002494 hideOrShowToolbarButton(true, configureButton, showSeq);
2495 hideOrShowToolbarButton(true, marketButton, showSeq);
2496 hideOrShowToolbarButton(false, searchButton, hideSeq);
2497 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002498 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002499 break;
2500 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002501 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2502 hideOrShowToolbarButton(false, searchButton, hideSeq);
2503 hideOrShowToolbarButton(false, marketButton, hideSeq);
2504 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002505 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002506 break;
2507 }
2508 }
2509
2510 /**
2511 * Helper method for the cameraZoomIn/cameraZoomOut animations
2512 * @param view The view being animated
2513 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2514 * @param scaleFactor The scale factor used for the zoom
2515 */
2516 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2517 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002518
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002519 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002520 // Set pivotY so that at the starting zoom factor, the view is partially
2521 // visible. Modifying initialHeightFactor changes how much of the view is
2522 // initially showing, and hence the perceived angle from which the view enters.
2523 final float initialHeightFactor = 0.2f;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002524 if (state == State.ALL_APPS) {
Adam Cohen61033d32010-11-15 18:29:44 -08002525 view.setPivotY((1 + initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002526 } else {
Adam Cohen61033d32010-11-15 18:29:44 -08002527 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002528 }
2529 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002530
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002531 /**
2532 * Zoom the camera out from the workspace to reveal 'toView'.
2533 * Assumes that the view to show is anchored at either the very top or very bottom
2534 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002535 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002536 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002538 final Resources res = getResources();
2539 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2540 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 final boolean toAllApps = (toState == State.ALL_APPS);
2542 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002543
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002544 setPivotsForZoom(toView, toState, scale);
2545
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002546 if (toAllApps) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002547 mWorkspace.shrinkToBottom(animated);
2548 } else {
2549 mWorkspace.shrinkToTop(animated);
2550 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002551
2552 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002553 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2554 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2555 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2556 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002557
2558 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002559 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002560 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002561 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002562 // Prepare the position
2563 toView.setTranslationX(0.0f);
2564 toView.setTranslationY(0.0f);
2565 toView.setVisibility(View.VISIBLE);
Adam Cohen61033d32010-11-15 18:29:44 -08002566 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002568 @Override
2569 public void onAnimationEndOrCancel(Animator animation) {
2570 // If we don't set the final scale values here, if this animation is cancelled
2571 // it will have the wrong scale value and subsequent cameraPan animations will
2572 // not fix that
2573 toView.setScaleX(1.0f);
2574 toView.setScaleY(1.0f);
2575 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 });
2577
Chet Haaseb1254a62010-09-07 13:35:00 -07002578 AnimatorSet toolbarHideAnim = new AnimatorSet();
2579 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2581
2582 // toView should appear right at the end of the workspace shrink animation
2583 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2584
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002585 if (mStateAnimation != null) mStateAnimation.cancel();
2586 mStateAnimation = new AnimatorSet();
2587 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2588 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589
2590 // Show the new toolbar buttons just as the main animation is ending
2591 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002592 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2593 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002594 } else {
2595 toView.setTranslationX(0.0f);
2596 toView.setTranslationY(0.0f);
2597 toView.setScaleX(1.0f);
2598 toView.setScaleY(1.0f);
2599 toView.setVisibility(View.VISIBLE);
2600 hideAndShowToolbarButtons(toState, null, null);
2601 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002602 }
2603
2604 /**
2605 * Zoom the camera back into the workspace, hiding 'fromView'.
2606 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002607 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2608 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002609 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002610 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002611 Resources res = getResources();
2612 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2613 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 final View fromView =
2615 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2616
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002617 mCustomizePagedView.endChoiceMode();
2618 mAllAppsPagedView.endChoiceMode();
2619
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002620 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002621
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623
2624 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002625 if (mStateAnimation != null) mStateAnimation.cancel();
2626 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002627 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2628 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2629 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2630 scaleAnim.setDuration(duration);
Adam Cohen61033d32010-11-15 18:29:44 -08002631 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
2632
2633 ValueAnimator alphaAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2634 PropertyValuesHolder.ofFloat("alpha", 1.0f, 0.0f));
2635 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
2636 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002637 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002638 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002640 }
2641 });
2642
Chet Haaseb1254a62010-09-07 13:35:00 -07002643 AnimatorSet toolbarHideAnim = new AnimatorSet();
2644 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002645 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2646
Adam Cohen61033d32010-11-15 18:29:44 -08002647 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002648
2649 // Show the new toolbar buttons at the very end of the whole animation
2650 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2651 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002652 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2653 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002654 } else {
2655 fromView.setVisibility(View.GONE);
2656 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2657 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 }
2659
2660 /**
2661 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2662 * This is the transition used on xlarge screens to go between all apps and
2663 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2665 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2666 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002667 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002668 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002669 final Resources res = getResources();
2670 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002671 final int workspaceHeight = mWorkspace.getHeight();
2672
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002673 final boolean fromAllApps = (fromState == State.ALL_APPS);
2674 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2675 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002676
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002677 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2678 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2679 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2680 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002681
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002682 mCustomizePagedView.endChoiceMode();
2683 mAllAppsPagedView.endChoiceMode();
2684
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 if (toState == State.ALL_APPS) {
2686 mWorkspace.shrinkToBottom(animated);
2687 } else {
2688 mWorkspace.shrinkToTop(animated);
2689 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002690
2691 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002692 if (mStateAnimation != null) mStateAnimation.cancel();
2693 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002694 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002695 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002696 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002697 toView.setVisibility(View.VISIBLE);
2698 toView.setY(toViewStartY);
Adam Cohen61033d32010-11-15 18:29:44 -08002699 toView.setAlpha(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002701 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002702 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002703 fromView.setVisibility(View.GONE);
2704 }
2705 });
2706
Chet Haaseb1254a62010-09-07 13:35:00 -07002707 AnimatorSet toolbarHideAnim = new AnimatorSet();
2708 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002709 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2710
Chet Haase472b2812010-10-14 07:02:04 -07002711 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2712 fromViewStartY, fromViewEndY);
2713 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002714 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2715 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2716 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2717 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2718 );
Chet Haase472b2812010-10-14 07:02:04 -07002719 fromAnim.setDuration(duration);
2720 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721
2722 // Show the new toolbar buttons just as the main animation is ending
2723 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002724 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2725 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 } else {
2727 fromView.setY(fromViewEndY);
2728 fromView.setVisibility(View.GONE);
2729 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002730 toView.setScaleX(1.0f);
2731 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002732 toView.setVisibility(View.VISIBLE);
2733 hideAndShowToolbarButtons(toState, null, null);
2734 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002735 }
2736
2737 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002738 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002739 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002740 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002741
Michael Jurka79212d82010-07-30 16:36:20 -07002742 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002743 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002744 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002745 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002746 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002747 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002748 } else {
2749 mAllAppsGrid.zoom(1.0f, animated);
2750 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002751
Romain Guyc16fea72010-03-12 17:17:56 -08002752 ((View) mAllAppsGrid).setFocusable(true);
2753 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002754
Joe Onorato7c312c12009-08-13 21:36:53 -07002755 // TODO: fade these two too
2756 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002757 // Change the state *after* we've called all the transition code
2758 mState = State.ALL_APPS;
2759 }
2760
2761
2762 void showWorkspace(boolean animated) {
2763 showWorkspace(animated, null);
2764 }
2765
2766 void showWorkspace(boolean animated, CellLayout layout) {
2767 if (layout != null && animated) {
2768 mWorkspace.unshrink(layout);
2769 } else {
2770 mWorkspace.unshrink(animated);
2771 }
2772 if (mState == State.ALL_APPS) {
2773 closeAllApps(animated);
2774 } else if (mState == State.CUSTOMIZE) {
2775 hideCustomizationDrawer(animated);
2776 }
2777 // Change the state *after* we've called all the transition code
2778 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002779 }
2780
Joe Onoratob2061212009-11-24 16:13:54 -05002781 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002782 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002783 * - Home from workspace
2784 * - from center screen
2785 * - from other screens
2786 * - Home 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 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002790 * - from center screen
2791 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002792 * - Launch app from workspace and quit
2793 * - with back
2794 * - with home
2795 * - Launch app from all apps and quit
2796 * - with back
2797 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002798 * - Go to a screen that's not the default, then all
2799 * apps, and launch and app, and go back
2800 * - with back
2801 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002802 * - On workspace, long press power and go back
2803 * - with back
2804 * - with home
2805 * - On all apps, long press power and go back
2806 * - with back
2807 * - with home
2808 * - On workspace, power off
2809 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002810 * - Launch an app and turn off the screen while in that app
2811 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002812 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002813 * - From all apps
2814 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002815 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2816 * - From all apps
2817 * - From the center workspace
2818 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002819 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002820 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002821 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002822 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002823 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002824 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002825 } else {
2826 mAllAppsGrid.zoom(0.0f, animated);
2827 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002828 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002829 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002830 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002831 }
2832
Joe Onorato7c312c12009-08-13 21:36:53 -07002833 void lockAllApps() {
2834 // TODO
2835 }
2836
2837 void unlockAllApps() {
2838 // TODO
2839 }
2840
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 // Show the customization drawer (only exists in x-large configuration)
2842 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002843 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002844 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002845 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002846 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002847 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002848 // Change the state *after* we've called all the transition code
2849 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002850 }
2851
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002852 // Hide the customization drawer (only exists in x-large configuration)
2853 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002854 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002855 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002856 }
2857 }
2858
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002859 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2860 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2861 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002862 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002863 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002864
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002865 void onWorkspaceClick(CellLayout layout) {
2866 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002867 }
2868
Michael Jurka0423dcf2010-10-05 14:56:18 -07002869 private void updateButtonWithIconFromExternalActivity(
2870 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2871 ImageView button = (ImageView) findViewById(buttonId);
2872 Drawable toolbarIcon = null;
2873 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002874 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002875 // Look for the toolbar icon specified in the activity meta-data
2876 Bundle metaData = packageManager.getActivityInfo(
2877 activityName, PackageManager.GET_META_DATA).metaData;
2878 if (metaData != null) {
2879 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2880 if (iconResId != 0) {
2881 Resources res = packageManager.getResourcesForActivity(activityName);
2882 toolbarIcon = res.getDrawable(iconResId);
2883 }
2884 }
2885 } catch (NameNotFoundException e) {
2886 // Do nothing
2887 }
2888 // If we were unable to find the icon via the meta-data, use a generic one
2889 if (toolbarIcon == null) {
2890 button.setImageResource(fallbackDrawableId);
2891 } else {
2892 button.setImageDrawable(toolbarIcon);
2893 }
2894 }
2895
2896 private void updateGlobalSearchIcon() {
2897 if (LauncherApplication.isScreenXLarge()) {
2898 final SearchManager searchManager =
2899 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2900 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002901 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002902 updateButtonWithIconFromExternalActivity(
2903 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002904 }
2905 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002906 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002907
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002908 /**
2909 * Sets the app market icon (shown when all apps is visible on x-large screens)
2910 */
2911 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002912 if (LauncherApplication.isScreenXLarge()) {
2913 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2914 // Find the app market activity by resolving an intent.
2915 // (If multiple app markets are installed, it will return the ResolverActivity.)
2916 ComponentName activityName = intent.resolveActivity(getPackageManager());
2917 if (activityName != null) {
2918 mAppMarketIntent = intent;
2919 updateButtonWithIconFromExternalActivity(
2920 R.id.market_button, activityName, R.drawable.app_market_generic);
2921 }
2922 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002923 }
2924
2925 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 * Displays the shortcut creation dialog and launches, if necessary, the
2927 * appropriate activity.
2928 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002929 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002930 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2931 DialogInterface.OnShowListener {
2932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002933 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002936 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2939 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002940 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002941
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002942 builder.setInverseBackgroundForced(true);
2943
2944 AlertDialog dialog = builder.create();
2945 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002946 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002947 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002949 return dialog;
2950 }
2951
2952 public void onCancel(DialogInterface dialog) {
2953 mWaitingForResult = false;
2954 cleanup();
2955 }
2956
Romain Guycbb89e42009-06-08 15:52:54 -07002957 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002958 }
2959
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002961 try {
2962 dismissDialog(DIALOG_CREATE_SHORTCUT);
2963 } catch (Exception e) {
2964 // An exception is thrown if the dialog is not visible, which is fine
2965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
2967
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002968 /**
2969 * Handle the action clicked in the "Add to home" dialog.
2970 */
2971 public void onClick(DialogInterface dialog, int which) {
2972 Resources res = getResources();
2973 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002974
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002975 switch (which) {
2976 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002977 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002978 break;
2979 }
Romain Guycbb89e42009-06-08 15:52:54 -07002980
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002981 case AddAdapter.ITEM_APPWIDGET: {
2982 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002983
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002984 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2985 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002986 // start the pick activity
2987 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2988 break;
2989 }
Romain Guycbb89e42009-06-08 15:52:54 -07002990
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002991 case AddAdapter.ITEM_LIVE_FOLDER: {
2992 // Insert extra item to handle inserting folder
2993 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002994
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002995 ArrayList<String> shortcutNames = new ArrayList<String>();
2996 shortcutNames.add(res.getString(R.string.group_folder));
2997 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002998
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002999 ArrayList<ShortcutIconResource> shortcutIcons =
3000 new ArrayList<ShortcutIconResource>();
3001 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3002 R.drawable.ic_launcher_folder));
3003 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3004
3005 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3006 pickIntent.putExtra(Intent.EXTRA_INTENT,
3007 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3008 pickIntent.putExtra(Intent.EXTRA_TITLE,
3009 getText(R.string.title_select_live_folder));
3010 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003011
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003012 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3013 break;
3014 }
3015
3016 case AddAdapter.ITEM_WALLPAPER: {
3017 startWallpaper();
3018 break;
3019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003020 }
3021 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003022
3023 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003024 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003026 }
3027
3028 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003029 * Receives notifications when applications are added/removed.
3030 */
3031 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3032 @Override
3033 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003034 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003035 String reason = intent.getStringExtra("reason");
3036 if (!"homekey".equals(reason)) {
3037 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003038 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003039 animate = false;
3040 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003041 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003042 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003043 }
3044 }
3045
3046 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003047 * Receives notifications whenever the appwidgets are reset.
3048 */
3049 private class AppWidgetResetObserver extends ContentObserver {
3050 public AppWidgetResetObserver() {
3051 super(new Handler());
3052 }
3053
3054 @Override
3055 public void onChange(boolean selfChange) {
3056 onAppWidgetReset();
3057 }
3058 }
3059
3060 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003061 * If the activity is currently paused, signal that we need to re-run the loader
3062 * in onResume.
3063 *
3064 * This needs to be called from incoming places where resources might have been loaded
3065 * while we are paused. That is becaues the Configuration might be wrong
3066 * when we're not running, and if it comes back to what it was when we
3067 * were paused, we are not restarted.
3068 *
3069 * Implementation of the method from LauncherModel.Callbacks.
3070 *
3071 * @return true if we are currently paused. The caller might be able to
3072 * skip some work in that case since we will come back again.
3073 */
3074 public boolean setLoadOnResume() {
3075 if (mPaused) {
3076 Log.i(TAG, "setLoadOnResume");
3077 mOnResumeNeedsLoad = true;
3078 return true;
3079 } else {
3080 return false;
3081 }
3082 }
3083
3084 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003086 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003087 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003088 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003089 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003090 } else {
3091 return SCREEN_COUNT / 2;
3092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003094
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003095 void setAllAppsPagedView(PagedView view) {
3096 mAllAppsPagedView = view;
3097 }
3098
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 /**
3100 * Refreshes the shortcuts shown on the workspace.
3101 *
3102 * Implementation of the method from LauncherModel.Callbacks.
3103 */
3104 public void startBinding() {
3105 final Workspace workspace = mWorkspace;
3106 int count = workspace.getChildCount();
3107 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003108 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003109 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3110 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003111
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 if (DEBUG_USER_INTERFACE) {
3113 android.widget.Button finishButton = new android.widget.Button(this);
3114 finishButton.setText("Finish");
3115 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3116
3117 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3118 public void onClick(View v) {
3119 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 }
3123 }
3124
3125 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003126 * Bind the items start-end from the list.
3127 *
3128 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003129 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3131
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003132 setLoadOnResume();
3133
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 final Workspace workspace = mWorkspace;
3135
3136 for (int i=start; i<end; i++) {
3137 final ItemInfo item = shortcuts.get(i);
3138 mDesktopItems.add(item);
3139 switch (item.itemType) {
3140 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3141 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003142 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3144 false);
3145 break;
3146 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3147 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003148 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003149 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3151 false);
3152 break;
3153 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3154 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3155 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003156 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003157 (LiveFolderInfo) item);
3158 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3159 false);
3160 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003162 }
3163
Joe Onorato9c1289c2009-08-17 11:03:03 -04003164 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 }
3166
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 /**
3168 * Implementation of the method from LauncherModel.Callbacks.
3169 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003170 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003171 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003172 sFolders.clear();
3173 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003175
3176 /**
3177 * Add the views for a widget to the workspace.
3178 *
3179 * Implementation of the method from LauncherModel.Callbacks.
3180 */
3181 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003182 setLoadOnResume();
3183
Daniel Sandler843e8602010-06-07 14:59:01 -04003184 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3185 if (DEBUG_WIDGETS) {
3186 Log.d(TAG, "bindAppWidget: " + item);
3187 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188 final Workspace workspace = mWorkspace;
3189
3190 final int appWidgetId = item.appWidgetId;
3191 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003192 if (DEBUG_WIDGETS) {
3193 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3194 }
3195
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3197
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3199 item.hostView.setTag(item);
3200
3201 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3202 item.cellY, item.spanX, item.spanY, false);
3203
Adam Cohended9f8d2010-11-03 13:25:16 -07003204 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3205
Joe Onorato9c1289c2009-08-17 11:03:03 -04003206 workspace.requestLayout();
3207
3208 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003209
3210 if (DEBUG_WIDGETS) {
3211 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3212 + (SystemClock.uptimeMillis()-start) + "ms");
3213 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 }
3215
3216 /**
3217 * Callback saying that there aren't any more items to bind.
3218 *
3219 * Implementation of the method from LauncherModel.Callbacks.
3220 */
3221 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003222 setLoadOnResume();
3223
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 if (mSavedState != null) {
3225 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003226 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003227 }
3228
3229 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3230 if (userFolders != null) {
3231 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003232 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003233 if (info != null) {
3234 openFolder(info);
3235 }
3236 }
3237 final Folder openFolder = mWorkspace.getOpenFolder();
3238 if (openFolder != null) {
3239 openFolder.requestFocus();
3240 }
3241 }
3242
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 mSavedState = null;
3244 }
3245
3246 if (mSavedInstanceState != null) {
3247 super.onRestoreInstanceState(mSavedInstanceState);
3248 mSavedInstanceState = null;
3249 }
3250
Joe Onorato9c1289c2009-08-17 11:03:03 -04003251 mWorkspaceLoading = false;
3252 }
3253
3254 /**
3255 * Add the icons for all apps.
3256 *
3257 * Implementation of the method from LauncherModel.Callbacks.
3258 */
3259 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003260 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003261 if (mCustomizePagedView != null) {
3262 mCustomizePagedView.setApps(apps);
3263 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003264 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003265 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003266 }
3267
3268 /**
3269 * A package was installed.
3270 *
3271 * Implementation of the method from LauncherModel.Callbacks.
3272 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003273 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003274 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003276 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003277 if (mCustomizePagedView != null) {
3278 mCustomizePagedView.addApps(apps);
3279 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003280 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003281 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003282 }
3283
3284 /**
3285 * A package was updated.
3286 *
3287 * Implementation of the method from LauncherModel.Callbacks.
3288 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003289 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003290 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003292 mWorkspace.updateShortcuts(apps);
3293 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003294 if (mCustomizePagedView != null) {
3295 mCustomizePagedView.updateApps(apps);
3296 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003297 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003298 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003299 }
3300
3301 /**
3302 * A package was uninstalled.
3303 *
3304 * Implementation of the method from LauncherModel.Callbacks.
3305 */
Joe Onorato36115782010-06-17 13:28:48 -04003306 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003308 if (permanent) {
3309 mWorkspace.removeItems(apps);
3310 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003311 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003312 if (mCustomizePagedView != null) {
3313 mCustomizePagedView.removeApps(apps);
3314 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003315 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003316 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 }
Joe Onoratobe386092009-11-17 17:32:16 -08003318
3319 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003320 * A number of packages were updated.
3321 */
3322 public void bindPackagesUpdated() {
3323 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003324 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003325 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003326 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003327 }
3328
3329 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003330 * Prints out out state for debugging.
3331 */
3332 public void dumpState() {
3333 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003334 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003335 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3336 Log.d(TAG, "mRestoring=" + mRestoring);
3337 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3338 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3339 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003340 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003341 mModel.dumpState();
3342 mAllAppsGrid.dumpState();
3343 Log.d(TAG, "END launcher2 dump state");
3344 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003345}