blob: 855724117e7098bbe929878e072a1b7c31fff3ab [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Joe Onoratoa5902522009-07-30 13:37:37 -070017package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Michael Jurka0e260592010-06-30 17:07:39 -070019import com.android.common.Search;
20import com.android.launcher.R;
Bjorn Bringert3e244cf2010-02-10 14:17:35 +000021
Patrick Dubroy7247f632010-08-04 16:02:59 -070022import android.animation.Animatable;
23import android.animation.AnimatableListenerAdapter;
24import android.animation.Animator;
25import android.animation.PropertyAnimator;
26import android.animation.Sequencer;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Dianne Hackborn107f8392009-08-05 21:32:16 -070032import android.app.WallpaperManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Michael Jurka2c3af5f2010-08-03 13:53:20 -070042import android.content.Intent.ShortcutIconResource;
Patrick Dubroy7247f632010-08-04 16:02:59 -070043import android.content.IntentFilter;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040046import android.content.pm.ResolveInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040049import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080052import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080054import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070062import android.provider.LiveFolders;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
65import android.text.TextUtils;
66import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.View;
Patrick Dubroy7247f632010-08-04 16:02:59 -070076import android.view.View.OnLongClickListener;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070078import android.view.WindowManager;
Michael Jurka0e260592010-06-30 17:07:39 -070079import android.view.animation.Animation;
Michael Jurkaaf442092010-06-10 17:01:57 -070080import android.view.animation.AnimationUtils;
Patrick Dubroy7247f632010-08-04 16:02:59 -070081import android.view.animation.DecelerateInterpolator;
82import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070085import android.widget.ImageView;
86import android.widget.LinearLayout;
87import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070088import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.widget.TextView;
90import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091
Michael Jurka0e260592010-06-30 17:07:39 -070092import java.io.DataInputStream;
93import java.io.DataOutputStream;
94import java.io.FileNotFoundException;
95import java.io.IOException;
96import java.util.ArrayList;
97import java.util.HashMap;
98import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080099
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800100/**
101 * Default launcher application.
102 */
Michael Jurka946ad472010-07-09 18:05:18 -0700103public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700104 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
105 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800106 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700107 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800108
Joe Onorato9c1289c2009-08-17 11:03:03 -0400109 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400110 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700112
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113 private static final int WALLPAPER_SCREENS_SPAN = 2;
114
115 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800116 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
117
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118 private static final int MENU_ADD = Menu.FIRST + 1;
119 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
120 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
121 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700122 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 private static final int REQUEST_CREATE_SHORTCUT = 1;
125 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int REQUEST_PICK_APPLICATION = 6;
128 private static final int REQUEST_PICK_SHORTCUT = 7;
129 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700131 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
134
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800135 static final int SCREEN_COUNT = 5;
136 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Joe Onorato7c312c12009-08-13 21:36:53 -0700138 static final int DIALOG_CREATE_SHORTCUT = 1;
139 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 // Type: int
144 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
145 // Type: boolean
146 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
147 // Type: long
148 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
149 // Type: int
150 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
157 // Type: int
158 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
159 // Type: int
160 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
161 // Type: int
162 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
163 // Type: int[]
164 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
165 // Type: boolean
166 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
167 // Type: long
168 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
169
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800173 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800175 private final BroadcastReceiver mCloseSystemDialogsReceiver
176 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800177 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private LayoutInflater mInflater;
180
Joe Onorato00acb122009-08-04 16:04:30 -0400181 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700183
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700184 private AppWidgetManager mAppWidgetManager;
185 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private CellLayout.CellInfo mAddItemCellInfo;
188 private CellLayout.CellInfo mMenuAddInfo;
189 private final int[] mCellCoordinates = new int[2];
190 private FolderInfo mFolderInfo;
191
Joe Onorato7c312c12009-08-13 21:36:53 -0700192 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700194 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700195 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private Bundle mSavedState;
198
199 private SpannableStringBuilder mDefaultKeySsb = null;
200
Joe Onorato9c1289c2009-08-17 11:03:03 -0400201 private boolean mWorkspaceLoading = true;
202
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800203 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private boolean mRestoring;
205 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 private Bundle mSavedInstanceState;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211
Romain Guy84f296c2009-11-04 15:00:44 -0800212 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
213 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700214
Romain Guy1fbc1c82009-11-09 20:43:08 -0800215 private ImageView mPreviousView;
216 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500217
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400218 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400219 private static final int NUM_HOTSEATS = 2;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400220 private String[] mHotseatConfig = null;
221 private Intent[] mHotseats = null;
222 private Drawable[] mHotseatIcons = null;
223 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400224
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800225 @Override
226 protected void onCreate(Bundle savedInstanceState) {
227 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700228
Winson Chungaafa03c2010-06-11 17:34:16 -0700229 if (LauncherApplication.isInPlaceRotationEnabled()) {
230 // hide the status bar (temporary until we get the status bar design figured out)
231 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
232 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
233 }
234
Joe Onorato0589f0f2010-02-08 13:44:00 -0800235 LauncherApplication app = ((LauncherApplication)getApplication());
236 mModel = app.setLauncher(this);
237 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400238 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700241 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700242 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
243 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 if (PROFILE_STARTUP) {
246 android.os.Debug.startMethodTracing("/sdcard/launcher");
247 }
248
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400249 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 checkForLocaleChange();
251 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700253 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
254 if (mHomeCustomizationDrawer != null) {
255 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700256
Michael Jurka946ad472010-07-09 18:05:18 -0700257 String widgetsLabel = getString(R.string.widgets_tab_label);
258 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
259 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
260 String foldersLabel = getString(R.string.folders_tab_label);
261 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
262 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
263 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
264 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
265 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
266 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
267 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
268 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
269
270 mHomeCustomizationDrawer.setCurrentTab(0);
271 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setupViews();
273
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800274 registerContentObservers();
275
Joe Onorato7c312c12009-08-13 21:36:53 -0700276 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 mSavedState = savedInstanceState;
279 restoreState(mSavedState);
280
281 if (PROFILE_STARTUP) {
282 android.os.Debug.stopMethodTracing();
283 }
284
285 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400286 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 }
288
289 // For handling default keys
290 mDefaultKeySsb = new SpannableStringBuilder();
291 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292
293 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
294 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 }
Romain Guycbb89e42009-06-08 15:52:54 -0700296
Winson Chungaafa03c2010-06-11 17:34:16 -0700297 @Override
298 public void onConfigurationChanged(Configuration newConfig) {
299 // TODO Auto-generated method stub
300 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700301 }
302
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700305 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
306 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700307
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 final Configuration configuration = getResources().getConfiguration();
309
Romain Guy98d01652009-06-30 16:21:04 -0700310 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 final String locale = configuration.locale.toString();
312
Romain Guy98d01652009-06-30 16:21:04 -0700313 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 final int mcc = configuration.mcc;
315
Romain Guy98d01652009-06-30 16:21:04 -0700316 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 final int mnc = configuration.mnc;
318
Romain Guy84f296c2009-11-04 15:00:44 -0800319 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320
Romain Guy84f296c2009-11-04 15:00:44 -0800321 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700322 localeConfiguration.locale = locale;
323 localeConfiguration.mcc = mcc;
324 localeConfiguration.mnc = mnc;
325
326 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800327 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400328
329 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700330 }
331 }
332
333 private static class LocaleConfiguration {
334 public String locale;
335 public int mcc = -1;
336 public int mnc = -1;
337 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700338
Romain Guy98d01652009-06-30 16:21:04 -0700339 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
340 DataInputStream in = null;
341 try {
342 in = new DataInputStream(context.openFileInput(PREFERENCES));
343 configuration.locale = in.readUTF();
344 configuration.mcc = in.readInt();
345 configuration.mnc = in.readInt();
346 } catch (FileNotFoundException e) {
347 // Ignore
348 } catch (IOException e) {
349 // Ignore
350 } finally {
351 if (in != null) {
352 try {
353 in.close();
354 } catch (IOException e) {
355 // Ignore
356 }
357 }
358 }
359 }
360
361 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
362 DataOutputStream out = null;
363 try {
364 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
365 out.writeUTF(configuration.locale);
366 out.writeInt(configuration.mcc);
367 out.writeInt(configuration.mnc);
368 out.flush();
369 } catch (FileNotFoundException e) {
370 // Ignore
371 } catch (IOException e) {
372 //noinspection ResultOfMethodCallIgnored
373 context.getFileStreamPath(PREFERENCES).delete();
374 } finally {
375 if (out != null) {
376 try {
377 out.close();
378 } catch (IOException e) {
379 // Ignore
380 }
381 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 }
383 }
384
385 static int getScreen() {
386 synchronized (sLock) {
387 return sScreen;
388 }
389 }
390
391 static void setScreen(int screen) {
392 synchronized (sLock) {
393 sScreen = screen;
394 }
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700398 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399
400 Display display = getWindowManager().getDefaultDisplay();
401 boolean isPortrait = display.getWidth() < display.getHeight();
402
403 final int width = isPortrait ? display.getWidth() : display.getHeight();
404 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700405 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800406 }
407
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400408 // Note: This doesn't do all the client-id magic that BrowserProvider does
409 // in Browser. (http://b/2425179)
410 private Uri getDefaultBrowserUri() {
411 String url = getString(R.string.default_browser_url);
412 if (url.indexOf("{CID}") != -1) {
413 url = url.replace("{CID}", "android-google");
414 }
415 return Uri.parse(url);
416 }
417
418 // Load the Intent templates from arrays.xml to populate the hotseats. For
419 // each Intent, if it resolves to a single app, use that as the launch
420 // intent & use that app's label as the contentDescription. Otherwise,
421 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400422 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400423 if (mHotseatConfig == null) {
424 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
425 if (mHotseatConfig.length > 0) {
426 mHotseats = new Intent[mHotseatConfig.length];
427 mHotseatLabels = new CharSequence[mHotseatConfig.length];
428 mHotseatIcons = new Drawable[mHotseatConfig.length];
429 } else {
430 mHotseats = null;
431 mHotseatIcons = null;
432 mHotseatLabels = null;
433 }
434
435 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
436 for (int i=0; i<mHotseatConfig.length; i++) {
437 // load icon for this slot; currently unrelated to the actual activity
438 try {
439 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
440 } catch (ArrayIndexOutOfBoundsException ex) {
441 Log.w(TAG, "Missing hotseat_icons array item #" + i);
442 mHotseatIcons[i] = null;
443 }
444 }
445 hotseatIconDrawables.recycle();
446 }
447
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400448 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400449 for (int i=0; i<mHotseatConfig.length; i++) {
450 Intent intent = null;
451 if (mHotseatConfig[i].equals("*BROWSER*")) {
452 // magic value meaning "launch user's default web browser"
453 // replace it with a generic web request so we can see if there is indeed a default
454 String defaultUri = getString(R.string.default_browser_url);
455 intent = new Intent(
456 Intent.ACTION_VIEW,
457 ((defaultUri != null)
458 ? Uri.parse(defaultUri)
459 : getDefaultBrowserUri())
460 ).addCategory(Intent.CATEGORY_BROWSABLE);
461 // note: if the user launches this without a default set, she
462 // will always be taken to the default URL above; this is
463 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700464 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 // URL is unavoidably sent to the chosen app.
466 } else {
467 try {
468 intent = Intent.parseUri(mHotseatConfig[i], 0);
469 } catch (java.net.URISyntaxException ex) {
470 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
471 // bogus; leave intent=null
472 }
473 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700474
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400475 if (intent == null) {
476 mHotseats[i] = null;
477 mHotseatLabels[i] = getText(R.string.activity_not_found);
478 continue;
479 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400480
481 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700482 Log.d(TAG, "loadHotseats: hotseat " + i
483 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400484 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400485 + "]");
486 }
487
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400488 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
489 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700490 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 Log.d(TAG, "Best match for intent: " + bestMatch);
492 Log.d(TAG, "All matches: ");
493 for (ResolveInfo ri : allMatches) {
494 Log.d(TAG, " --> " + ri);
495 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400496 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400497 // did this resolve to a single app, or the resolver?
498 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700499 // can't find any activity to handle this. let's leave the
500 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400501 // to launch.
502 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400503
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400504 // set accessibility text to "Not installed"
505 mHotseatLabels[i] = getText(R.string.activity_not_found);
506 } else {
507 boolean found = false;
508 for (ResolveInfo ri : allMatches) {
509 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
510 && bestMatch.activityInfo.applicationInfo.packageName
511 .equals(ri.activityInfo.applicationInfo.packageName)) {
512 found = true;
513 break;
514 }
515 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700516
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (!found) {
518 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
519 // the bestMatch is probably the ResolveActivity, meaning the
520 // user has not yet selected a default
521 // so: we'll keep the original intent for now
522 mHotseats[i] = intent;
523
524 // set the accessibility text to "Select shortcut"
525 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
526 } else {
527 // we have an app!
528 // now reconstruct the intent to launch it through the front
529 // door
530 ComponentName com = new ComponentName(
531 bestMatch.activityInfo.applicationInfo.packageName,
532 bestMatch.activityInfo.name);
533 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
534
535 // load the app label for accessibility
536 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
537 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400538 }
539
540 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 Log.d(TAG, "loadHotseats: hotseat " + i
542 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 + ((mHotseats[i] == null)
544 ? "null"
545 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546 + "] label=[" + mHotseatLabels[i]
547 + "]"
548 );
549 }
550 }
551 }
552
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 @Override
554 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700555 mWaitingForResult = false;
556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 // The pattern used here is that a user PICKs a specific application,
558 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
561 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700562
Romain Guy94dabf12009-07-21 10:55:43 -0700563 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 switch (requestCode) {
565 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400566 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 break;
568 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800569 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 break;
571 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400572 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800573 break;
574 case REQUEST_PICK_LIVE_FOLDER:
575 addLiveFolder(data);
576 break;
577 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400578 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700580 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700581 addAppWidgetFromPick(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800582 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583 case REQUEST_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700584 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700585 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800586 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700587 case REQUEST_PICK_WALLPAPER:
588 // We just wanted the activity result here so we can clear mWaitingForResult
589 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
Romain Guy18042c82009-11-06 11:44:55 -0800591 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
592 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
593 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700594 // Clean up the appWidgetId if we canceled
595 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
596 if (appWidgetId != -1) {
597 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 }
601
602 @Override
603 protected void onResume() {
604 super.onResume();
605
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800606 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800607
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800608 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400609 mWorkspaceLoading = true;
610 mModel.startLoader(this, true);
611 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 }
613 }
614
615 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700616 protected void onPause() {
617 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700618 // Some launcher layouts don't have a previous and next view
619 if (mPreviousView != null) {
620 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700621 }
622 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700623 dismissPreview(mNextView);
624 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800625 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700626 }
Romain Guycbb89e42009-06-08 15:52:54 -0700627
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700628 @Override
629 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400630 // Flag the loader to stop early before switching
631 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800632 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800633 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700634 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700635
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800636 // We can't hide the IME if it was forced open. So don't bother
637 /*
638 @Override
639 public void onWindowFocusChanged(boolean hasFocus) {
640 super.onWindowFocusChanged(hasFocus);
641
642 if (hasFocus) {
643 final InputMethodManager inputManager = (InputMethodManager)
644 getSystemService(Context.INPUT_METHOD_SERVICE);
645 WindowManager.LayoutParams lp = getWindow().getAttributes();
646 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
647 android.os.Handler()) {
648 protected void onReceiveResult(int resultCode, Bundle resultData) {
649 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
650 }
651 });
652 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
653 }
654 }
655 */
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 private boolean acceptFilter() {
658 final InputMethodManager inputManager = (InputMethodManager)
659 getSystemService(Context.INPUT_METHOD_SERVICE);
660 return !inputManager.isFullscreenMode();
661 }
662
663 @Override
664 public boolean onKeyDown(int keyCode, KeyEvent event) {
665 boolean handled = super.onKeyDown(keyCode, event);
666 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
667 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
668 keyCode, event);
669 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700670 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700671 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700672 // showSearchDialog()
673 // If there are multiple keystrokes before the search dialog takes focus,
674 // onSearchRequested() will be called for every keystroke,
675 // but it is idempotent, so it's fine.
676 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 }
678 }
679
Joe Onorato8a9625e2010-01-28 15:55:35 -0800680 // Eat the long press event so the keyboard doesn't come up.
681 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
682 return true;
683 }
684
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800685 return handled;
686 }
687
Karl Rosaen138a0412009-04-23 19:00:21 -0700688 private String getTypedText() {
689 return mDefaultKeySsb.toString();
690 }
691
692 private void clearTypedText() {
693 mDefaultKeySsb.clear();
694 mDefaultKeySsb.clearSpans();
695 Selection.setSelection(mDefaultKeySsb, 0);
696 }
697
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 /**
699 * Restores the previous state, if it exists.
700 *
701 * @param savedState The previous state.
702 */
703 private void restoreState(Bundle savedState) {
704 if (savedState == null) {
705 return;
706 }
707
Joe Onorato3a8820b2009-11-10 15:06:42 -0800708 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
709 if (allApps) {
710 showAllApps(false);
711 }
712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
714 if (currentScreen > -1) {
715 mWorkspace.setCurrentScreen(currentScreen);
716 }
717
718 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
719 if (addScreen > -1) {
720 mAddItemCellInfo = new CellLayout.CellInfo();
721 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
722 addItemCellInfo.valid = true;
723 addItemCellInfo.screen = addScreen;
724 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
725 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
726 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
727 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
728 addItemCellInfo.findVacantCellsFromOccupied(
729 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
730 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
731 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
732 mRestoring = true;
733 }
734
735 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
736 if (renameFolder) {
737 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400738 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800739 mRestoring = true;
740 }
741 }
742
743 /**
744 * Finds all the views we need and configure them properly.
745 */
746 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400747 DragController dragController = mDragController;
748
Romain Guyb1b69f52009-08-10 15:10:15 -0700749 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400750 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800751
Joe Onorato7c312c12009-08-13 21:36:53 -0700752 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700753 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700754 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800755 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Mike Cleronb6082fa02009-10-19 17:03:36 -0700756 // Manage focusability manually since this thing is always visible
Winson Chungaafa03c2010-06-11 17:34:16 -0700757 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
760 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500761 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700762
Joe Onorato7c312c12009-08-13 21:36:53 -0700763 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
764 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700766 View handleView = findViewById(R.id.all_apps_button);
767 if (handleView != null && handleView instanceof HandleView) {
768 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700769 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700770 mHandleView.setLauncher(this);
771 mHandleView.setOnClickListener(this);
772 mHandleView.setOnLongClickListener(this);
773 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800774
Michael Jurka0e260592010-06-30 17:07:39 -0700775 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
776 if (widgetChooser != null) {
777 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
778 widgetChooser.setAdapter(widgetGalleryAdapter);
779 widgetChooser.setDragController(dragController);
780 widgetChooser.setLauncher(this);
781
782 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700783 IntentListAdapter folderTypes = new FolderListAdapter(
784 this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
Michael Jurka0e260592010-06-30 17:07:39 -0700785 folderChooser.setAdapter(folderTypes);
786 folderChooser.setLauncher(this);
787
788 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700789 IntentListAdapter shortcutTypes = new IntentListAdapter(
790 this, Intent.ACTION_CREATE_SHORTCUT);
Michael Jurka0e260592010-06-30 17:07:39 -0700791 shortcutChooser.setAdapter(shortcutTypes);
792 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700793 } else {
794 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
795 hotseatLeft.setContentDescription(mHotseatLabels[0]);
796 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
797 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
798 hotseatRight.setContentDescription(mHotseatLabels[1]);
799 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400800
Michael Jurkaaf442092010-06-10 17:01:57 -0700801 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
802 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800803
Michael Jurkaaf442092010-06-10 17:01:57 -0700804 Drawable previous = mPreviousView.getDrawable();
805 Drawable next = mNextView.getDrawable();
806 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807
Michael Jurkaaf442092010-06-10 17:01:57 -0700808 mPreviousView.setHapticFeedbackEnabled(false);
809 mPreviousView.setOnLongClickListener(this);
810 mNextView.setHapticFeedbackEnabled(false);
811 mNextView.setOnLongClickListener(this);
812 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400815 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817
818 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400819 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700820 int deleteZoneHandleId;
821 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700822 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700823 } else {
824 deleteZoneHandleId = R.id.all_apps_button_cluster;
825 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700826 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827
Joe Onorato00acb122009-08-04 16:04:30 -0400828 dragController.setDragScoller(workspace);
829 dragController.setDragListener(deleteZone);
830 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800831 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700832
Joe Onorato00acb122009-08-04 16:04:30 -0400833 // The order here is bottom to top.
834 dragController.addDropTarget(workspace);
835 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800836 }
837
Romain Guy8a73c512009-11-09 19:19:59 -0800838 @SuppressWarnings({"UnusedDeclaration"})
839 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800840 if (!isAllAppsVisible()) {
841 mWorkspace.scrollLeft();
842 }
Romain Guy8a73c512009-11-09 19:19:59 -0800843 }
844
845 @SuppressWarnings({"UnusedDeclaration"})
846 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800847 if (!isAllAppsVisible()) {
848 mWorkspace.scrollRight();
849 }
Romain Guy8a73c512009-11-09 19:19:59 -0800850 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400851
852 @SuppressWarnings({"UnusedDeclaration"})
853 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400854 if (isAllAppsVisible()) return;
855
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400856 int index = -1;
857 if (v.getId() == R.id.hotseat_left) {
858 index = 0;
859 } else if (v.getId() == R.id.hotseat_right) {
860 index = 1;
861 }
862
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400863 // reload these every tap; you never know when they might change
864 loadHotseats();
865 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
866 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400867 startActivitySafely(
868 mHotseats[index],
869 "hotseat"
870 );
871 }
872 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700873
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800874 /**
875 * Creates a view representing a shortcut.
876 *
877 * @param info The data structure describing the shortcut.
878 *
879 * @return A View inflated from R.layout.application.
880 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800881 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 return createShortcut(R.layout.application,
883 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
884 }
885
886 /**
887 * Creates a view representing a shortcut inflated from the specified resource.
888 *
889 * @param layoutResId The id of the XML layout used to create the shortcut.
890 * @param parent The group the shortcut belongs to.
891 * @param info The data structure describing the shortcut.
892 *
893 * @return A View inflated from layoutResId.
894 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800895 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
897
Joe Onorato0589f0f2010-02-08 13:44:00 -0800898 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
899 new FastBitmapDrawable(info.getIcon(mIconCache)),
900 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 favorite.setText(info.title);
902 favorite.setTag(info);
903 favorite.setOnClickListener(this);
904
905 return favorite;
906 }
907
908 /**
909 * Add an application shortcut to the workspace.
910 *
911 * @param data The intent describing the application.
912 * @param cellInfo The position on screen where to create the shortcut.
913 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400914 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 cellInfo.screen = mWorkspace.getCurrentScreen();
916 if (!findSingleSlot(cellInfo)) return;
917
Joe Onorato0589f0f2010-02-08 13:44:00 -0800918 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
919 data, context);
920
Romain Guy73b979d2009-06-09 12:57:21 -0700921 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800924 info.container = ItemInfo.NO_ID;
925 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
926 } else {
927 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 }
929 }
Romain Guycbb89e42009-06-08 15:52:54 -0700930
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 /**
932 * Add a shortcut to the workspace.
933 *
934 * @param data The intent describing the shortcut.
935 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400937 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800938 cellInfo.screen = mWorkspace.getCurrentScreen();
939 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700940
Joe Onorato0589f0f2010-02-08 13:44:00 -0800941 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942
943 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400945 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
946 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
948 }
949
Romain Guycbb89e42009-06-08 15:52:54 -0700950
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700952 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700954 * @param data The intent describing the appWidgetId.
955 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700957 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700959
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700962 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 // Try finding open space on Launcher screen
965 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800966 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
967 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
968 return;
969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700971 // Build Launcher-specific widget info and save to database
972 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 launcherInfo.spanX = spans[0];
974 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 LauncherModel.addItemToDatabase(this, launcherInfo,
977 LauncherSettings.Favorites.CONTAINER_DESKTOP,
978 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
979
980 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400981 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700982
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700984 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700985
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700988
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400990 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
995 mDesktopItems.remove(launcherInfo);
996 launcherInfo.hostView = null;
997 }
998
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999 public LauncherAppWidgetHost getAppWidgetHost() {
1000 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
Romain Guycbb89e42009-06-08 15:52:54 -07001002
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001003 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001004 getWindow().closeAllPanels();
1005
1006 try {
1007 dismissDialog(DIALOG_CREATE_SHORTCUT);
1008 // Unlock the workspace if the dialog was showing
1009 } catch (Exception e) {
1010 // An exception is thrown if the dialog is not visible, which is fine
1011 }
1012
1013 try {
1014 dismissDialog(DIALOG_RENAME_FOLDER);
1015 // Unlock the workspace if the dialog was showing
1016 } catch (Exception e) {
1017 // An exception is thrown if the dialog is not visible, which is fine
1018 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001019
1020 // Whatever we were doing is hereby canceled.
1021 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001022 }
1023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 @Override
1025 protected void onNewIntent(Intent intent) {
1026 super.onNewIntent(intent);
1027
1028 // Close the menu
1029 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001030 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001031 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001032
Joe Onorato14f122b2009-11-19 14:06:36 -08001033 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1034 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1035 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001036
1037 // TODO: Figure out the right thing to do in XLarge mode here
Joe Onorato3a8820b2009-11-10 15:06:42 -08001038 if (!mWorkspace.isDefaultScreenShowing()) {
Joe Onorato14f122b2009-11-19 14:06:36 -08001039 mWorkspace.moveToDefaultScreen(alreadyOnHome && !allAppsVisible);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001040 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001041 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001042 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001043 mWorkspace.unshrink();
Romain Guy1dd3a072009-07-16 13:21:01 -07001044
Joe Onorato3a8820b2009-11-10 15:06:42 -08001045 final View v = getWindow().peekDecorView();
1046 if (v != null && v.getWindowToken() != null) {
1047 InputMethodManager imm = (InputMethodManager)getSystemService(
1048 INPUT_METHOD_SERVICE);
1049 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 }
1051 }
1052 }
1053
1054 @Override
1055 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1056 // Do not call super here
1057 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001058
1059 if (mHomeCustomizationDrawer != null) {
1060 String cur = savedInstanceState.getString("currentTab");
1061 if (cur != null) {
1062 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1063 }
1064 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066
1067 @Override
1068 protected void onSaveInstanceState(Bundle outState) {
1069 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1070
1071 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1072 if (folders.size() > 0) {
1073 final int count = folders.size();
1074 long[] ids = new long[count];
1075 for (int i = 0; i < count; i++) {
1076 final FolderInfo info = folders.get(i).getInfo();
1077 ids[i] = info.id;
1078 }
1079 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1080 } else {
1081 super.onSaveInstanceState(outState);
1082 }
1083
Joe Onoratofb0ca672009-09-14 17:55:46 -04001084 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001085 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001086 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001087 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001088
1089 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1090 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1091 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1092
1093 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1094 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1095 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1096 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1097 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1098 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1099 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1100 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001101 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
1103
1104 if (mFolderInfo != null && mWaitingForResult) {
1105 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1106 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1107 }
Michael Jurka946ad472010-07-09 18:05:18 -07001108
1109 if (mHomeCustomizationDrawer != null) {
1110 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1111 if (currentTabTag != null) {
1112 outState.putString("currentTab", currentTabTag);
1113 }
1114 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
1116
1117 @Override
1118 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001122 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001124 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126
1127 TextKeyListener.getInstance().release();
1128
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130
Joe Onorato9c1289c2009-08-17 11:03:03 -04001131 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001132
1133 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001134
Patrick Dubroyab962b72010-07-26 12:10:10 -07001135 // Some launcher layouts don't have a previous and next view
1136 if (mPreviousView != null) {
1137 dismissPreview(mPreviousView);
1138 }
1139 if (mNextView != null) {
1140 dismissPreview(mNextView);
1141 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001142
1143 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 }
1145
1146 @Override
1147 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001148 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 super.startActivityForResult(intent, requestCode);
1150 }
1151
1152 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001153 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001155
Joe Onorato7bb17492009-09-24 17:51:01 -07001156 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001157
Karl Rosaen138a0412009-04-23 19:00:21 -07001158 if (initialQuery == null) {
1159 // Use any text typed in the launcher as the initial query
1160 initialQuery = getTypedText();
1161 clearTypedText();
1162 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 if (appSearchData == null) {
1164 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001165 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001166 }
Romain Guycbb89e42009-06-08 15:52:54 -07001167
Karl Rosaen138a0412009-04-23 19:00:21 -07001168 final SearchManager searchManager =
1169 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001171 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 }
1173
1174 @Override
1175 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001176 if (isWorkspaceLocked()) {
1177 return false;
1178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179
1180 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1183 .setIcon(android.R.drawable.ic_menu_add)
1184 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001185 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 .setIcon(android.R.drawable.ic_menu_gallery)
1187 .setAlphabeticShortcut('W');
1188 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1189 .setIcon(android.R.drawable.ic_search_category_default)
1190 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1191 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1192 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1193 .setAlphabeticShortcut('N');
1194
1195 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001196 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1197 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198
1199 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1200 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1201 .setIntent(settings);
1202
1203 return true;
1204 }
1205
1206 @Override
1207 public boolean onPrepareOptionsMenu(Menu menu) {
1208 super.onPrepareOptionsMenu(menu);
1209
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001210 // If all apps is animating, don't show the menu, because we don't know
1211 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001212 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001213 return false;
1214 }
1215
1216 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001217 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1219 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1220
1221 // Disable add if the workspace is full.
1222 if (visible) {
1223 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1224 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226
1227 return true;
1228 }
1229
Michael Jurka0e260592010-06-30 17:07:39 -07001230 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1231 // using the settings menu to add an item, something similar happens in showAddDialog
1232 public void prepareAddItemFromHomeCustomizationDrawer() {
1233 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1234 mAddItemCellInfo = mMenuAddInfo;
1235 }
1236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 @Override
1238 public boolean onOptionsItemSelected(MenuItem item) {
1239 switch (item.getItemId()) {
1240 case MENU_ADD:
1241 addItems();
1242 return true;
1243 case MENU_WALLPAPER_SETTINGS:
1244 startWallpaper();
1245 return true;
1246 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001247 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 return true;
1249 case MENU_NOTIFICATIONS:
1250 showNotifications();
1251 return true;
1252 }
1253
1254 return super.onOptionsItemSelected(item);
1255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
Karl Rosaen138a0412009-04-23 19:00:21 -07001257 /**
1258 * Indicates that we want global search for this activity by setting the globalSearch
1259 * argument for {@link #startSearch} to true.
1260 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001262 @Override
1263 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001264 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001265 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001266 }
1267
Joe Onorato9c1289c2009-08-17 11:03:03 -04001268 public boolean isWorkspaceLocked() {
1269 return mWorkspaceLoading || mWaitingForResult;
1270 }
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 private void addItems() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001273 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001274 if (LauncherApplication.isScreenXLarge()) {
1275 // Animate the widget chooser up from the bottom of the screen
1276 if (!isCustomizationDrawerVisible()) {
1277 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001278 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001279 }
1280 } else {
1281 showAddDialog(mMenuAddInfo);
1282 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
Michael Jurkaaf442092010-06-10 17:01:57 -07001285 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1286 mAddItemCellInfo = cellInfo;
1287 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1288 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1289 addAppWidgetImpl(appWidgetId);
1290 }
1291
1292 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001293 // TODO: catch bad widget exception when sent
1294 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001295 // TODO: Is this log message meaningful?
1296 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1297 addAppWidgetImpl(appWidgetId);
1298 }
1299
1300 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001301 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302
Bjorn Bringert7984c942009-12-09 15:38:25 +00001303 if (appWidget.configure != null) {
1304 // Launch over to configure widget, if needed
1305 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1306 intent.setComponent(appWidget.configure);
1307 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1308
Romain Guy8e633c52010-03-04 12:51:36 -08001309 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001311 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001312 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 }
1314 }
Romain Guycbb89e42009-06-08 15:52:54 -07001315
Joe Onoratodeb98af2010-02-19 14:59:39 -08001316 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001317 // Handle case where user selected "Applications"
1318 String applicationName = getResources().getString(R.string.group_applications);
1319 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001320
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001321 if (applicationName != null && applicationName.equals(shortcutName)) {
1322 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1323 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001324
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001325 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1326 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001327 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001328 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001329 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001331 }
1332
1333 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001334 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001335 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001336 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001337
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001338 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001339 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001340 } else {
1341 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1342 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
1344
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 UserFolderInfo folderInfo = new UserFolderInfo();
1347 folderInfo.title = getText(R.string.folder_name);
1348
1349 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1350 cellInfo.screen = mWorkspace.getCurrentScreen();
1351 if (!findSingleSlot(cellInfo)) return;
1352
1353 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 LauncherModel.addItemToDatabase(this, folderInfo,
1355 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
1359 // Create the view
1360 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1361 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1362 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
Romain Guycbb89e42009-06-08 15:52:54 -07001365
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 void removeFolder(FolderInfo folder) {
1367 mFolders.remove(folder.id);
1368 }
1369
1370 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 cellInfo.screen = mWorkspace.getCurrentScreen();
1372 if (!findSingleSlot(cellInfo)) return;
1373
1374 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1375
1376 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1378 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001379 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1380 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 }
1382 }
1383
1384 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1385 CellLayout.CellInfo cellInfo, boolean notify) {
1386
1387 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1388 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1389
1390 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 Intent.ShortcutIconResource iconResource = null;
1392
1393 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1394 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1395 try {
1396 iconResource = (Intent.ShortcutIconResource) extra;
1397 final PackageManager packageManager = context.getPackageManager();
1398 Resources resources = packageManager.getResourcesForApplication(
1399 iconResource.packageName);
1400 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1401 icon = resources.getDrawable(id);
1402 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001403 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 }
1405 }
1406
1407 if (icon == null) {
1408 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1409 }
1410
1411 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001412 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 info.title = name;
1414 info.iconResource = iconResource;
1415 info.uri = data.getData();
1416 info.baseIntent = baseIntent;
1417 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1418 LiveFolders.DISPLAY_MODE_GRID);
1419
1420 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1421 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001422 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423
1424 return info;
1425 }
1426
1427 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1428 final int[] xy = new int[2];
1429 if (findSlot(cellInfo, xy, 1, 1)) {
1430 cellInfo.cellX = xy[0];
1431 cellInfo.cellY = xy[1];
1432 return true;
1433 }
1434 return false;
1435 }
1436
1437 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1438 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1439 boolean[] occupied = mSavedState != null ?
1440 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1441 cellInfo = mWorkspace.findAllVacantCells(occupied);
1442 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1443 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1444 return false;
1445 }
1446 }
1447 return true;
1448 }
1449
1450 private void showNotifications() {
1451 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1452 if (statusBar != null) {
1453 statusBar.expand();
1454 }
1455 }
1456
1457 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001458 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001460 Intent chooser = Intent.createChooser(pickWallpaper,
1461 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001462 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1463 // Removed in Eclair MR1
1464// WallpaperManager wm = (WallpaperManager)
1465// getSystemService(Context.WALLPAPER_SERVICE);
1466// WallpaperInfo wi = wm.getWallpaperInfo();
1467// if (wi != null && wi.getSettingsActivity() != null) {
1468// LabeledIntent li = new LabeledIntent(getPackageName(),
1469// R.string.configure_wallpaper, 0);
1470// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1471// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1472// }
Mike Clerona0618e42009-10-22 13:55:21 -07001473 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 }
1475
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001476 /**
1477 * Registers various content observers. The current implementation registers
1478 * only a favorites observer to keep track of the favorites applications.
1479 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001480 private void registerContentObservers() {
1481 ContentResolver resolver = getContentResolver();
1482 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1483 true, mWidgetObserver);
1484 }
1485
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 @Override
1487 public boolean dispatchKeyEvent(KeyEvent event) {
1488 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1489 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001491 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001492 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001493 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001494 dumpState();
1495 return true;
1496 }
1497 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001498 }
1499 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1500 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001501 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 return true;
1503 }
1504 }
1505
1506 return super.dispatchKeyEvent(event);
1507 }
1508
Joe Onorato88ec0992009-11-19 13:16:06 -08001509 @Override
1510 public void onBackPressed() {
1511 if (isAllAppsVisible()) {
1512 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001513 } else if (isCustomizationDrawerVisible()) {
1514 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001515 mWorkspace.unshrink();
Joe Onorato88ec0992009-11-19 13:16:06 -08001516 } else {
1517 closeFolder();
1518 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001519 // Some launcher layouts don't have a previous and next view
1520 if (mPreviousView != null) {
1521 dismissPreview(mPreviousView);
1522 dismissPreview(mNextView);
1523 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001524 }
1525
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 private void closeFolder() {
1527 Folder folder = mWorkspace.getOpenFolder();
1528 if (folder != null) {
1529 closeFolder(folder);
1530 }
1531 }
1532
1533 void closeFolder(Folder folder) {
1534 folder.getInfo().opened = false;
1535 ViewGroup parent = (ViewGroup) folder.getParent();
1536 if (parent != null) {
1537 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001538 if (folder instanceof DropTarget) {
1539 // Live folders aren't DropTargets.
1540 mDragController.removeDropTarget((DropTarget)folder);
1541 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 }
1543 folder.onClose();
1544 }
1545
1546 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001547 * Re-listen when widgets are reset.
1548 */
1549 private void onAppWidgetReset() {
1550 mAppWidgetHost.startListening();
1551 }
1552
1553 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1555 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001557 private void unbindDesktopItems() {
1558 for (ItemInfo item: mDesktopItems) {
1559 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 /**
1564 * Launches the intent referred by the clicked shortcut.
1565 *
1566 * @param v The view representing the clicked shortcut.
1567 */
1568 public void onClick(View v) {
1569 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001570 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001572 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001573 int[] pos = new int[2];
1574 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001575 intent.setSourceBounds(new Rect(pos[0], pos[1],
1576 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001577 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 } else if (tag instanceof FolderInfo) {
1579 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001580 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001581 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001582 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001583 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001584 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001585 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 }
1587 }
1588
Michael Jurka0e260592010-06-30 17:07:39 -07001589 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001590 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001591 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001592 hideCustomizationDrawer();
Michael Jurkadee05892010-07-27 10:01:56 -07001593 mWorkspace.unshrink();
Michael Jurka0e260592010-06-30 17:07:39 -07001594 return false;
1595 }
1596
Michael Jurkaaf442092010-06-10 17:01:57 -07001597 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001598 * Event handler for the search button
1599 *
1600 * @param v The view that was clicked.
1601 */
1602 public void onClickSearchButton(View v) {
1603 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1604 View button = findViewById(R.id.search_button);
1605 i.setSourceBounds(
1606 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1607 startActivity(i);
1608 }
1609
1610 /**
1611 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001612 * enters home screen customization mode.
1613 *
1614 * @param v The view that was clicked.
1615 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001616 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001617 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001618 }
1619
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001620 /**
1621 * Event handler for the "grid" button that appears on the home screen, which
1622 * enters all apps mode.
1623 *
1624 * @param v The view that was clicked.
1625 */
1626 public void onClickAllAppsButton(View v) {
1627 showAllApps(true);
1628 }
1629
Joe Onoratof984e852010-03-25 09:47:45 -07001630 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1632 try {
1633 startActivity(intent);
1634 } catch (ActivityNotFoundException e) {
1635 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001636 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 } catch (SecurityException e) {
1638 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001639 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001641 "or use the exported attribute for this activity. "
1642 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
1644 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001645
Romain Guy8e633c52010-03-04 12:51:36 -08001646 void startActivityForResultSafely(Intent intent, int requestCode) {
1647 try {
1648 startActivityForResult(intent, requestCode);
1649 } catch (ActivityNotFoundException e) {
1650 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1651 } catch (SecurityException e) {
1652 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1653 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1654 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1655 "or use the exported attribute for this activity.", e);
1656 }
1657 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001658
1659 private void handleFolderClick(FolderInfo folderInfo) {
1660 if (!folderInfo.opened) {
1661 // Close any open folder
1662 closeFolder();
1663 // Open the requested folder
1664 openFolder(folderInfo);
1665 } else {
1666 // Find the open folder...
1667 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1668 int folderScreen;
1669 if (openFolder != null) {
1670 folderScreen = mWorkspace.getScreenForView(openFolder);
1671 // .. and close it
1672 closeFolder(openFolder);
1673 if (folderScreen != mWorkspace.getCurrentScreen()) {
1674 // Close any folder open on the current screen
1675 closeFolder();
1676 // Pull the folder onto this screen
1677 openFolder(folderInfo);
1678 }
1679 }
1680 }
1681 }
1682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 /**
1684 * Opens the user fodler described by the specified tag. The opening of the folder
1685 * is animated relative to the specified View. If the View is null, no animation
1686 * is played.
1687 *
1688 * @param folderInfo The FolderInfo describing the folder to open.
1689 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001690 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001691 Folder openFolder;
1692
1693 if (folderInfo instanceof UserFolderInfo) {
1694 openFolder = UserFolder.fromXml(this);
1695 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001696 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001697 } else {
1698 return;
1699 }
1700
Joe Onorato00acb122009-08-04 16:04:30 -04001701 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 openFolder.setLauncher(this);
1703
1704 openFolder.bind(folderInfo);
1705 folderInfo.opened = true;
1706
Winson Chungaafa03c2010-06-11 17:34:16 -07001707 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1708
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 openFolder.onOpen();
1710 }
1711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001713 switch (v.getId()) {
1714 case R.id.previous_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001715 if (!isAllAppsVisible()) {
1716 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1717 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001718 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001719 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001720 return true;
1721 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001722 if (!isAllAppsVisible()) {
1723 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1724 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001725 showPreviews(v);
1726 }
1727 return true;
1728 case R.id.all_apps_button:
1729 if (!isAllAppsVisible()) {
1730 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1731 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1732 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001733 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001734 return true;
1735 }
1736
Joe Onorato9c1289c2009-08-17 11:03:03 -04001737 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 return false;
1739 }
1740
1741 if (!(v instanceof CellLayout)) {
1742 v = (View) v.getParent();
1743 }
1744
1745 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1746
1747 // This happens when long clicking an item with the dpad/trackball
1748 if (cellInfo == null) {
1749 return true;
1750 }
1751
1752 if (mWorkspace.allowLongPress()) {
1753 if (cellInfo.cell == null) {
1754 if (cellInfo.valid) {
1755 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001756 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001757 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1758 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 showAddDialog(cellInfo);
1760 }
1761 } else {
1762 if (!(cellInfo.cell instanceof Folder)) {
1763 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001764 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1765 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 mWorkspace.startDrag(cellInfo);
1767 }
1768 }
1769 }
1770 return true;
1771 }
1772
Romain Guye6b8e2f2009-11-10 11:56:55 -08001773 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001774 private void dismissPreview(final View v) {
1775 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001776 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001777 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1778 public void onDismiss() {
1779 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1780 int count = group.getChildCount();
1781 for (int i = 0; i < count; i++) {
1782 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1783 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001784 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1785 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1786
1787 v.setTag(R.id.workspace, null);
1788 v.setTag(R.id.icon, null);
1789 window.setOnDismissListener(null);
1790 }
1791 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001792 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001793 }
1794 v.setTag(null);
1795 }
1796
Romain Guyf8e6a802009-12-07 17:48:02 -08001797 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001798 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001799 }
1800
Romain Guya6abce82009-11-10 02:54:41 -08001801 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001802 final Resources resources = getResources();
1803 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001804
Romain Guya6abce82009-11-10 02:54:41 -08001805 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001806
Romain Guy9d31e9f2009-11-11 18:54:28 -08001807 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001808
Romain Guyf8e6a802009-12-07 17:48:02 -08001809 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001810 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001811 int extraW = (int) ((r.left + r.right) * max);
1812 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001813
1814 int aW = cell.getWidth() - extraW;
1815 float w = aW / max;
1816
1817 int width = cell.getWidth();
1818 int height = cell.getHeight();
1819 int x = cell.getLeftPadding();
1820 int y = cell.getTopPadding();
1821 width -= (x + cell.getRightPadding());
1822 height -= (y + cell.getBottomPadding());
1823
1824 float scale = w / width;
1825
1826 int count = end - start;
1827
1828 final float sWidth = width * scale;
1829 float sHeight = height * scale;
1830
Romain Guye6b8e2f2009-11-10 11:56:55 -08001831 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001832
Romain Guye6b8e2f2009-11-10 11:56:55 -08001833 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1834 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001835
1836 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001837 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001838 cell = (CellLayout) workspace.getChildAt(i);
1839
Romain Guyf8e6a802009-12-07 17:48:02 -08001840 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001841 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001842
1843 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001844 c.scale(scale, scale);
1845 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1846 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001847
Romain Guy9d31e9f2009-11-11 18:54:28 -08001848 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001849 image.setImageBitmap(bitmap);
1850 image.setTag(i);
1851 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001852 image.setOnFocusChangeListener(handler);
1853 image.setFocusable(true);
1854 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001855
1856 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001857 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1858
Winson Chungaafa03c2010-06-11 17:34:16 -07001859 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001860 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001861
1862 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001863 p.setContentView(preview);
1864 p.setWidth((int) (sWidth * count + extraW));
1865 p.setHeight((int) (sHeight + extraH));
1866 p.setAnimationStyle(R.style.AnimationPreview);
1867 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001868 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001869 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001870 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001871
Romain Guye6b8e2f2009-11-10 11:56:55 -08001872 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1873 public void onDismiss() {
1874 dismissPreview(anchor);
1875 }
1876 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001877
1878 anchor.setTag(p);
1879 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001880 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001881 }
1882
Romain Guy9d31e9f2009-11-11 18:54:28 -08001883 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001884 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001885
Romain Guye6b8e2f2009-11-10 11:56:55 -08001886 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001887 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001888 }
1889
1890 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001891 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001892 v.post(this);
1893 }
1894
1895 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001896 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001897 }
1898
1899 public void onFocusChange(View v, boolean hasFocus) {
1900 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001901 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001902 }
Romain Guya6abce82009-11-10 02:54:41 -08001903 }
1904 }
1905
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 Workspace getWorkspace() {
1907 return mWorkspace;
1908 }
1909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 @Override
1911 protected Dialog onCreateDialog(int id) {
1912 switch (id) {
1913 case DIALOG_CREATE_SHORTCUT:
1914 return new CreateShortcut().createDialog();
1915 case DIALOG_RENAME_FOLDER:
1916 return new RenameFolder().createDialog();
1917 }
1918
1919 return super.onCreateDialog(id);
1920 }
1921
1922 @Override
1923 protected void onPrepareDialog(int id, Dialog dialog) {
1924 switch (id) {
1925 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 break;
1927 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001928 if (mFolderInfo != null) {
1929 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1930 final CharSequence text = mFolderInfo.title;
1931 input.setText(text);
1932 input.setSelection(0, text.length());
1933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 break;
1935 }
1936 }
1937
1938 void showRenameDialog(FolderInfo info) {
1939 mFolderInfo = info;
1940 mWaitingForResult = true;
1941 showDialog(DIALOG_RENAME_FOLDER);
1942 }
1943
1944 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1945 mAddItemCellInfo = cellInfo;
1946 mWaitingForResult = true;
1947 showDialog(DIALOG_CREATE_SHORTCUT);
1948 }
1949
Joe Onoratodeb98af2010-02-19 14:59:39 -08001950 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001951 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001952 Bundle bundle = new Bundle();
1953
1954 ArrayList<String> shortcutNames = new ArrayList<String>();
1955 shortcutNames.add(getString(R.string.group_applications));
1956 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1957
1958 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1959 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1960 R.drawable.ic_launcher_application));
1961 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1962
1963 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1964 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001965 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001966 pickIntent.putExtras(bundle);
1967
Joe Onoratodeb98af2010-02-19 14:59:39 -08001968 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001969 }
1970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 private class RenameFolder {
1972 private EditText mInput;
1973
1974 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1976 mInput = (EditText) layout.findViewById(R.id.folder_name);
1977
1978 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1979 builder.setIcon(0);
1980 builder.setTitle(getString(R.string.rename_folder_title));
1981 builder.setCancelable(true);
1982 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1983 public void onCancel(DialogInterface dialog) {
1984 cleanup();
1985 }
1986 });
1987 builder.setNegativeButton(getString(R.string.cancel_action),
1988 new Dialog.OnClickListener() {
1989 public void onClick(DialogInterface dialog, int which) {
1990 cleanup();
1991 }
1992 }
1993 );
1994 builder.setPositiveButton(getString(R.string.rename_action),
1995 new Dialog.OnClickListener() {
1996 public void onClick(DialogInterface dialog, int which) {
1997 changeFolderName();
1998 }
1999 }
2000 );
2001 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002002
2003 final AlertDialog dialog = builder.create();
2004 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2005 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002006 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002007 mInput.requestFocus();
2008 InputMethodManager inputManager = (InputMethodManager)
2009 getSystemService(Context.INPUT_METHOD_SERVICE);
2010 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002011 }
2012 });
2013
2014 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
2016
2017 private void changeFolderName() {
2018 final String name = mInput.getText().toString();
2019 if (!TextUtils.isEmpty(name)) {
2020 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002021 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 mFolderInfo.title = name;
2023 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2024
Joe Onorato9c1289c2009-08-17 11:03:03 -04002025 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002026 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002027 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 } else {
2029 final FolderIcon folderIcon = (FolderIcon)
2030 mWorkspace.getViewForTag(mFolderInfo);
2031 if (folderIcon != null) {
2032 folderIcon.setText(name);
2033 getWorkspace().requestLayout();
2034 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002035 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002036 mWorkspaceLoading = true;
2037 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039 }
2040 }
2041 cleanup();
2042 }
2043
2044 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 dismissDialog(DIALOG_RENAME_FOLDER);
2046 mWaitingForResult = false;
2047 mFolderInfo = null;
2048 }
2049 }
2050
Daniel Sandler843e8602010-06-07 14:59:01 -04002051 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2052 public boolean isAllAppsVisible() {
2053 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002054 }
2055
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002056 // AllAppsView.Watcher
2057 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002058 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2059 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002060 mWorkspace.setVisibility(View.GONE);
2061 }
2062 }
2063
Joe Onorato3a8820b2009-11-10 15:06:42 -08002064 void showAllApps(boolean animated) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002065 hideCustomizationDrawer();
2066
Michael Jurka79212d82010-07-30 16:36:20 -07002067 if (LauncherApplication.isScreenXLarge()) {
2068 mWorkspace.shrinkToBottom(animated);
2069 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002070 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -07002071 final View allApps = (View)mAllAppsGrid;
2072
2073 final Resources res = getResources();
2074 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2075 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2076 final int height = allApps.getHeight();
2077
2078 // All apps should appear right at the end of the workspace shrink animation
2079 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2080
2081 Interpolator interp = new DecelerateInterpolator(2.0f);
2082
2083 allApps.setPivotX(allApps.getWidth() / 2.0f);
2084 allApps.setPivotY(height);
2085
2086 Animator scaleXAnim = new PropertyAnimator(duration, allApps, "scaleX", scale, 1.0f);
2087 scaleXAnim.setInterpolator(interp);
2088 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2089 public void onAnimationStart(Animatable animation) {
2090 // Not really a zoom -- this just makes the view visible
2091 mAllAppsGrid.zoom(1.0f, false);
2092 }
2093 });
2094
2095 Animator scaleYAnim = new PropertyAnimator(duration, allApps, "scaleY", scale, 1.0f);
2096 scaleYAnim.setInterpolator(interp);
2097
2098 // Translate down by 20% of the total height
2099 float oldY = (-allApps.getHeight() * 0.2f);
2100 Animator yAnim = new PropertyAnimator(duration, allApps, "y", oldY, 0.0f);
2101 yAnim.setInterpolator(interp);
2102
2103 Sequencer s = new Sequencer();
2104 s.playTogether(scaleXAnim, scaleYAnim, yAnim);
2105 s.play(scaleXAnim).after(startDelay);
2106 s.start();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002107 } else {
2108 mAllAppsGrid.zoom(1.0f, animated);
2109 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002110
Romain Guyc16fea72010-03-12 17:17:56 -08002111 ((View) mAllAppsGrid).setFocusable(true);
2112 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002113
Joe Onorato7c312c12009-08-13 21:36:53 -07002114 // TODO: fade these two too
2115 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002116 }
2117
Joe Onoratob2061212009-11-24 16:13:54 -05002118 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002119 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002120 * - Home from workspace
2121 * - from center screen
2122 * - from other screens
2123 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002124 * - from center screen
2125 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002126 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002127 * - from center screen
2128 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002129 * - Launch app from workspace and quit
2130 * - with back
2131 * - with home
2132 * - Launch app from all apps and quit
2133 * - with back
2134 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002135 * - Go to a screen that's not the default, then all
2136 * apps, and launch and app, and go back
2137 * - with back
2138 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002139 * - On workspace, long press power and go back
2140 * - with back
2141 * - with home
2142 * - On all apps, long press power and go back
2143 * - with back
2144 * - with home
2145 * - On workspace, power off
2146 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002147 * - Launch an app and turn off the screen while in that app
2148 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002149 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002150 * - From all apps
2151 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002152 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2153 * - From all apps
2154 * - From the center workspace
2155 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002156 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002157 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002158 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002159 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002160 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy7247f632010-08-04 16:02:59 -07002161 Resources res = getResources();
2162 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2163 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2164 View allApps = (View) mAllAppsGrid;
2165
2166 allApps.setPivotX(allApps.getWidth() / 2.0f);
2167 allApps.setPivotY(allApps.getHeight());
2168
2169 // Translate up by 20% of the total height
2170 float newY = allApps.getY() - allApps.getHeight() * 0.2f;
2171
2172 Sequencer seq = new Sequencer();
2173 seq.playTogether(
2174 new PropertyAnimator(duration, allApps, "scaleX", scaleFactor),
2175 new PropertyAnimator(duration, allApps, "scaleY", scaleFactor),
2176 new PropertyAnimator(duration, allApps, "y", newY));
2177 seq.addListener(new AnimatableListenerAdapter() {
2178 public void onAnimationEnd(Animatable animation) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002179 mAllAppsGrid.zoom(0.0f, false);
2180 }
2181 });
Patrick Dubroy7247f632010-08-04 16:02:59 -07002182 // Start the AllApps animation at the same time as the workspace unshrink
2183 seq.start();
Michael Jurka213d9632010-07-28 11:29:25 -07002184 mWorkspace.unshrink();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002185 } else {
2186 mAllAppsGrid.zoom(0.0f, animated);
2187 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002188 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002189 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002190 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002191 }
2192
Joe Onorato7c312c12009-08-13 21:36:53 -07002193 void lockAllApps() {
2194 // TODO
2195 }
2196
2197 void unlockAllApps() {
2198 // TODO
2199 }
2200
Patrick Dubroy558654c2010-07-23 16:48:11 -07002201 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002202 return mHomeCustomizationDrawer != null &&
2203 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002204 }
2205
2206 private void showCustomizationDrawer() {
2207 if (isAllAppsVisible()) {
2208 // TODO: Make a smoother transition here
2209 closeAllApps(false);
2210 }
2211 mHomeCustomizationDrawer.setVisibility(View.VISIBLE);
Michael Jurka54f7ac32010-08-02 13:56:46 -07002212 mHomeCustomizationDrawer.startAnimation(
2213 AnimationUtils.loadAnimation(this, R.anim.home_customization_drawer_slide_up));
Patrick Dubroy558654c2010-07-23 16:48:11 -07002214 }
2215
Michael Jurka213d9632010-07-28 11:29:25 -07002216 private void hideCustomizationDrawer() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002217 if (isCustomizationDrawerVisible()) {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002218 Animation slideDownAnimation = AnimationUtils.loadAnimation(
2219 this, R.anim.home_customization_drawer_slide_down);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002220 slideDownAnimation.setAnimationListener(new Animation.AnimationListener() {
2221 public void onAnimationEnd(Animation animation) {
2222 mHomeCustomizationDrawer.setVisibility(View.GONE);
2223 }
2224 public void onAnimationRepeat(Animation animation) {}
2225 public void onAnimationStart(Animation animation) {}
2226 });
2227 mHomeCustomizationDrawer.startAnimation(slideDownAnimation);
2228 }
2229 }
2230
Michael Jurka213d9632010-07-28 11:29:25 -07002231 void onWorkspaceUnshrink() {
2232 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002233 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002234 }
2235 if (isCustomizationDrawerVisible()) {
2236 hideCustomizationDrawer();
2237 }
2238 }
2239
Joe Onorato7404ee42009-07-31 11:54:44 -07002240 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 * Displays the shortcut creation dialog and launches, if necessary, the
2242 * appropriate activity.
2243 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002244 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002245 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2246 DialogInterface.OnShowListener {
2247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002253 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2254 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002255 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002256
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002257 builder.setInverseBackgroundForced(true);
2258
2259 AlertDialog dialog = builder.create();
2260 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002261 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002262 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002264 return dialog;
2265 }
2266
2267 public void onCancel(DialogInterface dialog) {
2268 mWaitingForResult = false;
2269 cleanup();
2270 }
2271
Romain Guycbb89e42009-06-08 15:52:54 -07002272 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002273 }
2274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002276 try {
2277 dismissDialog(DIALOG_CREATE_SHORTCUT);
2278 } catch (Exception e) {
2279 // An exception is thrown if the dialog is not visible, which is fine
2280 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 }
2282
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002283 /**
2284 * Handle the action clicked in the "Add to home" dialog.
2285 */
2286 public void onClick(DialogInterface dialog, int which) {
2287 Resources res = getResources();
2288 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002289
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002290 switch (which) {
2291 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002292 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002293 break;
2294 }
Romain Guycbb89e42009-06-08 15:52:54 -07002295
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002296 case AddAdapter.ITEM_APPWIDGET: {
2297 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002298
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002299 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2300 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002301 // start the pick activity
2302 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2303 break;
2304 }
Romain Guycbb89e42009-06-08 15:52:54 -07002305
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002306 case AddAdapter.ITEM_LIVE_FOLDER: {
2307 // Insert extra item to handle inserting folder
2308 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002309
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002310 ArrayList<String> shortcutNames = new ArrayList<String>();
2311 shortcutNames.add(res.getString(R.string.group_folder));
2312 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002313
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002314 ArrayList<ShortcutIconResource> shortcutIcons =
2315 new ArrayList<ShortcutIconResource>();
2316 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2317 R.drawable.ic_launcher_folder));
2318 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2319
2320 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2321 pickIntent.putExtra(Intent.EXTRA_INTENT,
2322 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2323 pickIntent.putExtra(Intent.EXTRA_TITLE,
2324 getText(R.string.title_select_live_folder));
2325 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002326
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002327 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2328 break;
2329 }
2330
2331 case AddAdapter.ITEM_WALLPAPER: {
2332 startWallpaper();
2333 break;
2334 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 }
2336 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002337
2338 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002339 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002341 }
2342
2343 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002344 * Receives notifications when applications are added/removed.
2345 */
2346 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2347 @Override
2348 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002349 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002350 String reason = intent.getStringExtra("reason");
2351 if (!"homekey".equals(reason)) {
2352 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002353 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002354 animate = false;
2355 }
Joe Onoratob2061212009-11-24 16:13:54 -05002356 closeAllApps(animate);
2357 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002358 }
2359 }
2360
2361 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002362 * Receives notifications whenever the appwidgets are reset.
2363 */
2364 private class AppWidgetResetObserver extends ContentObserver {
2365 public AppWidgetResetObserver() {
2366 super(new Handler());
2367 }
2368
2369 @Override
2370 public void onChange(boolean selfChange) {
2371 onAppWidgetReset();
2372 }
2373 }
2374
2375 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002376 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002378 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002379 if (mWorkspace != null) {
2380 return mWorkspace.getCurrentScreen();
2381 } else {
2382 return SCREEN_COUNT / 2;
2383 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002384 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002385
Joe Onorato9c1289c2009-08-17 11:03:03 -04002386 /**
2387 * Refreshes the shortcuts shown on the workspace.
2388 *
2389 * Implementation of the method from LauncherModel.Callbacks.
2390 */
2391 public void startBinding() {
2392 final Workspace workspace = mWorkspace;
2393 int count = workspace.getChildCount();
2394 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002395 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2397 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002398
Joe Onorato9c1289c2009-08-17 11:03:03 -04002399 if (DEBUG_USER_INTERFACE) {
2400 android.widget.Button finishButton = new android.widget.Button(this);
2401 finishButton.setText("Finish");
2402 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2403
2404 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2405 public void onClick(View v) {
2406 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002408 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409 }
2410 }
2411
2412 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002413 * Bind the items start-end from the list.
2414 *
2415 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002416 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002417 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2418
2419 final Workspace workspace = mWorkspace;
2420
2421 for (int i=start; i<end; i++) {
2422 final ItemInfo item = shortcuts.get(i);
2423 mDesktopItems.add(item);
2424 switch (item.itemType) {
2425 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2426 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002427 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002428 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2429 false);
2430 break;
2431 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2432 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2433 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2434 (UserFolderInfo) item);
2435 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2436 false);
2437 break;
2438 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2439 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2440 R.layout.live_folder_icon, this,
2441 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2442 (LiveFolderInfo) item);
2443 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2444 false);
2445 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002447 }
2448
Joe Onorato9c1289c2009-08-17 11:03:03 -04002449 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450 }
2451
Joe Onorato9c1289c2009-08-17 11:03:03 -04002452 /**
2453 * Implementation of the method from LauncherModel.Callbacks.
2454 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002455 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2456 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002457 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002459
2460 /**
2461 * Add the views for a widget to the workspace.
2462 *
2463 * Implementation of the method from LauncherModel.Callbacks.
2464 */
2465 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002466 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2467 if (DEBUG_WIDGETS) {
2468 Log.d(TAG, "bindAppWidget: " + item);
2469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002470 final Workspace workspace = mWorkspace;
2471
2472 final int appWidgetId = item.appWidgetId;
2473 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002474 if (DEBUG_WIDGETS) {
2475 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2476 }
2477
Joe Onorato9c1289c2009-08-17 11:03:03 -04002478 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2479
Joe Onorato9c1289c2009-08-17 11:03:03 -04002480 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2481 item.hostView.setTag(item);
2482
2483 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2484 item.cellY, item.spanX, item.spanY, false);
2485
2486 workspace.requestLayout();
2487
2488 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002489
2490 if (DEBUG_WIDGETS) {
2491 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2492 + (SystemClock.uptimeMillis()-start) + "ms");
2493 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002494 }
2495
2496 /**
2497 * Callback saying that there aren't any more items to bind.
2498 *
2499 * Implementation of the method from LauncherModel.Callbacks.
2500 */
2501 public void finishBindingItems() {
2502 if (mSavedState != null) {
2503 if (!mWorkspace.hasFocus()) {
2504 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2505 }
2506
2507 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2508 if (userFolders != null) {
2509 for (long folderId : userFolders) {
2510 final FolderInfo info = mFolders.get(folderId);
2511 if (info != null) {
2512 openFolder(info);
2513 }
2514 }
2515 final Folder openFolder = mWorkspace.getOpenFolder();
2516 if (openFolder != null) {
2517 openFolder.requestFocus();
2518 }
2519 }
2520
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521 mSavedState = null;
2522 }
2523
2524 if (mSavedInstanceState != null) {
2525 super.onRestoreInstanceState(mSavedInstanceState);
2526 mSavedInstanceState = null;
2527 }
2528
Joe Onorato9c1289c2009-08-17 11:03:03 -04002529 mWorkspaceLoading = false;
2530 }
2531
2532 /**
2533 * Add the icons for all apps.
2534 *
2535 * Implementation of the method from LauncherModel.Callbacks.
2536 */
2537 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002538 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002539 }
2540
2541 /**
2542 * A package was installed.
2543 *
2544 * Implementation of the method from LauncherModel.Callbacks.
2545 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002546 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002547 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002548 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002549 }
2550
2551 /**
2552 * A package was updated.
2553 *
2554 * Implementation of the method from LauncherModel.Callbacks.
2555 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002556 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002557 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002558 mWorkspace.updateShortcuts(apps);
2559 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002560 }
2561
2562 /**
2563 * A package was uninstalled.
2564 *
2565 * Implementation of the method from LauncherModel.Callbacks.
2566 */
Joe Onorato36115782010-06-17 13:28:48 -04002567 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002568 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002569 if (permanent) {
2570 mWorkspace.removeItems(apps);
2571 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002572 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002573 }
Joe Onoratobe386092009-11-17 17:32:16 -08002574
2575 /**
2576 * Prints out out state for debugging.
2577 */
2578 public void dumpState() {
2579 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002580 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002581 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2582 Log.d(TAG, "mRestoring=" + mRestoring);
2583 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2584 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2585 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2586 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002587 mModel.dumpState();
2588 mAllAppsGrid.dumpState();
2589 Log.d(TAG, "END launcher2 dump state");
2590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002591}