blob: 81e1847ffaf8d6d816ea93258b42fcde611c9d3b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Winson Chung68846fd2010-10-29 11:00:27 -070020import java.io.DataInputStream;
21import java.io.DataOutputStream;
22import java.io.FileNotFoundException;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.HashMap;
26import java.util.List;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070027
Gilles Debunnedd6c9922010-10-25 11:23:41 -070028import android.animation.Animator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070029import android.animation.AnimatorSet;
Chet Haaseb1254a62010-09-07 13:35:00 -070030import android.animation.ObjectAnimator;
Patrick Dubroy07a0de42010-08-26 11:48:35 -070031import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070032import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070033import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.app.Dialog;
36import android.app.SearchManager;
37import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070038import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070039import android.appwidget.AppWidgetManager;
40import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080042import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070043import android.content.ClipData;
44import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040045import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080046import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.Context;
48import android.content.DialogInterface;
49import android.content.Intent;
Winson Chung68846fd2010-10-29 11:00:27 -070050import android.content.IntentFilter;
Adam Cohended9f8d2010-11-03 13:25:16 -070051import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070052import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080053import android.content.pm.PackageManager;
Winson Chung68846fd2010-10-29 11:00:27 -070054import android.content.pm.ResolveInfo;
Adam Cohended9f8d2010-11-03 13:25:16 -070055import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070057import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040058import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080061import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070062import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080063import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070064import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040065import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070066import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020068import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080069import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070070import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040072import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080073import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070074import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070075import android.provider.Settings;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.text.Selection;
77import android.text.SpannableStringBuilder;
78import android.text.TextUtils;
79import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070080import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080081import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080082import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.KeyEvent;
84import android.view.LayoutInflater;
85import android.view.Menu;
86import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.view.View;
89import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070090import android.view.WindowManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.view.View.OnLongClickListener;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070092import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070095import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080096import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070097import android.widget.ImageView;
98import android.widget.LinearLayout;
99import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -0700100import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -0700101import android.widget.TextView;
102import android.widget.Toast;
Adam Cohended9f8d2010-11-03 13:25:16 -0700103import android.widget.TabHost.OnTabChangeListener;
104import android.widget.TabHost.TabContentFactory;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700105
Winson Chung68846fd2010-10-29 11:00:27 -0700106import com.android.common.Search;
107import com.android.launcher.R;
Romain Guyedcce092010-03-04 13:03:17 -0800108
Adam Cohended9f8d2010-11-03 13:25:16 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
134 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
138 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato7c312c12009-08-13 21:36:53 -0700147 static final int DIALOG_CREATE_SHORTCUT = 1;
148 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: long
157 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Winson Chung80baf5a2010-08-09 16:03:15 -0700169 // tags for the customization tabs
170 private static final String WIDGETS_TAG = "widgets";
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700171 private static final String APPLICATIONS_TAG = "applications";
Winson Chung80baf5a2010-08-09 16:03:15 -0700172 private static final String SHORTCUTS_TAG = "shortcuts";
173 private static final String WALLPAPERS_TAG = "wallpapers";
174
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700175 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
176
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700177 /** The different states that Launcher can be in. */
178 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE, OVERVIEW };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700179 private State mState = State.WORKSPACE;
180 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700181
Joe Onorato9c1289c2009-08-17 11:03:03 -0400182 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800185 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800187 private final BroadcastReceiver mCloseSystemDialogsReceiver
188 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800189 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
190
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private LayoutInflater mInflater;
192
Joe Onorato00acb122009-08-04 16:04:30 -0400193 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700196 private AppWidgetManager mAppWidgetManager;
197 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700198
Michael Jurka0280c3b2010-09-17 15:00:07 -0700199 private int mAddScreen = -1;
200 private int mAddIntersectCellX = -1;
201 private int mAddIntersectCellY = -1;
202 private int[] mAddDropPosition;
203 private int[] mTmpAddItemCellCoordinates = new int[2];
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private FolderInfo mFolderInfo;
206
Joe Onorato7c312c12009-08-13 21:36:53 -0700207 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700208 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700209 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700210 private TabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700211 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700212
213 private PagedView mAllAppsPagedView = null;
214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Romain Guy1fbc1c82009-11-09 20:43:08 -0800241 private ImageView mPreviousView;
242 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500243
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400244 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400245 private String[] mHotseatConfig = null;
246 private Intent[] mHotseats = null;
247 private Drawable[] mHotseatIcons = null;
248 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400249
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700250 private Intent mAppMarketIntent = null;
251
Adam Cohended9f8d2010-11-03 13:25:16 -0700252 // Related to the auto-advancing of widgets
253 private final int ADVANCE_MSG = 1;
254 private final int mAdvanceInterval = 20000;
255 private final int mAdvanceStagger = 250;
256 private long mAutoAdvanceSentTime;
257 private long mAutoAdvanceTimeLeft = -1;
258 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
259 new HashMap<View, AppWidgetProviderInfo>();
260
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 @Override
262 protected void onCreate(Bundle savedInstanceState) {
263 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700264
Winson Chungaafa03c2010-06-11 17:34:16 -0700265 if (LauncherApplication.isInPlaceRotationEnabled()) {
266 // hide the status bar (temporary until we get the status bar design figured out)
267 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
268 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
269 }
270
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 LauncherApplication app = ((LauncherApplication)getApplication());
272 mModel = app.setLauncher(this);
273 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400274 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700276
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
279 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200282 android.os.Debug.startMethodTracing(
283 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 }
285
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400286 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 checkForLocaleChange();
288 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700290 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
291 if (mHomeCustomizationDrawer != null) {
292 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700293
Winson Chung80baf5a2010-08-09 16:03:15 -0700294 // share the same customization workspace across all the tabs
Winson Chunge3193b92010-09-10 11:44:42 -0700295 mCustomizePagedView = (CustomizePagedView) mInflater.inflate(
296 R.layout.customization_drawer, mHomeCustomizationDrawer, false);
Winson Chung80baf5a2010-08-09 16:03:15 -0700297 TabContentFactory contentFactory = new TabContentFactory() {
298 public View createTabContent(String tag) {
299 return mCustomizePagedView;
300 }
301 };
302
Michael Jurka946ad472010-07-09 18:05:18 -0700303 String widgetsLabel = getString(R.string.widgets_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700304 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WIDGETS_TAG)
305 .setIndicator(widgetsLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700306 String applicationsLabel = getString(R.string.applications_tab_label);
307 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(APPLICATIONS_TAG)
308 .setIndicator(applicationsLabel).setContent(contentFactory));
Michael Jurka946ad472010-07-09 18:05:18 -0700309 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
Winson Chung80baf5a2010-08-09 16:03:15 -0700310 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(WALLPAPERS_TAG)
311 .setIndicator(wallpapersLabel).setContent(contentFactory));
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700312 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
313 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec(SHORTCUTS_TAG)
314 .setIndicator(shortcutsLabel).setContent(contentFactory));
Winson Chung80baf5a2010-08-09 16:03:15 -0700315
Winson Chung80baf5a2010-08-09 16:03:15 -0700316 mHomeCustomizationDrawer.setOnTabChangedListener(new OnTabChangeListener() {
317 public void onTabChanged(String tabId) {
318 // animate the changing of the tab content by fading pages in and out
319 final int duration = 150;
320 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700321 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(mCustomizePagedView,
Winson Chung80baf5a2010-08-09 16:03:15 -0700322 "alpha", alpha, 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700323 alphaAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700324 alphaAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700325 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -0700326 public void onAnimationEndOrCancel(Animator animation) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700327 String tag = mHomeCustomizationDrawer.getCurrentTabTag();
328 if (tag == WIDGETS_TAG) {
329 mCustomizePagedView.setCustomizationFilter(
330 CustomizePagedView.CustomizationType.WidgetCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700331 } else if (tag == APPLICATIONS_TAG) {
332 mCustomizePagedView.setCustomizationFilter(
333 CustomizePagedView.CustomizationType.ApplicationCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700334 } else if (tag == WALLPAPERS_TAG) {
335 mCustomizePagedView.setCustomizationFilter(
336 CustomizePagedView.CustomizationType.WallpaperCustomization);
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700337 } else if (tag == SHORTCUTS_TAG) {
338 mCustomizePagedView.setCustomizationFilter(
339 CustomizePagedView.CustomizationType.ShortcutCustomization);
Winson Chung80baf5a2010-08-09 16:03:15 -0700340 }
341
342 final float alpha = mCustomizePagedView.getAlpha();
Chet Haase472b2812010-10-14 07:02:04 -0700343 ValueAnimator alphaAnim = ObjectAnimator.ofFloat(
Winson Chung5ffd8ea2010-09-23 18:40:29 -0700344 mCustomizePagedView, "alpha", alpha, 1.0f);
Chet Haase472b2812010-10-14 07:02:04 -0700345 alphaAnim.setDuration(duration);
Winson Chung80baf5a2010-08-09 16:03:15 -0700346 alphaAnim.start();
347 }
348 });
349 alphaAnim.start();
350 }
351 });
Michael Jurka946ad472010-07-09 18:05:18 -0700352 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 setupViews();
354
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800355 registerContentObservers();
356
Joe Onorato7c312c12009-08-13 21:36:53 -0700357 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700358
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 mSavedState = savedInstanceState;
360 restoreState(mSavedState);
361
362 if (PROFILE_STARTUP) {
363 android.os.Debug.stopMethodTracing();
364 }
365
366 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400367 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 }
369
370 // For handling default keys
371 mDefaultKeySsb = new SpannableStringBuilder();
372 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800373
374 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
375 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 }
Romain Guycbb89e42009-06-08 15:52:54 -0700377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700379 if (sLocaleConfiguration == null) {
380 new AsyncTask<Void, Void, LocaleConfiguration>() {
381 @Override
382 protected LocaleConfiguration doInBackground(Void... unused) {
383 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
384 readConfiguration(Launcher.this, localeConfiguration);
385 return localeConfiguration;
386 }
387
388 @Override
389 protected void onPostExecute(LocaleConfiguration result) {
390 sLocaleConfiguration = result;
391 checkForLocaleChange(); // recursive, but now with a locale configuration
392 }
393 }.execute();
394 return;
395 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 final Configuration configuration = getResources().getConfiguration();
398
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700399 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800400 final String locale = configuration.locale.toString();
401
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700402 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 final int mcc = configuration.mcc;
404
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 final int mnc = configuration.mnc;
407
Romain Guy84f296c2009-11-04 15:00:44 -0800408 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409
Romain Guy84f296c2009-11-04 15:00:44 -0800410 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700411 sLocaleConfiguration.locale = locale;
412 sLocaleConfiguration.mcc = mcc;
413 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700414
Joe Onorato0589f0f2010-02-08 13:44:00 -0800415 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400416 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417
418 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
419 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700420 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700421 public void run() {
422 writeConfiguration(Launcher.this, localeConfiguration);
423 }
424 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700425 }
426 }
427
428 private static class LocaleConfiguration {
429 public String locale;
430 public int mcc = -1;
431 public int mnc = -1;
432 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700433
Romain Guy98d01652009-06-30 16:21:04 -0700434 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
435 DataInputStream in = null;
436 try {
437 in = new DataInputStream(context.openFileInput(PREFERENCES));
438 configuration.locale = in.readUTF();
439 configuration.mcc = in.readInt();
440 configuration.mnc = in.readInt();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 // Ignore
445 } finally {
446 if (in != null) {
447 try {
448 in.close();
449 } catch (IOException e) {
450 // Ignore
451 }
452 }
453 }
454 }
455
456 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
457 DataOutputStream out = null;
458 try {
459 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
460 out.writeUTF(configuration.locale);
461 out.writeInt(configuration.mcc);
462 out.writeInt(configuration.mnc);
463 out.flush();
464 } catch (FileNotFoundException e) {
465 // Ignore
466 } catch (IOException e) {
467 //noinspection ResultOfMethodCallIgnored
468 context.getFileStreamPath(PREFERENCES).delete();
469 } finally {
470 if (out != null) {
471 try {
472 out.close();
473 } catch (IOException e) {
474 // Ignore
475 }
476 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477 }
478 }
479
480 static int getScreen() {
481 synchronized (sLock) {
482 return sScreen;
483 }
484 }
485
486 static void setScreen(int screen) {
487 synchronized (sLock) {
488 sScreen = screen;
489 }
490 }
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700493 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
495 Display display = getWindowManager().getDefaultDisplay();
Romain Guy5bbc91b2010-08-18 11:38:46 -0700496 // TODO: Put back when we decide about scrolling the wallpaper
497 // boolean isPortrait = display.getWidth() < display.getHeight();
498 // final int width = isPortrait ? display.getWidth() : display.getHeight();
499 // final int height = isPortrait ? display.getHeight() : display.getWidth();
500 wpm.suggestDesiredDimensions(Math.max(display.getWidth(), display.getHeight()),
501 Math.max(display.getWidth(), display.getHeight()));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 }
503
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 // Note: This doesn't do all the client-id magic that BrowserProvider does
505 // in Browser. (http://b/2425179)
506 private Uri getDefaultBrowserUri() {
507 String url = getString(R.string.default_browser_url);
508 if (url.indexOf("{CID}") != -1) {
509 url = url.replace("{CID}", "android-google");
510 }
511 return Uri.parse(url);
512 }
513
514 // Load the Intent templates from arrays.xml to populate the hotseats. For
515 // each Intent, if it resolves to a single app, use that as the launch
516 // intent & use that app's label as the contentDescription. Otherwise,
517 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400518 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 if (mHotseatConfig == null) {
520 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
521 if (mHotseatConfig.length > 0) {
522 mHotseats = new Intent[mHotseatConfig.length];
523 mHotseatLabels = new CharSequence[mHotseatConfig.length];
524 mHotseatIcons = new Drawable[mHotseatConfig.length];
525 } else {
526 mHotseats = null;
527 mHotseatIcons = null;
528 mHotseatLabels = null;
529 }
530
531 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
532 for (int i=0; i<mHotseatConfig.length; i++) {
533 // load icon for this slot; currently unrelated to the actual activity
534 try {
535 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
536 } catch (ArrayIndexOutOfBoundsException ex) {
537 Log.w(TAG, "Missing hotseat_icons array item #" + i);
538 mHotseatIcons[i] = null;
539 }
540 }
541 hotseatIconDrawables.recycle();
542 }
543
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400544 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 for (int i=0; i<mHotseatConfig.length; i++) {
546 Intent intent = null;
547 if (mHotseatConfig[i].equals("*BROWSER*")) {
548 // magic value meaning "launch user's default web browser"
549 // replace it with a generic web request so we can see if there is indeed a default
550 String defaultUri = getString(R.string.default_browser_url);
551 intent = new Intent(
552 Intent.ACTION_VIEW,
553 ((defaultUri != null)
554 ? Uri.parse(defaultUri)
555 : getDefaultBrowserUri())
556 ).addCategory(Intent.CATEGORY_BROWSABLE);
557 // note: if the user launches this without a default set, she
558 // will always be taken to the default URL above; this is
559 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700560 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 // URL is unavoidably sent to the chosen app.
562 } else {
563 try {
564 intent = Intent.parseUri(mHotseatConfig[i], 0);
565 } catch (java.net.URISyntaxException ex) {
566 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
567 // bogus; leave intent=null
568 }
569 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700570
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400571 if (intent == null) {
572 mHotseats[i] = null;
573 mHotseatLabels[i] = getText(R.string.activity_not_found);
574 continue;
575 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400576
577 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700578 Log.d(TAG, "loadHotseats: hotseat " + i
579 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400580 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 + "]");
582 }
583
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400584 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
585 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700586 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400587 Log.d(TAG, "Best match for intent: " + bestMatch);
588 Log.d(TAG, "All matches: ");
589 for (ResolveInfo ri : allMatches) {
590 Log.d(TAG, " --> " + ri);
591 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400592 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400593 // did this resolve to a single app, or the resolver?
594 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700595 // can't find any activity to handle this. let's leave the
596 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400597 // to launch.
598 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400599
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400600 // set accessibility text to "Not installed"
601 mHotseatLabels[i] = getText(R.string.activity_not_found);
602 } else {
603 boolean found = false;
604 for (ResolveInfo ri : allMatches) {
605 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
606 && bestMatch.activityInfo.applicationInfo.packageName
607 .equals(ri.activityInfo.applicationInfo.packageName)) {
608 found = true;
609 break;
610 }
611 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700612
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400613 if (!found) {
614 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
615 // the bestMatch is probably the ResolveActivity, meaning the
616 // user has not yet selected a default
617 // so: we'll keep the original intent for now
618 mHotseats[i] = intent;
619
620 // set the accessibility text to "Select shortcut"
621 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
622 } else {
623 // we have an app!
624 // now reconstruct the intent to launch it through the front
625 // door
626 ComponentName com = new ComponentName(
627 bestMatch.activityInfo.applicationInfo.packageName,
628 bestMatch.activityInfo.name);
629 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
630
631 // load the app label for accessibility
632 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
633 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400634 }
635
636 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700637 Log.d(TAG, "loadHotseats: hotseat " + i
638 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400639 + ((mHotseats[i] == null)
640 ? "null"
641 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400642 + "] label=[" + mHotseatLabels[i]
643 + "]"
644 );
645 }
646 }
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 @Override
650 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700651 mWaitingForResult = false;
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700658
Michael Jurka0280c3b2010-09-17 15:00:07 -0700659 if (resultCode == RESULT_OK && mAddScreen != -1) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 switch (requestCode) {
661 case REQUEST_PICK_APPLICATION:
Michael Jurka28750fb2010-09-24 17:43:49 -0700662 completeAddApplication(
663 this, data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 break;
665 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800666 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 break;
668 case REQUEST_CREATE_SHORTCUT:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700669 completeAddShortcut(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 break;
671 case REQUEST_PICK_LIVE_FOLDER:
672 addLiveFolder(data);
673 break;
674 case REQUEST_CREATE_LIVE_FOLDER:
Michael Jurka0280c3b2010-09-17 15:00:07 -0700675 completeAddLiveFolder(data, mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700677 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700678 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700680 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700681 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700682 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700684 case REQUEST_PICK_WALLPAPER:
685 // We just wanted the activity result here so we can clear mWaitingForResult
686 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 }
Romain Guy18042c82009-11-06 11:44:55 -0800688 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
689 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
690 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700691 // Clean up the appWidgetId if we canceled
692 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
693 if (appWidgetId != -1) {
694 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 }
698
699 @Override
700 protected void onResume() {
701 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800702 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700703 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400704 mWorkspaceLoading = true;
705 mModel.startLoader(this, true);
706 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700707 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700709 // When we resume Launcher, a different Activity might be responsible for the app
710 // market intent, so refresh the icon
711 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
713
714 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700715 protected void onPause() {
716 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700717 // Some launcher layouts don't have a previous and next view
718 if (mPreviousView != null) {
719 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700720 }
721 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700722 dismissPreview(mNextView);
723 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700724 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800725 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700726 }
Romain Guycbb89e42009-06-08 15:52:54 -0700727
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700728 @Override
729 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400730 // Flag the loader to stop early before switching
731 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800732 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800733 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700734 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700735
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800736 // We can't hide the IME if it was forced open. So don't bother
737 /*
738 @Override
739 public void onWindowFocusChanged(boolean hasFocus) {
740 super.onWindowFocusChanged(hasFocus);
741
742 if (hasFocus) {
743 final InputMethodManager inputManager = (InputMethodManager)
744 getSystemService(Context.INPUT_METHOD_SERVICE);
745 WindowManager.LayoutParams lp = getWindow().getAttributes();
746 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
747 android.os.Handler()) {
748 protected void onReceiveResult(int resultCode, Bundle resultData) {
749 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
750 }
751 });
752 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
753 }
754 }
755 */
756
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800757 private boolean acceptFilter() {
758 final InputMethodManager inputManager = (InputMethodManager)
759 getSystemService(Context.INPUT_METHOD_SERVICE);
760 return !inputManager.isFullscreenMode();
761 }
762
763 @Override
764 public boolean onKeyDown(int keyCode, KeyEvent event) {
765 boolean handled = super.onKeyDown(keyCode, event);
766 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
767 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
768 keyCode, event);
769 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700770 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700771 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700772 // showSearchDialog()
773 // If there are multiple keystrokes before the search dialog takes focus,
774 // onSearchRequested() will be called for every keystroke,
775 // but it is idempotent, so it's fine.
776 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 }
778 }
779
Joe Onorato8a9625e2010-01-28 15:55:35 -0800780 // Eat the long press event so the keyboard doesn't come up.
781 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
782 return true;
783 }
784
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 return handled;
786 }
787
Karl Rosaen138a0412009-04-23 19:00:21 -0700788 private String getTypedText() {
789 return mDefaultKeySsb.toString();
790 }
791
792 private void clearTypedText() {
793 mDefaultKeySsb.clear();
794 mDefaultKeySsb.clearSpans();
795 Selection.setSelection(mDefaultKeySsb, 0);
796 }
797
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700799 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
800 * State
801 */
802 private static State intToState(int stateOrdinal) {
803 State state = State.WORKSPACE;
804 final State[] stateValues = State.values();
805 for (int i = 0; i < stateValues.length; i++) {
806 if (stateValues[i].ordinal() == stateOrdinal) {
807 state = stateValues[i];
808 break;
809 }
810 }
811 return state;
812 }
813
814 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 * Restores the previous state, if it exists.
816 *
817 * @param savedState The previous state.
818 */
819 private void restoreState(Bundle savedState) {
820 if (savedState == null) {
821 return;
822 }
823
Michael Jurka883f55b2010-10-21 15:47:14 -0700824 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
825
826 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800827 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700828 } else if (state == State.CUSTOMIZE) {
829 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800830 }
831
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
833 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700834 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836
837 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700840 mAddScreen = addScreen;
841 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
842 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 mRestoring = true;
844 }
845
846 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
847 if (renameFolder) {
848 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700849 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 mRestoring = true;
851 }
852 }
853
854 /**
855 * Finds all the views we need and configure them properly.
856 */
857 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700858 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400859
Romain Guyb1b69f52009-08-10 15:10:15 -0700860 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400861 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862
Joe Onorato7c312c12009-08-13 21:36:53 -0700863 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700864 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700865 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800866 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700867 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700868 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700869
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700870 if (LauncherApplication.isScreenXLarge()) {
871 // They need to be INVISIBLE initially so that they will be measured in the layout.
872 // Otherwise the animations are messed up when we show them for the first time.
873 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
874 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
875 }
876
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
878 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500879 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700880
Joe Onorato7c312c12009-08-13 21:36:53 -0700881 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
882 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700884 View handleView = findViewById(R.id.all_apps_button);
885 if (handleView != null && handleView instanceof HandleView) {
886 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700887 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700888 mHandleView.setLauncher(this);
889 mHandleView.setOnClickListener(this);
890 mHandleView.setOnLongClickListener(this);
891 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800892
Winson Chung80baf5a2010-08-09 16:03:15 -0700893 if (mCustomizePagedView != null) {
894 mCustomizePagedView.setLauncher(this);
895 mCustomizePagedView.setDragController(dragController);
896 mCustomizePagedView.update();
Michael Jurkaaf442092010-06-10 17:01:57 -0700897 } else {
898 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
899 hotseatLeft.setContentDescription(mHotseatLabels[0]);
900 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
901 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
902 hotseatRight.setContentDescription(mHotseatLabels[1]);
903 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400904
Michael Jurkaaf442092010-06-10 17:01:57 -0700905 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
906 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800907
Michael Jurkaaf442092010-06-10 17:01:57 -0700908 Drawable previous = mPreviousView.getDrawable();
909 Drawable next = mNextView.getDrawable();
910 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911
Michael Jurkaaf442092010-06-10 17:01:57 -0700912 mPreviousView.setHapticFeedbackEnabled(false);
913 mPreviousView.setOnLongClickListener(this);
914 mNextView.setHapticFeedbackEnabled(false);
915 mNextView.setOnLongClickListener(this);
916 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400919 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800920 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921
922 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400923 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700924 int deleteZoneHandleId;
925 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700926 deleteZoneHandleId = R.id.all_apps_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700927 } else {
928 deleteZoneHandleId = R.id.all_apps_button_cluster;
929 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700930 deleteZone.setHandle(findViewById(deleteZoneHandleId));
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700931 dragController.addDragListener(deleteZone);
932
933 ApplicationInfoDropTarget infoButton = (ApplicationInfoDropTarget)findViewById(R.id.info_button);
934 if (infoButton != null) {
935 infoButton.setLauncher(this);
936 infoButton.setHandle(findViewById(R.id.configure_button));
937 infoButton.setDragColor(getResources().getColor(R.color.app_info_filter));
938 dragController.addDragListener(infoButton);
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940
Joe Onorato00acb122009-08-04 16:04:30 -0400941 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400942 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800943 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700944
Joe Onorato00acb122009-08-04 16:04:30 -0400945 // The order here is bottom to top.
946 dragController.addDropTarget(workspace);
947 dragController.addDropTarget(deleteZone);
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700948 if (infoButton != null) {
949 dragController.addDropTarget(infoButton);
950 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952
Romain Guy8a73c512009-11-09 19:19:59 -0800953 @SuppressWarnings({"UnusedDeclaration"})
954 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700955 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800956 mWorkspace.scrollLeft();
957 }
Romain Guy8a73c512009-11-09 19:19:59 -0800958 }
959
960 @SuppressWarnings({"UnusedDeclaration"})
961 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700962 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800963 mWorkspace.scrollRight();
964 }
Romain Guy8a73c512009-11-09 19:19:59 -0800965 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400966
967 @SuppressWarnings({"UnusedDeclaration"})
968 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700969 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400970
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400971 int index = -1;
972 if (v.getId() == R.id.hotseat_left) {
973 index = 0;
974 } else if (v.getId() == R.id.hotseat_right) {
975 index = 1;
976 }
977
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400978 // reload these every tap; you never know when they might change
979 loadHotseats();
980 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
981 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400982 startActivitySafely(
983 mHotseats[index],
984 "hotseat"
985 );
986 }
987 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 /**
990 * Creates a view representing a shortcut.
991 *
992 * @param info The data structure describing the shortcut.
993 *
994 * @return A View inflated from R.layout.application.
995 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800996 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700998 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 }
1000
1001 /**
1002 * Creates a view representing a shortcut inflated from the specified resource.
1003 *
1004 * @param layoutResId The id of the XML layout used to create the shortcut.
1005 * @param parent The group the shortcut belongs to.
1006 * @param info The data structure describing the shortcut.
1007 *
1008 * @return A View inflated from layoutResId.
1009 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001010 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
1012
Patrick Dubroy9e482382010-11-01 15:16:13 -07001013 Bitmap b = info.getIcon(mIconCache);
1014
Joe Onorato0589f0f2010-02-08 13:44:00 -08001015 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
Winson Chung0e41ae62010-10-27 18:10:32 -07001016 new FastBitmapDrawable(b),
Joe Onorato0589f0f2010-02-08 13:44:00 -08001017 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 favorite.setText(info.title);
1019 favorite.setTag(info);
1020 favorite.setOnClickListener(this);
1021
1022 return favorite;
1023 }
1024
1025 /**
1026 * Add an application shortcut to the workspace.
1027 *
1028 * @param data The intent describing the application.
1029 * @param cellInfo The position on screen where to create the shortcut.
1030 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001031 void completeAddApplication(Context context, Intent data, int screen,
1032 int intersectCellX, int intersectCellY) {
1033 final int[] cellXY = mTmpAddItemCellCoordinates;
1034 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1035
1036 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1037 showOutOfSpaceMessage();
1038 return;
1039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
Joe Onorato0589f0f2010-02-08 13:44:00 -08001041 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
1042 data, context);
1043
Romain Guy73b979d2009-06-09 12:57:21 -07001044 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001045 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001047 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001048 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1049 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001050 } else {
1051 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 }
1053 }
Romain Guycbb89e42009-06-08 15:52:54 -07001054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 /**
1056 * Add a shortcut to the workspace.
1057 *
1058 * @param data The intent describing the shortcut.
1059 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061 private void completeAddShortcut(Intent data, int screen,
1062 int intersectCellX, int intersectCellY) {
1063 final int[] cellXY = mTmpAddItemCellCoordinates;
1064 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001065
Michael Jurka0280c3b2010-09-17 15:00:07 -07001066 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1067 showOutOfSpaceMessage();
1068 return;
1069 }
1070
1071 final ShortcutInfo info = mModel.addShortcut(
1072 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001073
1074 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 }
1078 }
1079
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001081 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001083 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001084 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 private void completeAddAppWidget(int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001087 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001088
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001089 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1091 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001092
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001093 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001094 // We have saved the position to which the widget was dragged-- this really only matters
1095 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001096 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001097
1098 // For now, we don't save the coordinate where we dropped the icon because we're not
1099 // supporting spring-loaded mini-screens; however, leaving the ability to directly place
1100 // a widget on the home screen in case we want to add it in the future
Winson Chung55cef262010-10-28 14:14:18 -07001101 int[] touchXY = null;
1102 if (mAddDropPosition[0] > -1 && mAddDropPosition[1] > -1) {
1103 touchXY = mAddDropPosition;
1104 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001105 boolean findNearestVacantAreaFailed = false;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001106 boolean foundCellSpan = false;
1107 if (touchXY != null) {
1108 // when dragging and dropping, just find the closest free spot
1109 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1110 int[] result = screenLayout.findNearestVacantArea(
1111 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001112 findNearestVacantAreaFailed = (result == null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001113 foundCellSpan = !findNearestVacantAreaFailed;
1114 } else {
1115 if (mAddIntersectCellX != -1 && mAddIntersectCellY != -1) {
1116 // if we long pressed on an empty cell to bring up a menu,
1117 // make sure we intersect the empty cell
1118 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1119 mAddIntersectCellX, mAddIntersectCellY);
1120 } else {
1121 // if we went through the menu -> add, just find any spot
1122 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
1123 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001124 }
1125
Michael Jurka0280c3b2010-09-17 15:00:07 -07001126 if (!foundCellSpan) {
Romain Guy18042c82009-11-06 11:44:55 -08001127 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001128 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001129 return;
1130 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001132 // Build Launcher-specific widget info and save to database
1133 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 launcherInfo.spanX = spanXY[0];
1135 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 LauncherModel.addItemToDatabase(this, launcherInfo,
1138 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001139 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140
1141 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001142 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001143
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001145 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001146
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001147 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001148 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001149
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001151 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001152
1153 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 }
1155 }
Romain Guycbb89e42009-06-08 15:52:54 -07001156
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 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
Adam Cohend113e0c2010-11-11 10:48:05 -08001181 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001182 mVisible = true;
1183 }
1184
1185 @Override
1186 public void onDetachedFromWindow() {
1187 super.onDetachedFromWindow();
1188 mVisible = false;
1189
Adam Cohend113e0c2010-11-11 10:48:05 -08001190 if (mAttached) {
1191 unregisterReceiver(mReceiver);
1192 mAttached = false;
1193 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 updateRunning();
1195 }
1196
1197 public void onWindowVisibilityChanged(int visibility) {
1198 mVisible = visibility == View.VISIBLE;
1199 updateRunning();
1200 }
1201
1202 private void sendAdvanceMessage(long delay) {
1203 mHandler.removeMessages(ADVANCE_MSG);
1204 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1205 mHandler.sendMessageDelayed(msg, delay);
1206 mAutoAdvanceSentTime = System.currentTimeMillis();
1207 }
1208
1209 private void updateRunning() {
1210 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1211 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1212 mAutoAdvanceRunning = autoAdvanceRunning;
1213 if (autoAdvanceRunning) {
1214 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1215 sendAdvanceMessage(delay);
1216 } else {
1217 if (!mWidgetsToAdvance.isEmpty()) {
1218 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1219 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1220 }
1221 mHandler.removeMessages(ADVANCE_MSG);
1222 }
1223 }
1224 }
1225
1226 private final Handler mHandler = new Handler() {
1227 @Override
1228 public void handleMessage(Message msg) {
1229 if (msg.what == ADVANCE_MSG) {
1230 int i = 0;
1231 for (View key: mWidgetsToAdvance.keySet()) {
1232 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1233 final int delay = mAdvanceStagger * i;
1234 if (v instanceof Advanceable) {
1235 postDelayed(new Runnable() {
1236 public void run() {
1237 ((Advanceable) v).advance();
1238 }
1239 }, delay);
1240 }
1241 i++;
1242 }
1243 sendAdvanceMessage(mAdvanceInterval);
1244 }
1245 }
1246 };
1247
1248 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1249 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1250 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1251 if (v instanceof Advanceable) {
1252 mWidgetsToAdvance.put(hostView, appWidgetInfo);
1253 ((Advanceable) v).willBeAdvancedByHost();
1254 updateRunning();
1255 }
1256 }
1257
1258 void removeWidgetToAutoAdvance(View hostView) {
1259 if (mWidgetsToAdvance.containsKey(hostView)) {
1260 mWidgetsToAdvance.remove(hostView);
1261 updateRunning();
1262 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001263 }
1264
Joe Onorato9c1289c2009-08-17 11:03:03 -04001265 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1266 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001267 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 launcherInfo.hostView = null;
1269 }
1270
Adam Cohended9f8d2010-11-03 13:25:16 -07001271 void showOutOfSpaceMessage() {
1272 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1273 }
1274
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001275 public LauncherAppWidgetHost getAppWidgetHost() {
1276 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 }
Romain Guycbb89e42009-06-08 15:52:54 -07001278
Winson Chunga9abd0e2010-10-27 17:18:37 -07001279 public LauncherModel getModel() {
1280 return mModel;
1281 }
1282
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001283 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001284 getWindow().closeAllPanels();
1285
1286 try {
1287 dismissDialog(DIALOG_CREATE_SHORTCUT);
1288 // Unlock the workspace if the dialog was showing
1289 } catch (Exception e) {
1290 // An exception is thrown if the dialog is not visible, which is fine
1291 }
1292
1293 try {
1294 dismissDialog(DIALOG_RENAME_FOLDER);
1295 // Unlock the workspace if the dialog was showing
1296 } catch (Exception e) {
1297 // An exception is thrown if the dialog is not visible, which is fine
1298 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001299
1300 // Whatever we were doing is hereby canceled.
1301 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001302 }
1303
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 @Override
1305 protected void onNewIntent(Intent intent) {
1306 super.onNewIntent(intent);
1307
1308 // Close the menu
1309 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001310 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001311 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001312
Joe Onorato14f122b2009-11-19 14:06:36 -08001313 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1314 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001315
Michael Jurka4cb37242010-08-09 21:05:32 -07001316 // in all these cases, only animate if we're already on home
1317 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001318 mWorkspace.unshrink(alreadyOnHome);
1319 }
1320 if (!mWorkspace.isDefaultPageShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001321 // on the phone, we don't animate the change to the workspace if all apps is visible
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001322 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1323 (LauncherApplication.isScreenXLarge() || mState != State.ALL_APPS));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001324 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001325 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001326
Joe Onorato3a8820b2009-11-10 15:06:42 -08001327 final View v = getWindow().peekDecorView();
1328 if (v != null && v.getWindowToken() != null) {
1329 InputMethodManager imm = (InputMethodManager)getSystemService(
1330 INPUT_METHOD_SERVICE);
1331 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 }
1333 }
1334 }
1335
1336 @Override
1337 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1338 // Do not call super here
1339 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001340
1341 if (mHomeCustomizationDrawer != null) {
1342 String cur = savedInstanceState.getString("currentTab");
1343 if (cur != null) {
1344 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1345 }
1346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348
1349 @Override
1350 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001351 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352
1353 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1354 if (folders.size() > 0) {
1355 final int count = folders.size();
1356 long[] ids = new long[count];
1357 for (int i = 0; i < count; i++) {
1358 final FolderInfo info = folders.get(i).getInfo();
1359 ids[i] = info.id;
1360 }
1361 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1362 } else {
1363 super.onSaveInstanceState(outState);
1364 }
1365
Michael Jurka883f55b2010-10-21 15:47:14 -07001366 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 if (mAddScreen > -1 && mWaitingForResult) {
1369 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1370 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1371 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 }
1373
1374 if (mFolderInfo != null && mWaitingForResult) {
1375 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1376 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1377 }
Michael Jurka946ad472010-07-09 18:05:18 -07001378
1379 if (mHomeCustomizationDrawer != null) {
1380 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1381 if (currentTabTag != null) {
1382 outState.putString("currentTab", currentTabTag);
1383 }
1384 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
1386
1387 @Override
1388 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001392 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001394 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396
1397 TextKeyListener.getInstance().release();
1398
Joe Onorato9c1289c2009-08-17 11:03:03 -04001399 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400
Joe Onorato9c1289c2009-08-17 11:03:03 -04001401 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001402
1403 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001404
Patrick Dubroyab962b72010-07-26 12:10:10 -07001405 // Some launcher layouts don't have a previous and next view
1406 if (mPreviousView != null) {
1407 dismissPreview(mPreviousView);
1408 }
1409 if (mNextView != null) {
1410 dismissPreview(mNextView);
1411 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001412
1413 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
1416 @Override
1417 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001418 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 super.startActivityForResult(intent, requestCode);
1420 }
1421
1422 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001423 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001424 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001425
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001426 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Karl Rosaen138a0412009-04-23 19:00:21 -07001428 if (initialQuery == null) {
1429 // Use any text typed in the launcher as the initial query
1430 initialQuery = getTypedText();
1431 clearTypedText();
1432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 if (appSearchData == null) {
1434 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001435 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 }
Romain Guycbb89e42009-06-08 15:52:54 -07001437
Karl Rosaen138a0412009-04-23 19:00:21 -07001438 final SearchManager searchManager =
1439 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001440 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001441 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443
1444 @Override
1445 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001446 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001447 return false;
1448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449
1450 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1453 .setIcon(android.R.drawable.ic_menu_add)
1454 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001455 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1456 .setIcon(android.R.drawable.ic_menu_manage)
1457 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001458 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 .setIcon(android.R.drawable.ic_menu_gallery)
1460 .setAlphabeticShortcut('W');
1461 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1462 .setIcon(android.R.drawable.ic_search_category_default)
1463 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1464 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1465 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1466 .setAlphabeticShortcut('N');
1467
1468 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001469 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1470 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471
1472 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1473 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1474 .setIntent(settings);
1475
1476 return true;
1477 }
1478
1479 @Override
1480 public boolean onPrepareOptionsMenu(Menu menu) {
1481 super.onPrepareOptionsMenu(menu);
1482
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001483 // If all apps is animating, don't show the menu, because we don't know
1484 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001485 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001486 return false;
1487 }
1488
1489 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001490 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001491 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1492 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1493
1494 // Disable add if the workspace is full.
1495 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001496 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1497 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499
1500 return true;
1501 }
1502
1503 @Override
1504 public boolean onOptionsItemSelected(MenuItem item) {
1505 switch (item.getItemId()) {
1506 case MENU_ADD:
1507 addItems();
1508 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001509 case MENU_MANAGE_APPS:
1510 manageApps();
1511 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 case MENU_WALLPAPER_SETTINGS:
1513 startWallpaper();
1514 return true;
1515 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001516 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 return true;
1518 case MENU_NOTIFICATIONS:
1519 showNotifications();
1520 return true;
1521 }
1522
1523 return super.onOptionsItemSelected(item);
1524 }
Romain Guycbb89e42009-06-08 15:52:54 -07001525
Karl Rosaen138a0412009-04-23 19:00:21 -07001526 /**
1527 * Indicates that we want global search for this activity by setting the globalSearch
1528 * argument for {@link #startSearch} to true.
1529 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001531 @Override
1532 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001533 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001535 }
1536
Joe Onorato9c1289c2009-08-17 11:03:03 -04001537 public boolean isWorkspaceLocked() {
1538 return mWorkspaceLoading || mWaitingForResult;
1539 }
1540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001542 if (LauncherApplication.isScreenXLarge()) {
1543 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001544 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001545 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001546 }
1547 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001548 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001549 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001550 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 }
1552
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 private void resetAddInfo() {
1554 mAddScreen = -1;
1555 mAddIntersectCellX = -1;
1556 mAddIntersectCellY = -1;
1557 mAddDropPosition = null;
1558 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001559
Winson Chung55cef262010-10-28 14:14:18 -07001560 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 resetAddInfo();
1562 mAddScreen = screen;
1563
1564 // only set mAddDropPosition if we dropped on home screen in "spring-loaded" manner
1565 mAddDropPosition = position;
1566
Michael Jurkaaf442092010-06-10 17:01:57 -07001567 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001568 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1569 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001570 }
1571
Winson Chung7ad01412010-09-27 11:33:03 -07001572 private void manageApps() {
1573 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1574 }
1575
Michael Jurkaaf442092010-06-10 17:01:57 -07001576 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001577 // TODO: catch bad widget exception when sent
1578 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001579 // TODO: Is this log message meaningful?
1580 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001581 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001582 }
1583
Winson Chung55cef262010-10-28 14:14:18 -07001584 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001585 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586
Bjorn Bringert7984c942009-12-09 15:38:25 +00001587 if (appWidget.configure != null) {
1588 // Launch over to configure widget, if needed
1589 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1590 intent.setComponent(appWidget.configure);
1591 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001592 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001593 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1594 intent.putExtra(
1595 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1596 info.mimeType);
1597
1598 final String mimeType = info.mimeType;
1599 final ClipData clipData = (ClipData) info.configurationData;
1600 final ClipDescription clipDesc = clipData.getDescription();
1601 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1602 if (clipDesc.getMimeType(i).equals(mimeType)) {
1603 final ClipData.Item item = clipData.getItem(i);
1604 final CharSequence stringData = item.getText();
1605 final Uri uriData = item.getUri();
1606 final Intent intentData = item.getIntent();
1607 final String key =
1608 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1609 if (uriData != null) {
1610 intent.putExtra(key, uriData);
1611 } else if (intentData != null) {
1612 intent.putExtra(key, intentData);
1613 } else if (stringData != null) {
1614 intent.putExtra(key, stringData);
1615 }
1616 break;
1617 }
1618 }
1619 }
Winson Chung55cef262010-10-28 14:14:18 -07001620 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001621
Romain Guy8e633c52010-03-04 12:51:36 -08001622 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001623 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001624 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001625 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
1627 }
Romain Guycbb89e42009-06-08 15:52:54 -07001628
Michael Jurka0280c3b2010-09-17 15:00:07 -07001629 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1630 resetAddInfo();
1631 mAddScreen = screen;
1632 mAddDropPosition = position;
1633
1634 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1635 createShortcutIntent.setComponent(componentName);
1636 processShortcut(createShortcutIntent);
1637 }
1638
Joe Onoratodeb98af2010-02-19 14:59:39 -08001639 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001640 // Handle case where user selected "Applications"
1641 String applicationName = getResources().getString(R.string.group_applications);
1642 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001643
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001644 if (applicationName != null && applicationName.equals(shortcutName)) {
1645 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1646 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001647
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001648 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1649 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001650 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001651 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001652 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001653 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001654 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 }
1656
Winson Chung24ab2f12010-09-16 14:10:47 -07001657 void processWallpaper(Intent intent) {
1658 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1659 }
1660
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1662 resetAddInfo();
1663 mAddScreen = screen;
1664 mAddDropPosition = position;
1665
1666 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1667 createFolderIntent.setComponent(componentName);
1668
1669 addLiveFolder(createFolderIntent);
1670 }
1671
1672 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001673 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001674 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001675 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001676
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001677 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001678 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001679 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001680 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001681 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 }
1683
Michael Jurka0280c3b2010-09-17 15:00:07 -07001684 void addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 UserFolderInfo folderInfo = new UserFolderInfo();
1686 folderInfo.title = getText(R.string.folder_name);
1687
Michael Jurka0280c3b2010-09-17 15:00:07 -07001688 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1689 final int[] cellXY = mTmpAddItemCellCoordinates;
1690 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1691 showOutOfSpaceMessage();
1692 return;
1693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694
1695 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001696 LauncherModel.addItemToDatabase(this, folderInfo,
1697 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001698 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001699 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700
1701 // Create the view
1702 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001703 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1704 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001705 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 }
Romain Guycbb89e42009-06-08 15:52:54 -07001707
Joe Onorato9c1289c2009-08-17 11:03:03 -04001708 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001709 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001710 }
1711
Michael Jurka28750fb2010-09-24 17:43:49 -07001712 private void completeAddLiveFolder(
1713 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001714 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1715 final int[] cellXY = mTmpAddItemCellCoordinates;
1716 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1717 showOutOfSpaceMessage();
1718 return;
1719 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001720
Michael Jurka0280c3b2010-09-17 15:00:07 -07001721 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001722
1723 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001724 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001725 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001726 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 }
1728 }
1729
1730 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001731 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732
1733 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1734 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1735
1736 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 Intent.ShortcutIconResource iconResource = null;
1738
1739 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1740 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1741 try {
1742 iconResource = (Intent.ShortcutIconResource) extra;
1743 final PackageManager packageManager = context.getPackageManager();
1744 Resources resources = packageManager.getResourcesForApplication(
1745 iconResource.packageName);
1746 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1747 icon = resources.getDrawable(id);
1748 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001749 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
1751 }
1752
1753 if (icon == null) {
1754 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1755 }
1756
1757 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001758 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 info.title = name;
1760 info.iconResource = iconResource;
1761 info.uri = data.getData();
1762 info.baseIntent = baseIntent;
1763 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1764 LiveFolders.DISPLAY_MODE_GRID);
1765
1766 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001767 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001768 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769
1770 return info;
1771 }
1772
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 private void showNotifications() {
1774 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1775 if (statusBar != null) {
1776 statusBar.expand();
1777 }
1778 }
1779
1780 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001781 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001783 Intent chooser = Intent.createChooser(pickWallpaper,
1784 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001785 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1786 // Removed in Eclair MR1
1787// WallpaperManager wm = (WallpaperManager)
1788// getSystemService(Context.WALLPAPER_SERVICE);
1789// WallpaperInfo wi = wm.getWallpaperInfo();
1790// if (wi != null && wi.getSettingsActivity() != null) {
1791// LabeledIntent li = new LabeledIntent(getPackageName(),
1792// R.string.configure_wallpaper, 0);
1793// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1794// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1795// }
Mike Clerona0618e42009-10-22 13:55:21 -07001796 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 }
1798
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001799 /**
1800 * Registers various content observers. The current implementation registers
1801 * only a favorites observer to keep track of the favorites applications.
1802 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001803 private void registerContentObservers() {
1804 ContentResolver resolver = getContentResolver();
1805 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1806 true, mWidgetObserver);
1807 }
1808
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 @Override
1810 public boolean dispatchKeyEvent(KeyEvent event) {
1811 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1812 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001814 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001815 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001816 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001817 dumpState();
1818 return true;
1819 }
1820 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001821 }
1822 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1823 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001824 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001825 return true;
1826 }
1827 }
1828
1829 return super.dispatchKeyEvent(event);
1830 }
1831
Joe Onorato88ec0992009-11-19 13:16:06 -08001832 @Override
1833 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001834 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1835 showWorkspace(true);
Joe Onorato88ec0992009-11-19 13:16:06 -08001836 } else {
1837 closeFolder();
1838 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001839 // Some launcher layouts don't have a previous and next view
1840 if (mPreviousView != null) {
1841 dismissPreview(mPreviousView);
1842 dismissPreview(mNextView);
1843 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001844 }
1845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 private void closeFolder() {
1847 Folder folder = mWorkspace.getOpenFolder();
1848 if (folder != null) {
1849 closeFolder(folder);
1850 }
1851 }
1852
1853 void closeFolder(Folder folder) {
1854 folder.getInfo().opened = false;
1855 ViewGroup parent = (ViewGroup) folder.getParent();
1856 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001857 CellLayout cl = (CellLayout) parent;
1858 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001859 if (folder instanceof DropTarget) {
1860 // Live folders aren't DropTargets.
1861 mDragController.removeDropTarget((DropTarget)folder);
1862 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 }
1864 folder.onClose();
1865 }
1866
1867 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001868 * Re-listen when widgets are reset.
1869 */
1870 private void onAppWidgetReset() {
1871 mAppWidgetHost.startListening();
1872 }
1873
1874 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001875 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1876 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 private void unbindDesktopItems() {
1879 for (ItemInfo item: mDesktopItems) {
1880 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
1882 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001883
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 /**
1885 * Launches the intent referred by the clicked shortcut.
1886 *
1887 * @param v The view representing the clicked shortcut.
1888 */
1889 public void onClick(View v) {
1890 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001891 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001893 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001894 int[] pos = new int[2];
1895 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001896 intent.setSourceBounds(new Rect(pos[0], pos[1],
1897 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001898 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 } else if (tag instanceof FolderInfo) {
1900 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001901 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001902 if (mState == State.ALL_APPS) {
1903 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001904 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001905 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908 }
1909
Michael Jurka0e260592010-06-30 17:07:39 -07001910 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001911 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001912 // clicking anywhere on the workspace causes the customization drawer to slide down
1913 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001914 return false;
1915 }
1916
Michael Jurkaaf442092010-06-10 17:01:57 -07001917 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001918 * Event handler for the search button
1919 *
1920 * @param v The view that was clicked.
1921 */
1922 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001923 startSearch(null, false, null, true);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001924 }
1925
1926 /**
1927 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001928 * enters home screen customization mode.
1929 *
1930 * @param v The view that was clicked.
1931 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001932 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001933 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001934 }
1935
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001936 /**
1937 * Event handler for the "grid" button that appears on the home screen, which
1938 * enters all apps mode.
1939 *
1940 * @param v The view that was clicked.
1941 */
1942 public void onClickAllAppsButton(View v) {
1943 showAllApps(true);
1944 }
1945
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001946 public void onClickAppMarketButton(View v) {
1947 if (mAppMarketIntent != null) {
1948 startActivitySafely(mAppMarketIntent, "app market");
1949 }
1950 }
1951
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001952 void startApplicationDetailsActivity(ComponentName componentName) {
1953 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001954 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1955 Uri.fromParts("package", packageName, null));
1956 startActivity(intent);
1957 }
1958
Patrick Dubroy5539af72010-09-07 15:22:01 -07001959 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1960 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1961 // System applications cannot be installed. For now, show a toast explaining that.
1962 // We may give them the option of disabling apps this way.
1963 int messageId = R.string.uninstall_system_app_text;
1964 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1965 } else {
1966 String packageName = appInfo.componentName.getPackageName();
1967 String className = appInfo.componentName.getClassName();
1968 Intent intent = new Intent(
1969 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1970 startActivity(intent);
1971 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001972 }
1973
Joe Onoratof984e852010-03-25 09:47:45 -07001974 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1976 try {
1977 startActivity(intent);
1978 } catch (ActivityNotFoundException e) {
1979 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001980 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 } catch (SecurityException e) {
1982 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001983 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001985 "or use the exported attribute for this activity. "
1986 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001989
Romain Guy8e633c52010-03-04 12:51:36 -08001990 void startActivityForResultSafely(Intent intent, int requestCode) {
1991 try {
1992 startActivityForResult(intent, requestCode);
1993 } catch (ActivityNotFoundException e) {
1994 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1995 } catch (SecurityException e) {
1996 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1997 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1998 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1999 "or use the exported attribute for this activity.", e);
2000 }
2001 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002
2003 private void handleFolderClick(FolderInfo folderInfo) {
2004 if (!folderInfo.opened) {
2005 // Close any open folder
2006 closeFolder();
2007 // Open the requested folder
2008 openFolder(folderInfo);
2009 } else {
2010 // Find the open folder...
2011 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2012 int folderScreen;
2013 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002014 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 // .. and close it
2016 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002017 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 // Close any folder open on the current screen
2019 closeFolder();
2020 // Pull the folder onto this screen
2021 openFolder(folderInfo);
2022 }
2023 }
2024 }
2025 }
2026
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002028 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 * is animated relative to the specified View. If the View is null, no animation
2030 * is played.
2031 *
2032 * @param folderInfo The FolderInfo describing the folder to open.
2033 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002034 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 Folder openFolder;
2036
2037 if (folderInfo instanceof UserFolderInfo) {
2038 openFolder = UserFolder.fromXml(this);
2039 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002040 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 } else {
2042 return;
2043 }
2044
Joe Onorato00acb122009-08-04 16:04:30 -04002045 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 openFolder.setLauncher(this);
2047
2048 openFolder.bind(folderInfo);
2049 folderInfo.opened = true;
2050
Winson Chungaafa03c2010-06-11 17:34:16 -07002051 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 openFolder.onOpen();
2054 }
2055
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08002057 switch (v.getId()) {
2058 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002059 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002060 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2061 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002062 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002063 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002064 return true;
2065 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002066 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002067 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2068 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002069 showPreviews(v);
2070 }
2071 return true;
2072 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002073 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002074 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2075 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2076 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002077 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002078 return true;
2079 }
2080
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002082 return false;
2083 }
2084
2085 if (!(v instanceof CellLayout)) {
2086 v = (View) v.getParent();
2087 }
2088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089
Michael Jurka0280c3b2010-09-17 15:00:07 -07002090 resetAddInfo();
2091 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002093 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 return true;
2095 }
2096
Michael Jurka0280c3b2010-09-17 15:00:07 -07002097 final View itemUnderLongClick = longClickCellInfo.cell;
2098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 if (mWorkspace.allowLongPress()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002100 if (itemUnderLongClick == null) {
2101 // User long pressed on empty space
2102 mWorkspace.setAllowLongPress(false);
2103 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2104 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka56170562010-10-05 14:14:56 -07002105 if (!LauncherApplication.isScreenXLarge()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002106 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002109 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002111 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2112 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002113 mAddIntersectCellX = longClickCellInfo.cellX;
2114 mAddIntersectCellY = longClickCellInfo.cellY;
2115 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117 }
2118 }
2119 return true;
2120 }
2121
Romain Guye6b8e2f2009-11-10 11:56:55 -08002122 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002123 private void dismissPreview(final View v) {
2124 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002125 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002126 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2127 public void onDismiss() {
2128 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2129 int count = group.getChildCount();
2130 for (int i = 0; i < count; i++) {
2131 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2132 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002133 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2134 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2135
2136 v.setTag(R.id.workspace, null);
2137 v.setTag(R.id.icon, null);
2138 window.setOnDismissListener(null);
2139 }
2140 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002141 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002142 }
2143 v.setTag(null);
2144 }
2145
Romain Guyf8e6a802009-12-07 17:48:02 -08002146 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002147 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002148 }
2149
Romain Guya6abce82009-11-10 02:54:41 -08002150 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002151 final Resources resources = getResources();
2152 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002153
Romain Guya6abce82009-11-10 02:54:41 -08002154 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002155
Romain Guy9d31e9f2009-11-11 18:54:28 -08002156 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002157
Romain Guyf8e6a802009-12-07 17:48:02 -08002158 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002159 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002160 int extraW = (int) ((r.left + r.right) * max);
2161 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002162
2163 int aW = cell.getWidth() - extraW;
2164 float w = aW / max;
2165
2166 int width = cell.getWidth();
2167 int height = cell.getHeight();
2168 int x = cell.getLeftPadding();
2169 int y = cell.getTopPadding();
2170 width -= (x + cell.getRightPadding());
2171 height -= (y + cell.getBottomPadding());
2172
2173 float scale = w / width;
2174
2175 int count = end - start;
2176
2177 final float sWidth = width * scale;
2178 float sHeight = height * scale;
2179
Romain Guye6b8e2f2009-11-10 11:56:55 -08002180 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002181
Romain Guye6b8e2f2009-11-10 11:56:55 -08002182 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2183 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002184
2185 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002186 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002187 cell = (CellLayout) workspace.getChildAt(i);
2188
Romain Guyf8e6a802009-12-07 17:48:02 -08002189 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002190 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002191
2192 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002193 c.scale(scale, scale);
2194 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002195 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002196
Romain Guy9d31e9f2009-11-11 18:54:28 -08002197 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002198 image.setImageBitmap(bitmap);
2199 image.setTag(i);
2200 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002201 image.setOnFocusChangeListener(handler);
2202 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002203 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002204
2205 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002206 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2207
Winson Chungaafa03c2010-06-11 17:34:16 -07002208 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002209 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002210
2211 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002212 p.setContentView(preview);
2213 p.setWidth((int) (sWidth * count + extraW));
2214 p.setHeight((int) (sHeight + extraH));
2215 p.setAnimationStyle(R.style.AnimationPreview);
2216 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002217 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002218 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002219 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002220
Romain Guye6b8e2f2009-11-10 11:56:55 -08002221 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2222 public void onDismiss() {
2223 dismissPreview(anchor);
2224 }
2225 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002226
2227 anchor.setTag(p);
2228 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002229 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002230 }
2231
Romain Guy9d31e9f2009-11-11 18:54:28 -08002232 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002233 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002234
Romain Guye6b8e2f2009-11-10 11:56:55 -08002235 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002236 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002237 }
2238
2239 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002240 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002241 v.post(this);
2242 }
2243
2244 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002245 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002246 }
2247
2248 public void onFocusChange(View v, boolean hasFocus) {
2249 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002250 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002251 }
Romain Guya6abce82009-11-10 02:54:41 -08002252 }
2253 }
2254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002255 Workspace getWorkspace() {
2256 return mWorkspace;
2257 }
2258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 @Override
2260 protected Dialog onCreateDialog(int id) {
2261 switch (id) {
2262 case DIALOG_CREATE_SHORTCUT:
2263 return new CreateShortcut().createDialog();
2264 case DIALOG_RENAME_FOLDER:
2265 return new RenameFolder().createDialog();
2266 }
2267
2268 return super.onCreateDialog(id);
2269 }
2270
2271 @Override
2272 protected void onPrepareDialog(int id, Dialog dialog) {
2273 switch (id) {
2274 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 break;
2276 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002277 if (mFolderInfo != null) {
2278 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2279 final CharSequence text = mFolderInfo.title;
2280 input.setText(text);
2281 input.setSelection(0, text.length());
2282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002283 break;
2284 }
2285 }
2286
2287 void showRenameDialog(FolderInfo info) {
2288 mFolderInfo = info;
2289 mWaitingForResult = true;
2290 showDialog(DIALOG_RENAME_FOLDER);
2291 }
2292
Michael Jurka0280c3b2010-09-17 15:00:07 -07002293 private void showAddDialog(int intersectX, int intersectY) {
2294 resetAddInfo();
2295 mAddIntersectCellX = intersectX;
2296 mAddIntersectCellY = intersectY;
2297 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 mWaitingForResult = true;
2299 showDialog(DIALOG_CREATE_SHORTCUT);
2300 }
2301
Joe Onoratodeb98af2010-02-19 14:59:39 -08002302 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002303 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002304 Bundle bundle = new Bundle();
2305
2306 ArrayList<String> shortcutNames = new ArrayList<String>();
2307 shortcutNames.add(getString(R.string.group_applications));
2308 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2309
2310 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2311 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2312 R.drawable.ic_launcher_application));
2313 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2314
2315 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2316 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002317 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002318 pickIntent.putExtras(bundle);
2319
Joe Onoratodeb98af2010-02-19 14:59:39 -08002320 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002321 }
2322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002323 private class RenameFolder {
2324 private EditText mInput;
2325
2326 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002327 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2328 mInput = (EditText) layout.findViewById(R.id.folder_name);
2329
2330 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2331 builder.setIcon(0);
2332 builder.setTitle(getString(R.string.rename_folder_title));
2333 builder.setCancelable(true);
2334 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2335 public void onCancel(DialogInterface dialog) {
2336 cleanup();
2337 }
2338 });
2339 builder.setNegativeButton(getString(R.string.cancel_action),
2340 new Dialog.OnClickListener() {
2341 public void onClick(DialogInterface dialog, int which) {
2342 cleanup();
2343 }
2344 }
2345 );
2346 builder.setPositiveButton(getString(R.string.rename_action),
2347 new Dialog.OnClickListener() {
2348 public void onClick(DialogInterface dialog, int which) {
2349 changeFolderName();
2350 }
2351 }
2352 );
2353 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002354
2355 final AlertDialog dialog = builder.create();
2356 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2357 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002358 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002359 mInput.requestFocus();
2360 InputMethodManager inputManager = (InputMethodManager)
2361 getSystemService(Context.INPUT_METHOD_SERVICE);
2362 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002363 }
2364 });
2365
2366 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002367 }
2368
2369 private void changeFolderName() {
2370 final String name = mInput.getText().toString();
2371 if (!TextUtils.isEmpty(name)) {
2372 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002373 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002374 mFolderInfo.title = name;
2375 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2376
Joe Onorato9c1289c2009-08-17 11:03:03 -04002377 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002378 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002379 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 } else {
2381 final FolderIcon folderIcon = (FolderIcon)
2382 mWorkspace.getViewForTag(mFolderInfo);
2383 if (folderIcon != null) {
2384 folderIcon.setText(name);
2385 getWorkspace().requestLayout();
2386 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002387 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002388 mWorkspaceLoading = true;
2389 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002390 }
2391 }
2392 }
2393 cleanup();
2394 }
2395
2396 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002397 dismissDialog(DIALOG_RENAME_FOLDER);
2398 mWaitingForResult = false;
2399 mFolderInfo = null;
2400 }
2401 }
2402
Daniel Sandler843e8602010-06-07 14:59:01 -04002403 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2404 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002405 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002406 }
2407
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002408 // AllAppsView.Watcher
2409 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002410 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2411 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002412 mWorkspace.setVisibility(View.GONE);
2413 }
2414 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002415
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002416 private void showToolbarButton(View button) {
2417 button.setAlpha(1.0f);
2418 button.setVisibility(View.VISIBLE);
2419 button.setFocusable(true);
2420 button.setClickable(true);
2421 }
2422
2423 private void hideToolbarButton(View button) {
2424 button.setAlpha(0.0f);
2425 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2426 button.setVisibility(View.INVISIBLE);
2427 button.setFocusable(false);
2428 button.setClickable(false);
2429 }
2430
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002431 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002432 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002433 *
2434 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2435 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002436 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002437 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002438 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002439 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002440 final boolean showing = show;
2441 final boolean hiding = !show;
2442
2443 final int duration = show ?
2444 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2445 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2446
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002447 if (seq != null) {
Chet Haase472b2812010-10-14 07:02:04 -07002448 Animator anim = ObjectAnimator.ofFloat(view, "alpha", show ? 1.0f : 0.0f);
2449 anim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002450 anim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002451 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002452 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002453 if (showing) showToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002454 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002455 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002456 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002457 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002458 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002459 });
2460 seq.play(anim);
2461 } else {
2462 if (showing) {
2463 showToolbarButton(view);
2464 } else {
2465 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002466 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002467 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002468 }
2469
2470 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002471 * Show/hide the appropriate toolbar buttons for newState.
2472 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002473 *
2474 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002475 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2476 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002477 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002478 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002479 final View searchButton = findViewById(R.id.search_button);
2480 final View allAppsButton = findViewById(R.id.all_apps_button);
2481 final View marketButton = findViewById(R.id.market_button);
2482 final View configureButton = findViewById(R.id.configure_button);
2483
2484 switch (newState) {
2485 case WORKSPACE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002486 hideOrShowToolbarButton(true, searchButton, showSeq);
2487 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2488 hideOrShowToolbarButton(true, configureButton, showSeq);
2489 hideOrShowToolbarButton(false, marketButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002490 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002491 break;
2492 case ALL_APPS:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002493 hideOrShowToolbarButton(true, configureButton, showSeq);
2494 hideOrShowToolbarButton(true, marketButton, showSeq);
2495 hideOrShowToolbarButton(false, searchButton, hideSeq);
2496 hideOrShowToolbarButton(false, allAppsButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002497 mDeleteZone.setHandle(marketButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002498 break;
2499 case CUSTOMIZE:
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 hideOrShowToolbarButton(true, allAppsButton, showSeq);
2501 hideOrShowToolbarButton(false, searchButton, hideSeq);
2502 hideOrShowToolbarButton(false, marketButton, hideSeq);
2503 hideOrShowToolbarButton(false, configureButton, hideSeq);
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002504 mDeleteZone.setHandle(allAppsButton);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002505 break;
2506 }
2507 }
2508
2509 /**
2510 * Helper method for the cameraZoomIn/cameraZoomOut animations
2511 * @param view The view being animated
2512 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2513 * @param scaleFactor The scale factor used for the zoom
2514 */
2515 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2516 final int height = view.getHeight();
2517 view.setPivotX(view.getWidth() / 2.0f);
2518 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2519 // Assumes that the view is normally anchored to either the top or bottom of the screen
2520 final int margin = getResources().getInteger(R.integer.config_allAppsVerticalOffset);
2521 if (state == State.ALL_APPS) {
2522 view.setPivotY(height + ((view.getTop() + height) / scaleFactor) + margin);
2523 } else {
2524 view.setPivotY(0.0f - (view.getTop() / scaleFactor) - margin);
2525 }
2526 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002527
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002528 /**
2529 * Zoom the camera out from the workspace to reveal 'toView'.
2530 * Assumes that the view to show is anchored at either the very top or very bottom
2531 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002532 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002533 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002535 final Resources res = getResources();
2536 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2537 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002538 final boolean toAllApps = (toState == State.ALL_APPS);
2539 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002540
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002541 setPivotsForZoom(toView, toState, scale);
2542
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002543 if (toAllApps) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002544 mWorkspace.shrinkToBottom(animated);
2545 } else {
2546 mWorkspace.shrinkToTop(animated);
2547 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002548
2549 if (animated) {
Chet Haase472b2812010-10-14 07:02:04 -07002550 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2551 PropertyValuesHolder.ofFloat("scaleX", scale, 1.0f),
2552 PropertyValuesHolder.ofFloat("scaleY", scale, 1.0f));
2553 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002554 scaleAnim.setInterpolator(new DecelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002555 scaleAnim.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002556 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002557 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002558 // Prepare the position
2559 toView.setTranslationX(0.0f);
2560 toView.setTranslationY(0.0f);
2561 toView.setVisibility(View.VISIBLE);
2562 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002563 @Override
2564 public void onAnimationEndOrCancel(Animator animation) {
2565 // If we don't set the final scale values here, if this animation is cancelled
2566 // it will have the wrong scale value and subsequent cameraPan animations will
2567 // not fix that
2568 toView.setScaleX(1.0f);
2569 toView.setScaleY(1.0f);
2570 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002571 });
2572
Chet Haaseb1254a62010-09-07 13:35:00 -07002573 AnimatorSet toolbarHideAnim = new AnimatorSet();
2574 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002575 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2576
2577 // toView should appear right at the end of the workspace shrink animation
2578 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2579
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002580 if (mStateAnimation != null) mStateAnimation.cancel();
2581 mStateAnimation = new AnimatorSet();
2582 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2583 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002584
2585 // Show the new toolbar buttons just as the main animation is ending
2586 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002587 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2588 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 } else {
2590 toView.setTranslationX(0.0f);
2591 toView.setTranslationY(0.0f);
2592 toView.setScaleX(1.0f);
2593 toView.setScaleY(1.0f);
2594 toView.setVisibility(View.VISIBLE);
2595 hideAndShowToolbarButtons(toState, null, null);
2596 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002597 }
2598
2599 /**
2600 * Zoom the camera back into the workspace, hiding 'fromView'.
2601 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2603 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002604 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002605 private void cameraZoomIn(State fromState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002606 Resources res = getResources();
2607 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2608 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002609 final View fromView =
2610 (fromState == State.ALL_APPS) ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2611
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002612 mCustomizePagedView.endChoiceMode();
2613 mAllAppsPagedView.endChoiceMode();
2614
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002615 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002616
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 mWorkspace.unshrink(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002618
2619 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002620 if (mStateAnimation != null) mStateAnimation.cancel();
2621 mStateAnimation = new AnimatorSet();
Chet Haase472b2812010-10-14 07:02:04 -07002622 ValueAnimator scaleAnim = ObjectAnimator.ofPropertyValuesHolder(fromView,
2623 PropertyValuesHolder.ofFloat("scaleX", scaleFactor),
2624 PropertyValuesHolder.ofFloat("scaleY", scaleFactor));
2625 scaleAnim.setDuration(duration);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002626 scaleAnim.setInterpolator(new AccelerateInterpolator());
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002627 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002628 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002629 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002630 fromView.setVisibility(View.GONE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002631 }
2632 });
2633
Chet Haaseb1254a62010-09-07 13:35:00 -07002634 AnimatorSet toolbarHideAnim = new AnimatorSet();
2635 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2637
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002638 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002639
2640 // Show the new toolbar buttons at the very end of the whole animation
2641 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2642 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002643 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2644 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002645 } else {
2646 fromView.setVisibility(View.GONE);
2647 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2648 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002649 }
2650
2651 /**
2652 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2653 * This is the transition used on xlarge screens to go between all apps and
2654 * the home customization drawer.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002655 * @param fromState The view to pan away from. Must be ALL_APPS or CUSTOMIZE.
2656 * @param toState The view to pan into the frame. Must be ALL_APPS or CUSTOMIZE.
2657 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002658 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 private void cameraPan(State fromState, State toState, boolean animated) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002660 final Resources res = getResources();
2661 final int duration = res.getInteger(R.integer.config_allAppsCameraPanTime);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002662 final int workspaceHeight = mWorkspace.getHeight();
2663
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002664 final boolean fromAllApps = (fromState == State.ALL_APPS);
2665 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
2666 final View toView = fromAllApps ? mHomeCustomizationDrawer : (View) mAllAppsGrid;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002667
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002668 final float fromViewStartY = fromAllApps ? 0.0f : fromView.getY();
2669 final float fromViewEndY = fromAllApps ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2670 final float toViewStartY = fromAllApps ? workspaceHeight * 2 : -toView.getHeight() * 2;
2671 final float toViewEndY = fromAllApps ? workspaceHeight - toView.getHeight() : 0.0f;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002672
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002673 mCustomizePagedView.endChoiceMode();
2674 mAllAppsPagedView.endChoiceMode();
2675
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002676 if (toState == State.ALL_APPS) {
2677 mWorkspace.shrinkToBottom(animated);
2678 } else {
2679 mWorkspace.shrinkToTop(animated);
2680 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681
2682 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002683 if (mStateAnimation != null) mStateAnimation.cancel();
2684 mStateAnimation = new AnimatorSet();
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002685 mStateAnimation.addListener(new LauncherAnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002686 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002687 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002688 toView.setVisibility(View.VISIBLE);
2689 toView.setY(toViewStartY);
2690 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002691 @Override
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002692 public void onAnimationEndOrCancel(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002693 fromView.setVisibility(View.GONE);
2694 }
2695 });
2696
Chet Haaseb1254a62010-09-07 13:35:00 -07002697 AnimatorSet toolbarHideAnim = new AnimatorSet();
2698 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002699 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2700
Chet Haase472b2812010-10-14 07:02:04 -07002701 ObjectAnimator fromAnim = ObjectAnimator.ofFloat(fromView, "y",
2702 fromViewStartY, fromViewEndY);
2703 fromAnim.setDuration(duration);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002704 ObjectAnimator toAnim = ObjectAnimator.ofPropertyValuesHolder(toView,
2705 PropertyValuesHolder.ofFloat("y", toViewStartY, toViewEndY),
2706 PropertyValuesHolder.ofFloat("scaleX", toView.getScaleX(), 1.0f),
2707 PropertyValuesHolder.ofFloat("scaleY", toView.getScaleY(), 1.0f)
2708 );
Chet Haase472b2812010-10-14 07:02:04 -07002709 fromAnim.setDuration(duration);
2710 mStateAnimation.playTogether(toolbarHideAnim, fromAnim, toAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002711
2712 // Show the new toolbar buttons just as the main animation is ending
2713 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002714 mStateAnimation.play(toolbarShowAnim).after(duration - fadeInTime);
2715 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002716 } else {
2717 fromView.setY(fromViewEndY);
2718 fromView.setVisibility(View.GONE);
2719 toView.setY(toViewEndY);
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002720 toView.setScaleX(1.0f);
2721 toView.setScaleY(1.0f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002722 toView.setVisibility(View.VISIBLE);
2723 hideAndShowToolbarButtons(toState, null, null);
2724 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002725 }
2726
2727 void showAllApps(boolean animated) {
Michael Jurka883f55b2010-10-21 15:47:14 -07002728 if (mState == State.ALL_APPS) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002729 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002730 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002731
Michael Jurka79212d82010-07-30 16:36:20 -07002732 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002733 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002734 cameraPan(State.CUSTOMIZE, State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002735 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002736 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002737 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002738 } else {
2739 mAllAppsGrid.zoom(1.0f, animated);
2740 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002741
Romain Guyc16fea72010-03-12 17:17:56 -08002742 ((View) mAllAppsGrid).setFocusable(true);
2743 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002744
Joe Onorato7c312c12009-08-13 21:36:53 -07002745 // TODO: fade these two too
2746 mDeleteZone.setVisibility(View.GONE);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002747 // Change the state *after* we've called all the transition code
2748 mState = State.ALL_APPS;
2749 }
2750
2751
2752 void showWorkspace(boolean animated) {
2753 showWorkspace(animated, null);
2754 }
2755
2756 void showWorkspace(boolean animated, CellLayout layout) {
2757 if (layout != null && animated) {
2758 mWorkspace.unshrink(layout);
2759 } else {
2760 mWorkspace.unshrink(animated);
2761 }
2762 if (mState == State.ALL_APPS) {
2763 closeAllApps(animated);
2764 } else if (mState == State.CUSTOMIZE) {
2765 hideCustomizationDrawer(animated);
2766 }
2767 // Change the state *after* we've called all the transition code
2768 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04002769 }
2770
Joe Onoratob2061212009-11-24 16:13:54 -05002771 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002772 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002773 * - Home from workspace
2774 * - from center screen
2775 * - from other screens
2776 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002777 * - from center screen
2778 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002779 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002780 * - from center screen
2781 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002782 * - Launch app from workspace and quit
2783 * - with back
2784 * - with home
2785 * - Launch app from all apps and quit
2786 * - with back
2787 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002788 * - Go to a screen that's not the default, then all
2789 * apps, and launch and app, and go back
2790 * - with back
2791 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002792 * - On workspace, long press power and go back
2793 * - with back
2794 * - with home
2795 * - On all apps, long press power and go back
2796 * - with back
2797 * - with home
2798 * - On workspace, power off
2799 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002800 * - Launch an app and turn off the screen while in that app
2801 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002802 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002803 * - From all apps
2804 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002805 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2806 * - From all apps
2807 * - From the center workspace
2808 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002809 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002810 void closeAllApps(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002811 if (mState == State.ALL_APPS) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002812 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002813 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002814 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002815 } else {
2816 mAllAppsGrid.zoom(0.0f, animated);
2817 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002818 ((View)mAllAppsGrid).setFocusable(false);
Michael Jurka0142d492010-08-25 17:46:15 -07002819 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002820 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002821 }
2822
Joe Onorato7c312c12009-08-13 21:36:53 -07002823 void lockAllApps() {
2824 // TODO
2825 }
2826
2827 void unlockAllApps() {
2828 // TODO
2829 }
2830
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002831 // Show the customization drawer (only exists in x-large configuration)
2832 private void showCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002833 if (mState == State.ALL_APPS) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002834 cameraPan(State.ALL_APPS, State.CUSTOMIZE, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002835 } else {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002836 cameraZoomOut(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002837 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002838 // Change the state *after* we've called all the transition code
2839 mState = State.CUSTOMIZE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002840 }
2841
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002842 // Hide the customization drawer (only exists in x-large configuration)
2843 void hideCustomizationDrawer(boolean animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002844 if (mState == State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002845 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002846 }
2847 }
2848
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002849 void addExternalItemToScreen(ItemInfo itemInfo, CellLayout layout) {
2850 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2851 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002852 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002853 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002854
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002855 void onWorkspaceClick(CellLayout layout) {
2856 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07002857 }
2858
Michael Jurka0423dcf2010-10-05 14:56:18 -07002859 private void updateButtonWithIconFromExternalActivity(
2860 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2861 ImageView button = (ImageView) findViewById(buttonId);
2862 Drawable toolbarIcon = null;
2863 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002864 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002865 // Look for the toolbar icon specified in the activity meta-data
2866 Bundle metaData = packageManager.getActivityInfo(
2867 activityName, PackageManager.GET_META_DATA).metaData;
2868 if (metaData != null) {
2869 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2870 if (iconResId != 0) {
2871 Resources res = packageManager.getResourcesForActivity(activityName);
2872 toolbarIcon = res.getDrawable(iconResId);
2873 }
2874 }
2875 } catch (NameNotFoundException e) {
2876 // Do nothing
2877 }
2878 // If we were unable to find the icon via the meta-data, use a generic one
2879 if (toolbarIcon == null) {
2880 button.setImageResource(fallbackDrawableId);
2881 } else {
2882 button.setImageDrawable(toolbarIcon);
2883 }
2884 }
2885
2886 private void updateGlobalSearchIcon() {
2887 if (LauncherApplication.isScreenXLarge()) {
2888 final SearchManager searchManager =
2889 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2890 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002891 if (activityName != null) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002892 updateButtonWithIconFromExternalActivity(
2893 R.id.search_button, activityName, R.drawable.search_button_generic);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002894 }
2895 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002896 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002897
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002898 /**
2899 * Sets the app market icon (shown when all apps is visible on x-large screens)
2900 */
2901 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002902 if (LauncherApplication.isScreenXLarge()) {
2903 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2904 // Find the app market activity by resolving an intent.
2905 // (If multiple app markets are installed, it will return the ResolverActivity.)
2906 ComponentName activityName = intent.resolveActivity(getPackageManager());
2907 if (activityName != null) {
2908 mAppMarketIntent = intent;
2909 updateButtonWithIconFromExternalActivity(
2910 R.id.market_button, activityName, R.drawable.app_market_generic);
2911 }
2912 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002913 }
2914
2915 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002916 * Displays the shortcut creation dialog and launches, if necessary, the
2917 * appropriate activity.
2918 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002919 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002920 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2921 DialogInterface.OnShowListener {
2922
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002923 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002924
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002925 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2929 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002930 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002931
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002932 builder.setInverseBackgroundForced(true);
2933
2934 AlertDialog dialog = builder.create();
2935 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002936 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002937 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002938
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939 return dialog;
2940 }
2941
2942 public void onCancel(DialogInterface dialog) {
2943 mWaitingForResult = false;
2944 cleanup();
2945 }
2946
Romain Guycbb89e42009-06-08 15:52:54 -07002947 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002948 }
2949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002950 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002951 try {
2952 dismissDialog(DIALOG_CREATE_SHORTCUT);
2953 } catch (Exception e) {
2954 // An exception is thrown if the dialog is not visible, which is fine
2955 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002956 }
2957
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002958 /**
2959 * Handle the action clicked in the "Add to home" dialog.
2960 */
2961 public void onClick(DialogInterface dialog, int which) {
2962 Resources res = getResources();
2963 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002964
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002965 switch (which) {
2966 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002967 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002968 break;
2969 }
Romain Guycbb89e42009-06-08 15:52:54 -07002970
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002971 case AddAdapter.ITEM_APPWIDGET: {
2972 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002973
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002974 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2975 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002976 // start the pick activity
2977 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2978 break;
2979 }
Romain Guycbb89e42009-06-08 15:52:54 -07002980
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002981 case AddAdapter.ITEM_LIVE_FOLDER: {
2982 // Insert extra item to handle inserting folder
2983 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002984
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002985 ArrayList<String> shortcutNames = new ArrayList<String>();
2986 shortcutNames.add(res.getString(R.string.group_folder));
2987 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002988
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002989 ArrayList<ShortcutIconResource> shortcutIcons =
2990 new ArrayList<ShortcutIconResource>();
2991 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2992 R.drawable.ic_launcher_folder));
2993 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2994
2995 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2996 pickIntent.putExtra(Intent.EXTRA_INTENT,
2997 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2998 pickIntent.putExtra(Intent.EXTRA_TITLE,
2999 getText(R.string.title_select_live_folder));
3000 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003001
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003002 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3003 break;
3004 }
3005
3006 case AddAdapter.ITEM_WALLPAPER: {
3007 startWallpaper();
3008 break;
3009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 }
3011 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003012
3013 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003014 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003016 }
3017
3018 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003019 * Receives notifications when applications are added/removed.
3020 */
3021 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3022 @Override
3023 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003024 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003025 String reason = intent.getStringExtra("reason");
3026 if (!"homekey".equals(reason)) {
3027 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003028 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003029 animate = false;
3030 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003031 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003032 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003033 }
3034 }
3035
3036 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003037 * Receives notifications whenever the appwidgets are reset.
3038 */
3039 private class AppWidgetResetObserver extends ContentObserver {
3040 public AppWidgetResetObserver() {
3041 super(new Handler());
3042 }
3043
3044 @Override
3045 public void onChange(boolean selfChange) {
3046 onAppWidgetReset();
3047 }
3048 }
3049
3050 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003051 * If the activity is currently paused, signal that we need to re-run the loader
3052 * in onResume.
3053 *
3054 * This needs to be called from incoming places where resources might have been loaded
3055 * while we are paused. That is becaues the Configuration might be wrong
3056 * when we're not running, and if it comes back to what it was when we
3057 * were paused, we are not restarted.
3058 *
3059 * Implementation of the method from LauncherModel.Callbacks.
3060 *
3061 * @return true if we are currently paused. The caller might be able to
3062 * skip some work in that case since we will come back again.
3063 */
3064 public boolean setLoadOnResume() {
3065 if (mPaused) {
3066 Log.i(TAG, "setLoadOnResume");
3067 mOnResumeNeedsLoad = true;
3068 return true;
3069 } else {
3070 return false;
3071 }
3072 }
3073
3074 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003076 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003078 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003079 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003080 } else {
3081 return SCREEN_COUNT / 2;
3082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003084
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003085 void setAllAppsPagedView(PagedView view) {
3086 mAllAppsPagedView = view;
3087 }
3088
Joe Onorato9c1289c2009-08-17 11:03:03 -04003089 /**
3090 * Refreshes the shortcuts shown on the workspace.
3091 *
3092 * Implementation of the method from LauncherModel.Callbacks.
3093 */
3094 public void startBinding() {
3095 final Workspace workspace = mWorkspace;
3096 int count = workspace.getChildCount();
3097 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003098 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04003099 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
3100 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003101
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 if (DEBUG_USER_INTERFACE) {
3103 android.widget.Button finishButton = new android.widget.Button(this);
3104 finishButton.setText("Finish");
3105 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3106
3107 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3108 public void onClick(View v) {
3109 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003112 }
3113 }
3114
3115 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 * Bind the items start-end from the list.
3117 *
3118 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003119 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3121
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003122 setLoadOnResume();
3123
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 final Workspace workspace = mWorkspace;
3125
3126 for (int i=start; i<end; i++) {
3127 final ItemInfo item = shortcuts.get(i);
3128 mDesktopItems.add(item);
3129 switch (item.itemType) {
3130 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3131 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003132 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003133 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3134 false);
3135 break;
3136 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3137 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003138 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003139 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003140 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3141 false);
3142 break;
3143 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3144 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3145 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003146 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 (LiveFolderInfo) item);
3148 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3149 false);
3150 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 }
3153
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 }
3156
Joe Onorato9c1289c2009-08-17 11:03:03 -04003157 /**
3158 * Implementation of the method from LauncherModel.Callbacks.
3159 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003160 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003161 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003162 sFolders.clear();
3163 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003164 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003165
3166 /**
3167 * Add the views for a widget to the workspace.
3168 *
3169 * Implementation of the method from LauncherModel.Callbacks.
3170 */
3171 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003172 setLoadOnResume();
3173
Daniel Sandler843e8602010-06-07 14:59:01 -04003174 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3175 if (DEBUG_WIDGETS) {
3176 Log.d(TAG, "bindAppWidget: " + item);
3177 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 final Workspace workspace = mWorkspace;
3179
3180 final int appWidgetId = item.appWidgetId;
3181 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003182 if (DEBUG_WIDGETS) {
3183 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3184 }
3185
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3187
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3189 item.hostView.setTag(item);
3190
3191 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3192 item.cellY, item.spanX, item.spanY, false);
3193
Adam Cohended9f8d2010-11-03 13:25:16 -07003194 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3195
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 workspace.requestLayout();
3197
3198 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003199
3200 if (DEBUG_WIDGETS) {
3201 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3202 + (SystemClock.uptimeMillis()-start) + "ms");
3203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003204 }
3205
3206 /**
3207 * Callback saying that there aren't any more items to bind.
3208 *
3209 * Implementation of the method from LauncherModel.Callbacks.
3210 */
3211 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003212 setLoadOnResume();
3213
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 if (mSavedState != null) {
3215 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003216 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003217 }
3218
3219 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3220 if (userFolders != null) {
3221 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003222 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 if (info != null) {
3224 openFolder(info);
3225 }
3226 }
3227 final Folder openFolder = mWorkspace.getOpenFolder();
3228 if (openFolder != null) {
3229 openFolder.requestFocus();
3230 }
3231 }
3232
Joe Onorato9c1289c2009-08-17 11:03:03 -04003233 mSavedState = null;
3234 }
3235
3236 if (mSavedInstanceState != null) {
3237 super.onRestoreInstanceState(mSavedInstanceState);
3238 mSavedInstanceState = null;
3239 }
3240
Joe Onorato9c1289c2009-08-17 11:03:03 -04003241 mWorkspaceLoading = false;
3242 }
3243
3244 /**
3245 * Add the icons for all apps.
3246 *
3247 * Implementation of the method from LauncherModel.Callbacks.
3248 */
3249 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003250 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003251 if (mCustomizePagedView != null) {
3252 mCustomizePagedView.setApps(apps);
3253 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003254 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003255 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 }
3257
3258 /**
3259 * A package was installed.
3260 *
3261 * Implementation of the method from LauncherModel.Callbacks.
3262 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003263 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003264 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003265 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003266 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003267 if (mCustomizePagedView != null) {
3268 mCustomizePagedView.addApps(apps);
3269 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003270 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003271 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 }
3273
3274 /**
3275 * A package was updated.
3276 *
3277 * Implementation of the method from LauncherModel.Callbacks.
3278 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003279 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003280 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05003282 mWorkspace.updateShortcuts(apps);
3283 mAllAppsGrid.updateApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003284 if (mCustomizePagedView != null) {
3285 mCustomizePagedView.updateApps(apps);
3286 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003287 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003288 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289 }
3290
3291 /**
3292 * A package was uninstalled.
3293 *
3294 * Implementation of the method from LauncherModel.Callbacks.
3295 */
Joe Onorato36115782010-06-17 13:28:48 -04003296 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003297 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003298 if (permanent) {
3299 mWorkspace.removeItems(apps);
3300 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003301 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003302 if (mCustomizePagedView != null) {
3303 mCustomizePagedView.removeApps(apps);
3304 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003305 updateAppMarketIcon();
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003306 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 }
Joe Onoratobe386092009-11-17 17:32:16 -08003308
3309 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003310 * A number of packages were updated.
3311 */
3312 public void bindPackagesUpdated() {
3313 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003314 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003315 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003316 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003317 }
3318
3319 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003320 * Prints out out state for debugging.
3321 */
3322 public void dumpState() {
3323 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003324 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003325 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3326 Log.d(TAG, "mRestoring=" + mRestoring);
3327 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3328 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3329 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003330 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003331 mModel.dumpState();
3332 mAllAppsGrid.dumpState();
3333 Log.d(TAG, "END launcher2 dump state");
3334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003335}