blob: 4f6f7d8f5c2e4724059e7139e96d9c8301e1fa2f [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;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400231
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700232 private static LocaleConfiguration sLocaleConfiguration = null;
233
Romain Guy84f296c2009-11-04 15:00:44 -0800234 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700235
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700236 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700237
Romain Guy1fbc1c82009-11-09 20:43:08 -0800238 private ImageView mPreviousView;
239 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500240
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400241 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400242 private String[] mHotseatConfig = null;
243 private Intent[] mHotseats = null;
244 private Drawable[] mHotseatIcons = null;
245 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400246
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700247 private Intent mAppMarketIntent = null;
248
Adam Cohended9f8d2010-11-03 13:25:16 -0700249 // Related to the auto-advancing of widgets
250 private final int ADVANCE_MSG = 1;
251 private final int mAdvanceInterval = 20000;
252 private final int mAdvanceStagger = 250;
253 private long mAutoAdvanceSentTime;
254 private long mAutoAdvanceTimeLeft = -1;
255 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
256 new HashMap<View, AppWidgetProviderInfo>();
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 @Override
259 protected void onCreate(Bundle savedInstanceState) {
260 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700261
Winson Chungaafa03c2010-06-11 17:34:16 -0700262 if (LauncherApplication.isInPlaceRotationEnabled()) {
263 // hide the status bar (temporary until we get the status bar design figured out)
264 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
265 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
266 }
267
Joe Onorato0589f0f2010-02-08 13:44:00 -0800268 LauncherApplication app = ((LauncherApplication)getApplication());
269 mModel = app.setLauncher(this);
270 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400271 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700273
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700274 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700275 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
276 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200279 android.os.Debug.startMethodTracing(
280 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400283 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 checkForLocaleChange();
285 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700287 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
288 if (mHomeCustomizationDrawer != null) {
289 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700290
Winson Chung80baf5a2010-08-09 16:03:15 -0700291 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700292 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
293 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 TabContentFactory contentFactory = new TabContentFactory() {
295 public View createTabContent(String tag) {
296 return mCustomizePagedView;
297 }
298 };
299
Michael Jurka946ad472010-07-09 18:05:18 -0700300 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700301 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
302 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700303 String applicationsLabel = getString(R.string.applications_tab_label);
304 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
305 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700306 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700307 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
308 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700309 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
310 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
311 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700312
Winson Chung80baf5a2010-08-09 16:03:15 -0700313 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
314 public void onTabChanged(String tabId) {
315 // animate the changing of the tab content by fading pages in and out
316 final int duration = 150;
317 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700318 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700319 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700320 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700321 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700322 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700323 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700324 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
325 if (tag == WIDGETS_TAG) {
326 mCustomizePagedView.setCustomizationFilter(
327 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700328 } else if (tag == APPLICATIONS_TAG) {
329 mCustomizePagedView.setCustomizationFilter(
330 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700331 } else if (tag == WALLPAPERS_TAG) {
332 mCustomizePagedView.setCustomizationFilter(
333 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700334 } else if (tag == SHORTCUTS_TAG) {
335 mCustomizePagedView.setCustomizationFilter(
336 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700337 }
338
339 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700340 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700341 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700342 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700343 alphaAnim.start();
344 }
345 });
346 alphaAnim.start();
347 }
348 });
Michael Jurka946ad472010-07-09 18:05:18 -0700349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 setupViews();
351
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800352 registerContentObservers();
353
Joe Onorato7c312c12009-08-13 21:36:53 -0700354 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 mSavedState = savedInstanceState;
357 restoreState(mSavedState);
358
359 if (PROFILE_STARTUP) {
360 android.os.Debug.stopMethodTracing();
361 }
362
363 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400364 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
366
367 // For handling default keys
368 mDefaultKeySsb = new SpannableStringBuilder();
369 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800370
371 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
372 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 }
Romain Guycbb89e42009-06-08 15:52:54 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 if (sLocaleConfiguration == null) {
377 new AsyncTask<Void, Void, LocaleConfiguration>() {
378 @Override
379 protected LocaleConfiguration doInBackground(Void... unused) {
380 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
381 readConfiguration(Launcher.this, localeConfiguration);
382 return localeConfiguration;
383 }
384
385 @Override
386 protected void onPostExecute(LocaleConfiguration result) {
387 sLocaleConfiguration = result;
388 checkForLocaleChange(); // recursive, but now with a locale configuration
389 }
390 }.execute();
391 return;
392 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 final Configuration configuration = getResources().getConfiguration();
395
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final String locale = configuration.locale.toString();
398
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final int mcc = configuration.mcc;
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final int mnc = configuration.mnc;
404
Romain Guy84f296c2009-11-04 15:00:44 -0800405 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406
Romain Guy84f296c2009-11-04 15:00:44 -0800407 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700408 sLocaleConfiguration.locale = locale;
409 sLocaleConfiguration.mcc = mcc;
410 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700411
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400413 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414
415 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
416 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700417 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 public void run() {
419 writeConfiguration(Launcher.this, localeConfiguration);
420 }
421 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700422 }
423 }
424
425 private static class LocaleConfiguration {
426 public String locale;
427 public int mcc = -1;
428 public int mnc = -1;
429 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700430
Romain Guy98d01652009-06-30 16:21:04 -0700431 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
432 DataInputStream in = null;
433 try {
434 in = new DataInputStream(context.openFileInput(PREFERENCES));
435 configuration.locale = in.readUTF();
436 configuration.mcc = in.readInt();
437 configuration.mnc = in.readInt();
438 } catch (FileNotFoundException e) {
439 // Ignore
440 } catch (IOException e) {
441 // Ignore
442 } finally {
443 if (in != null) {
444 try {
445 in.close();
446 } catch (IOException e) {
447 // Ignore
448 }
449 }
450 }
451 }
452
453 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
454 DataOutputStream out = null;
455 try {
456 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
457 out.writeUTF(configuration.locale);
458 out.writeInt(configuration.mcc);
459 out.writeInt(configuration.mnc);
460 out.flush();
461 } catch (FileNotFoundException e) {
462 // Ignore
463 } catch (IOException e) {
464 //noinspection ResultOfMethodCallIgnored
465 context.getFileStreamPath(PREFERENCES).delete();
466 } finally {
467 if (out != null) {
468 try {
469 out.close();
470 } catch (IOException e) {
471 // Ignore
472 }
473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 }
475 }
476
477 static int getScreen() {
478 synchronized (sLock) {
479 return sScreen;
480 }
481 }
482
483 static void setScreen(int screen) {
484 synchronized (sLock) {
485 sScreen = screen;
486 }
487 }
488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700490 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
492 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700493 // TODO: Put back when we decide about scrolling the wallpaper
494 // boolean isPortrait = display.getWidth() < display.getHeight();
495 // final int width = isPortrait ? display.getWidth() : display.getHeight();
496 // final int height = isPortrait ? display.getHeight() : display.getWidth();
497 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
498 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
500
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400501 // Note: This doesn't do all the client-id magic that BrowserProvider does
502 // in Browser. (http://b/2425179)
503 private Uri getDefaultBrowserUri() {
504 String url = getString(R.string.default_browser_url);
505 if (url.indexOf("{CID}") != -1) {
506 url = url.replace("{CID}", "android-google");
507 }
508 return Uri.parse(url);
509 }
510
511 // Load the Intent templates from arrays.xml to populate the hotseats. For
512 // each Intent, if it resolves to a single app, use that as the launch
513 // intent & use that app's label as the contentDescription. Otherwise,
514 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400515 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400516 if (mHotseatConfig == null) {
517 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
518 if (mHotseatConfig.length > 0) {
519 mHotseats = new Intent[mHotseatConfig.length];
520 mHotseatLabels = new CharSequence[mHotseatConfig.length];
521 mHotseatIcons = new Drawable[mHotseatConfig.length];
522 } else {
523 mHotseats = null;
524 mHotseatIcons = null;
525 mHotseatLabels = null;
526 }
527
528 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
529 for (int i=0; i<mHotseatConfig.length; i++) {
530 // load icon for this slot; currently unrelated to the actual activity
531 try {
532 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
533 } catch (ArrayIndexOutOfBoundsException ex) {
534 Log.w(TAG, "Missing hotseat_icons array item #" + i);
535 mHotseatIcons[i] = null;
536 }
537 }
538 hotseatIconDrawables.recycle();
539 }
540
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400541 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400542 for (int i=0; i<mHotseatConfig.length; i++) {
543 Intent intent = null;
544 if (mHotseatConfig[i].equals("*BROWSER*")) {
545 // magic value meaning "launch user's default web browser"
546 // replace it with a generic web request so we can see if there is indeed a default
547 String defaultUri = getString(R.string.default_browser_url);
548 intent = new Intent(
549 Intent.ACTION_VIEW,
550 ((defaultUri != null)
551 ? Uri.parse(defaultUri)
552 : getDefaultBrowserUri())
553 ).addCategory(Intent.CATEGORY_BROWSABLE);
554 // note: if the user launches this without a default set, she
555 // will always be taken to the default URL above; this is
556 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700557 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400558 // URL is unavoidably sent to the chosen app.
559 } else {
560 try {
561 intent = Intent.parseUri(mHotseatConfig[i], 0);
562 } catch (java.net.URISyntaxException ex) {
563 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
564 // bogus; leave intent=null
565 }
566 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700567
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400568 if (intent == null) {
569 mHotseats[i] = null;
570 mHotseatLabels[i] = getText(R.string.activity_not_found);
571 continue;
572 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400573
574 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 Log.d(TAG, "loadHotseats: hotseat " + i
576 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400577 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400578 + "]");
579 }
580
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400581 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
582 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 Log.d(TAG, "Best match for intent: " + bestMatch);
585 Log.d(TAG, "All matches: ");
586 for (ResolveInfo ri : allMatches) {
587 Log.d(TAG, " --> " + ri);
588 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400589 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400590 // did this resolve to a single app, or the resolver?
591 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700592 // can't find any activity to handle this. let's leave the
593 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400594 // to launch.
595 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400596
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400597 // set accessibility text to "Not installed"
598 mHotseatLabels[i] = getText(R.string.activity_not_found);
599 } else {
600 boolean found = false;
601 for (ResolveInfo ri : allMatches) {
602 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
603 && bestMatch.activityInfo.applicationInfo.packageName
604 .equals(ri.activityInfo.applicationInfo.packageName)) {
605 found = true;
606 break;
607 }
608 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700609
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400610 if (!found) {
611 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
612 // the bestMatch is probably the ResolveActivity, meaning the
613 // user has not yet selected a default
614 // so: we'll keep the original intent for now
615 mHotseats[i] = intent;
616
617 // set the accessibility text to "Select shortcut"
618 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
619 } else {
620 // we have an app!
621 // now reconstruct the intent to launch it through the front
622 // door
623 ComponentName com = new ComponentName(
624 bestMatch.activityInfo.applicationInfo.packageName,
625 bestMatch.activityInfo.name);
626 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
627
628 // load the app label for accessibility
629 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
630 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631 }
632
633 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700634 Log.d(TAG, "loadHotseats: hotseat " + i
635 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400636 + ((mHotseats[i] == null)
637 ? "null"
638 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400639 + "] label=[" + mHotseatLabels[i]
640 + "]"
641 );
642 }
643 }
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 @Override
647 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700648 mWaitingForResult = false;
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 // The pattern used here is that a user PICKs a specific application,
651 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
654 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
Michael Jurka0280c3b2010-09-17 15:00:07 -0700656 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 switch (requestCode) {
658 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700659 completeAddApplication(
660 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 break;
662 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800663 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
665 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700666 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
668 case REQUEST_PICK_LIVE_FOLDER:
669 addLiveFolder(data);
670 break;
671 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700672 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700674 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700675 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700679 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700681 case REQUEST_PICK_WALLPAPER:
682 // We just wanted the activity result here so we can clear mWaitingForResult
683 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
Romain Guy18042c82009-11-06 11:44:55 -0800685 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
686 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
687 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700688 // Clean up the appWidgetId if we canceled
689 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
690 if (appWidgetId != -1) {
691 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
696 @Override
697 protected void onResume() {
698 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800699 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700700 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400701 mWorkspaceLoading = true;
702 mModel.startLoader(this, true);
703 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700704 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700706 // When we resume Launcher, a different Activity might be responsible for the app
707 // market intent, so refresh the icon
708 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 }
710
711 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700712 protected void onPause() {
713 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700714 // Some launcher layouts don't have a previous and next view
715 if (mPreviousView != null) {
716 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700717 }
718 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700719 dismissPreview(mNextView);
720 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700721 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800722 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 }
Romain Guycbb89e42009-06-08 15:52:54 -0700724
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700725 @Override
726 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400727 // Flag the loader to stop early before switching
728 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800729 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800730 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700731 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700732
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800733 // We can't hide the IME if it was forced open. So don't bother
734 /*
735 @Override
736 public void onWindowFocusChanged(boolean hasFocus) {
737 super.onWindowFocusChanged(hasFocus);
738
739 if (hasFocus) {
740 final InputMethodManager inputManager = (InputMethodManager)
741 getSystemService(Context.INPUT_METHOD_SERVICE);
742 WindowManager.LayoutParams lp = getWindow().getAttributes();
743 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
744 android.os.Handler()) {
745 protected void onReceiveResult(int resultCode, Bundle resultData) {
746 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
747 }
748 });
749 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
750 }
751 }
752 */
753
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 private boolean acceptFilter() {
755 final InputMethodManager inputManager = (InputMethodManager)
756 getSystemService(Context.INPUT_METHOD_SERVICE);
757 return !inputManager.isFullscreenMode();
758 }
759
760 @Override
761 public boolean onKeyDown(int keyCode, KeyEvent event) {
762 boolean handled = super.onKeyDown(keyCode, event);
763 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
764 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
765 keyCode, event);
766 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700767 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700768 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700769 // showSearchDialog()
770 // If there are multiple keystrokes before the search dialog takes focus,
771 // onSearchRequested() will be called for every keystroke,
772 // but it is idempotent, so it's fine.
773 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775 }
776
Joe Onorato8a9625e2010-01-28 15:55:35 -0800777 // Eat the long press event so the keyboard doesn't come up.
778 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
779 return true;
780 }
781
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 return handled;
783 }
784
Karl Rosaen138a0412009-04-23 19:00:21 -0700785 private String getTypedText() {
786 return mDefaultKeySsb.toString();
787 }
788
789 private void clearTypedText() {
790 mDefaultKeySsb.clear();
791 mDefaultKeySsb.clearSpans();
792 Selection.setSelection(mDefaultKeySsb, 0);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700796 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
797 * State
798 */
799 private static State intToState(int stateOrdinal) {
800 State state = State.WORKSPACE;
801 final State[] stateValues = State.values();
802 for (int i = 0; i < stateValues.length; i++) {
803 if (stateValues[i].ordinal() == stateOrdinal) {
804 state = stateValues[i];
805 break;
806 }
807 }
808 return state;
809 }
810
811 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 * Restores the previous state, if it exists.
813 *
814 * @param savedState The previous state.
815 */
816 private void restoreState(Bundle savedState) {
817 if (savedState == null) {
818 return;
819 }
820
Michael Jurka883f55b2010-10-21 15:47:14 -0700821 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
822
823 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800824 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700825 } else if (state == State.CUSTOMIZE) {
826 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
830 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700831 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
834 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700835
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700837 mAddScreen = addScreen;
838 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
839 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 mRestoring = true;
841 }
842
843 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
844 if (renameFolder) {
845 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700846 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847 mRestoring = true;
848 }
849 }
850
851 /**
852 * Finds all the views we need and configure them properly.
853 */
854 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700855 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400856
Romain Guyb1b69f52009-08-10 15:10:15 -0700857 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400858 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859
Joe Onorato7c312c12009-08-13 21:36:53 -0700860 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700861 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700862 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800863 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700864 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700865 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700866
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700867 if (LauncherApplication.isScreenXLarge()) {
868 // They need to be INVISIBLE initially so that they will be measured in the layout.
869 // Otherwise the animations are messed up when we show them for the first time.
870 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
871 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
872 }
873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
875 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500876 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700877
Joe Onorato7c312c12009-08-13 21:36:53 -0700878 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
879 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700881 View handleView = findViewById(R.id.all_apps_button);
882 if (handleView != null && handleView instanceof HandleView) {
883 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700884 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700885 mHandleView.setLauncher(this);
886 mHandleView.setOnClickListener(this);
887 mHandleView.setOnLongClickListener(this);
888 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800889
Winson Chung80baf5a2010-08-09 16:03:15 -0700890 if (mCustomizePagedView != null) {
891 mCustomizePagedView.setLauncher(this);
892 mCustomizePagedView.setDragController(dragController);
893 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700894 } else {
895 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
896 hotseatLeft.setContentDescription(mHotseatLabels[0]);
897 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
898 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
899 hotseatRight.setContentDescription(mHotseatLabels[1]);
900 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400901
Michael Jurkaaf442092010-06-10 17:01:57 -0700902 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
903 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800904
Michael Jurkaaf442092010-06-10 17:01:57 -0700905 Drawable previous = mPreviousView.getDrawable();
906 Drawable next = mNextView.getDrawable();
907 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
Michael Jurkaaf442092010-06-10 17:01:57 -0700909 mPreviousView.setHapticFeedbackEnabled(false);
910 mPreviousView.setOnLongClickListener(this);
911 mNextView.setHapticFeedbackEnabled(false);
912 mNextView.setOnLongClickListener(this);
913 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800914
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400916 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918
919 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400920 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700921 int deleteZoneHandleId;
922 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700923 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700924 } else {
925 deleteZoneHandleId = R.id.all_apps_button_cluster;
926 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700927 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700928 dragController.addDragListener(deleteZone);
929
930 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
931 if (infoButton != null) {
932 infoButton.setLauncher(this);
933 infoButton.setHandle(findViewById(R.id.configure_button));
934 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
935 dragController.addDragListener(infoButton);
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937
Joe Onorato00acb122009-08-04 16:04:30 -0400938 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400939 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800940 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700941
Joe Onorato00acb122009-08-04 16:04:30 -0400942 // The order here is bottom to top.
943 dragController.addDropTarget(workspace);
944 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700945 if (infoButton != null) {
946 dragController.addDropTarget(infoButton);
947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
Romain Guy8a73c512009-11-09 19:19:59 -0800950 @SuppressWarnings({"UnusedDeclaration"})
951 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700952 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800953 mWorkspace.scrollLeft();
954 }
Romain Guy8a73c512009-11-09 19:19:59 -0800955 }
956
957 @SuppressWarnings({"UnusedDeclaration"})
958 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700959 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800960 mWorkspace.scrollRight();
961 }
Romain Guy8a73c512009-11-09 19:19:59 -0800962 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400963
964 @SuppressWarnings({"UnusedDeclaration"})
965 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700966 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400967
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400968 int index = -1;
969 if (v.getId() == R.id.hotseat_left) {
970 index = 0;
971 } else if (v.getId() == R.id.hotseat_right) {
972 index = 1;
973 }
974
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400975 // reload these every tap; you never know when they might change
976 loadHotseats();
977 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
978 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400979 startActivitySafely(
980 mHotseats[index],
981 "hotseat"
982 );
983 }
984 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 /**
987 * Creates a view representing a shortcut.
988 *
989 * @param info The data structure describing the shortcut.
990 *
991 * @return A View inflated from R.layout.application.
992 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700995 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 }
997
998 /**
999 * Creates a view representing a shortcut inflated from the specified resource.
1000 *
1001 * @param layoutResId The id of the XML layout used to create the shortcut.
1002 * @param parent The group the shortcut belongs to.
1003 * @param info The data structure describing the shortcut.
1004 *
1005 * @return A View inflated from layoutResId.
1006 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001007 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1009
Patrick Dubroy9e482382010-11-01 15:16:13 -07001010 Bitmap b = info.getIcon(mIconCache);
1011
Joe Onorato0589f0f2010-02-08 13:44:00 -08001012 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001013 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 favorite.setText(info.title);
1016 favorite.setTag(info);
1017 favorite.setOnClickListener(this);
1018
1019 return favorite;
1020 }
1021
1022 /**
1023 * Add an application shortcut to the workspace.
1024 *
1025 * @param data The intent describing the application.
1026 * @param cellInfo The position on screen where to create the shortcut.
1027 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 void completeAddApplication(Context context, Intent data, int screen,
1029 int intersectCellX, int intersectCellY) {
1030 final int[] cellXY = mTmpAddItemCellCoordinates;
1031 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1032
1033 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1034 showOutOfSpaceMessage();
1035 return;
1036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037
Joe Onorato0589f0f2010-02-08 13:44:00 -08001038 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1039 data, context);
1040
Romain Guy73b979d2009-06-09 12:57:21 -07001041 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001042 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001044 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001045 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1046 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 } else {
1048 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
1050 }
Romain Guycbb89e42009-06-08 15:52:54 -07001051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 /**
1053 * Add a shortcut to the workspace.
1054 *
1055 * @param data The intent describing the shortcut.
1056 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001058 private void completeAddShortcut(Intent data, int screen,
1059 int intersectCellX, int intersectCellY) {
1060 final int[] cellXY = mTmpAddItemCellCoordinates;
1061 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001062
Michael Jurka0280c3b2010-09-17 15:00:07 -07001063 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1064 showOutOfSpaceMessage();
1065 return;
1066 }
1067
1068 final ShortcutInfo info = mModel.addShortcut(
1069 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070
1071 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075 }
1076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001080 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001083 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001084 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001085
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001086 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1088 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001091 // We have saved the position to which the widget was dragged-- this really only matters
1092 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001094
1095 // For now, we don't save the coordinate where we dropped the icon because we're not
1096 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1097 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001098 int[] touchXY = null;
1099 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1100 touchXY = mAddDropPosition;
1101 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001102 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001103 boolean foundCellSpan = false;
1104 if (touchXY != null) {
1105 // when dragging and dropping, just find the closest free spot
1106 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1107 int[] result = screenLayout.findNearestVacantArea(
1108 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001109 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 foundCellSpan = !findNearestVacantAreaFailed;
1111 } else {
1112 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1113 // if we long pressed on an empty cell to bring up a menu,
1114 // make sure we intersect the empty cell
1115 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1116 mAddIntersectCellX, mAddIntersectCellY);
1117 } else {
1118 // if we went through the menu -> add, just find any spot
1119 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1120 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001121 }
1122
Michael Jurka0280c3b2010-09-17 15:00:07 -07001123 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001124 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001126 return;
1127 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 // Build Launcher-specific widget info and save to database
1130 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001131 launcherInfo.spanX = spanXY[0];
1132 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 LauncherModel.addItemToDatabase(this, launcherInfo,
1135 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001136 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137
1138 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001142 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001143
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001144 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
Michael Jurka0280c3b2010-09-17 15:00:07 -07001147 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001148 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001149
1150 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151 }
1152 }
Romain Guycbb89e42009-06-08 15:52:54 -07001153
Adam Cohended9f8d2010-11-03 13:25:16 -07001154 private boolean mUserPresent = true;
1155 private boolean mVisible = false;
1156
1157 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1158 @Override
1159 public void onReceive(Context context, Intent intent) {
1160 final String action = intent.getAction();
1161 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1162 mUserPresent = false;
1163 updateRunning();
1164 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1165 mUserPresent = true;
1166 updateRunning();
1167 }
1168 }
1169 };
1170
1171 @Override
1172 public void onAttachedToWindow() {
1173 super.onAttachedToWindow();
1174
1175 // Listen for broadcasts related to user-presence
1176 final IntentFilter filter = new IntentFilter();
1177 filter.addAction(Intent.ACTION_SCREEN_OFF);
1178 filter.addAction(Intent.ACTION_USER_PRESENT);
1179 registerReceiver(mReceiver, filter);
1180
1181 mVisible = true;
1182 }
1183
1184 @Override
1185 public void onDetachedFromWindow() {
1186 super.onDetachedFromWindow();
1187 mVisible = false;
1188
1189 unregisterReceiver(mReceiver);
1190 updateRunning();
1191 }
1192
1193 public void onWindowVisibilityChanged(int visibility) {
1194 mVisible = visibility == View.VISIBLE;
1195 updateRunning();
1196 }
1197
1198 private void sendAdvanceMessage(long delay) {
1199 mHandler.removeMessages(ADVANCE_MSG);
1200 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1201 mHandler.sendMessageDelayed(msg, delay);
1202 mAutoAdvanceSentTime = System.currentTimeMillis();
1203 }
1204
1205 private void updateRunning() {
1206 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1207 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1208 mAutoAdvanceRunning = autoAdvanceRunning;
1209 if (autoAdvanceRunning) {
1210 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1211 sendAdvanceMessage(delay);
1212 } else {
1213 if (!mWidgetsToAdvance.isEmpty()) {
1214 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1215 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1216 }
1217 mHandler.removeMessages(ADVANCE_MSG);
1218 }
1219 }
1220 }
1221
1222 private final Handler mHandler = new Handler() {
1223 @Override
1224 public void handleMessage(Message msg) {
1225 if (msg.what == ADVANCE_MSG) {
1226 int i = 0;
1227 for (View key: mWidgetsToAdvance.keySet()) {
1228 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1229 final int delay = mAdvanceStagger * i;
1230 if (v instanceof Advanceable) {
1231 postDelayed(new Runnable() {
1232 public void run() {
1233 ((Advanceable) v).advance();
1234 }
1235 }, delay);
1236 }
1237 i++;
1238 }
1239 sendAdvanceMessage(mAdvanceInterval);
1240 }
1241 }
1242 };
1243
1244 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1245 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1246 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1247 if (v instanceof Advanceable) {
1248 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1249 ((Advanceable) v).willBeAdvancedByHost();
1250 updateRunning();
1251 }
1252 }
1253
1254 void removeWidgetToAutoAdvance(View hostView) {
1255 if (mWidgetsToAdvance.containsKey(hostView)) {
1256 mWidgetsToAdvance.remove(hostView);
1257 updateRunning();
1258 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001259 }
1260
Joe Onorato9c1289c2009-08-17 11:03:03 -04001261 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1262 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001263 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001264 launcherInfo.hostView = null;
1265 }
1266
Adam Cohended9f8d2010-11-03 13:25:16 -07001267 void showOutOfSpaceMessage() {
1268 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1269 }
1270
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001271 public LauncherAppWidgetHost getAppWidgetHost() {
1272 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Winson Chunga9abd0e2010-10-27 17:18:37 -07001275 public LauncherModel getModel() {
1276 return mModel;
1277 }
1278
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001279 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001280 getWindow().closeAllPanels();
1281
1282 try {
1283 dismissDialog(DIALOG_CREATE_SHORTCUT);
1284 // Unlock the workspace if the dialog was showing
1285 } catch (Exception e) {
1286 // An exception is thrown if the dialog is not visible, which is fine
1287 }
1288
1289 try {
1290 dismissDialog(DIALOG_RENAME_FOLDER);
1291 // Unlock the workspace if the dialog was showing
1292 } catch (Exception e) {
1293 // An exception is thrown if the dialog is not visible, which is fine
1294 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001295
1296 // Whatever we were doing is hereby canceled.
1297 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001298 }
1299
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 @Override
1301 protected void onNewIntent(Intent intent) {
1302 super.onNewIntent(intent);
1303
1304 // Close the menu
1305 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001306 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001307 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001308
Joe Onorato14f122b2009-11-19 14:06:36 -08001309 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1310 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001311
Michael Jurka4cb37242010-08-09 21:05:32 -07001312 // in all these cases, only animate if we're already on home
1313 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001314 mWorkspace.unshrink(alreadyOnHome);
1315 }
1316 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001317 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001318 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1319 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001320 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001321 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001322
Joe Onorato3a8820b2009-11-10 15:06:42 -08001323 final View v = getWindow().peekDecorView();
1324 if (v != null && v.getWindowToken() != null) {
1325 InputMethodManager imm = (InputMethodManager)getSystemService(
1326 INPUT_METHOD_SERVICE);
1327 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328 }
1329 }
1330 }
1331
1332 @Override
1333 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1334 // Do not call super here
1335 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001336
1337 if (mHomeCustomizationDrawer != null) {
1338 String cur = savedInstanceState.getString("currentTab");
1339 if (cur != null) {
1340 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1341 }
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
1345 @Override
1346 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001347 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001348
1349 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1350 if (folders.size() > 0) {
1351 final int count = folders.size();
1352 long[] ids = new long[count];
1353 for (int i = 0; i < count; i++) {
1354 final FolderInfo info = folders.get(i).getInfo();
1355 ids[i] = info.id;
1356 }
1357 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1358 } else {
1359 super.onSaveInstanceState(outState);
1360 }
1361
Michael Jurka883f55b2010-10-21 15:47:14 -07001362 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 if (mAddScreen > -1 && mWaitingForResult) {
1365 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1366 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1367 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 }
1369
1370 if (mFolderInfo != null && mWaitingForResult) {
1371 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1372 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1373 }
Michael Jurka946ad472010-07-09 18:05:18 -07001374
1375 if (mHomeCustomizationDrawer != null) {
1376 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1377 if (currentTabTag != null) {
1378 outState.putString("currentTab", currentTabTag);
1379 }
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382
1383 @Override
1384 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001386
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001388 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001390 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392
1393 TextKeyListener.getInstance().release();
1394
Joe Onorato9c1289c2009-08-17 11:03:03 -04001395 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396
Joe Onorato9c1289c2009-08-17 11:03:03 -04001397 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001398
1399 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001400
Patrick Dubroyab962b72010-07-26 12:10:10 -07001401 // Some launcher layouts don't have a previous and next view
1402 if (mPreviousView != null) {
1403 dismissPreview(mPreviousView);
1404 }
1405 if (mNextView != null) {
1406 dismissPreview(mNextView);
1407 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001408
1409 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411
1412 @Override
1413 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001414 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 super.startActivityForResult(intent, requestCode);
1416 }
1417
1418 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001419 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001421
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001422 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001423
Karl Rosaen138a0412009-04-23 19:00:21 -07001424 if (initialQuery == null) {
1425 // Use any text typed in the launcher as the initial query
1426 initialQuery = getTypedText();
1427 clearTypedText();
1428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 if (appSearchData == null) {
1430 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001431 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 }
Romain Guycbb89e42009-06-08 15:52:54 -07001433
Karl Rosaen138a0412009-04-23 19:00:21 -07001434 final SearchManager searchManager =
1435 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001436 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001437 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
1440 @Override
1441 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001442 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001443 return false;
1444 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001445
1446 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001447
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1449 .setIcon(android.R.drawable.ic_menu_add)
1450 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001451 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1452 .setIcon(android.R.drawable.ic_menu_manage)
1453 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001454 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 .setIcon(android.R.drawable.ic_menu_gallery)
1456 .setAlphabeticShortcut('W');
1457 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1458 .setIcon(android.R.drawable.ic_search_category_default)
1459 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1460 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1461 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1462 .setAlphabeticShortcut('N');
1463
1464 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001465 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1466 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467
1468 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1469 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1470 .setIntent(settings);
1471
1472 return true;
1473 }
1474
1475 @Override
1476 public boolean onPrepareOptionsMenu(Menu menu) {
1477 super.onPrepareOptionsMenu(menu);
1478
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001479 // If all apps is animating, don't show the menu, because we don't know
1480 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001481 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001482 return false;
1483 }
1484
1485 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001486 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001487 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1488 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1489
1490 // Disable add if the workspace is full.
1491 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1493 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001494 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495
1496 return true;
1497 }
1498
1499 @Override
1500 public boolean onOptionsItemSelected(MenuItem item) {
1501 switch (item.getItemId()) {
1502 case MENU_ADD:
1503 addItems();
1504 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001505 case MENU_MANAGE_APPS:
1506 manageApps();
1507 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 case MENU_WALLPAPER_SETTINGS:
1509 startWallpaper();
1510 return true;
1511 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001512 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return true;
1514 case MENU_NOTIFICATIONS:
1515 showNotifications();
1516 return true;
1517 }
1518
1519 return super.onOptionsItemSelected(item);
1520 }
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Karl Rosaen138a0412009-04-23 19:00:21 -07001522 /**
1523 * Indicates that we want global search for this activity by setting the globalSearch
1524 * argument for {@link #startSearch} to true.
1525 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001527 @Override
1528 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001529 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001531 }
1532
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533 public boolean isWorkspaceLocked() {
1534 return mWorkspaceLoading || mWaitingForResult;
1535 }
1536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001538 if (LauncherApplication.isScreenXLarge()) {
1539 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001540 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001541 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001542 }
1543 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001544 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 }
1548
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 private void resetAddInfo() {
1550 mAddScreen = -1;
1551 mAddIntersectCellX = -1;
1552 mAddIntersectCellY = -1;
1553 mAddDropPosition = null;
1554 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001555
Winson Chung55cef262010-10-28 14:14:18 -07001556 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001557 resetAddInfo();
1558 mAddScreen = screen;
1559
1560 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1561 mAddDropPosition = position;
1562
Michael Jurkaaf442092010-06-10 17:01:57 -07001563 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001564 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1565 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001566 }
1567
Winson Chung7ad01412010-09-27 11:33:03 -07001568 private void manageApps() {
1569 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1570 }
1571
Michael Jurkaaf442092010-06-10 17:01:57 -07001572 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001573 // TODO: catch bad widget exception when sent
1574 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001575 // TODO: Is this log message meaningful?
1576 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001577 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001578 }
1579
Winson Chung55cef262010-10-28 14:14:18 -07001580 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001581 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582
Bjorn Bringert7984c942009-12-09 15:38:25 +00001583 if (appWidget.configure != null) {
1584 // Launch over to configure widget, if needed
1585 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1586 intent.setComponent(appWidget.configure);
1587 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001588 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001589 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1590 intent.putExtra(
1591 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1592 info.mimeType);
1593
1594 final String mimeType = info.mimeType;
1595 final ClipData clipData = (ClipData) info.configurationData;
1596 final ClipDescription clipDesc = clipData.getDescription();
1597 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1598 if (clipDesc.getMimeType(i).equals(mimeType)) {
1599 final ClipData.Item item = clipData.getItem(i);
1600 final CharSequence stringData = item.getText();
1601 final Uri uriData = item.getUri();
1602 final Intent intentData = item.getIntent();
1603 final String key =
1604 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1605 if (uriData != null) {
1606 intent.putExtra(key, uriData);
1607 } else if (intentData != null) {
1608 intent.putExtra(key, intentData);
1609 } else if (stringData != null) {
1610 intent.putExtra(key, stringData);
1611 }
1612 break;
1613 }
1614 }
1615 }
Winson Chung55cef262010-10-28 14:14:18 -07001616 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001617
Romain Guy8e633c52010-03-04 12:51:36 -08001618 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001620 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001621 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
1623 }
Romain Guycbb89e42009-06-08 15:52:54 -07001624
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1626 resetAddInfo();
1627 mAddScreen = screen;
1628 mAddDropPosition = position;
1629
1630 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1631 createShortcutIntent.setComponent(componentName);
1632 processShortcut(createShortcutIntent);
1633 }
1634
Joe Onoratodeb98af2010-02-19 14:59:39 -08001635 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001636 // Handle case where user selected "Applications"
1637 String applicationName = getResources().getString(R.string.group_applications);
1638 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001639
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001640 if (applicationName != null && applicationName.equals(shortcutName)) {
1641 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1642 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001643
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001644 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1645 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001646 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001647 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001648 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001649 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652
Winson Chung24ab2f12010-09-16 14:10:47 -07001653 void processWallpaper(Intent intent) {
1654 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1655 }
1656
Michael Jurka0280c3b2010-09-17 15:00:07 -07001657 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1658 resetAddInfo();
1659 mAddScreen = screen;
1660 mAddDropPosition = position;
1661
1662 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1663 createFolderIntent.setComponent(componentName);
1664
1665 addLiveFolder(createFolderIntent);
1666 }
1667
1668 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001669 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001670 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001671 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001672
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001673 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001674 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001675 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001676 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001677 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001678 }
1679
Michael Jurka0280c3b2010-09-17 15:00:07 -07001680 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 UserFolderInfo folderInfo = new UserFolderInfo();
1682 folderInfo.title = getText(R.string.folder_name);
1683
Michael Jurka0280c3b2010-09-17 15:00:07 -07001684 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1685 final int[] cellXY = mTmpAddItemCellCoordinates;
1686 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1687 showOutOfSpaceMessage();
1688 return;
1689 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690
1691 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001692 LauncherModel.addItemToDatabase(this, folderInfo,
1693 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001694 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001695 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696
1697 // Create the view
1698 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001699 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1700 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
Romain Guycbb89e42009-06-08 15:52:54 -07001703
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001705 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001706 }
1707
Michael Jurka28750fb2010-09-24 17:43:49 -07001708 private void completeAddLiveFolder(
1709 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001710 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1711 final int[] cellXY = mTmpAddItemCellCoordinates;
1712 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1713 showOutOfSpaceMessage();
1714 return;
1715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716
Michael Jurka0280c3b2010-09-17 15:00:07 -07001717 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718
1719 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001721 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001722 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724 }
1725
1726 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001727 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728
1729 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1730 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1731
1732 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001733 Intent.ShortcutIconResource iconResource = null;
1734
1735 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1736 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1737 try {
1738 iconResource = (Intent.ShortcutIconResource) extra;
1739 final PackageManager packageManager = context.getPackageManager();
1740 Resources resources = packageManager.getResourcesForApplication(
1741 iconResource.packageName);
1742 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1743 icon = resources.getDrawable(id);
1744 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001745 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747 }
1748
1749 if (icon == null) {
1750 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1751 }
1752
1753 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001754 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 info.title = name;
1756 info.iconResource = iconResource;
1757 info.uri = data.getData();
1758 info.baseIntent = baseIntent;
1759 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1760 LiveFolders.DISPLAY_MODE_GRID);
1761
1762 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001763 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001764 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765
1766 return info;
1767 }
1768
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 private void showNotifications() {
1770 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1771 if (statusBar != null) {
1772 statusBar.expand();
1773 }
1774 }
1775
1776 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001777 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001779 Intent chooser = Intent.createChooser(pickWallpaper,
1780 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001781 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1782 // Removed in Eclair MR1
1783// WallpaperManager wm = (WallpaperManager)
1784// getSystemService(Context.WALLPAPER_SERVICE);
1785// WallpaperInfo wi = wm.getWallpaperInfo();
1786// if (wi != null && wi.getSettingsActivity() != null) {
1787// LabeledIntent li = new LabeledIntent(getPackageName(),
1788// R.string.configure_wallpaper, 0);
1789// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1790// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1791// }
Mike Clerona0618e42009-10-22 13:55:21 -07001792 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 }
1794
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001795 /**
1796 * Registers various content observers. The current implementation registers
1797 * only a favorites observer to keep track of the favorites applications.
1798 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001799 private void registerContentObservers() {
1800 ContentResolver resolver = getContentResolver();
1801 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1802 true, mWidgetObserver);
1803 }
1804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 @Override
1806 public boolean dispatchKeyEvent(KeyEvent event) {
1807 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1808 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001810 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001811 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001812 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001813 dumpState();
1814 return true;
1815 }
1816 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001817 }
1818 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1819 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001820 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 return true;
1822 }
1823 }
1824
1825 return super.dispatchKeyEvent(event);
1826 }
1827
Joe Onorato88ec0992009-11-19 13:16:06 -08001828 @Override
1829 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001830 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1831 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001832 } else {
1833 closeFolder();
1834 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001835 // Some launcher layouts don't have a previous and next view
1836 if (mPreviousView != null) {
1837 dismissPreview(mPreviousView);
1838 dismissPreview(mNextView);
1839 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001840 }
1841
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001842 private void closeFolder() {
1843 Folder folder = mWorkspace.getOpenFolder();
1844 if (folder != null) {
1845 closeFolder(folder);
1846 }
1847 }
1848
1849 void closeFolder(Folder folder) {
1850 folder.getInfo().opened = false;
1851 ViewGroup parent = (ViewGroup) folder.getParent();
1852 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001853 CellLayout cl = (CellLayout) parent;
1854 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001855 if (folder instanceof DropTarget) {
1856 // Live folders aren't DropTargets.
1857 mDragController.removeDropTarget((DropTarget)folder);
1858 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001859 }
1860 folder.onClose();
1861 }
1862
1863 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001864 * Re-listen when widgets are reset.
1865 */
1866 private void onAppWidgetReset() {
1867 mAppWidgetHost.startListening();
1868 }
1869
1870 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001871 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1872 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001874 private void unbindDesktopItems() {
1875 for (ItemInfo item: mDesktopItems) {
1876 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 }
1878 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 /**
1881 * Launches the intent referred by the clicked shortcut.
1882 *
1883 * @param v The view representing the clicked shortcut.
1884 */
1885 public void onClick(View v) {
1886 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001887 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001889 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001890 int[] pos = new int[2];
1891 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001892 intent.setSourceBounds(new Rect(pos[0], pos[1],
1893 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001894 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 } else if (tag instanceof FolderInfo) {
1896 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001897 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001898 if (mState == State.ALL_APPS) {
1899 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001900 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001901 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 }
1904 }
1905
Michael Jurka0e260592010-06-30 17:07:39 -07001906 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001907 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001908 // clicking anywhere on the workspace causes the customization drawer to slide down
1909 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001910 return false;
1911 }
1912
Michael Jurkaaf442092010-06-10 17:01:57 -07001913 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001914 * Event handler for the search button
1915 *
1916 * @param v The view that was clicked.
1917 */
1918 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001919 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001920 }
1921
1922 /**
1923 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001924 * enters home screen customization mode.
1925 *
1926 * @param v The view that was clicked.
1927 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001928 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001929 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001930 }
1931
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001932 /**
1933 * Event handler for the "grid" button that appears on the home screen, which
1934 * enters all apps mode.
1935 *
1936 * @param v The view that was clicked.
1937 */
1938 public void onClickAllAppsButton(View v) {
1939 showAllApps(true);
1940 }
1941
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001942 public void onClickAppMarketButton(View v) {
1943 if (mAppMarketIntent != null) {
1944 startActivitySafely(mAppMarketIntent, "app market");
1945 }
1946 }
1947
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001948 void startApplicationDetailsActivity(ComponentName componentName) {
1949 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001950 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1951 Uri.fromParts("package", packageName, null));
1952 startActivity(intent);
1953 }
1954
Patrick Dubroy5539af72010-09-07 15:22:01 -07001955 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1956 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1957 // System applications cannot be installed. For now, show a toast explaining that.
1958 // We may give them the option of disabling apps this way.
1959 int messageId = R.string.uninstall_system_app_text;
1960 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1961 } else {
1962 String packageName = appInfo.componentName.getPackageName();
1963 String className = appInfo.componentName.getClassName();
1964 Intent intent = new Intent(
1965 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1966 startActivity(intent);
1967 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001968 }
1969
Joe Onoratof984e852010-03-25 09:47:45 -07001970 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1972 try {
1973 startActivity(intent);
1974 } catch (ActivityNotFoundException e) {
1975 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001976 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 } catch (SecurityException e) {
1978 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001979 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001981 "or use the exported attribute for this activity. "
1982 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001985
Romain Guy8e633c52010-03-04 12:51:36 -08001986 void startActivityForResultSafely(Intent intent, int requestCode) {
1987 try {
1988 startActivityForResult(intent, requestCode);
1989 } catch (ActivityNotFoundException e) {
1990 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1991 } catch (SecurityException e) {
1992 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1993 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1994 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1995 "or use the exported attribute for this activity.", e);
1996 }
1997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998
1999 private void handleFolderClick(FolderInfo folderInfo) {
2000 if (!folderInfo.opened) {
2001 // Close any open folder
2002 closeFolder();
2003 // Open the requested folder
2004 openFolder(folderInfo);
2005 } else {
2006 // Find the open folder...
2007 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2008 int folderScreen;
2009 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002010 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 // .. and close it
2012 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002013 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 // Close any folder open on the current screen
2015 closeFolder();
2016 // Pull the folder onto this screen
2017 openFolder(folderInfo);
2018 }
2019 }
2020 }
2021 }
2022
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002024 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 * is animated relative to the specified View. If the View is null, no animation
2026 * is played.
2027 *
2028 * @param folderInfo The FolderInfo describing the folder to open.
2029 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002030 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 Folder openFolder;
2032
2033 if (folderInfo instanceof UserFolderInfo) {
2034 openFolder = UserFolder.fromXml(this);
2035 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002036 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 } else {
2038 return;
2039 }
2040
Joe Onorato00acb122009-08-04 16:04:30 -04002041 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 openFolder.setLauncher(this);
2043
2044 openFolder.bind(folderInfo);
2045 folderInfo.opened = true;
2046
Winson Chungaafa03c2010-06-11 17:34:16 -07002047 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2048
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 openFolder.onOpen();
2050 }
2051
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002053 switch (v.getId()) {
2054 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002055 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002056 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2057 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002058 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002059 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002060 return true;
2061 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002062 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002063 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2064 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002065 showPreviews(v);
2066 }
2067 return true;
2068 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002069 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002070 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2071 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2072 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002073 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002074 return true;
2075 }
2076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 return false;
2079 }
2080
2081 if (!(v instanceof CellLayout)) {
2082 v = (View) v.getParent();
2083 }
2084
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
Michael Jurka0280c3b2010-09-17 15:00:07 -07002086 resetAddInfo();
2087 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002088 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002089 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 return true;
2091 }
2092
Michael Jurka0280c3b2010-09-17 15:00:07 -07002093 final View itemUnderLongClick = longClickCellInfo.cell;
2094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002096 if (itemUnderLongClick == null) {
2097 // User long pressed on empty space
2098 mWorkspace.setAllowLongPress(false);
2099 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2100 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002101 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002102 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
2104 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002105 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002107 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2108 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002109 mAddIntersectCellX = longClickCellInfo.cellX;
2110 mAddIntersectCellY = longClickCellInfo.cellY;
2111 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113 }
2114 }
2115 return true;
2116 }
2117
Romain Guye6b8e2f2009-11-10 11:56:55 -08002118 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002119 private void dismissPreview(final View v) {
2120 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002121 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002122 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2123 public void onDismiss() {
2124 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2125 int count = group.getChildCount();
2126 for (int i = 0; i < count; i++) {
2127 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2128 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002129 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2130 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2131
2132 v.setTag(R.id.workspace, null);
2133 v.setTag(R.id.icon, null);
2134 window.setOnDismissListener(null);
2135 }
2136 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002137 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002138 }
2139 v.setTag(null);
2140 }
2141
Romain Guyf8e6a802009-12-07 17:48:02 -08002142 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002143 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002144 }
2145
Romain Guya6abce82009-11-10 02:54:41 -08002146 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002147 final Resources resources = getResources();
2148 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002149
Romain Guya6abce82009-11-10 02:54:41 -08002150 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002151
Romain Guy9d31e9f2009-11-11 18:54:28 -08002152 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002153
Romain Guyf8e6a802009-12-07 17:48:02 -08002154 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002155 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002156 int extraW = (int) ((r.left + r.right) * max);
2157 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002158
2159 int aW = cell.getWidth() - extraW;
2160 float w = aW / max;
2161
2162 int width = cell.getWidth();
2163 int height = cell.getHeight();
2164 int x = cell.getLeftPadding();
2165 int y = cell.getTopPadding();
2166 width -= (x + cell.getRightPadding());
2167 height -= (y + cell.getBottomPadding());
2168
2169 float scale = w / width;
2170
2171 int count = end - start;
2172
2173 final float sWidth = width * scale;
2174 float sHeight = height * scale;
2175
Romain Guye6b8e2f2009-11-10 11:56:55 -08002176 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002177
Romain Guye6b8e2f2009-11-10 11:56:55 -08002178 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2179 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002180
2181 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002182 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002183 cell = (CellLayout) workspace.getChildAt(i);
2184
Romain Guyf8e6a802009-12-07 17:48:02 -08002185 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002186 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002187
2188 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002189 c.scale(scale, scale);
2190 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002191 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002192
Romain Guy9d31e9f2009-11-11 18:54:28 -08002193 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002194 image.setImageBitmap(bitmap);
2195 image.setTag(i);
2196 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002197 image.setOnFocusChangeListener(handler);
2198 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002199 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002200
2201 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002202 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2203
Winson Chungaafa03c2010-06-11 17:34:16 -07002204 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002205 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002206
2207 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002208 p.setContentView(preview);
2209 p.setWidth((int) (sWidth * count + extraW));
2210 p.setHeight((int) (sHeight + extraH));
2211 p.setAnimationStyle(R.style.AnimationPreview);
2212 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002213 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002214 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002215 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002216
Romain Guye6b8e2f2009-11-10 11:56:55 -08002217 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2218 public void onDismiss() {
2219 dismissPreview(anchor);
2220 }
2221 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002222
2223 anchor.setTag(p);
2224 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002225 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226 }
2227
Romain Guy9d31e9f2009-11-11 18:54:28 -08002228 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002229 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002230
Romain Guye6b8e2f2009-11-10 11:56:55 -08002231 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002232 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002233 }
2234
2235 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002236 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002237 v.post(this);
2238 }
2239
2240 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002241 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002242 }
2243
2244 public void onFocusChange(View v, boolean hasFocus) {
2245 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002246 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002247 }
Romain Guya6abce82009-11-10 02:54:41 -08002248 }
2249 }
2250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 Workspace getWorkspace() {
2252 return mWorkspace;
2253 }
2254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 @Override
2256 protected Dialog onCreateDialog(int id) {
2257 switch (id) {
2258 case DIALOG_CREATE_SHORTCUT:
2259 return new CreateShortcut().createDialog();
2260 case DIALOG_RENAME_FOLDER:
2261 return new RenameFolder().createDialog();
2262 }
2263
2264 return super.onCreateDialog(id);
2265 }
2266
2267 @Override
2268 protected void onPrepareDialog(int id, Dialog dialog) {
2269 switch (id) {
2270 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 break;
2272 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002273 if (mFolderInfo != null) {
2274 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2275 final CharSequence text = mFolderInfo.title;
2276 input.setText(text);
2277 input.setSelection(0, text.length());
2278 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 break;
2280 }
2281 }
2282
2283 void showRenameDialog(FolderInfo info) {
2284 mFolderInfo = info;
2285 mWaitingForResult = true;
2286 showDialog(DIALOG_RENAME_FOLDER);
2287 }
2288
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 private void showAddDialog(int intersectX, int intersectY) {
2290 resetAddInfo();
2291 mAddIntersectCellX = intersectX;
2292 mAddIntersectCellY = intersectY;
2293 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002294 mWaitingForResult = true;
2295 showDialog(DIALOG_CREATE_SHORTCUT);
2296 }
2297
Joe Onoratodeb98af2010-02-19 14:59:39 -08002298 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002299 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002300 Bundle bundle = new Bundle();
2301
2302 ArrayList<String> shortcutNames = new ArrayList<String>();
2303 shortcutNames.add(getString(R.string.group_applications));
2304 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2305
2306 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2307 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2308 R.drawable.ic_launcher_application));
2309 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2310
2311 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2312 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002313 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002314 pickIntent.putExtras(bundle);
2315
Joe Onoratodeb98af2010-02-19 14:59:39 -08002316 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002317 }
2318
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002319 private class RenameFolder {
2320 private EditText mInput;
2321
2322 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2324 mInput = (EditText) layout.findViewById(R.id.folder_name);
2325
2326 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2327 builder.setIcon(0);
2328 builder.setTitle(getString(R.string.rename_folder_title));
2329 builder.setCancelable(true);
2330 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2331 public void onCancel(DialogInterface dialog) {
2332 cleanup();
2333 }
2334 });
2335 builder.setNegativeButton(getString(R.string.cancel_action),
2336 new Dialog.OnClickListener() {
2337 public void onClick(DialogInterface dialog, int which) {
2338 cleanup();
2339 }
2340 }
2341 );
2342 builder.setPositiveButton(getString(R.string.rename_action),
2343 new Dialog.OnClickListener() {
2344 public void onClick(DialogInterface dialog, int which) {
2345 changeFolderName();
2346 }
2347 }
2348 );
2349 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002350
2351 final AlertDialog dialog = builder.create();
2352 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2353 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002354 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002355 mInput.requestFocus();
2356 InputMethodManager inputManager = (InputMethodManager)
2357 getSystemService(Context.INPUT_METHOD_SERVICE);
2358 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002359 }
2360 });
2361
2362 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002363 }
2364
2365 private void changeFolderName() {
2366 final String name = mInput.getText().toString();
2367 if (!TextUtils.isEmpty(name)) {
2368 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002369 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002370 mFolderInfo.title = name;
2371 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2372
Joe Onorato9c1289c2009-08-17 11:03:03 -04002373 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002374 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002375 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002376 } else {
2377 final FolderIcon folderIcon = (FolderIcon)
2378 mWorkspace.getViewForTag(mFolderInfo);
2379 if (folderIcon != null) {
2380 folderIcon.setText(name);
2381 getWorkspace().requestLayout();
2382 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002383 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002384 mWorkspaceLoading = true;
2385 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002386 }
2387 }
2388 }
2389 cleanup();
2390 }
2391
2392 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002393 dismissDialog(DIALOG_RENAME_FOLDER);
2394 mWaitingForResult = false;
2395 mFolderInfo = null;
2396 }
2397 }
2398
Daniel Sandler843e8602010-06-07 14:59:01 -04002399 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2400 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002401 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002402 }
2403
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002404 // AllAppsView.Watcher
2405 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002406 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2407 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002408 mWorkspace.setVisibility(View.GONE);
2409 }
2410 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002411
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002412 private void showToolbarButton(View button) {
2413 button.setAlpha(1.0f);
2414 button.setVisibility(View.VISIBLE);
2415 button.setFocusable(true);
2416 button.setClickable(true);
2417 }
2418
2419 private void hideToolbarButton(View button) {
2420 button.setAlpha(0.0f);
2421 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2422 button.setVisibility(View.INVISIBLE);
2423 button.setFocusable(false);
2424 button.setClickable(false);
2425 }
2426
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002427 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002428 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002429 *
2430 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2431 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002432 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002434 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002435 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002436 final boolean showing = show;
2437 final boolean hiding = !show;
2438
2439 final int duration = show ?
2440 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2441 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2442
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002443 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002444 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2445 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002446 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002447 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002448 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002449 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002450 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002451 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002452 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002453 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002454 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002455 });
2456 seq.play(anim);
2457 } else {
2458 if (showing) {
2459 showToolbarButton(view);
2460 } else {
2461 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002462 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 }
2465
2466 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002467 * Show/hide the appropriate toolbar buttons for newState.
2468 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002469 *
2470 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002471 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2472 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002473 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002474 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002475 final View searchButton = findViewById(R.id.search_button);
2476 final View allAppsButton = findViewById(R.id.all_apps_button);
2477 final View marketButton = findViewById(R.id.market_button);
2478 final View configureButton = findViewById(R.id.configure_button);
2479
2480 switch (newState) {
2481 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002482 hideOrShowToolbarButton(true, searchButton, showSeq);
2483 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2484 hideOrShowToolbarButton(true, configureButton, showSeq);
2485 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002486 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002487 break;
2488 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002489 hideOrShowToolbarButton(true, configureButton, showSeq);
2490 hideOrShowToolbarButton(true, marketButton, showSeq);
2491 hideOrShowToolbarButton(false, searchButton, hideSeq);
2492 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002493 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002494 break;
2495 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002496 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2497 hideOrShowToolbarButton(false, searchButton, hideSeq);
2498 hideOrShowToolbarButton(false, marketButton, hideSeq);
2499 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002500 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002501 break;
2502 }
2503 }
2504
2505 /**
2506 * Helper method for the cameraZoomIn/cameraZoomOut animations
2507 * @param view The view being animated
2508 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2509 * @param scaleFactor The scale factor used for the zoom
2510 */
2511 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2512 final int height = view.getHeight();
2513 view.setPivotX(view.getWidth() / 2.0f);
2514 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2515 // Assumes that the view is normally anchored to either the top or bottom of the screen
2516 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2517 if (state == State.ALL_APPS) {
2518 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2519 } else {
2520 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2521 }
2522 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002523
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002524 /**
2525 * Zoom the camera out from the workspace to reveal 'toView'.
2526 * Assumes that the view to show is anchored at either the very top or very bottom
2527 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002528 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002529 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002530 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002531 final Resources res = getResources();
2532 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2533 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002534 final boolean toAllApps = (toState == State.ALL_APPS);
2535 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002536
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002537 setPivotsForZoom(toView, toState, scale);
2538
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002539 if (toAllApps) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002540 mWorkspace.shrinkToBottom(animated);
2541 } else {
2542 mWorkspace.shrinkToTop(animated);
2543 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002544
2545 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002546 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2547 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2548 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2549 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002550 scaleAnim.setInterpolator(new DecelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002551 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002552 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002553 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 // Prepare the position
2555 toView.setTranslationX(0.0f);
2556 toView.setTranslationY(0.0f);
2557 toView.setVisibility(View.VISIBLE);
2558 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002559 @Override
2560 public void onAnimationEndOrCancel(Animator animation) {
2561 // If we don't set the final scale values here, if this animation is cancelled
2562 // it will have the wrong scale value and subsequent cameraPan animations will
2563 // not fix that
2564 toView.setScaleX(1.0f);
2565 toView.setScaleY(1.0f);
2566 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002567 });
2568
Chet Haaseb1254a62010-09-07 13:35:00 -07002569 AnimatorSet toolbarHideAnim = new AnimatorSet();
2570 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2572
2573 // toView should appear right at the end of the workspace shrink animation
2574 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2575
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002576 if (mStateAnimation != null) mStateAnimation.cancel();
2577 mStateAnimation = new AnimatorSet();
2578 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2579 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002580
2581 // Show the new toolbar buttons just as the main animation is ending
2582 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002583 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2584 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002585 } else {
2586 toView.setTranslationX(0.0f);
2587 toView.setTranslationY(0.0f);
2588 toView.setScaleX(1.0f);
2589 toView.setScaleY(1.0f);
2590 toView.setVisibility(View.VISIBLE);
2591 hideAndShowToolbarButtons(toState, null, null);
2592 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002593 }
2594
2595 /**
2596 * Zoom the camera back into the workspace, hiding 'fromView'.
2597 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002598 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2599 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002600 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002601 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002602 Resources res = getResources();
2603 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2604 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002605 final View fromView =
2606 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2607
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002608 mCustomizePagedView.endChoiceMode();
2609 mAllAppsPagedView.endChoiceMode();
2610
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002612
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002613 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614
2615 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002616 if (mStateAnimation != null) mStateAnimation.cancel();
2617 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002618 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2619 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2620 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2621 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002622 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002623 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002624 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002625 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002627 }
2628 });
2629
Chet Haaseb1254a62010-09-07 13:35:00 -07002630 AnimatorSet toolbarHideAnim = new AnimatorSet();
2631 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002632 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2633
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002634 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635
2636 // Show the new toolbar buttons at the very end of the whole animation
2637 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2638 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002639 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2640 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002641 } else {
2642 fromView.setVisibility(View.GONE);
2643 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2644 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002645 }
2646
2647 /**
2648 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2649 * This is the transition used on xlarge screens to go between all apps and
2650 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002651 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2652 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2653 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002654 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 final Resources res = getResources();
2657 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 final int workspaceHeight = mWorkspace.getHeight();
2659
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 final boolean fromAllApps = (fromState == State.ALL_APPS);
2661 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2662 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002663
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2665 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2666 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2667 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002668
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002669 mCustomizePagedView.endChoiceMode();
2670 mAllAppsPagedView.endChoiceMode();
2671
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002672 if (toState == State.ALL_APPS) {
2673 mWorkspace.shrinkToBottom(animated);
2674 } else {
2675 mWorkspace.shrinkToTop(animated);
2676 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002677
2678 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002679 if (mStateAnimation != null) mStateAnimation.cancel();
2680 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002681 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002682 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002683 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002684 toView.setVisibility(View.VISIBLE);
2685 toView.setY(toViewStartY);
2686 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002687 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002688 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689 fromView.setVisibility(View.GONE);
2690 }
2691 });
2692
Chet Haaseb1254a62010-09-07 13:35:00 -07002693 AnimatorSet toolbarHideAnim = new AnimatorSet();
2694 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002695 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2696
Chet Haase472b2812010-10-14 07:02:04 -07002697 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2698 fromViewStartY, fromViewEndY);
2699 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002700 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2701 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2702 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2703 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2704 );
Chet Haase472b2812010-10-14 07:02:04 -07002705 fromAnim.setDuration(duration);
2706 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002707
2708 // Show the new toolbar buttons just as the main animation is ending
2709 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002710 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2711 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002712 } else {
2713 fromView.setY(fromViewEndY);
2714 fromView.setVisibility(View.GONE);
2715 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002716 toView.setScaleX(1.0f);
2717 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002718 toView.setVisibility(View.VISIBLE);
2719 hideAndShowToolbarButtons(toState, null, null);
2720 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002721 }
2722
2723 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002724 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002725 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002726 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002727
Michael Jurka79212d82010-07-30 16:36:20 -07002728 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002729 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002730 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002731 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002732 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002733 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002734 } else {
2735 mAllAppsGrid.zoom(1.0f, animated);
2736 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002737
Romain Guyc16fea72010-03-12 17:17:56 -08002738 ((View) mAllAppsGrid).setFocusable(true);
2739 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002740
Joe Onorato7c312c12009-08-13 21:36:53 -07002741 // TODO: fade these two too
2742 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002743 // Change the state *after* we've called all the transition code
2744 mState = State.ALL_APPS;
2745 }
2746
2747
2748 void showWorkspace(boolean animated) {
2749 showWorkspace(animated, null);
2750 }
2751
2752 void showWorkspace(boolean animated, CellLayout layout) {
2753 if (layout != null && animated) {
2754 mWorkspace.unshrink(layout);
2755 } else {
2756 mWorkspace.unshrink(animated);
2757 }
2758 if (mState == State.ALL_APPS) {
2759 closeAllApps(animated);
2760 } else if (mState == State.CUSTOMIZE) {
2761 hideCustomizationDrawer(animated);
2762 }
2763 // Change the state *after* we've called all the transition code
2764 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002765 }
2766
Joe Onoratob2061212009-11-24 16:13:54 -05002767 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002768 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002769 * - Home from workspace
2770 * - from center screen
2771 * - from other screens
2772 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002773 * - from center screen
2774 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002775 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002776 * - from center screen
2777 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002778 * - Launch app from workspace and quit
2779 * - with back
2780 * - with home
2781 * - Launch app from all apps and quit
2782 * - with back
2783 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002784 * - Go to a screen that's not the default, then all
2785 * apps, and launch and app, and go back
2786 * - with back
2787 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002788 * - On workspace, long press power and go back
2789 * - with back
2790 * - with home
2791 * - On all apps, long press power and go back
2792 * - with back
2793 * - with home
2794 * - On workspace, power off
2795 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002796 * - Launch an app and turn off the screen while in that app
2797 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002798 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002799 * - From all apps
2800 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002801 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2802 * - From all apps
2803 * - From the center workspace
2804 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002805 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002806 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002807 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002808 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002810 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002811 } else {
2812 mAllAppsGrid.zoom(0.0f, animated);
2813 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002814 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002815 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002816 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002817 }
2818
Joe Onorato7c312c12009-08-13 21:36:53 -07002819 void lockAllApps() {
2820 // TODO
2821 }
2822
2823 void unlockAllApps() {
2824 // TODO
2825 }
2826
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002827 // Show the customization drawer (only exists in x-large configuration)
2828 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002829 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002830 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002831 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002832 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002833 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002834 // Change the state *after* we've called all the transition code
2835 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002836 }
2837
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002838 // Hide the customization drawer (only exists in x-large configuration)
2839 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002840 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002841 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002842 }
2843 }
2844
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002845 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2846 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2847 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002848 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002849 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002850
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002851 void onWorkspaceClick(CellLayout layout) {
2852 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002853 }
2854
Michael Jurka0423dcf2010-10-05 14:56:18 -07002855 private void updateButtonWithIconFromExternalActivity(
2856 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2857 ImageView button = (ImageView) findViewById(buttonId);
2858 Drawable toolbarIcon = null;
2859 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002860 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002861 // Look for the toolbar icon specified in the activity meta-data
2862 Bundle metaData = packageManager.getActivityInfo(
2863 activityName, PackageManager.GET_META_DATA).metaData;
2864 if (metaData != null) {
2865 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2866 if (iconResId != 0) {
2867 Resources res = packageManager.getResourcesForActivity(activityName);
2868 toolbarIcon = res.getDrawable(iconResId);
2869 }
2870 }
2871 } catch (NameNotFoundException e) {
2872 // Do nothing
2873 }
2874 // If we were unable to find the icon via the meta-data, use a generic one
2875 if (toolbarIcon == null) {
2876 button.setImageResource(fallbackDrawableId);
2877 } else {
2878 button.setImageDrawable(toolbarIcon);
2879 }
2880 }
2881
2882 private void updateGlobalSearchIcon() {
2883 if (LauncherApplication.isScreenXLarge()) {
2884 final SearchManager searchManager =
2885 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2886 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002887 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002888 updateButtonWithIconFromExternalActivity(
2889 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002890 }
2891 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002892 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002893
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002894 /**
2895 * Sets the app market icon (shown when all apps is visible on x-large screens)
2896 */
2897 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002898 if (LauncherApplication.isScreenXLarge()) {
2899 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2900 // Find the app market activity by resolving an intent.
2901 // (If multiple app markets are installed, it will return the ResolverActivity.)
2902 ComponentName activityName = intent.resolveActivity(getPackageManager());
2903 if (activityName != null) {
2904 mAppMarketIntent = intent;
2905 updateButtonWithIconFromExternalActivity(
2906 R.id.market_button, activityName, R.drawable.app_market_generic);
2907 }
2908 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002909 }
2910
2911 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 * Displays the shortcut creation dialog and launches, if necessary, the
2913 * appropriate activity.
2914 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002915 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002916 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2917 DialogInterface.OnShowListener {
2918
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002920
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002921 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002923
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2925 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002926 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 builder.setInverseBackgroundForced(true);
2929
2930 AlertDialog dialog = builder.create();
2931 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002932 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002933 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 return dialog;
2936 }
2937
2938 public void onCancel(DialogInterface dialog) {
2939 mWaitingForResult = false;
2940 cleanup();
2941 }
2942
Romain Guycbb89e42009-06-08 15:52:54 -07002943 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002944 }
2945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002946 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002947 try {
2948 dismissDialog(DIALOG_CREATE_SHORTCUT);
2949 } catch (Exception e) {
2950 // An exception is thrown if the dialog is not visible, which is fine
2951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002952 }
2953
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002954 /**
2955 * Handle the action clicked in the "Add to home" dialog.
2956 */
2957 public void onClick(DialogInterface dialog, int which) {
2958 Resources res = getResources();
2959 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002960
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002961 switch (which) {
2962 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002963 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002964 break;
2965 }
Romain Guycbb89e42009-06-08 15:52:54 -07002966
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002967 case AddAdapter.ITEM_APPWIDGET: {
2968 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002969
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002970 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2971 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002972 // start the pick activity
2973 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2974 break;
2975 }
Romain Guycbb89e42009-06-08 15:52:54 -07002976
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002977 case AddAdapter.ITEM_LIVE_FOLDER: {
2978 // Insert extra item to handle inserting folder
2979 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002980
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002981 ArrayList<String> shortcutNames = new ArrayList<String>();
2982 shortcutNames.add(res.getString(R.string.group_folder));
2983 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002984
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002985 ArrayList<ShortcutIconResource> shortcutIcons =
2986 new ArrayList<ShortcutIconResource>();
2987 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2988 R.drawable.ic_launcher_folder));
2989 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2990
2991 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2992 pickIntent.putExtra(Intent.EXTRA_INTENT,
2993 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2994 pickIntent.putExtra(Intent.EXTRA_TITLE,
2995 getText(R.string.title_select_live_folder));
2996 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002997
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002998 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2999 break;
3000 }
3001
3002 case AddAdapter.ITEM_WALLPAPER: {
3003 startWallpaper();
3004 break;
3005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003006 }
3007 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003008
3009 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003010 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003012 }
3013
3014 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003015 * Receives notifications when applications are added/removed.
3016 */
3017 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3018 @Override
3019 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003020 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003021 String reason = intent.getStringExtra("reason");
3022 if (!"homekey".equals(reason)) {
3023 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003024 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003025 animate = false;
3026 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003027 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003028 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003029 }
3030 }
3031
3032 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003033 * Receives notifications whenever the appwidgets are reset.
3034 */
3035 private class AppWidgetResetObserver extends ContentObserver {
3036 public AppWidgetResetObserver() {
3037 super(new Handler());
3038 }
3039
3040 @Override
3041 public void onChange(boolean selfChange) {
3042 onAppWidgetReset();
3043 }
3044 }
3045
3046 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003047 * If the activity is currently paused, signal that we need to re-run the loader
3048 * in onResume.
3049 *
3050 * This needs to be called from incoming places where resources might have been loaded
3051 * while we are paused. That is becaues the Configuration might be wrong
3052 * when we're not running, and if it comes back to what it was when we
3053 * were paused, we are not restarted.
3054 *
3055 * Implementation of the method from LauncherModel.Callbacks.
3056 *
3057 * @return true if we are currently paused. The caller might be able to
3058 * skip some work in that case since we will come back again.
3059 */
3060 public boolean setLoadOnResume() {
3061 if (mPaused) {
3062 Log.i(TAG, "setLoadOnResume");
3063 mOnResumeNeedsLoad = true;
3064 return true;
3065 } else {
3066 return false;
3067 }
3068 }
3069
3070 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003071 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003072 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003073 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003074 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003075 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003076 } else {
3077 return SCREEN_COUNT / 2;
3078 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003079 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003080
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003081 void setAllAppsPagedView(PagedView view) {
3082 mAllAppsPagedView = view;
3083 }
3084
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 /**
3086 * Refreshes the shortcuts shown on the workspace.
3087 *
3088 * Implementation of the method from LauncherModel.Callbacks.
3089 */
3090 public void startBinding() {
3091 final Workspace workspace = mWorkspace;
3092 int count = workspace.getChildCount();
3093 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003094 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003095 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3096 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003097
Joe Onorato9c1289c2009-08-17 11:03:03 -04003098 if (DEBUG_USER_INTERFACE) {
3099 android.widget.Button finishButton = new android.widget.Button(this);
3100 finishButton.setText("Finish");
3101 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3102
3103 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3104 public void onClick(View v) {
3105 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108 }
3109 }
3110
3111 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 * Bind the items start-end from the list.
3113 *
3114 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3117
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003118 setLoadOnResume();
3119
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 final Workspace workspace = mWorkspace;
3121
3122 for (int i=start; i<end; i++) {
3123 final ItemInfo item = shortcuts.get(i);
3124 mDesktopItems.add(item);
3125 switch (item.itemType) {
3126 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3127 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003128 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003129 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3130 false);
3131 break;
3132 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3133 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003134 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003135 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003136 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3137 false);
3138 break;
3139 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3140 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3141 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003142 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003143 (LiveFolderInfo) item);
3144 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3145 false);
3146 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003148 }
3149
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151 }
3152
Joe Onorato9c1289c2009-08-17 11:03:03 -04003153 /**
3154 * Implementation of the method from LauncherModel.Callbacks.
3155 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003156 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003157 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003158 sFolders.clear();
3159 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003160 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003161
3162 /**
3163 * Add the views for a widget to the workspace.
3164 *
3165 * Implementation of the method from LauncherModel.Callbacks.
3166 */
3167 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003168 setLoadOnResume();
3169
Daniel Sandler843e8602010-06-07 14:59:01 -04003170 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3171 if (DEBUG_WIDGETS) {
3172 Log.d(TAG, "bindAppWidget: " + item);
3173 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003174 final Workspace workspace = mWorkspace;
3175
3176 final int appWidgetId = item.appWidgetId;
3177 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003178 if (DEBUG_WIDGETS) {
3179 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3180 }
3181
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3183
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3185 item.hostView.setTag(item);
3186
3187 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3188 item.cellY, item.spanX, item.spanY, false);
3189
Adam Cohended9f8d2010-11-03 13:25:16 -07003190 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3191
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 workspace.requestLayout();
3193
3194 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003195
3196 if (DEBUG_WIDGETS) {
3197 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3198 + (SystemClock.uptimeMillis()-start) + "ms");
3199 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 }
3201
3202 /**
3203 * Callback saying that there aren't any more items to bind.
3204 *
3205 * Implementation of the method from LauncherModel.Callbacks.
3206 */
3207 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003208 setLoadOnResume();
3209
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 if (mSavedState != null) {
3211 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003212 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003213 }
3214
3215 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3216 if (userFolders != null) {
3217 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003218 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 if (info != null) {
3220 openFolder(info);
3221 }
3222 }
3223 final Folder openFolder = mWorkspace.getOpenFolder();
3224 if (openFolder != null) {
3225 openFolder.requestFocus();
3226 }
3227 }
3228
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 mSavedState = null;
3230 }
3231
3232 if (mSavedInstanceState != null) {
3233 super.onRestoreInstanceState(mSavedInstanceState);
3234 mSavedInstanceState = null;
3235 }
3236
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 mWorkspaceLoading = false;
3238 }
3239
3240 /**
3241 * Add the icons for all apps.
3242 *
3243 * Implementation of the method from LauncherModel.Callbacks.
3244 */
3245 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003246 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003247 if (mCustomizePagedView != null) {
3248 mCustomizePagedView.setApps(apps);
3249 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003250 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003251 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003252 }
3253
3254 /**
3255 * A package was installed.
3256 *
3257 * Implementation of the method from LauncherModel.Callbacks.
3258 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003259 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003260 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003262 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003263 if (mCustomizePagedView != null) {
3264 mCustomizePagedView.addApps(apps);
3265 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003266 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003267 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 }
3269
3270 /**
3271 * A package was updated.
3272 *
3273 * Implementation of the method from LauncherModel.Callbacks.
3274 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003275 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003276 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003278 mWorkspace.updateShortcuts(apps);
3279 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003280 if (mCustomizePagedView != null) {
3281 mCustomizePagedView.updateApps(apps);
3282 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003283 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003284 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003285 }
3286
3287 /**
3288 * A package was uninstalled.
3289 *
3290 * Implementation of the method from LauncherModel.Callbacks.
3291 */
Joe Onorato36115782010-06-17 13:28:48 -04003292 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003294 if (permanent) {
3295 mWorkspace.removeItems(apps);
3296 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003297 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003298 if (mCustomizePagedView != null) {
3299 mCustomizePagedView.removeApps(apps);
3300 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003301 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003302 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003303 }
Joe Onoratobe386092009-11-17 17:32:16 -08003304
3305 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003306 * A number of packages were updated.
3307 */
3308 public void bindPackagesUpdated() {
3309 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003310 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003311 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003312 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003313 }
3314
3315 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003316 * Prints out out state for debugging.
3317 */
3318 public void dumpState() {
3319 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003320 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003321 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3322 Log.d(TAG, "mRestoring=" + mRestoring);
3323 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3324 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3325 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003326 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003327 mModel.dumpState();
3328 mAllAppsGrid.dumpState();
3329 Log.d(TAG, "END launcher2 dump state");
3330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003331}