blob: 008b6361c65448ccaf05b637bc3178a74500adc1 [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;
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -070079import android.view.animation.AccelerateInterpolator;
Patrick Dubroy7247f632010-08-04 16:02:59 -070080import android.view.animation.DecelerateInterpolator;
81import android.view.animation.Interpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080082import android.view.inputmethod.InputMethodManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080083import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070084import android.widget.ImageView;
85import android.widget.LinearLayout;
86import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070087import android.widget.TabHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.TextView;
89import android.widget.Toast;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090
Michael Jurka0e260592010-06-30 17:07:39 -070091import java.io.DataInputStream;
92import java.io.DataOutputStream;
93import java.io.FileNotFoundException;
94import java.io.IOException;
95import java.util.ArrayList;
96import java.util.HashMap;
97import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -080098
The Android Open Source Project31dd5032009-03-03 19:32:27 -080099/**
100 * Default launcher application.
101 */
Michael Jurka946ad472010-07-09 18:05:18 -0700102public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700103 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
104 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800105 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700106 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107
Joe Onorato9c1289c2009-08-17 11:03:03 -0400108 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400109 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400110 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700111
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800112 private static final int WALLPAPER_SCREENS_SPAN = 2;
113
114 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800115 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117 private static final int MENU_ADD = Menu.FIRST + 1;
118 private static final int MENU_WALLPAPER_SETTINGS = MENU_ADD + 1;
119 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
120 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700121 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
124 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int REQUEST_PICK_APPLICATION = 6;
127 private static final int REQUEST_PICK_SHORTCUT = 7;
128 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700130 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
133
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800134 static final int SCREEN_COUNT = 5;
135 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato7c312c12009-08-13 21:36:53 -0700137 static final int DIALOG_CREATE_SHORTCUT = 1;
138 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Romain Guy98d01652009-06-30 16:21:04 -0700140 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 // Type: int
143 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
144 // Type: boolean
145 private static final String RUNTIME_STATE_ALL_APPS_FOLDER = "launcher.all_apps_folder";
146 // Type: long
147 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
148 // Type: int
149 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
150 // Type: int
151 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
152 // Type: int
153 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
154 // Type: int
155 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_spanX";
156 // Type: int
157 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_spanY";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_X = "launcher.add_countX";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_COUNT_Y = "launcher.add_countY";
162 // Type: int[]
163 private static final String RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS = "launcher.add_occupied_cells";
164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800172 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800174 private final BroadcastReceiver mCloseSystemDialogsReceiver
175 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800176 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private LayoutInflater mInflater;
179
Joe Onorato00acb122009-08-04 16:04:30 -0400180 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700183 private AppWidgetManager mAppWidgetManager;
184 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private CellLayout.CellInfo mAddItemCellInfo;
187 private CellLayout.CellInfo mMenuAddInfo;
188 private final int[] mCellCoordinates = new int[2];
189 private FolderInfo mFolderInfo;
190
Joe Onorato7c312c12009-08-13 21:36:53 -0700191 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700193 private AllAppsView mAllAppsGrid;
Michael Jurka0e260592010-06-30 17:07:39 -0700194 private TabHost mHomeCustomizationDrawer;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Bundle mSavedState;
197
198 private SpannableStringBuilder mDefaultKeySsb = null;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private boolean mWorkspaceLoading = true;
201
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800202 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private boolean mRestoring;
204 private boolean mWaitingForResult;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
206 private Bundle mSavedInstanceState;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800209 private IconCache mIconCache;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400210
Romain Guy84f296c2009-11-04 15:00:44 -0800211 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
212 private static HashMap<Long, FolderInfo> mFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700213
Romain Guy1fbc1c82009-11-09 20:43:08 -0800214 private ImageView mPreviousView;
215 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500216
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400217 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400218 private String[] mHotseatConfig = null;
219 private Intent[] mHotseats = null;
220 private Drawable[] mHotseatIcons = null;
221 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 @Override
224 protected void onCreate(Bundle savedInstanceState) {
225 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700226
Winson Chungaafa03c2010-06-11 17:34:16 -0700227 if (LauncherApplication.isInPlaceRotationEnabled()) {
228 // hide the status bar (temporary until we get the status bar design figured out)
229 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
230 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
231 }
232
Joe Onorato0589f0f2010-02-08 13:44:00 -0800233 LauncherApplication app = ((LauncherApplication)getApplication());
234 mModel = app.setLauncher(this);
235 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400236 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700238
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700239 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700240 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
241 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700242
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243 if (PROFILE_STARTUP) {
244 android.os.Debug.startMethodTracing("/sdcard/launcher");
245 }
246
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400247 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248 checkForLocaleChange();
249 setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 setContentView(R.layout.launcher);
Michael Jurka946ad472010-07-09 18:05:18 -0700251 mHomeCustomizationDrawer = (TabHost) findViewById(com.android.internal.R.id.tabhost);
252 if (mHomeCustomizationDrawer != null) {
253 mHomeCustomizationDrawer.setup();
Winson Chungaafa03c2010-06-11 17:34:16 -0700254
Michael Jurka946ad472010-07-09 18:05:18 -0700255 String widgetsLabel = getString(R.string.widgets_tab_label);
256 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("widgets")
257 .setIndicator(widgetsLabel).setContent(R.id.widget_chooser));
258 String foldersLabel = getString(R.string.folders_tab_label);
259 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("folders")
260 .setIndicator(foldersLabel).setContent(R.id.folder_chooser));
261 String shortcutsLabel = getString(R.string.shortcuts_tab_label);
262 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("shortcuts")
263 .setIndicator(shortcutsLabel).setContent(R.id.shortcut_chooser));
264 String wallpapersLabel = getString(R.string.wallpapers_tab_label);
265 mHomeCustomizationDrawer.addTab(mHomeCustomizationDrawer.newTabSpec("wallpapers")
266 .setIndicator(wallpapersLabel).setContent(R.id.wallpaperstab));
267
268 mHomeCustomizationDrawer.setCurrentTab(0);
269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800270 setupViews();
271
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800272 registerContentObservers();
273
Joe Onorato7c312c12009-08-13 21:36:53 -0700274 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 mSavedState = savedInstanceState;
277 restoreState(mSavedState);
278
279 if (PROFILE_STARTUP) {
280 android.os.Debug.stopMethodTracing();
281 }
282
283 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400284 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
286
287 // For handling default keys
288 mDefaultKeySsb = new SpannableStringBuilder();
289 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800290
291 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
292 registerReceiver(mCloseSystemDialogsReceiver, filter);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 }
Romain Guycbb89e42009-06-08 15:52:54 -0700294
Winson Chungaafa03c2010-06-11 17:34:16 -0700295 @Override
296 public void onConfigurationChanged(Configuration newConfig) {
297 // TODO Auto-generated method stub
298 super.onConfigurationChanged(newConfig);
Winson Chungaafa03c2010-06-11 17:34:16 -0700299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 private void checkForLocaleChange() {
Romain Guy98d01652009-06-30 16:21:04 -0700302 final LocaleConfiguration localeConfiguration = new LocaleConfiguration();
303 readConfiguration(this, localeConfiguration);
Jason Samsfd22dac2009-09-20 17:24:16 -0700304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 final Configuration configuration = getResources().getConfiguration();
306
Romain Guy98d01652009-06-30 16:21:04 -0700307 final String previousLocale = localeConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 final String locale = configuration.locale.toString();
309
Romain Guy98d01652009-06-30 16:21:04 -0700310 final int previousMcc = localeConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 final int mcc = configuration.mcc;
312
Romain Guy98d01652009-06-30 16:21:04 -0700313 final int previousMnc = localeConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800314 final int mnc = configuration.mnc;
315
Romain Guy84f296c2009-11-04 15:00:44 -0800316 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317
Romain Guy84f296c2009-11-04 15:00:44 -0800318 if (localeChanged) {
Romain Guy98d01652009-06-30 16:21:04 -0700319 localeConfiguration.locale = locale;
320 localeConfiguration.mcc = mcc;
321 localeConfiguration.mnc = mnc;
322
323 writeConfiguration(this, localeConfiguration);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800324 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400325
326 loadHotseats();
Romain Guy98d01652009-06-30 16:21:04 -0700327 }
328 }
329
330 private static class LocaleConfiguration {
331 public String locale;
332 public int mcc = -1;
333 public int mnc = -1;
334 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700335
Romain Guy98d01652009-06-30 16:21:04 -0700336 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
337 DataInputStream in = null;
338 try {
339 in = new DataInputStream(context.openFileInput(PREFERENCES));
340 configuration.locale = in.readUTF();
341 configuration.mcc = in.readInt();
342 configuration.mnc = in.readInt();
343 } catch (FileNotFoundException e) {
344 // Ignore
345 } catch (IOException e) {
346 // Ignore
347 } finally {
348 if (in != null) {
349 try {
350 in.close();
351 } catch (IOException e) {
352 // Ignore
353 }
354 }
355 }
356 }
357
358 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
359 DataOutputStream out = null;
360 try {
361 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
362 out.writeUTF(configuration.locale);
363 out.writeInt(configuration.mcc);
364 out.writeInt(configuration.mnc);
365 out.flush();
366 } catch (FileNotFoundException e) {
367 // Ignore
368 } catch (IOException e) {
369 //noinspection ResultOfMethodCallIgnored
370 context.getFileStreamPath(PREFERENCES).delete();
371 } finally {
372 if (out != null) {
373 try {
374 out.close();
375 } catch (IOException e) {
376 // Ignore
377 }
378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 }
380 }
381
382 static int getScreen() {
383 synchronized (sLock) {
384 return sScreen;
385 }
386 }
387
388 static void setScreen(int screen) {
389 synchronized (sLock) {
390 sScreen = screen;
391 }
392 }
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 private void setWallpaperDimension() {
Dianne Hackborn107f8392009-08-05 21:32:16 -0700395 WallpaperManager wpm = (WallpaperManager)getSystemService(WALLPAPER_SERVICE);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396
397 Display display = getWindowManager().getDefaultDisplay();
398 boolean isPortrait = display.getWidth() < display.getHeight();
399
400 final int width = isPortrait ? display.getWidth() : display.getHeight();
401 final int height = isPortrait ? display.getHeight() : display.getWidth();
Dianne Hackborn64271802009-08-08 20:54:24 -0700402 wpm.suggestDesiredDimensions(width * WALLPAPER_SCREENS_SPAN, height);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 }
404
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400405 // Note: This doesn't do all the client-id magic that BrowserProvider does
406 // in Browser. (http://b/2425179)
407 private Uri getDefaultBrowserUri() {
408 String url = getString(R.string.default_browser_url);
409 if (url.indexOf("{CID}") != -1) {
410 url = url.replace("{CID}", "android-google");
411 }
412 return Uri.parse(url);
413 }
414
415 // Load the Intent templates from arrays.xml to populate the hotseats. For
416 // each Intent, if it resolves to a single app, use that as the launch
417 // intent & use that app's label as the contentDescription. Otherwise,
418 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400419 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400420 if (mHotseatConfig == null) {
421 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
422 if (mHotseatConfig.length > 0) {
423 mHotseats = new Intent[mHotseatConfig.length];
424 mHotseatLabels = new CharSequence[mHotseatConfig.length];
425 mHotseatIcons = new Drawable[mHotseatConfig.length];
426 } else {
427 mHotseats = null;
428 mHotseatIcons = null;
429 mHotseatLabels = null;
430 }
431
432 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
433 for (int i=0; i<mHotseatConfig.length; i++) {
434 // load icon for this slot; currently unrelated to the actual activity
435 try {
436 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
437 } catch (ArrayIndexOutOfBoundsException ex) {
438 Log.w(TAG, "Missing hotseat_icons array item #" + i);
439 mHotseatIcons[i] = null;
440 }
441 }
442 hotseatIconDrawables.recycle();
443 }
444
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400445 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400446 for (int i=0; i<mHotseatConfig.length; i++) {
447 Intent intent = null;
448 if (mHotseatConfig[i].equals("*BROWSER*")) {
449 // magic value meaning "launch user's default web browser"
450 // replace it with a generic web request so we can see if there is indeed a default
451 String defaultUri = getString(R.string.default_browser_url);
452 intent = new Intent(
453 Intent.ACTION_VIEW,
454 ((defaultUri != null)
455 ? Uri.parse(defaultUri)
456 : getDefaultBrowserUri())
457 ).addCategory(Intent.CATEGORY_BROWSABLE);
458 // note: if the user launches this without a default set, she
459 // will always be taken to the default URL above; this is
460 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700461 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400462 // URL is unavoidably sent to the chosen app.
463 } else {
464 try {
465 intent = Intent.parseUri(mHotseatConfig[i], 0);
466 } catch (java.net.URISyntaxException ex) {
467 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
468 // bogus; leave intent=null
469 }
470 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700471
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400472 if (intent == null) {
473 mHotseats[i] = null;
474 mHotseatLabels[i] = getText(R.string.activity_not_found);
475 continue;
476 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400477
478 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700479 Log.d(TAG, "loadHotseats: hotseat " + i
480 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400481 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400482 + "]");
483 }
484
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400485 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
486 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700487 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400488 Log.d(TAG, "Best match for intent: " + bestMatch);
489 Log.d(TAG, "All matches: ");
490 for (ResolveInfo ri : allMatches) {
491 Log.d(TAG, " --> " + ri);
492 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400493 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400494 // did this resolve to a single app, or the resolver?
495 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700496 // can't find any activity to handle this. let's leave the
497 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400498 // to launch.
499 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400500
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400501 // set accessibility text to "Not installed"
502 mHotseatLabels[i] = getText(R.string.activity_not_found);
503 } else {
504 boolean found = false;
505 for (ResolveInfo ri : allMatches) {
506 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
507 && bestMatch.activityInfo.applicationInfo.packageName
508 .equals(ri.activityInfo.applicationInfo.packageName)) {
509 found = true;
510 break;
511 }
512 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700513
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400514 if (!found) {
515 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
516 // the bestMatch is probably the ResolveActivity, meaning the
517 // user has not yet selected a default
518 // so: we'll keep the original intent for now
519 mHotseats[i] = intent;
520
521 // set the accessibility text to "Select shortcut"
522 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
523 } else {
524 // we have an app!
525 // now reconstruct the intent to launch it through the front
526 // door
527 ComponentName com = new ComponentName(
528 bestMatch.activityInfo.applicationInfo.packageName,
529 bestMatch.activityInfo.name);
530 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
531
532 // load the app label for accessibility
533 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
534 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400535 }
536
537 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700538 Log.d(TAG, "loadHotseats: hotseat " + i
539 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 + ((mHotseats[i] == null)
541 ? "null"
542 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400543 + "] label=[" + mHotseatLabels[i]
544 + "]"
545 );
546 }
547 }
548 }
549
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800550 @Override
551 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700552 mWaitingForResult = false;
553
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 // The pattern used here is that a user PICKs a specific application,
555 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700556
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800557 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
558 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700559
Romain Guy94dabf12009-07-21 10:55:43 -0700560 if (resultCode == RESULT_OK && mAddItemCellInfo != null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 switch (requestCode) {
562 case REQUEST_PICK_APPLICATION:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400563 completeAddApplication(this, data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 break;
565 case REQUEST_PICK_SHORTCUT:
Joe Onoratodeb98af2010-02-19 14:59:39 -0800566 processShortcut(data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800567 break;
568 case REQUEST_CREATE_SHORTCUT:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400569 completeAddShortcut(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800570 break;
571 case REQUEST_PICK_LIVE_FOLDER:
572 addLiveFolder(data);
573 break;
574 case REQUEST_CREATE_LIVE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -0400575 completeAddLiveFolder(data, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 break;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700577 case REQUEST_PICK_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700578 addAppWidgetFromPick(data);
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_CREATE_APPWIDGET:
Michael Jurkaaf442092010-06-10 17:01:57 -0700581 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -0700582 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 break;
Mike Clerona0618e42009-10-22 13:55:21 -0700584 case REQUEST_PICK_WALLPAPER:
585 // We just wanted the activity result here so we can clear mWaitingForResult
586 break;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
Romain Guy18042c82009-11-06 11:44:55 -0800588 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
589 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
590 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700591 // Clean up the appWidgetId if we canceled
592 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
593 if (appWidgetId != -1) {
594 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 }
596 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 }
598
599 @Override
600 protected void onResume() {
601 super.onResume();
602
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800603 mPaused = false;
Joe Onorato7e4ed992009-12-03 13:10:49 -0800604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 if (mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400606 mWorkspaceLoading = true;
607 mModel.startLoader(this, true);
608 mRestoring = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 }
610 }
611
612 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700613 protected void onPause() {
614 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700615 // Some launcher layouts don't have a previous and next view
616 if (mPreviousView != null) {
617 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700618 }
619 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700620 dismissPreview(mNextView);
621 }
Joe Onorato24b6fd82009-11-12 13:47:09 -0800622 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700623 }
Romain Guycbb89e42009-06-08 15:52:54 -0700624
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700625 @Override
626 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 // Flag the loader to stop early before switching
628 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800629 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800630 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700631 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700632
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800633 // We can't hide the IME if it was forced open. So don't bother
634 /*
635 @Override
636 public void onWindowFocusChanged(boolean hasFocus) {
637 super.onWindowFocusChanged(hasFocus);
638
639 if (hasFocus) {
640 final InputMethodManager inputManager = (InputMethodManager)
641 getSystemService(Context.INPUT_METHOD_SERVICE);
642 WindowManager.LayoutParams lp = getWindow().getAttributes();
643 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
644 android.os.Handler()) {
645 protected void onReceiveResult(int resultCode, Bundle resultData) {
646 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
647 }
648 });
649 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
650 }
651 }
652 */
653
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800654 private boolean acceptFilter() {
655 final InputMethodManager inputManager = (InputMethodManager)
656 getSystemService(Context.INPUT_METHOD_SERVICE);
657 return !inputManager.isFullscreenMode();
658 }
659
660 @Override
661 public boolean onKeyDown(int keyCode, KeyEvent event) {
662 boolean handled = super.onKeyDown(keyCode, event);
663 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
664 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
665 keyCode, event);
666 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700667 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700668 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700669 // showSearchDialog()
670 // If there are multiple keystrokes before the search dialog takes focus,
671 // onSearchRequested() will be called for every keystroke,
672 // but it is idempotent, so it's fine.
673 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800674 }
675 }
676
Joe Onorato8a9625e2010-01-28 15:55:35 -0800677 // Eat the long press event so the keyboard doesn't come up.
678 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
679 return true;
680 }
681
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 return handled;
683 }
684
Karl Rosaen138a0412009-04-23 19:00:21 -0700685 private String getTypedText() {
686 return mDefaultKeySsb.toString();
687 }
688
689 private void clearTypedText() {
690 mDefaultKeySsb.clear();
691 mDefaultKeySsb.clearSpans();
692 Selection.setSelection(mDefaultKeySsb, 0);
693 }
694
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 /**
696 * Restores the previous state, if it exists.
697 *
698 * @param savedState The previous state.
699 */
700 private void restoreState(Bundle savedState) {
701 if (savedState == null) {
702 return;
703 }
704
Joe Onorato3a8820b2009-11-10 15:06:42 -0800705 final boolean allApps = savedState.getBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, false);
706 if (allApps) {
707 showAllApps(false);
708 }
709
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800710 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
711 if (currentScreen > -1) {
712 mWorkspace.setCurrentScreen(currentScreen);
713 }
714
715 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
716 if (addScreen > -1) {
717 mAddItemCellInfo = new CellLayout.CellInfo();
718 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
719 addItemCellInfo.valid = true;
720 addItemCellInfo.screen = addScreen;
721 addItemCellInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
722 addItemCellInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
723 addItemCellInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
724 addItemCellInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
725 addItemCellInfo.findVacantCellsFromOccupied(
726 savedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS),
727 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_X),
728 savedState.getInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y));
729 mRestoring = true;
730 }
731
732 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
733 if (renameFolder) {
734 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400735 mFolderInfo = mModel.getFolderById(this, mFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800736 mRestoring = true;
737 }
738 }
739
740 /**
741 * Finds all the views we need and configure them properly.
742 */
743 private void setupViews() {
Joe Onorato00acb122009-08-04 16:04:30 -0400744 DragController dragController = mDragController;
745
Romain Guyb1b69f52009-08-10 15:10:15 -0700746 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400747 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748
Joe Onorato7c312c12009-08-13 21:36:53 -0700749 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700750 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700751 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800752 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700753 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700754 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700755
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700756 if (LauncherApplication.isScreenXLarge()) {
757 // They need to be INVISIBLE initially so that they will be measured in the layout.
758 // Otherwise the animations are messed up when we show them for the first time.
759 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
760 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
764 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500765 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700766
Joe Onorato7c312c12009-08-13 21:36:53 -0700767 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
768 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700770 View handleView = findViewById(R.id.all_apps_button);
771 if (handleView != null && handleView instanceof HandleView) {
772 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700773 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700774 mHandleView.setLauncher(this);
775 mHandleView.setOnClickListener(this);
776 mHandleView.setOnLongClickListener(this);
777 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800778
Michael Jurka0e260592010-06-30 17:07:39 -0700779 WidgetChooser widgetChooser = (WidgetChooser) findViewById(R.id.widget_chooser);
780 if (widgetChooser != null) {
781 WidgetListAdapter widgetGalleryAdapter = new WidgetListAdapter(this);
782 widgetChooser.setAdapter(widgetGalleryAdapter);
783 widgetChooser.setDragController(dragController);
784 widgetChooser.setLauncher(this);
785
786 FolderChooser folderChooser = (FolderChooser) findViewById(R.id.folder_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700787 IntentListAdapter folderTypes = new FolderListAdapter(
788 this, LiveFolders.ACTION_CREATE_LIVE_FOLDER);
Michael Jurka0e260592010-06-30 17:07:39 -0700789 folderChooser.setAdapter(folderTypes);
790 folderChooser.setLauncher(this);
791
792 ShortcutChooser shortcutChooser = (ShortcutChooser) findViewById(R.id.shortcut_chooser);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700793 IntentListAdapter shortcutTypes = new IntentListAdapter(
794 this, Intent.ACTION_CREATE_SHORTCUT);
Michael Jurka0e260592010-06-30 17:07:39 -0700795 shortcutChooser.setAdapter(shortcutTypes);
796 shortcutChooser.setLauncher(this);
Michael Jurkaaf442092010-06-10 17:01:57 -0700797 } else {
798 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
799 hotseatLeft.setContentDescription(mHotseatLabels[0]);
800 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
801 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
802 hotseatRight.setContentDescription(mHotseatLabels[1]);
803 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400804
Michael Jurkaaf442092010-06-10 17:01:57 -0700805 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
806 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800807
Michael Jurkaaf442092010-06-10 17:01:57 -0700808 Drawable previous = mPreviousView.getDrawable();
809 Drawable next = mNextView.getDrawable();
810 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811
Michael Jurkaaf442092010-06-10 17:01:57 -0700812 mPreviousView.setHapticFeedbackEnabled(false);
813 mPreviousView.setOnLongClickListener(this);
814 mNextView.setHapticFeedbackEnabled(false);
815 mNextView.setOnLongClickListener(this);
816 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800817
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800818 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400819 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 workspace.setLauncher(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821
822 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400823 deleteZone.setDragController(dragController);
Michael Jurka54f7ac32010-08-02 13:56:46 -0700824 int deleteZoneHandleId;
825 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700826 deleteZoneHandleId = R.id.configure_button;
Michael Jurka54f7ac32010-08-02 13:56:46 -0700827 } else {
828 deleteZoneHandleId = R.id.all_apps_button_cluster;
829 }
Michael Jurkaaf442092010-06-10 17:01:57 -0700830 deleteZone.setHandle(findViewById(deleteZoneHandleId));
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831
Joe Onorato00acb122009-08-04 16:04:30 -0400832 dragController.setDragScoller(workspace);
833 dragController.setDragListener(deleteZone);
834 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800835 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -0700836
Joe Onorato00acb122009-08-04 16:04:30 -0400837 // The order here is bottom to top.
838 dragController.addDropTarget(workspace);
839 dragController.addDropTarget(deleteZone);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840 }
841
Romain Guy8a73c512009-11-09 19:19:59 -0800842 @SuppressWarnings({"UnusedDeclaration"})
843 public void previousScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800844 if (!isAllAppsVisible()) {
845 mWorkspace.scrollLeft();
846 }
Romain Guy8a73c512009-11-09 19:19:59 -0800847 }
848
849 @SuppressWarnings({"UnusedDeclaration"})
850 public void nextScreen(View v) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800851 if (!isAllAppsVisible()) {
852 mWorkspace.scrollRight();
853 }
Romain Guy8a73c512009-11-09 19:19:59 -0800854 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400855
856 @SuppressWarnings({"UnusedDeclaration"})
857 public void launchHotSeat(View v) {
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400858 if (isAllAppsVisible()) return;
859
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400860 int index = -1;
861 if (v.getId() == R.id.hotseat_left) {
862 index = 0;
863 } else if (v.getId() == R.id.hotseat_right) {
864 index = 1;
865 }
866
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400867 // reload these every tap; you never know when they might change
868 loadHotseats();
869 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
870 Intent intent = mHotseats[index];
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400871 startActivitySafely(
872 mHotseats[index],
873 "hotseat"
874 );
875 }
876 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700877
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800878 /**
879 * Creates a view representing a shortcut.
880 *
881 * @param info The data structure describing the shortcut.
882 *
883 * @return A View inflated from R.layout.application.
884 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800885 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 return createShortcut(R.layout.application,
887 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
888 }
889
890 /**
891 * Creates a view representing a shortcut inflated from the specified resource.
892 *
893 * @param layoutResId The id of the XML layout used to create the shortcut.
894 * @param parent The group the shortcut belongs to.
895 * @param info The data structure describing the shortcut.
896 *
897 * @return A View inflated from layoutResId.
898 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800899 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 TextView favorite = (TextView) mInflater.inflate(layoutResId, parent, false);
901
Joe Onorato0589f0f2010-02-08 13:44:00 -0800902 favorite.setCompoundDrawablesWithIntrinsicBounds(null,
903 new FastBitmapDrawable(info.getIcon(mIconCache)),
904 null, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 favorite.setText(info.title);
906 favorite.setTag(info);
907 favorite.setOnClickListener(this);
908
909 return favorite;
910 }
911
912 /**
913 * Add an application shortcut to the workspace.
914 *
915 * @param data The intent describing the application.
916 * @param cellInfo The position on screen where to create the shortcut.
917 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400918 void completeAddApplication(Context context, Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 cellInfo.screen = mWorkspace.getCurrentScreen();
920 if (!findSingleSlot(cellInfo)) return;
921
Joe Onorato0589f0f2010-02-08 13:44:00 -0800922 final ShortcutInfo info = mModel.getShortcutInfo(context.getPackageManager(),
923 data, context);
924
Romain Guy73b979d2009-06-09 12:57:21 -0700925 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800926 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800928 info.container = ItemInfo.NO_ID;
929 mWorkspace.addApplicationShortcut(info, cellInfo, isWorkspaceLocked());
930 } else {
931 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 }
933 }
Romain Guycbb89e42009-06-08 15:52:54 -0700934
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800935 /**
936 * Add a shortcut to the workspace.
937 *
938 * @param data The intent describing the shortcut.
939 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 */
Joe Onorato9c1289c2009-08-17 11:03:03 -0400941 private void completeAddShortcut(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 cellInfo.screen = mWorkspace.getCurrentScreen();
943 if (!findSingleSlot(cellInfo)) return;
Romain Guycbb89e42009-06-08 15:52:54 -0700944
Joe Onorato0589f0f2010-02-08 13:44:00 -0800945 final ShortcutInfo info = mModel.addShortcut(this, data, cellInfo, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946
947 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 final View view = createShortcut(info);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400949 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
950 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 }
952 }
953
Romain Guycbb89e42009-06-08 15:52:54 -0700954
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 *
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958 * @param data The intent describing the appWidgetId.
959 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 */
Michael Jurkaaf442092010-06-10 17:01:57 -0700961 private void completeAddAppWidget(int appWidgetId, CellLayout.CellInfo cellInfo) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700962 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700963
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700964 // Calculate the grid spans needed to fit this widget
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 CellLayout layout = (CellLayout) mWorkspace.getChildAt(cellInfo.screen);
Patrick Dubroy8f86ddc2010-07-16 13:55:32 -0700966 int[] spans = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700967
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 // Try finding open space on Launcher screen
969 final int[] xy = mCellCoordinates;
Romain Guy18042c82009-11-06 11:44:55 -0800970 if (!findSlot(cellInfo, xy, spans[0], spans[1])) {
971 if (appWidgetId != -1) mAppWidgetHost.deleteAppWidgetId(appWidgetId);
972 return;
973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700975 // Build Launcher-specific widget info and save to database
976 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 launcherInfo.spanX = spans[0];
978 launcherInfo.spanY = spans[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700979
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 LauncherModel.addItemToDatabase(this, launcherInfo,
981 LauncherSettings.Favorites.CONTAINER_DESKTOP,
982 mWorkspace.getCurrentScreen(), xy[0], xy[1], false);
983
984 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400985 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700986
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800987 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700989
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700990 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 mWorkspace.addInCurrentScreen(launcherInfo.hostView, xy[0], xy[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400994 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996 }
Romain Guycbb89e42009-06-08 15:52:54 -0700997
Joe Onorato9c1289c2009-08-17 11:03:03 -0400998 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
999 mDesktopItems.remove(launcherInfo);
1000 launcherInfo.hostView = null;
1001 }
1002
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001003 public LauncherAppWidgetHost getAppWidgetHost() {
1004 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
Romain Guycbb89e42009-06-08 15:52:54 -07001006
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001007 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001008 getWindow().closeAllPanels();
1009
1010 try {
1011 dismissDialog(DIALOG_CREATE_SHORTCUT);
1012 // Unlock the workspace if the dialog was showing
1013 } catch (Exception e) {
1014 // An exception is thrown if the dialog is not visible, which is fine
1015 }
1016
1017 try {
1018 dismissDialog(DIALOG_RENAME_FOLDER);
1019 // Unlock the workspace if the dialog was showing
1020 } catch (Exception e) {
1021 // An exception is thrown if the dialog is not visible, which is fine
1022 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001023
1024 // Whatever we were doing is hereby canceled.
1025 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001026 }
1027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001028 @Override
1029 protected void onNewIntent(Intent intent) {
1030 super.onNewIntent(intent);
1031
1032 // Close the menu
1033 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001034 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001035 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001036
Joe Onorato14f122b2009-11-19 14:06:36 -08001037 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1038 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1039 boolean allAppsVisible = isAllAppsVisible();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001040
Michael Jurka4cb37242010-08-09 21:05:32 -07001041 // in all these cases, only animate if we're already on home
1042 if (LauncherApplication.isScreenXLarge()) {
1043 mWorkspace.unshrink(alreadyOnHome);
1044 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08001045 if (!mWorkspace.isDefaultScreenShowing()) {
Michael Jurka4cb37242010-08-09 21:05:32 -07001046 // on the phone, we don't animate the change to the workspace if all apps is visible
1047 // on xlarge screens, however, we want an animated transition
1048 mWorkspace.moveToDefaultScreen(alreadyOnHome &&
1049 (LauncherApplication.isScreenXLarge() || !allAppsVisible));
Joe Onorato3a8820b2009-11-10 15:06:42 -08001050 }
Joe Onorato14f122b2009-11-19 14:06:36 -08001051 closeAllApps(alreadyOnHome && allAppsVisible);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001052 hideCustomizationDrawer();
Romain Guy1dd3a072009-07-16 13:21:01 -07001053
Joe Onorato3a8820b2009-11-10 15:06:42 -08001054 final View v = getWindow().peekDecorView();
1055 if (v != null && v.getWindowToken() != null) {
1056 InputMethodManager imm = (InputMethodManager)getSystemService(
1057 INPUT_METHOD_SERVICE);
1058 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 }
1060 }
1061 }
1062
1063 @Override
1064 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1065 // Do not call super here
1066 mSavedInstanceState = savedInstanceState;
Michael Jurka946ad472010-07-09 18:05:18 -07001067
1068 if (mHomeCustomizationDrawer != null) {
1069 String cur = savedInstanceState.getString("currentTab");
1070 if (cur != null) {
1071 mHomeCustomizationDrawer.setCurrentTabByTag(cur);
1072 }
1073 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001074 }
1075
1076 @Override
1077 protected void onSaveInstanceState(Bundle outState) {
1078 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentScreen());
1079
1080 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1081 if (folders.size() > 0) {
1082 final int count = folders.size();
1083 long[] ids = new long[count];
1084 for (int i = 0; i < count; i++) {
1085 final FolderInfo info = folders.get(i).getInfo();
1086 ids[i] = info.id;
1087 }
1088 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1089 } else {
1090 super.onSaveInstanceState(outState);
1091 }
1092
Joe Onoratofb0ca672009-09-14 17:55:46 -04001093 // TODO should not do this if the drawer is currently closing.
Joe Onorato3a8820b2009-11-10 15:06:42 -08001094 if (isAllAppsVisible()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 outState.putBoolean(RUNTIME_STATE_ALL_APPS_FOLDER, true);
Romain Guy5a941392009-04-28 15:18:25 -07001096 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097
1098 if (mAddItemCellInfo != null && mAddItemCellInfo.valid && mWaitingForResult) {
1099 final CellLayout.CellInfo addItemCellInfo = mAddItemCellInfo;
1100 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(addItemCellInfo.screen);
1101
1102 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, addItemCellInfo.screen);
1103 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, addItemCellInfo.cellX);
1104 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, addItemCellInfo.cellY);
1105 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, addItemCellInfo.spanX);
1106 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, addItemCellInfo.spanY);
1107 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_X, layout.getCountX());
1108 outState.putInt(RUNTIME_STATE_PENDING_ADD_COUNT_Y, layout.getCountY());
1109 outState.putBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS,
Patrick Dubroy6569f2c2010-07-12 14:25:18 -07001110 layout.getOccupiedCellsFlattened());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112
1113 if (mFolderInfo != null && mWaitingForResult) {
1114 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1115 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1116 }
Michael Jurka946ad472010-07-09 18:05:18 -07001117
1118 if (mHomeCustomizationDrawer != null) {
1119 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1120 if (currentTabTag != null) {
1121 outState.putString("currentTab", currentTabTag);
1122 }
1123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125
1126 @Override
1127 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001129
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001130 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001131 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001133 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 }
1135
1136 TextKeyListener.getInstance().release();
1137
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139
Joe Onorato9c1289c2009-08-17 11:03:03 -04001140 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001141
1142 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001143
Patrick Dubroyab962b72010-07-26 12:10:10 -07001144 // Some launcher layouts don't have a previous and next view
1145 if (mPreviousView != null) {
1146 dismissPreview(mPreviousView);
1147 }
1148 if (mNextView != null) {
1149 dismissPreview(mNextView);
1150 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001151
1152 unregisterReceiver(mCloseSystemDialogsReceiver);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 }
1154
1155 @Override
1156 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001157 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001158 super.startActivityForResult(intent, requestCode);
1159 }
1160
1161 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001162 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001163 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001164
Joe Onorato7bb17492009-09-24 17:51:01 -07001165 closeAllApps(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001166
Karl Rosaen138a0412009-04-23 19:00:21 -07001167 if (initialQuery == null) {
1168 // Use any text typed in the launcher as the initial query
1169 initialQuery = getTypedText();
1170 clearTypedText();
1171 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 if (appSearchData == null) {
1173 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001174 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
Karl Rosaen138a0412009-04-23 19:00:21 -07001177 final SearchManager searchManager =
1178 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001179 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001180 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181 }
1182
1183 @Override
1184 public boolean onCreateOptionsMenu(Menu menu) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001185 if (isWorkspaceLocked()) {
1186 return false;
1187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
1189 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001190
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001191 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1192 .setIcon(android.R.drawable.ic_menu_add)
1193 .setAlphabeticShortcut('A');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001194 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 .setIcon(android.R.drawable.ic_menu_gallery)
1196 .setAlphabeticShortcut('W');
1197 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1198 .setIcon(android.R.drawable.ic_search_category_default)
1199 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1200 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1201 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1202 .setAlphabeticShortcut('N');
1203
1204 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001205 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1206 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207
1208 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1209 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1210 .setIntent(settings);
1211
1212 return true;
1213 }
1214
1215 @Override
1216 public boolean onPrepareOptionsMenu(Menu menu) {
1217 super.onPrepareOptionsMenu(menu);
1218
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001219 // If all apps is animating, don't show the menu, because we don't know
1220 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001221 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001222 return false;
1223 }
1224
1225 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001226 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001227 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1228 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1229
1230 // Disable add if the workspace is full.
1231 if (visible) {
1232 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1233 menu.setGroupEnabled(MENU_GROUP_ADD, mMenuAddInfo != null && mMenuAddInfo.valid);
1234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001235
1236 return true;
1237 }
1238
Michael Jurka0e260592010-06-30 17:07:39 -07001239 // we need to initialize mAddItemCellInfo before adding something to the homescreen -- when
1240 // using the settings menu to add an item, something similar happens in showAddDialog
1241 public void prepareAddItemFromHomeCustomizationDrawer() {
1242 mMenuAddInfo = mWorkspace.findAllVacantCells(null);
1243 mAddItemCellInfo = mMenuAddInfo;
1244 }
1245
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 @Override
1247 public boolean onOptionsItemSelected(MenuItem item) {
1248 switch (item.getItemId()) {
1249 case MENU_ADD:
1250 addItems();
1251 return true;
1252 case MENU_WALLPAPER_SETTINGS:
1253 startWallpaper();
1254 return true;
1255 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001256 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 return true;
1258 case MENU_NOTIFICATIONS:
1259 showNotifications();
1260 return true;
1261 }
1262
1263 return super.onOptionsItemSelected(item);
1264 }
Romain Guycbb89e42009-06-08 15:52:54 -07001265
Karl Rosaen138a0412009-04-23 19:00:21 -07001266 /**
1267 * Indicates that we want global search for this activity by setting the globalSearch
1268 * argument for {@link #startSearch} to true.
1269 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001271 @Override
1272 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001273 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001274 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001275 }
1276
Joe Onorato9c1289c2009-08-17 11:03:03 -04001277 public boolean isWorkspaceLocked() {
1278 return mWorkspaceLoading || mWaitingForResult;
1279 }
1280
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001282 if (LauncherApplication.isScreenXLarge()) {
1283 // Animate the widget chooser up from the bottom of the screen
1284 if (!isCustomizationDrawerVisible()) {
1285 showCustomizationDrawer();
Michael Jurka213d9632010-07-28 11:29:25 -07001286 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07001287 }
1288 } else {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001289 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001290 showAddDialog(mMenuAddInfo);
1291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 }
1293
Michael Jurkaaf442092010-06-10 17:01:57 -07001294 void addAppWidgetFromDrop(ComponentName appWidgetProvider, CellLayout.CellInfo cellInfo) {
1295 mAddItemCellInfo = cellInfo;
1296 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1297 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, appWidgetProvider);
1298 addAppWidgetImpl(appWidgetId);
1299 }
1300
1301 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001302 // TODO: catch bad widget exception when sent
1303 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001304 // TODO: Is this log message meaningful?
1305 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
1306 addAppWidgetImpl(appWidgetId);
1307 }
1308
1309 void addAppWidgetImpl(int appWidgetId) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001310 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311
Bjorn Bringert7984c942009-12-09 15:38:25 +00001312 if (appWidget.configure != null) {
1313 // Launch over to configure widget, if needed
1314 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1315 intent.setComponent(appWidget.configure);
1316 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1317
Romain Guy8e633c52010-03-04 12:51:36 -08001318 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001320 // Otherwise just add it
Michael Jurkaaf442092010-06-10 17:01:57 -07001321 completeAddAppWidget(appWidgetId, mAddItemCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323 }
Romain Guycbb89e42009-06-08 15:52:54 -07001324
Joe Onoratodeb98af2010-02-19 14:59:39 -08001325 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001326 // Handle case where user selected "Applications"
1327 String applicationName = getResources().getString(R.string.group_applications);
1328 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001329
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001330 if (applicationName != null && applicationName.equals(shortcutName)) {
1331 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1332 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001333
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001334 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1335 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Joe Onoratodeb98af2010-02-19 14:59:39 -08001336 startActivityForResult(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001337 } else {
Joe Onoratodeb98af2010-02-19 14:59:39 -08001338 startActivityForResult(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001339 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 }
1341
1342 void addLiveFolder(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001343 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001344 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001345 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001346
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001347 if (folderName != null && folderName.equals(shortcutName)) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001348 addFolder();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001349 } else {
1350 startActivityForResult(intent, REQUEST_CREATE_LIVE_FOLDER);
1351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 void addFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 UserFolderInfo folderInfo = new UserFolderInfo();
1356 folderInfo.title = getText(R.string.folder_name);
1357
1358 CellLayout.CellInfo cellInfo = mAddItemCellInfo;
1359 cellInfo.screen = mWorkspace.getCurrentScreen();
1360 if (!findSingleSlot(cellInfo)) return;
1361
1362 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001363 LauncherModel.addItemToDatabase(this, folderInfo,
1364 LauncherSettings.Favorites.CONTAINER_DESKTOP,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 mWorkspace.getCurrentScreen(), cellInfo.cellX, cellInfo.cellY, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001366 mFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
1368 // Create the view
1369 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
1370 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), folderInfo);
1371 mWorkspace.addInCurrentScreen(newFolder,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001372 cellInfo.cellX, cellInfo.cellY, 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Romain Guycbb89e42009-06-08 15:52:54 -07001374
Joe Onorato9c1289c2009-08-17 11:03:03 -04001375 void removeFolder(FolderInfo folder) {
1376 mFolders.remove(folder.id);
1377 }
1378
1379 private void completeAddLiveFolder(Intent data, CellLayout.CellInfo cellInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 cellInfo.screen = mWorkspace.getCurrentScreen();
1381 if (!findSingleSlot(cellInfo)) return;
1382
1383 final LiveFolderInfo info = addLiveFolder(this, data, cellInfo, false);
1384
1385 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
1387 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentScreen()), info);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001388 mWorkspace.addInCurrentScreen(view, cellInfo.cellX, cellInfo.cellY, 1, 1,
1389 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 }
1391 }
1392
1393 static LiveFolderInfo addLiveFolder(Context context, Intent data,
1394 CellLayout.CellInfo cellInfo, boolean notify) {
1395
1396 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1397 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1398
1399 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 Intent.ShortcutIconResource iconResource = null;
1401
1402 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1403 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1404 try {
1405 iconResource = (Intent.ShortcutIconResource) extra;
1406 final PackageManager packageManager = context.getPackageManager();
1407 Resources resources = packageManager.getResourcesForApplication(
1408 iconResource.packageName);
1409 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1410 icon = resources.getDrawable(id);
1411 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001412 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
1414 }
1415
1416 if (icon == null) {
1417 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1418 }
1419
1420 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001421 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 info.title = name;
1423 info.iconResource = iconResource;
1424 info.uri = data.getData();
1425 info.baseIntent = baseIntent;
1426 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1427 LiveFolders.DISPLAY_MODE_GRID);
1428
1429 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
1430 cellInfo.screen, cellInfo.cellX, cellInfo.cellY, notify);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001431 mFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
1433 return info;
1434 }
1435
1436 private boolean findSingleSlot(CellLayout.CellInfo cellInfo) {
1437 final int[] xy = new int[2];
1438 if (findSlot(cellInfo, xy, 1, 1)) {
1439 cellInfo.cellX = xy[0];
1440 cellInfo.cellY = xy[1];
1441 return true;
1442 }
1443 return false;
1444 }
1445
1446 private boolean findSlot(CellLayout.CellInfo cellInfo, int[] xy, int spanX, int spanY) {
1447 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1448 boolean[] occupied = mSavedState != null ?
1449 mSavedState.getBooleanArray(RUNTIME_STATE_PENDING_ADD_OCCUPIED_CELLS) : null;
1450 cellInfo = mWorkspace.findAllVacantCells(occupied);
1451 if (!cellInfo.findCellForSpan(xy, spanX, spanY)) {
1452 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1453 return false;
1454 }
1455 }
1456 return true;
1457 }
1458
1459 private void showNotifications() {
1460 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1461 if (statusBar != null) {
1462 statusBar.expand();
1463 }
1464 }
1465
1466 private void startWallpaper() {
Joe Onoratoe6168662009-11-08 13:39:30 -05001467 closeAllApps(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001469 Intent chooser = Intent.createChooser(pickWallpaper,
1470 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001471 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1472 // Removed in Eclair MR1
1473// WallpaperManager wm = (WallpaperManager)
1474// getSystemService(Context.WALLPAPER_SERVICE);
1475// WallpaperInfo wi = wm.getWallpaperInfo();
1476// if (wi != null && wi.getSettingsActivity() != null) {
1477// LabeledIntent li = new LabeledIntent(getPackageName(),
1478// R.string.configure_wallpaper, 0);
1479// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1480// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1481// }
Mike Clerona0618e42009-10-22 13:55:21 -07001482 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 }
1484
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001485 /**
1486 * Registers various content observers. The current implementation registers
1487 * only a favorites observer to keep track of the favorites applications.
1488 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001489 private void registerContentObservers() {
1490 ContentResolver resolver = getContentResolver();
1491 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1492 true, mWidgetObserver);
1493 }
1494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 @Override
1496 public boolean dispatchKeyEvent(KeyEvent event) {
1497 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1498 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001500 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001501 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001502 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001503 dumpState();
1504 return true;
1505 }
1506 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001507 }
1508 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1509 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001510 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 return true;
1512 }
1513 }
1514
1515 return super.dispatchKeyEvent(event);
1516 }
1517
Joe Onorato88ec0992009-11-19 13:16:06 -08001518 @Override
1519 public void onBackPressed() {
1520 if (isAllAppsVisible()) {
1521 closeAllApps(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001522 } else if (isCustomizationDrawerVisible()) {
1523 hideCustomizationDrawer();
Joe Onorato88ec0992009-11-19 13:16:06 -08001524 } else {
1525 closeFolder();
1526 }
Michael Jurkaaf442092010-06-10 17:01:57 -07001527 // Some launcher layouts don't have a previous and next view
1528 if (mPreviousView != null) {
1529 dismissPreview(mPreviousView);
1530 dismissPreview(mNextView);
1531 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001532 }
1533
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 private void closeFolder() {
1535 Folder folder = mWorkspace.getOpenFolder();
1536 if (folder != null) {
1537 closeFolder(folder);
1538 }
1539 }
1540
1541 void closeFolder(Folder folder) {
1542 folder.getInfo().opened = false;
1543 ViewGroup parent = (ViewGroup) folder.getParent();
1544 if (parent != null) {
1545 parent.removeView(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001546 if (folder instanceof DropTarget) {
1547 // Live folders aren't DropTargets.
1548 mDragController.removeDropTarget((DropTarget)folder);
1549 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
1551 folder.onClose();
1552 }
1553
1554 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001555 * Re-listen when widgets are reset.
1556 */
1557 private void onAppWidgetReset() {
1558 mAppWidgetHost.startListening();
1559 }
1560
1561 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1563 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 private void unbindDesktopItems() {
1566 for (ItemInfo item: mDesktopItems) {
1567 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 }
1569 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 /**
1572 * Launches the intent referred by the clicked shortcut.
1573 *
1574 * @param v The view representing the clicked shortcut.
1575 */
1576 public void onClick(View v) {
1577 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001578 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001580 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001581 int[] pos = new int[2];
1582 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001583 intent.setSourceBounds(new Rect(pos[0], pos[1],
1584 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Joe Onoratof984e852010-03-25 09:47:45 -07001585 startActivitySafely(intent, tag);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 } else if (tag instanceof FolderInfo) {
1587 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07001588 } else if (v == mHandleView) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04001589 if (isAllAppsVisible()) {
Joe Onorato7bb17492009-09-24 17:51:01 -07001590 closeAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001591 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001592 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001593 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 }
1595 }
1596
Michael Jurka0e260592010-06-30 17:07:39 -07001597 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001598 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurka0e260592010-06-30 17:07:39 -07001599 // clicking anywhere on the workspace causes the drawer to slide down
Patrick Dubroy558654c2010-07-23 16:48:11 -07001600 hideCustomizationDrawer();
Michael Jurka0e260592010-06-30 17:07:39 -07001601 return false;
1602 }
1603
Michael Jurkaaf442092010-06-10 17:01:57 -07001604 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001605 * Event handler for the search button
1606 *
1607 * @param v The view that was clicked.
1608 */
1609 public void onClickSearchButton(View v) {
1610 Intent i = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1611 View button = findViewById(R.id.search_button);
1612 i.setSourceBounds(
1613 new Rect(button.getLeft(), button.getTop(), button.getRight(), button.getBottom()));
1614 startActivity(i);
1615 }
1616
1617 /**
1618 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001619 * enters home screen customization mode.
1620 *
1621 * @param v The view that was clicked.
1622 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001623 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001624 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001625 }
1626
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001627 /**
1628 * Event handler for the "grid" button that appears on the home screen, which
1629 * enters all apps mode.
1630 *
1631 * @param v The view that was clicked.
1632 */
1633 public void onClickAllAppsButton(View v) {
1634 showAllApps(true);
1635 }
1636
Joe Onoratof984e852010-03-25 09:47:45 -07001637 void startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1639 try {
1640 startActivity(intent);
1641 } catch (ActivityNotFoundException e) {
1642 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001643 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 } catch (SecurityException e) {
1645 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001646 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001648 "or use the exported attribute for this activity. "
1649 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
1651 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001652
Romain Guy8e633c52010-03-04 12:51:36 -08001653 void startActivityForResultSafely(Intent intent, int requestCode) {
1654 try {
1655 startActivityForResult(intent, requestCode);
1656 } catch (ActivityNotFoundException e) {
1657 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1658 } catch (SecurityException e) {
1659 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1660 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1661 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1662 "or use the exported attribute for this activity.", e);
1663 }
1664 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001665
1666 private void handleFolderClick(FolderInfo folderInfo) {
1667 if (!folderInfo.opened) {
1668 // Close any open folder
1669 closeFolder();
1670 // Open the requested folder
1671 openFolder(folderInfo);
1672 } else {
1673 // Find the open folder...
1674 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
1675 int folderScreen;
1676 if (openFolder != null) {
1677 folderScreen = mWorkspace.getScreenForView(openFolder);
1678 // .. and close it
1679 closeFolder(openFolder);
1680 if (folderScreen != mWorkspace.getCurrentScreen()) {
1681 // Close any folder open on the current screen
1682 closeFolder();
1683 // Pull the folder onto this screen
1684 openFolder(folderInfo);
1685 }
1686 }
1687 }
1688 }
1689
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 /**
1691 * Opens the user fodler described by the specified tag. The opening of the folder
1692 * is animated relative to the specified View. If the View is null, no animation
1693 * is played.
1694 *
1695 * @param folderInfo The FolderInfo describing the folder to open.
1696 */
Winson Chungaafa03c2010-06-11 17:34:16 -07001697 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 Folder openFolder;
1699
1700 if (folderInfo instanceof UserFolderInfo) {
1701 openFolder = UserFolder.fromXml(this);
1702 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07001703 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 } else {
1705 return;
1706 }
1707
Joe Onorato00acb122009-08-04 16:04:30 -04001708 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 openFolder.setLauncher(this);
1710
1711 openFolder.bind(folderInfo);
1712 folderInfo.opened = true;
1713
Winson Chungaafa03c2010-06-11 17:34:16 -07001714 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
1715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 openFolder.onOpen();
1717 }
1718
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001719 public boolean onLongClick(View v) {
Romain Guy1fbc1c82009-11-09 20:43:08 -08001720 switch (v.getId()) {
1721 case R.id.previous_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);
Joe Onorato0d44e942009-11-16 18:20:51 -08001726 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001727 return true;
1728 case R.id.next_screen:
Joe Onorato0d44e942009-11-16 18:20:51 -08001729 if (!isAllAppsVisible()) {
1730 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1731 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08001732 showPreviews(v);
1733 }
1734 return true;
1735 case R.id.all_apps_button:
1736 if (!isAllAppsVisible()) {
1737 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1738 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1739 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001740 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001741 return true;
1742 }
1743
Joe Onorato9c1289c2009-08-17 11:03:03 -04001744 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 return false;
1746 }
1747
1748 if (!(v instanceof CellLayout)) {
1749 v = (View) v.getParent();
1750 }
1751
1752 CellLayout.CellInfo cellInfo = (CellLayout.CellInfo) v.getTag();
1753
1754 // This happens when long clicking an item with the dpad/trackball
1755 if (cellInfo == null) {
1756 return true;
1757 }
1758
1759 if (mWorkspace.allowLongPress()) {
1760 if (cellInfo.cell == null) {
1761 if (cellInfo.valid) {
1762 // User long pressed on empty space
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001763 mWorkspace.setAllowLongPress(false);
Joe Onoratof0dde092010-02-16 18:25:23 -05001764 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1765 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 showAddDialog(cellInfo);
1767 }
1768 } else {
1769 if (!(cellInfo.cell instanceof Folder)) {
1770 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08001771 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1772 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 mWorkspace.startDrag(cellInfo);
1774 }
1775 }
1776 }
1777 return true;
1778 }
1779
Romain Guye6b8e2f2009-11-10 11:56:55 -08001780 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08001781 private void dismissPreview(final View v) {
1782 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001783 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001784 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
1785 public void onDismiss() {
1786 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
1787 int count = group.getChildCount();
1788 for (int i = 0; i < count; i++) {
1789 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
1790 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08001791 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
1792 for (Bitmap bitmap : bitmaps) bitmap.recycle();
1793
1794 v.setTag(R.id.workspace, null);
1795 v.setTag(R.id.icon, null);
1796 window.setOnDismissListener(null);
1797 }
1798 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001799 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08001800 }
1801 v.setTag(null);
1802 }
1803
Romain Guyf8e6a802009-12-07 17:48:02 -08001804 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08001805 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08001806 }
1807
Romain Guya6abce82009-11-10 02:54:41 -08001808 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001809 final Resources resources = getResources();
1810 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001811
Romain Guya6abce82009-11-10 02:54:41 -08001812 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07001813
Romain Guy9d31e9f2009-11-11 18:54:28 -08001814 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07001815
Romain Guyf8e6a802009-12-07 17:48:02 -08001816 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08001817 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001818 int extraW = (int) ((r.left + r.right) * max);
1819 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08001820
1821 int aW = cell.getWidth() - extraW;
1822 float w = aW / max;
1823
1824 int width = cell.getWidth();
1825 int height = cell.getHeight();
1826 int x = cell.getLeftPadding();
1827 int y = cell.getTopPadding();
1828 width -= (x + cell.getRightPadding());
1829 height -= (y + cell.getBottomPadding());
1830
1831 float scale = w / width;
1832
1833 int count = end - start;
1834
1835 final float sWidth = width * scale;
1836 float sHeight = height * scale;
1837
Romain Guye6b8e2f2009-11-10 11:56:55 -08001838 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08001839
Romain Guye6b8e2f2009-11-10 11:56:55 -08001840 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
1841 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08001842
1843 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001844 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08001845 cell = (CellLayout) workspace.getChildAt(i);
1846
Romain Guyf8e6a802009-12-07 17:48:02 -08001847 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08001848 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08001849
1850 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001851 c.scale(scale, scale);
1852 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
1853 cell.dispatchDraw(c);
Romain Guya6abce82009-11-10 02:54:41 -08001854
Romain Guy9d31e9f2009-11-11 18:54:28 -08001855 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08001856 image.setImageBitmap(bitmap);
1857 image.setTag(i);
1858 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001859 image.setOnFocusChangeListener(handler);
1860 image.setFocusable(true);
1861 if (i == mWorkspace.getCurrentScreen()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08001862
1863 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08001864 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
1865
Winson Chungaafa03c2010-06-11 17:34:16 -07001866 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08001867 }
Romain Guyf8e6a802009-12-07 17:48:02 -08001868
1869 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08001870 p.setContentView(preview);
1871 p.setWidth((int) (sWidth * count + extraW));
1872 p.setHeight((int) (sHeight + extraH));
1873 p.setAnimationStyle(R.style.AnimationPreview);
1874 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001875 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08001876 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08001877 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08001878
Romain Guye6b8e2f2009-11-10 11:56:55 -08001879 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
1880 public void onDismiss() {
1881 dismissPreview(anchor);
1882 }
1883 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08001884
1885 anchor.setTag(p);
1886 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07001887 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08001888 }
1889
Romain Guy9d31e9f2009-11-11 18:54:28 -08001890 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08001891 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08001892
Romain Guye6b8e2f2009-11-10 11:56:55 -08001893 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08001894 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08001895 }
1896
1897 public void onClick(View v) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08001898 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001899 v.post(this);
1900 }
1901
1902 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07001903 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08001904 }
1905
1906 public void onFocusChange(View v, boolean hasFocus) {
1907 if (hasFocus) {
Romain Guye47f55c2009-11-11 19:21:22 -08001908 mWorkspace.snapToScreen((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08001909 }
Romain Guya6abce82009-11-10 02:54:41 -08001910 }
1911 }
1912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 Workspace getWorkspace() {
1914 return mWorkspace;
1915 }
1916
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 @Override
1918 protected Dialog onCreateDialog(int id) {
1919 switch (id) {
1920 case DIALOG_CREATE_SHORTCUT:
1921 return new CreateShortcut().createDialog();
1922 case DIALOG_RENAME_FOLDER:
1923 return new RenameFolder().createDialog();
1924 }
1925
1926 return super.onCreateDialog(id);
1927 }
1928
1929 @Override
1930 protected void onPrepareDialog(int id, Dialog dialog) {
1931 switch (id) {
1932 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 break;
1934 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001935 if (mFolderInfo != null) {
1936 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1937 final CharSequence text = mFolderInfo.title;
1938 input.setText(text);
1939 input.setSelection(0, text.length());
1940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 break;
1942 }
1943 }
1944
1945 void showRenameDialog(FolderInfo info) {
1946 mFolderInfo = info;
1947 mWaitingForResult = true;
1948 showDialog(DIALOG_RENAME_FOLDER);
1949 }
1950
1951 private void showAddDialog(CellLayout.CellInfo cellInfo) {
1952 mAddItemCellInfo = cellInfo;
1953 mWaitingForResult = true;
1954 showDialog(DIALOG_CREATE_SHORTCUT);
1955 }
1956
Joe Onoratodeb98af2010-02-19 14:59:39 -08001957 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07001958 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07001959 Bundle bundle = new Bundle();
1960
1961 ArrayList<String> shortcutNames = new ArrayList<String>();
1962 shortcutNames.add(getString(R.string.group_applications));
1963 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
1964
1965 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
1966 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
1967 R.drawable.ic_launcher_application));
1968 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
1969
1970 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1971 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08001972 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07001973 pickIntent.putExtras(bundle);
1974
Joe Onoratodeb98af2010-02-19 14:59:39 -08001975 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07001976 }
1977
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 private class RenameFolder {
1979 private EditText mInput;
1980
1981 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1983 mInput = (EditText) layout.findViewById(R.id.folder_name);
1984
1985 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1986 builder.setIcon(0);
1987 builder.setTitle(getString(R.string.rename_folder_title));
1988 builder.setCancelable(true);
1989 builder.setOnCancelListener(new Dialog.OnCancelListener() {
1990 public void onCancel(DialogInterface dialog) {
1991 cleanup();
1992 }
1993 });
1994 builder.setNegativeButton(getString(R.string.cancel_action),
1995 new Dialog.OnClickListener() {
1996 public void onClick(DialogInterface dialog, int which) {
1997 cleanup();
1998 }
1999 }
2000 );
2001 builder.setPositiveButton(getString(R.string.rename_action),
2002 new Dialog.OnClickListener() {
2003 public void onClick(DialogInterface dialog, int which) {
2004 changeFolderName();
2005 }
2006 }
2007 );
2008 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002009
2010 final AlertDialog dialog = builder.create();
2011 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2012 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002013 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002014 mInput.requestFocus();
2015 InputMethodManager inputManager = (InputMethodManager)
2016 getSystemService(Context.INPUT_METHOD_SERVICE);
2017 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002018 }
2019 });
2020
2021 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 private void changeFolderName() {
2025 final String name = mInput.getText().toString();
2026 if (!TextUtils.isEmpty(name)) {
2027 // Make sure we have the right folder info
Joe Onorato9c1289c2009-08-17 11:03:03 -04002028 mFolderInfo = mFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 mFolderInfo.title = name;
2030 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2031
Joe Onorato9c1289c2009-08-17 11:03:03 -04002032 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002033 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002034 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 } else {
2036 final FolderIcon folderIcon = (FolderIcon)
2037 mWorkspace.getViewForTag(mFolderInfo);
2038 if (folderIcon != null) {
2039 folderIcon.setText(name);
2040 getWorkspace().requestLayout();
2041 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002042 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002043 mWorkspaceLoading = true;
2044 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046 }
2047 }
2048 cleanup();
2049 }
2050
2051 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 dismissDialog(DIALOG_RENAME_FOLDER);
2053 mWaitingForResult = false;
2054 mFolderInfo = null;
2055 }
2056 }
2057
Daniel Sandler843e8602010-06-07 14:59:01 -04002058 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2059 public boolean isAllAppsVisible() {
2060 return (mAllAppsGrid != null) ? mAllAppsGrid.isVisible() : false;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002061 }
2062
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002063 // AllAppsView.Watcher
2064 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002065 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2066 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002067 mWorkspace.setVisibility(View.GONE);
2068 }
2069 }
2070
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002071 /**
2072 * Zoom the camera out from the workspace to reveal 'toView'.
2073 * Assumes that the view to show is anchored at either the very top or very bottom
2074 * of the screen.
2075 * @param toView The view to show when the animation is complete
2076 * @param above If true, toView will appear from the top of the screen
2077 */
2078 private void cameraZoomOut(final View toView, boolean above) {
2079 final Resources res = getResources();
2080 final int duration = res.getInteger(R.integer.config_allAppsZoomInTime);
2081 final float scale = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2082 final int height = toView.getHeight();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002083
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002084 // toView should appear right at the end of the workspace shrink animation
2085 final int startDelay = res.getInteger(R.integer.config_workspaceShrinkTime) - duration;
2086
2087 Interpolator interp = new DecelerateInterpolator();
2088
2089 toView.setPivotX(toView.getWidth() / 2.0f);
2090 // Set pivotY so that at the starting zoom factor, the view is off-screen by a small margin
2091 // Assumes that the view is normally anchored to either the top or bottom of the screen
2092 final int margin = 200;
2093 if (above) {
2094 toView.setPivotY(height + ((toView.getTop() + height) / scale) + margin);
2095 } else {
2096 toView.setPivotY(0.0f - (toView.getTop() / scale) - margin);
2097 }
2098
2099 Animator scaleXAnim = new PropertyAnimator(duration, toView, "scaleX", scale, 1.0f);
2100 scaleXAnim.setInterpolator(interp);
2101 scaleXAnim.addListener(new AnimatableListenerAdapter() {
2102 public void onAnimationStart(Animatable animation) {
2103 // Prepare the position
2104 toView.setTranslationX(0.0f);
2105 toView.setTranslationY(0.0f);
2106 toView.setVisibility(View.VISIBLE);
2107 }
2108 });
2109
2110 Animator scaleYAnim = new PropertyAnimator(duration, toView, "scaleY", scale, 1.0f);
2111 scaleYAnim.setInterpolator(interp);
2112
2113 Sequencer s = new Sequencer();
2114 s.playTogether(scaleXAnim, scaleYAnim);
2115 s.play(scaleXAnim).after(startDelay);
2116 s.start();
2117 }
2118
2119 /**
2120 * Zoom the camera back into the workspace, hiding 'fromView'.
2121 * This is the opposite of cameraZoomOut.
2122 * @param fromView The currently-focused view, which will be hidden.
2123 */
2124 private void cameraZoomIn(final View fromView) {
2125 Resources res = getResources();
2126 int duration = res.getInteger(R.integer.config_allAppsZoomOutTime);
2127 float scaleFactor = (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor);
2128
2129 Interpolator interp = new AccelerateInterpolator();
2130
2131 Sequencer s = new Sequencer();
2132 Animator scaleXAnim = new PropertyAnimator(duration, fromView, "scaleX", scaleFactor);
2133 scaleXAnim.setInterpolator(interp);
2134 Animator scaleYAnim = new PropertyAnimator(duration, fromView, "scaleY", scaleFactor);
2135 scaleYAnim.setInterpolator(interp);
2136 s.playTogether(scaleXAnim, scaleYAnim);
2137 s.addListener(new AnimatableListenerAdapter() {
2138 public void onAnimationEnd(Animatable animation) {
2139 fromView.setVisibility(View.GONE);
2140 fromView.setScaleX(1.0f);
2141 fromView.setScaleY(1.0f);
2142 }
2143 });
2144 s.start();
2145 }
2146
2147 /**
2148 * Pan the camera in the vertical plane between 'fromView' and 'toView'.
2149 * This is the transition used on xlarge screens to go between all apps and
2150 * the home customization drawer.
2151 * @param fromView The view to pan away from.
2152 * @param toView The view to pan into the frame.
2153 */
2154 private void cameraPan(final View fromView, final View toView) {
2155 final int duration = getResources().getInteger(R.integer.config_allAppsCameraPanTime);
2156 final int workspaceHeight = mWorkspace.getHeight();
2157
2158 final boolean panDown = fromView.getY() < toView.getY();
2159
2160 final float fromViewStartY = panDown ? 0.0f : fromView.getY();
2161 final float fromViewEndY = panDown ? -fromView.getHeight() * 2 : workspaceHeight * 2;
2162 final float toViewStartY = panDown ? workspaceHeight * 2 : -toView.getHeight() * 2;
2163 final float toViewEndY = panDown ? workspaceHeight - toView.getHeight() : 0.0f;
2164
2165 Sequencer s = new Sequencer();
2166 s.playTogether(
2167 new PropertyAnimator(duration, fromView, "y", fromViewStartY, fromViewEndY),
2168 new PropertyAnimator(duration, toView, "y", toViewStartY, toViewEndY));
2169 s.addListener(new AnimatableListenerAdapter() {
2170 public void onAnimationStart(Animatable animation) {
2171 toView.setVisibility(View.VISIBLE);
2172 toView.setY(toViewStartY);
2173 }
2174 public void onAnimationEnd(Animatable animation) {
2175 fromView.setVisibility(View.GONE);
2176 }
2177 });
2178 s.start();
2179 }
2180
2181 void showAllApps(boolean animated) {
Michael Jurka79212d82010-07-30 16:36:20 -07002182 if (LauncherApplication.isScreenXLarge()) {
2183 mWorkspace.shrinkToBottom(animated);
2184 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002185
Patrick Dubroy558654c2010-07-23 16:48:11 -07002186 if (LauncherApplication.isScreenXLarge() && animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002187 if (isCustomizationDrawerVisible()) {
2188 cameraPan(mHomeCustomizationDrawer, (View) mAllAppsGrid);
2189 } else {
2190 cameraZoomOut((View) mAllAppsGrid, true);
2191 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002192 } else {
2193 mAllAppsGrid.zoom(1.0f, animated);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002194 hideCustomizationDrawer(); // TODO: Should be able to do this un-animated
Patrick Dubroy558654c2010-07-23 16:48:11 -07002195 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002196
Romain Guyc16fea72010-03-12 17:17:56 -08002197 ((View) mAllAppsGrid).setFocusable(true);
2198 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002199
Joe Onorato7c312c12009-08-13 21:36:53 -07002200 // TODO: fade these two too
2201 mDeleteZone.setVisibility(View.GONE);
Joe Onorato00acb122009-08-04 16:04:30 -04002202 }
2203
Joe Onoratob2061212009-11-24 16:13:54 -05002204 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002205 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002206 * - Home from workspace
2207 * - from center screen
2208 * - from other screens
2209 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002210 * - from center screen
2211 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002212 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002213 * - from center screen
2214 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002215 * - Launch app from workspace and quit
2216 * - with back
2217 * - with home
2218 * - Launch app from all apps and quit
2219 * - with back
2220 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002221 * - Go to a screen that's not the default, then all
2222 * apps, and launch and app, and go back
2223 * - with back
2224 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002225 * - On workspace, long press power and go back
2226 * - with back
2227 * - with home
2228 * - On all apps, long press power and go back
2229 * - with back
2230 * - with home
2231 * - On workspace, power off
2232 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002233 * - Launch an app and turn off the screen while in that app
2234 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002235 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002236 * - From all apps
2237 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002238 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2239 * - From all apps
2240 * - From the center workspace
2241 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002242 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002243 void closeAllApps(boolean animated) {
Joe Onoratofb0ca672009-09-14 17:55:46 -04002244 if (mAllAppsGrid.isVisible()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002245 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002246 if (LauncherApplication.isScreenXLarge() && animated) {
Michael Jurka213d9632010-07-28 11:29:25 -07002247 mWorkspace.unshrink();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002248 cameraZoomIn((View) mAllAppsGrid);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002249 } else {
2250 mAllAppsGrid.zoom(0.0f, animated);
2251 }
Daniel Sandler388f6792010-03-02 14:08:08 -05002252 ((View)mAllAppsGrid).setFocusable(false);
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002253 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002254 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002255 }
2256
Joe Onorato7c312c12009-08-13 21:36:53 -07002257 void lockAllApps() {
2258 // TODO
2259 }
2260
2261 void unlockAllApps() {
2262 // TODO
2263 }
2264
Patrick Dubroy558654c2010-07-23 16:48:11 -07002265 private boolean isCustomizationDrawerVisible() {
Michael Jurka54f7ac32010-08-02 13:56:46 -07002266 return mHomeCustomizationDrawer != null &&
2267 mHomeCustomizationDrawer.getVisibility() == View.VISIBLE;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002268 }
2269
2270 private void showCustomizationDrawer() {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002271 mWorkspace.shrinkToTop();
Patrick Dubroy558654c2010-07-23 16:48:11 -07002272 if (isAllAppsVisible()) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002273 cameraPan((View) mAllAppsGrid, mHomeCustomizationDrawer);
2274 } else {
2275 cameraZoomOut(mHomeCustomizationDrawer, false);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002276 }
Patrick Dubroy558654c2010-07-23 16:48:11 -07002277 }
2278
Michael Jurka4bb4f732010-08-04 18:46:01 -07002279 void hideCustomizationDrawer() {
2280 hideCustomizationDrawer(true);
2281 }
2282
2283 void hideCustomizationDrawer(boolean unshrinkWorkspace) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002284 if (isCustomizationDrawerVisible()) {
Michael Jurka4bb4f732010-08-04 18:46:01 -07002285 if (unshrinkWorkspace) {
2286 mWorkspace.unshrink();
2287 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002288 cameraZoomIn(mHomeCustomizationDrawer);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002289 }
2290 }
2291
Michael Jurka213d9632010-07-28 11:29:25 -07002292 void onWorkspaceUnshrink() {
2293 if (isAllAppsVisible()) {
Michael Jurka54dd7542010-07-30 14:47:52 -07002294 closeAllApps(true);
Michael Jurka213d9632010-07-28 11:29:25 -07002295 }
2296 if (isCustomizationDrawerVisible()) {
2297 hideCustomizationDrawer();
2298 }
2299 }
2300
Joe Onorato7404ee42009-07-31 11:54:44 -07002301 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 * Displays the shortcut creation dialog and launches, if necessary, the
2303 * appropriate activity.
2304 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002305 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002306 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2307 DialogInterface.OnShowListener {
2308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002313
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002314 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2315 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002316 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 builder.setInverseBackgroundForced(true);
2319
2320 AlertDialog dialog = builder.create();
2321 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002322 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002323 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 return dialog;
2326 }
2327
2328 public void onCancel(DialogInterface dialog) {
2329 mWaitingForResult = false;
2330 cleanup();
2331 }
2332
Romain Guycbb89e42009-06-08 15:52:54 -07002333 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07002334 }
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002337 try {
2338 dismissDialog(DIALOG_CREATE_SHORTCUT);
2339 } catch (Exception e) {
2340 // An exception is thrown if the dialog is not visible, which is fine
2341 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 }
2343
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002344 /**
2345 * Handle the action clicked in the "Add to home" dialog.
2346 */
2347 public void onClick(DialogInterface dialog, int which) {
2348 Resources res = getResources();
2349 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002350
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002351 switch (which) {
2352 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08002353 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002354 break;
2355 }
Romain Guycbb89e42009-06-08 15:52:54 -07002356
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002357 case AddAdapter.ITEM_APPWIDGET: {
2358 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07002359
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002360 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
2361 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002362 // start the pick activity
2363 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
2364 break;
2365 }
Romain Guycbb89e42009-06-08 15:52:54 -07002366
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002367 case AddAdapter.ITEM_LIVE_FOLDER: {
2368 // Insert extra item to handle inserting folder
2369 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07002370
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002371 ArrayList<String> shortcutNames = new ArrayList<String>();
2372 shortcutNames.add(res.getString(R.string.group_folder));
2373 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07002374
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002375 ArrayList<ShortcutIconResource> shortcutIcons =
2376 new ArrayList<ShortcutIconResource>();
2377 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2378 R.drawable.ic_launcher_folder));
2379 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2380
2381 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2382 pickIntent.putExtra(Intent.EXTRA_INTENT,
2383 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
2384 pickIntent.putExtra(Intent.EXTRA_TITLE,
2385 getText(R.string.title_select_live_folder));
2386 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07002387
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002388 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
2389 break;
2390 }
2391
2392 case AddAdapter.ITEM_WALLPAPER: {
2393 startWallpaper();
2394 break;
2395 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 }
2397 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002398
2399 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002400 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402 }
2403
2404 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002405 * Receives notifications when applications are added/removed.
2406 */
2407 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2408 @Override
2409 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002410 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002411 String reason = intent.getStringExtra("reason");
2412 if (!"homekey".equals(reason)) {
2413 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002414 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002415 animate = false;
2416 }
Joe Onoratob2061212009-11-24 16:13:54 -05002417 closeAllApps(animate);
Michael Jurka4cb37242010-08-09 21:05:32 -07002418 mWorkspace.unshrink(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002419 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002420 }
2421 }
2422
2423 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002424 * Receives notifications whenever the appwidgets are reset.
2425 */
2426 private class AppWidgetResetObserver extends ContentObserver {
2427 public AppWidgetResetObserver() {
2428 super(new Handler());
2429 }
2430
2431 @Override
2432 public void onChange(boolean selfChange) {
2433 onAppWidgetReset();
2434 }
2435 }
2436
2437 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002438 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002439 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002440 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002441 if (mWorkspace != null) {
2442 return mWorkspace.getCurrentScreen();
2443 } else {
2444 return SCREEN_COUNT / 2;
2445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002446 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002447
Joe Onorato9c1289c2009-08-17 11:03:03 -04002448 /**
2449 * Refreshes the shortcuts shown on the workspace.
2450 *
2451 * Implementation of the method from LauncherModel.Callbacks.
2452 */
2453 public void startBinding() {
2454 final Workspace workspace = mWorkspace;
2455 int count = workspace.getChildCount();
2456 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002457 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Joe Onorato9c1289c2009-08-17 11:03:03 -04002458 ((ViewGroup) workspace.getChildAt(i)).removeAllViewsInLayout();
2459 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002460
Joe Onorato9c1289c2009-08-17 11:03:03 -04002461 if (DEBUG_USER_INTERFACE) {
2462 android.widget.Button finishButton = new android.widget.Button(this);
2463 finishButton.setText("Finish");
2464 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
2465
2466 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
2467 public void onClick(View v) {
2468 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002469 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002470 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002471 }
2472 }
2473
2474 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002475 * Bind the items start-end from the list.
2476 *
2477 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002478 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002479 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
2480
2481 final Workspace workspace = mWorkspace;
2482
2483 for (int i=start; i<end; i++) {
2484 final ItemInfo item = shortcuts.get(i);
2485 mDesktopItems.add(item);
2486 switch (item.itemType) {
2487 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2488 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08002489 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002490 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
2491 false);
2492 break;
2493 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
2494 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
2495 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2496 (UserFolderInfo) item);
2497 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
2498 false);
2499 break;
2500 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
2501 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
2502 R.layout.live_folder_icon, this,
2503 (ViewGroup) workspace.getChildAt(workspace.getCurrentScreen()),
2504 (LiveFolderInfo) item);
2505 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
2506 false);
2507 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002508 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002509 }
2510
Joe Onorato9c1289c2009-08-17 11:03:03 -04002511 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 }
2513
Joe Onorato9c1289c2009-08-17 11:03:03 -04002514 /**
2515 * Implementation of the method from LauncherModel.Callbacks.
2516 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002517 public void bindFolders(HashMap<Long, FolderInfo> folders) {
2518 mFolders.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002519 mFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002521
2522 /**
2523 * Add the views for a widget to the workspace.
2524 *
2525 * Implementation of the method from LauncherModel.Callbacks.
2526 */
2527 public void bindAppWidget(LauncherAppWidgetInfo item) {
Daniel Sandler843e8602010-06-07 14:59:01 -04002528 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2529 if (DEBUG_WIDGETS) {
2530 Log.d(TAG, "bindAppWidget: " + item);
2531 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002532 final Workspace workspace = mWorkspace;
2533
2534 final int appWidgetId = item.appWidgetId;
2535 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002536 if (DEBUG_WIDGETS) {
2537 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2538 }
2539
Joe Onorato9c1289c2009-08-17 11:03:03 -04002540 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2541
Joe Onorato9c1289c2009-08-17 11:03:03 -04002542 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2543 item.hostView.setTag(item);
2544
2545 workspace.addInScreen(item.hostView, item.screen, item.cellX,
2546 item.cellY, item.spanX, item.spanY, false);
2547
2548 workspace.requestLayout();
2549
2550 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04002551
2552 if (DEBUG_WIDGETS) {
2553 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2554 + (SystemClock.uptimeMillis()-start) + "ms");
2555 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002556 }
2557
2558 /**
2559 * Callback saying that there aren't any more items to bind.
2560 *
2561 * Implementation of the method from LauncherModel.Callbacks.
2562 */
2563 public void finishBindingItems() {
2564 if (mSavedState != null) {
2565 if (!mWorkspace.hasFocus()) {
2566 mWorkspace.getChildAt(mWorkspace.getCurrentScreen()).requestFocus();
2567 }
2568
2569 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
2570 if (userFolders != null) {
2571 for (long folderId : userFolders) {
2572 final FolderInfo info = mFolders.get(folderId);
2573 if (info != null) {
2574 openFolder(info);
2575 }
2576 }
2577 final Folder openFolder = mWorkspace.getOpenFolder();
2578 if (openFolder != null) {
2579 openFolder.requestFocus();
2580 }
2581 }
2582
Joe Onorato9c1289c2009-08-17 11:03:03 -04002583 mSavedState = null;
2584 }
2585
2586 if (mSavedInstanceState != null) {
2587 super.onRestoreInstanceState(mSavedInstanceState);
2588 mSavedInstanceState = null;
2589 }
2590
Joe Onorato9c1289c2009-08-17 11:03:03 -04002591 mWorkspaceLoading = false;
2592 }
2593
2594 /**
2595 * Add the icons for all apps.
2596 *
2597 * Implementation of the method from LauncherModel.Callbacks.
2598 */
2599 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08002600 mAllAppsGrid.setApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002601 }
2602
2603 /**
2604 * A package was installed.
2605 *
2606 * Implementation of the method from LauncherModel.Callbacks.
2607 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002608 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002609 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07002610 mAllAppsGrid.addApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002611 }
2612
2613 /**
2614 * A package was updated.
2615 *
2616 * Implementation of the method from LauncherModel.Callbacks.
2617 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002618 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002619 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato64e6be72010-03-05 15:05:52 -05002620 mWorkspace.updateShortcuts(apps);
2621 mAllAppsGrid.updateApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002622 }
2623
2624 /**
2625 * A package was uninstalled.
2626 *
2627 * Implementation of the method from LauncherModel.Callbacks.
2628 */
Joe Onorato36115782010-06-17 13:28:48 -04002629 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002630 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002631 if (permanent) {
2632 mWorkspace.removeItems(apps);
2633 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07002634 mAllAppsGrid.removeApps(apps);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002635 }
Joe Onoratobe386092009-11-17 17:32:16 -08002636
2637 /**
2638 * Prints out out state for debugging.
2639 */
2640 public void dumpState() {
2641 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08002642 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08002643 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
2644 Log.d(TAG, "mRestoring=" + mRestoring);
2645 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
2646 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
2647 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
2648 Log.d(TAG, "mFolders.size=" + mFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08002649 mModel.dumpState();
2650 mAllAppsGrid.dumpState();
2651 Log.d(TAG, "END launcher2 dump state");
2652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002653}